userver: userver/tracing/span.hpp Source File
Loading...
Searching...
No Matches
span.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <optional>
7#include <string_view>
8
13#include <userver/utils/impl/internal_tag.hpp>
14#include <userver/utils/impl/source_location.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace engine {
19class TracePlugin;
20}
21
22namespace tracing {
23
35class Span final {
36public:
37 class Impl;
38
42 explicit Span(
43 std::string name,
44 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
45 );
46
50 explicit Span(
51 std::string name,
52 const Span* parent,
53 ReferenceType reference_type = ReferenceType::kChild,
54 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
55 );
56
57 Span(Span&& other) noexcept;
58 Span(const Span& other) = delete;
59 Span& operator=(Span&&) = delete;
60 Span& operator=(const Span&) = delete;
61 ~Span();
62
72 static Span& CurrentSpan();
73
77
86 static Span MakeSpan(
87 std::string name,
88 std::string_view trace_id,
89 std::string_view parent_span_id,
90 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
91 );
92
103 std::string name,
104 std::string_view trace_id,
105 std::string_view parent_span_id,
106 std::string_view link,
107 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
108 );
109
119 std::string name,
120 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
121 );
122
128 std::string name,
129 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
130 ) const;
131
132 Span CreateFollower(
133 std::string name,
134 const utils::impl::SourceLocation& source_location = utils::impl::SourceLocation::Current()
135 ) const;
136
139
146 ScopeTime CreateScopeTime(std::string name);
147
150 ScopeTime::Duration GetTotalDuration(std::string_view scope_name) const;
151
156 ScopeTime::DurationMillis GetTotalElapsedTime(std::string_view scope_name) const;
157
160 void AddTag(std::string key, logging::LogExtra::Value value);
161
164 void AddTagFrozen(std::string key, logging::LogExtra::Value value);
165
168 void AddNonInheritableTag(std::string key, logging::LogExtra::Value value);
169
171 void AddNonInheritableTags(const logging::LogExtra&);
172
174 void AddEvent(SpanEvent&& event);
175
178 void AddEvent(std::string_view event_name);
179
196
198 logging::Level GetLogLevel() const noexcept;
199
212 void SetLocalLogLevel(std::optional<logging::Level> log_level) noexcept;
213
215 std::optional<logging::Level> GetLocalLogLevel() const noexcept;
216
228 void SetSpanForcedWriting(bool span_writing_forced) noexcept;
229
231 bool IsSpanForcedWriting() const noexcept;
232
237 void SetLink(std::string_view link);
238
242 void SetParentLink(std::string_view parent_link);
243
248 std::string_view GetLink() const noexcept;
249
253 std::string_view GetParentLink() const noexcept;
254
259 std::string_view GetTraceId() const noexcept;
260
262 std::string_view GetSpanId() const noexcept;
263
265 std::string_view GetParentId() const noexcept;
266
269 std::optional<std::string_view> GetSpanIdForChildLogs() const noexcept;
270
274 std::string_view GetSpanIdForPropagation() const noexcept;
275
277 std::string_view GetName() const noexcept;
278
281 bool ShouldLogDefault() const noexcept;
282
286 bool IsSampled() const noexcept;
287
296 void SetSampled(bool sampled) noexcept;
297
300 void DetachFromCoroStack() noexcept;
301
305
306 std::chrono::system_clock::time_point GetStartSystemTime() const noexcept;
307
308 std::chrono::steady_clock::time_point GetStartSteadyTime() const noexcept;
309
311 // For internal use only.
312 void AddTags(const logging::LogExtra&, utils::impl::InternalTag);
313
314 // For internal use only.
315 impl::TimeStorage& GetTimeStorage(utils::impl::InternalTag) noexcept;
316
317 // For internal use only.
318 void LogTo(utils::impl::InternalTag, logging::impl::TagWriter writer) const;
320
321private:
322 struct OptionalDeleter {
323 void operator()(Impl*) const noexcept;
324
325 static OptionalDeleter ShouldDelete() noexcept;
326
327 static OptionalDeleter DoNotDelete() noexcept;
328
329 private:
330 constexpr explicit OptionalDeleter(bool do_delete) noexcept
331 : do_delete_(do_delete)
332 {}
333
334 const bool do_delete_;
335 };
336
337 friend class SpanBuilder;
338 friend class TagScope;
339 friend class InPlaceSpan;
340 friend class engine::TracePlugin;
341
342 explicit Span(std::unique_ptr<Impl, OptionalDeleter>&& pimpl);
343
344 std::string_view GetTag(std::string_view tag) const;
345
346 std::unique_ptr<Impl, OptionalDeleter> pimpl_;
347};
348
349namespace impl {
350
351class DetachLocalSpansScope final {
352public:
353 DetachLocalSpansScope() noexcept;
354 ~DetachLocalSpansScope();
355
356 DetachLocalSpansScope(DetachLocalSpansScope&&) = delete;
357 DetachLocalSpansScope& operator=(DetachLocalSpansScope&&) = delete;
358
359private:
360 struct Impl;
361 utils::FastPimpl<Impl, 16, 8> impl_;
362};
363
364struct LogSpanAsLastNoCurrent final {
365 const Span& span;
366};
367
368logging::LogHelper& operator<<(logging::LogHelper& lh, LogSpanAsLastNoCurrent span);
369
370} // namespace impl
371
372} // namespace tracing
373
374USERVER_NAMESPACE_END