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 /// Yandex Taxi/Lavka/Eda/... tracing:
48 /// @code
49 /// http::headers::kXYaTraceId -> tracing::Span::GetTraceId() -> http::headers::kXYaTraceId
50 /// http::headers::kXYaRequestId -> tracing::Span::GetParentLink(); tracing::Span::GetLink() -> http::headers::kXYaRequestId
51 /// http::headers::kXYaSpanId -> tracing::Span::GetParentId(); tracing::Span::GetSpanId() -> http::headers::kXYaSpanId
52 /// @endcode
53 kYandexTaxi = 1 << 1,
54
55 /// Yandex Search tracing:
56 /// http::headers::kXRequestId -> tracing::Span::GetTraceId() -> http::headers::kXRequestId
57 kYandex = 1 << 2,
58
59 /// Use http::headers::opentelemetry::kTraceState and
60 /// http::headers::opentelemetry::kTraceParent headers to fill the
61 /// tracing::opentelemetry::TraceParentData as per OpenTelemetry.
63
64 /// Openzipkin b3 alternative propagation, where Span ID goes to parent ID:
65 /// @code
66 /// b3::kTraceId -> tracing::Span::GetTraceId() -> b3::kTraceId
67 /// b3::kSpanId -> tracing::Span::GetParentId(); tracing::Span::GetSpanId() -> b3::kSpanId
68 /// span.GetParentId() -> b3::kParentSpanId
69 /// @endcode
70 /// See https://github.com/openzipkin/b3-propagation for more info.
72};
73// clang-format on
74
75/// Converts a textual representation of format into tracing::Format enum.
76Format FormatFromString(std::string_view format);
77
78bool TryFillSpanBuilderFromRequest(Format format, const server::http::HttpRequest& request, SpanBuilder& span_builder);
79
80void FillRequestWithTracingContext(Format format, const tracing::Span& span, clients::http::MiddlewareRequest request);
81
82void FillResponseWithTracingContext(Format format, const Span& span, server::http::HttpResponse& response);
83
84/// W3C trace-flags byte values from the @c traceparent header.
85/// @see https://www.w3.org/TR/trace-context/#trace-flags
87 kNoTracing = 0x00,
88 kSampled = 0x01,
89};
90
91/// @returns the W3C trace-flags byte from the incoming request, stored in
92/// @ref engine::TaskInheritedVariable. Defaults to @ref OtelTraceFlags::kSampled
93/// when absent.
95
96/// @returns the W3C `tracestate` header value from the incoming request, stored
97/// in @ref engine::TaskInheritedVariable. Empty string_view when absent.
98std::string_view GetInheritedOtelTraceState();
99
100/// Stores `tracestate` and the parsed `traceflags` hex byte into
101/// @ref engine::TaskInheritedVariable so they are available for outgoing
102/// request propagation within the current task tree.
103void SetInheritedOtelTracingData(std::string_view tracestate, std::string_view traceflags);
104
105/// @brief Generic tracing manager that knows about popular tracing
106/// headers and allows customising input and output headers.
107class GenericTracingManager final : public TracingManagerBase {
108public:
109 enum class SamplingEnabled : bool {
110 kNo = false,
111 kYes = true,
112 };
113
114 GenericTracingManager() = delete;
115
116 GenericTracingManager(
117 utils::Flags<Format> in_request_response,
118 utils::Flags<Format> new_request,
119 SamplingEnabled sampling = SamplingEnabled::kNo
120 )
121 : in_request_response_{in_request_response},
122 new_request_{new_request},
123 sampling_{sampling}
124 {}
125
126 bool TryFillSpanBuilderFromRequest(const server::http::HttpRequest& request, SpanBuilder& span_builder)
127 const override;
128
129 void FillRequestWithTracingContext(const tracing::Span& span, clients::http::MiddlewareRequest request)
130 const override;
131
132 void FillResponseWithTracingContext(const Span& span, server::http::HttpResponse& response) const override;
133
134private:
135 const utils::Flags<Format> in_request_response_;
136 const utils::Flags<Format> new_request_;
137 const SamplingEnabled sampling_{SamplingEnabled::kNo};
138};
139
140} // namespace tracing
141
142USERVER_NAMESPACE_END