userver: userver/tracing/manager.hpp Source File
Loading...
Searching...
No Matches
manager.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/tracing/manager.hpp
4/// @brief @copybrief tracing::TracingManagerBase
5
6#include <userver/clients/http/middlewares/base.hpp>
7#include <userver/clients/http/response.hpp>
8#include <userver/tracing/span.hpp>
9#include <userver/tracing/span_builder.hpp>
10#include <userver/utils/flags.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::http {
15class HttpResponse;
16class HttpRequest;
17} // namespace server::http
18
19namespace tracing {
20
21/// @ingroup userver_base_classes
22///
23/// @brief Base class for propagating trace context information in headers.
24///
25/// Mostly used by tracing::DefaultTracingManagerLocator.
27public:
28 virtual ~TracingManagerBase() = default;
29
30 /// Fill SpanBuilder params with actual tracing information extracted from the
31 /// request. You should build Span with SpanBuilder::Build, after calling
32 /// this.
33 /// @return Returns bool, that tells us was any of tracing headers used to
34 /// create new span
35 virtual bool TryFillSpanBuilderFromRequest(const server::http::HttpRequest& request, SpanBuilder& span_builder)
36 const = 0;
37
38 /// Fill new client requests with tracing information
39 virtual void FillRequestWithTracingContext(const Span& span, clients::http::MiddlewareRequest request) const = 0;
40
41 /// Fill response with tracing information
42 virtual void FillResponseWithTracingContext(const Span& span, server::http::HttpResponse& response) const = 0;
43};
44
45// clang-format off
46enum class Format : short {
47 kNone = 0,
48
49 /// Yandex Taxi/Lavka/Eda/... tracing:
50 /// @code
51 /// http::headers::kXYaTraceId -> tracing::Span::GetTraceId() -> http::headers::kXYaTraceId
52 /// http::headers::kXYaRequestId -> tracing::Span::GetParentLink(); tracing::Span::GetLink() -> http::headers::kXYaRequestId
53 /// http::headers::kXYaSpanId -> tracing::Span::GetParentId(); tracing::Span::GetSpanId() -> http::headers::kXYaSpanId
54 /// @endcode
55 kYandexTaxi = 1 << 1,
56
57 /// Yandex Search tracing:
58 /// http::headers::kXRequestId -> tracing::Span::GetTraceId() -> http::headers::kXRequestId
59 kYandex = 1 << 2,
60
61 /// Use http::headers::opentelemetry::kTraceState and
62 /// http::headers::opentelemetry::kTraceParent headers to fill the
63 /// tracing::opentelemetry::TraceParentData as per OpenTelemetry.
65
66 /// Openzipkin b3 alternative propagation, where Span ID goes to parent ID:
67 /// @code
68 /// b3::kTraceId -> tracing::Span::GetTraceId() -> b3::kTraceId
69 /// b3::kSpanId -> tracing::Span::GetParentId(); tracing::Span::GetSpanId() -> b3::kSpanId
70 /// span.GetParentId() -> b3::kParentSpanId
71 /// @endcode
72 /// See https://github.com/openzipkin/b3-propagation for more info.
74};
75// clang-format on
76
77/// Converts a textual representation of format into tracing::Format enum.
78Format FormatFromString(std::string_view format);
79
80bool TryFillSpanBuilderFromRequest(Format format, const server::http::HttpRequest& request, SpanBuilder& span_builder);
81
82void FillRequestWithTracingContext(Format format, const tracing::Span& span, clients::http::MiddlewareRequest request);
83
84void FillResponseWithTracingContext(Format format, const Span& span, server::http::HttpResponse& response);
85
86/// W3C trace-flags byte values from the @c traceparent header.
87/// @see https://www.w3.org/TR/trace-context/#trace-flags
89 kNoTracing = 0x00, ///< Tracing disabled
90 kSampled = 0x01, ///< Trace is sampled
91};
92
93/// @returns the W3C trace-flags byte from the incoming request, stored in
94/// @ref engine::TaskInheritedVariable. Defaults to @ref OtelTraceFlags::kSampled
95/// when absent.
97
98/// @returns the W3C `tracestate` header value from the incoming request, stored
99/// in @ref engine::TaskInheritedVariable. Empty string_view when absent.
100std::string_view GetInheritedOtelTraceState();
101
102/// Stores `tracestate` and the parsed `traceflags` hex byte into
103/// @ref engine::TaskInheritedVariable so they are available for outgoing
104/// request propagation within the current task tree.
105void SetInheritedOtelTracingData(std::string_view tracestate, std::string_view traceflags);
106
107/// @brief Generic tracing manager that knows about popular tracing
108/// headers and allows customising input and output headers.
109class GenericTracingManager final : public TracingManagerBase {
110public:
111 enum class SamplingEnabled : bool {
112 kNo = false,
113 kYes = true,
114 };
115
116 GenericTracingManager() = delete;
117
118 GenericTracingManager(
119 utils::Flags<Format> in_request_response,
120 utils::Flags<Format> new_request,
121 SamplingEnabled sampling = SamplingEnabled::kNo
122 )
123 : in_request_response_{in_request_response},
124 new_request_{new_request},
125 sampling_{sampling}
126 {}
127
128 bool TryFillSpanBuilderFromRequest(const server::http::HttpRequest& request, SpanBuilder& span_builder)
129 const override;
130
131 void FillRequestWithTracingContext(const tracing::Span& span, clients::http::MiddlewareRequest request)
132 const override;
133
134 void FillResponseWithTracingContext(const Span& span, server::http::HttpResponse& response) const override;
135
136private:
137 const utils::Flags<Format> in_request_response_;
138 const utils::Flags<Format> new_request_;
139 const SamplingEnabled sampling_{SamplingEnabled::kNo};
140};
141
142} // namespace tracing
143
144USERVER_NAMESPACE_END