userver: userver/tracing/tracer.hpp Source File
Loading...
Searching...
No Matches
tracer.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/tracing/tracer.hpp
4/// @brief Tracing configuration for span logging suppression
5
6#include <optional>
7#include <string>
8#include <string_view>
9
10#include <dynamic_config/variables/USERVER_NO_LOG_SPANS.hpp>
11#include <userver/logging/level.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15/// Tracing support via @ref tracing::Span
16namespace tracing {
17
18using NoLogSpans = ::dynamic_config::userver_no_log_spans::VariableType;
19
20/// Sets the global configuration for disabling logging some of the @ref tracing::Span.
21void SetNoLogSpans(NoLogSpans&& spans);
22
23/// Returns true iff the @ref tracing::Span with `name` is not logged.
24bool IsNoLogSpan(const std::string& name);
25
26/// Sets the global minimum log level for spans. `std::nullopt` makes spans use
27/// the default logger's level, preserving the legacy behavior.
28void SetSpanLogLevel(std::optional<logging::Level> log_level) noexcept;
29
30/// Returns the configured global span log level, or `std::nullopt` if spans
31/// use the default logger's level.
32std::optional<logging::Level> GetSpanLogLevel() noexcept;
33
34/// @brief Sets the probability with which traces are sampled. Intended for
35/// service degradation under high load. Passing `std::nullopt` disables the
36/// sampling (all traces are sampled), values are clamped to [0.0, 1.0]
37/// and rounded down to a multiple of 2^-64. NaN disables sampling.
38///
39/// The decision is made once per trace at the root @ref tracing::Span of the
40/// request (http/grpc handlers, tasks, etc.) deterministically by the trace id
41/// and is inherited by child spans, so spans of a single trace are either all
42/// written or all skipped. An explicit @ref tracing::Span::SetSampled or
43/// @ref tracing::SpanBuilder::SetSampled call (e.g. from an incoming
44/// opentelemetry `sampled` flag) overrides the decision.
45void SetTraceSamplingProbability(std::optional<double> probability) noexcept;
46
47/// @brief Returns the current trace sampling probability, `std::nullopt` means
48/// the sampling is disabled.
49std::optional<double> GetTraceSamplingProbability() noexcept;
50
51namespace impl {
52
53/// Per-trace sampling decision, deterministic by the trace id.
54bool ShouldSampleTrace(std::string_view trace_id) noexcept;
55
56} // namespace impl
57
58/// Isolates tests and benchmarks by resetting span logging configuration on
59/// construction: no suppressed spans, no span log level override and no sampling.
60/// Restores the previous configuration on destruction.
61class TracingStateGuard final {
62public:
63 TracingStateGuard();
64 ~TracingStateGuard();
65
66 TracingStateGuard(const TracingStateGuard&) = delete;
67 TracingStateGuard(TracingStateGuard&&) = delete;
68 TracingStateGuard& operator=(const TracingStateGuard&) = delete;
69 TracingStateGuard& operator=(TracingStateGuard&&) = delete;
70
71private:
72 NoLogSpans previous_no_log_spans_;
73 std::optional<logging::Level> previous_span_log_level_;
74 std::optional<double> previous_trace_sampling_probability_;
75};
76
77} // namespace tracing
78
79USERVER_NAMESPACE_END