userver: userver/tracing/span.hpp Source File
Loading...
Searching...
No Matches
span.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/tracing/span.hpp
4/// @brief @copybrief tracing::Span
5
6#include <optional>
7#include <string_view>
8
9#include <userver/logging/log.hpp>
10#include <userver/logging/log_extra.hpp>
11#include <userver/tracing/scope_time.hpp>
12#include <userver/tracing/tracer_fwd.hpp>
13#include <userver/utils/impl/internal_tag.hpp>
14#include <userver/utils/impl/source_location.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace tracing {
19
20class SpanBuilder;
21
22/// @brief Measures the execution time of the current code block, links it with
23/// the parent tracing::Spans and stores that info in the log.
24///
25/// See @ref scripts/docs/en/userver/logging.md for usage examples and more
26/// descriptions.
27///
28/// @warning Shall be created only as a local variable. Do not use it as a
29/// class member!
30class Span final {
31public:
32 class Impl;
33
34 explicit Span(
35 TracerPtr tracer,
36 std::string name,
37 const Span* parent,
38 ReferenceType reference_type,
39 logging::Level log_level = logging::Level::kInfo,
40 utils::impl::SourceLocation source_location = utils::impl::SourceLocation::Current()
41 );
42
43 /// Use default tracer and implicit coro local storage for parent
44 /// identification, takes TraceID from the parent.
45 ///
46 /// For extremely rare cases where a new Trace ID is required use
47 /// tracing::Span::MakeSpan().
48 explicit Span(
49 std::string name,
50 ReferenceType reference_type = ReferenceType::kChild,
51 logging::Level log_level = logging::Level::kInfo,
52 utils::impl::SourceLocation source_location = utils::impl::SourceLocation::Current()
53 );
54
55 /// @cond
56 // For internal use only
57 explicit Span(Span::Impl& impl);
58 /// @endcond
59
60 Span(Span&& other) noexcept;
61
62 ~Span();
63
64 Span& operator=(const Span&) = delete;
65
66 Span& operator=(Span&&) = delete;
67
68 /// @brief Returns the Span of the current task.
69 ///
70 /// Should not be called in non-coroutine
71 /// context. Should not be called from a task with no alive Span.
72 ///
73 /// Rule of thumb: it is safe to call it from a task created by
74 /// utils::Async/utils::CriticalAsync/utils::PeriodicTask. If current task was
75 /// created with an explicit engine::impl::*Async(), you have to create a Span
76 /// beforehand.
77 static Span& CurrentSpan();
78
79 /// @brief Returns nullptr if called in non-coroutine context or from a task
80 /// with no alive Span; otherwise returns the Span of the current task.
81 static Span* CurrentSpanUnchecked();
82
83 /// Factory function for extremely rare cases of creating a Span with custom
84 /// IDs; prefer Span constructor instead.
85 ///
86 /// @return A new Span attached to current Span (if any) but with a new
87 /// Trace ID.
88 /// @param name Name of a new Span
89 /// @param trace_id New Trace ID; if empty then the Trace ID is autogenerated
90 /// @param parent_span_id Id of the parent Span, could be empty.
91 static Span MakeSpan(std::string name, std::string_view trace_id, std::string_view parent_span_id);
92
93 /// Factory function for extremely rare cases of creating a Span with custom
94 /// IDs; prefer Span constructor instead.
95 ///
96 /// @return A new Span attached to current Span (if any), sets `link`.
97 /// @param name Name of a new Span
98 /// @param trace_id New Trace ID; if empty then the Trace ID is autogenerated
99 /// @param parent_span_id Id of the parent Span, could be empty.
100 /// @param link The new link
101 static Span
102 MakeSpan(std::string name, std::string_view trace_id, std::string_view parent_span_id, std::string link);
103
104 /// Factory function for rare cases of creating a root Span that starts
105 /// the trace_id chain, ignoring `CurrentSpan`, if any. Useful
106 /// in background jobs, periodics, distlock tasks, cron tasks, etc.
107 /// The result of such jobs is not directly requested by anything.
108 ///
109 /// @return A new Span that is the root of a new Span hierarchy.
110 /// @param name Name of a new Span
111 /// @param log_level Log level for the span's own log record
112 static Span MakeRootSpan(std::string name, logging::Level log_level = logging::Level::kInfo);
113
114 /// Create a child which can be used independently from the parent.
115 ///
116 /// The child shares no state with its parent. If you need to run code in
117 /// parallel, create a child span and use the child in a separate task.
118 Span CreateChild(std::string name) const;
119
120 Span CreateFollower(std::string name) const;
121
122 /// @brief Creates a tracing::ScopeTime attached to the span.
124
125 /// @brief Creates a tracing::ScopeTime attached to the Span and starts
126 /// measuring execution time.
127 /// Tag `{scope_name}_time` with elapsed time is added to result span.
128 ///
129 /// @note `name` parameter is expected to satisfy snake case.
130 /// Otherwise, it is converted to snake case.
131 ScopeTime CreateScopeTime(std::string name);
132
133 /// Returns total time elapsed for a certain scope of this span.
134 /// If there is no record for the scope, returns 0.
136
137 /// Returns total time elapsed for a certain scope of this span.
138 /// If there is no record for the scope, returns 0.
139 ///
140 /// Prefer using Span::GetTotalDuration()
142
143 /// Add a tag that is used on each logging in this Span and all
144 /// future children.
145 void AddTag(std::string key, logging::LogExtra::Value value);
146
147 /// Add a tag that is used on each logging in this Span and all
148 /// future children. It will not be possible to change its value.
149 void AddTagFrozen(std::string key, logging::LogExtra::Value value);
150
151 /// Add a tag that is local to the Span (IOW, it is not propagated to
152 /// future children) and logged only once in the destructor of the Span.
153 void AddNonInheritableTag(std::string key, logging::LogExtra::Value value);
154
155 /// @overload AddNonInheritableTag
156 void AddNonInheritableTags(const logging::LogExtra&);
157
158 /// @brief Sets level for tags logging
159 void SetLogLevel(logging::Level log_level);
160
161 /// @brief Returns level for tags logging
163
164 /// @brief Sets the local log level that disables logging of this span if
165 /// the local log level set and greater than the main log level of the Span.
166 void SetLocalLogLevel(std::optional<logging::Level> log_level);
167
168 /// @brief Returns the local log level that disables logging of this span if
169 /// it is set and greater than the main log level of the Span.
170 std::optional<logging::Level> GetLocalLogLevel() const;
171
172 /// Set link - a request ID within a service. Can be called only once.
173 ///
174 /// Propagates within a single service, but not from client to server. A new
175 /// link is generated for the "root" request handling task
176 void SetLink(std::string link);
177
178 /// Set parent_link - an ID . Can be called only once.
179 void SetParentLink(std::string parent_link);
180
181 /// Get link - a request ID within the service.
182 ///
183 /// Propagates within a single service, but not from client to server. A new
184 /// link is generated for the "root" request handling task
185 std::string GetLink() const;
186
187 std::string GetParentLink() const;
188
189 /// An ID of the request that does not change from service to service.
190 ///
191 /// Propagates both to sub-spans within a single service, and from client
192 /// to server
193 const std::string& GetTraceId() const;
194
195 /// Identifies a specific span. It does not propagate
196 const std::string& GetSpanId() const;
197 const std::string& GetParentId() const;
198
199 /// @returns true if this span would be logged with the current local and
200 /// global log levels to the default logger.
201 bool ShouldLogDefault() const noexcept;
202
203 /// Detach the Span from current engine::Task so it is not
204 /// returned by CurrentSpan() any more.
206
207 /// Attach the Span to current engine::Task so it is returned
208 /// by CurrentSpan().
210
211 std::chrono::system_clock::time_point GetStartSystemTime() const;
212
213 /// @cond
214 // For internal use only.
215 void AddTags(const logging::LogExtra&, utils::impl::InternalTag);
216
217 // For internal use only.
218 impl::TimeStorage& GetTimeStorage(utils::impl::InternalTag);
219
220 // For internal use only.
221 void LogTo(logging::impl::TagWriter writer) const&;
222 /// @endcond
223
224private:
225 struct OptionalDeleter {
226 void operator()(Impl*) const noexcept;
227
228 static OptionalDeleter ShouldDelete() noexcept;
229
230 static OptionalDeleter DoNotDelete() noexcept;
231
232 private:
233 explicit OptionalDeleter(bool do_delete) : do_delete(do_delete) {}
234
235 const bool do_delete;
236 };
237
238 friend class SpanBuilder;
239 friend class TagScope;
240
241 explicit Span(std::unique_ptr<Impl, OptionalDeleter>&& pimpl);
242
243 std::string GetTag(std::string_view tag) const;
244
245 std::unique_ptr<Impl, OptionalDeleter> pimpl_;
246};
247
248namespace impl {
249
250class DetachLocalSpansScope final {
251public:
252 DetachLocalSpansScope() noexcept;
253 ~DetachLocalSpansScope();
254
255 DetachLocalSpansScope(DetachLocalSpansScope&&) = delete;
256 DetachLocalSpansScope& operator=(DetachLocalSpansScope&&) = delete;
257
258private:
259 struct Impl;
260 utils::FastPimpl<Impl, 16, 8> impl_;
261};
262
263struct LogSpanAsLastNoCurrent final {
264 const Span& span;
265};
266
267logging::LogHelper& operator<<(logging::LogHelper& lh, LogSpanAsLastNoCurrent span);
268
269} // namespace impl
270
271} // namespace tracing
272
273USERVER_NAMESPACE_END