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/request_tracing_editor.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 HttpRequest;
16class HttpResponse;
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.
27 public:
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
36 const server::http::HttpRequest& request,
37 SpanBuilder& span_builder) const = 0;
38
39 /// Fill new client requests with tracing information
41 const Span& span, clients::http::RequestTracingEditor request) const = 0;
42
43 /// Fill response with tracing information
45 const Span& span, server::http::HttpResponse& response) const = 0;
46};
47
48// clang-format off
49enum class Format : short {
50 /// Yandex Taxi/Lavka/Eda/... tracing:
51 /// @code
52 /// http::headers::kXYaTraceId -> tracing::Span::GetTraceId() -> http::headers::kXYaTraceId
53 /// http::headers::kXYaRequestId -> tracing::Span::GetParentLink(); tracing::Span::GetLink() -> http::headers::kXYaRequestId
54 /// http::headers::kXYaSpanId -> tracing::Span::GetParentId(); tracing::Span::GetSpanId() -> http::headers::kXYaSpanId
55 /// @endcode
56 kYandexTaxi = 1 << 1,
57
58 /// Yandex Search tracing:
59 /// http::headers::kXRequestId -> tracing::Span::GetTraceId() -> http::headers::kXRequestId
60 kYandex = 1 << 2,
61
62 /// Use http::headers::opentelemetry::kTraceState and
63 /// http::headers::opentelemetry::kTraceParent headers to fill the
64 /// tracing::opentelemetry::TraceParentData as per OpenTelemetry.
65 kOpenTelemetry = 1 << 3,
66
67 /// Openzipkin b3 alternative propagation, where Span ID goes to partern ID:
68 /// @code
69 /// b3::kTraceId -> tracing::Span::GetTraceId() -> b3::kTraceId
70 /// b3::kSpanId -> tracing::Span::GetParentId(); tracing::Span::GetSpanId() -> b3::kSpanId
71 /// span.GetParentId() -> b3::kParentSpanId
72 /// @endcode
73 /// See https://github.com/openzipkin/b3-propagation for more info.
74 kB3Alternative = 1 << 4,
75};
76// clang-format on
77
78/// Converts a textual representation of format into tracing::Format enum.
79Format FormatFromString(std::string_view format);
80
81bool TryFillSpanBuilderFromRequest(Format format,
82 const server::http::HttpRequest& request,
83 SpanBuilder& span_builder);
84
85void FillRequestWithTracingContext(Format format, const tracing::Span& span,
86 clients::http::RequestTracingEditor request);
87
88void FillResponseWithTracingContext(Format format, const Span& span,
89 server::http::HttpResponse& response);
90
91/// @brief Generic tracing manager that knows about popular tracing
92/// headers and allows customising input and output headers.
93class GenericTracingManager final : public TracingManagerBase {
94 public:
95 GenericTracingManager() = delete;
96
97 GenericTracingManager(utils::Flags<Format> in_request_response,
98 utils::Flags<Format> new_request)
99 : in_request_response_{in_request_response}, new_request_{new_request} {}
100
101 bool TryFillSpanBuilderFromRequest(const server::http::HttpRequest& request,
102 SpanBuilder& span_builder) const override;
103
105 const tracing::Span& span,
106 clients::http::RequestTracingEditor request) const override;
107
109 const Span& span, server::http::HttpResponse& response) const override;
110
111 private:
112 const utils::Flags<Format> in_request_response_;
113 const utils::Flags<Format> new_request_;
114};
115
116} // namespace tracing
117
118USERVER_NAMESPACE_END