Github   Telegram
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
12#include <userver/tracing/tracer_fwd.hpp>
13#include <userver/utils/impl/source_location.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace tracing {
19
27class Span final {
28 public:
29 class Impl;
30
31 explicit Span(TracerPtr tracer, std::string name, const Span* parent,
32 ReferenceType reference_type,
33 logging::Level log_level = logging::Level::kInfo,
34 utils::impl::SourceLocation source_location =
35 utils::impl::SourceLocation::Current());
36
37 /* Use default tracer and implicit coro local storage for parent
38 * identification */
39 explicit Span(std::string name,
40 ReferenceType reference_type = ReferenceType::kChild,
41 logging::Level log_level = logging::Level::kInfo,
42 utils::impl::SourceLocation source_location =
43 utils::impl::SourceLocation::Current());
44
46 // For internal use only
47 explicit Span(Span::Impl& impl);
49
50 Span(Span&& other) noexcept;
51
52 ~Span();
53
54 Span& operator=(const Span&) = delete;
55
56 Span& operator=(Span&&) = delete;
57
67 static Span& CurrentSpan();
68
72
74 static Span MakeSpan(std::string name, std::string_view trace_id,
75 std::string_view parent_span_id);
76
78 static Span MakeSpan(std::string name, std::string_view trace_id,
79 std::string_view parent_span_id, std::string link);
80
85 Span CreateChild(std::string name) const;
86
87 Span CreateFollower(std::string name) const;
88
103
118 ScopeTime CreateScopeTime(std::string name);
119
122 ScopeTime::Duration GetTotalDuration(const std::string& scope_name) const;
123
128 ScopeTime::DurationMillis GetTotalElapsedTime(
129 const std::string& scope_name) const;
130
133 void AddTag(std::string key, logging::LogExtra::Value value);
134
137 void AddTagFrozen(std::string key, logging::LogExtra::Value value);
138
141 void AddNonInheritableTag(std::string key, logging::LogExtra::Value value);
142
145
148
151 void SetLocalLogLevel(std::optional<logging::Level> log_level);
152
155 std::optional<logging::Level> GetLocalLogLevel() const;
156
158 void SetLink(std::string link);
159
161 void SetParentLink(std::string parent_link);
162
163 std::string GetLink() const;
164
165 std::string GetParentLink() const;
166
167 const std::string& GetTraceId() const;
168 const std::string& GetSpanId() const;
169 const std::string& GetParentId() const;
170
171 void LogTo(logging::LogHelper& log_helper) const&;
172
175 bool ShouldLogDefault() const noexcept;
176
180
184
186 void AddTags(const logging::LogExtra&, utils::InternalTag);
187
188 impl::TimeStorage& GetTimeStorage();
190
191 private:
192 std::string GetTag(std::string_view tag) const;
193
194 struct OptionalDeleter {
195 void operator()(Impl*) const noexcept;
196
197 static OptionalDeleter ShouldDelete() noexcept;
198
199 static OptionalDeleter DoNotDelete() noexcept;
200
201 private:
202 explicit OptionalDeleter(bool do_delete) : do_delete(do_delete) {}
203
204 const bool do_delete;
205 };
206
207 std::unique_ptr<Impl, OptionalDeleter> pimpl_;
208};
209
210logging::LogHelper& operator<<(logging::LogHelper& lh, const Span& span);
211
212} // namespace tracing
213
214USERVER_NAMESPACE_END