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/plugin.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::PluginRequest 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.
62 kOpenTelemetry = 1 << 3,
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.
71 kB3Alternative = 1 << 4,
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::PluginRequest request);
81
82void FillResponseWithTracingContext(Format format, const Span& span, server::http::HttpResponse& response);
83
84/// @brief Generic tracing manager that knows about popular tracing
85/// headers and allows customising input and output headers.
86class GenericTracingManager final : public TracingManagerBase {
87public:
88 GenericTracingManager() = delete;
89
90 GenericTracingManager(utils::Flags<Format> in_request_response, utils::Flags<Format> new_request)
91 : in_request_response_{in_request_response}, new_request_{new_request} {}
92
93 bool TryFillSpanBuilderFromRequest(const server::http::HttpRequest& request, SpanBuilder& span_builder)
94 const override;
95
96 void FillRequestWithTracingContext(const tracing::Span& span, clients::http::PluginRequest request) const override;
97
98 void FillResponseWithTracingContext(const Span& span, server::http::HttpResponse& response) const override;
99
100private:
101 const utils::Flags<Format> in_request_response_;
102 const utils::Flags<Format> new_request_;
103};
104
105} // namespace tracing
106
107USERVER_NAMESPACE_END