userver: userver/logging/telemetry_settings.hpp Source File
Loading...
Searching...
No Matches
telemetry_settings.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/logging/telemetry_settings.hpp
4/// @brief Policies for @ref components::LoggingConfigurator.
5
6#include <userver/formats/json_fwd.hpp>
7#include <userver/logging/level.hpp>
8#include <userver/utils/default_dict.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace logging {
13
14/// Settings for one logger. Default values impose no additional restrictions.
15struct TelemetrySettings final {
16 /// Minimum severity, combined with the logger's level captured at registration.
17 Level min_log_level{Level::kTrace};
18 /// Fraction of logs below warning to retain, in [0, 1]; excludes spans and force-enabled logs.
19 double log_sample_rate{1.0};
20
21 /// Returns the higher minimum level and lower sampling rate without modifying either input.
22 /// Both inputs must satisfy @ref logging::TelemetrySettings::Validate.
23 [[nodiscard]] TelemetrySettings Merge(const TelemetrySettings& other) const;
24
25 /// @throws std::runtime_error if the sampling rate is non-finite or outside [0, 1].
26 void Validate() const;
27
28 /// Compares all settings.
29 bool operator==(const TelemetrySettings&) const = default;
30};
31
32/// Parses required min_log_level and log_sample_rate fields and validates the result.
33/// Missing or invalid fields are rejected; unknown fields are ignored.
34TelemetrySettings Parse(const formats::json::Value& value, formats::parse::To<TelemetrySettings>);
35
36/// Process-wide trace sampling, independent of logger sampling.
37struct TracingSettings final {
38 /// Fraction of traces to retain in [0, 1]; 1 imposes no sampling restriction.
39 double trace_sample_rate{1.0};
40
41 /// Returns the lower sampling rate without modifying either input.
42 /// Both inputs must satisfy @ref logging::TracingSettings::Validate.
43 [[nodiscard]] TracingSettings Merge(const TracingSettings& other) const;
44
45 /// @throws std::runtime_error if the sampling rate is non-finite or outside [0, 1].
46 void Validate() const;
47
48 /// Compares all settings.
49 bool operator==(const TracingSettings&) const = default;
50};
51
52/// Parses the required trace_sample_rate field and validates the result.
53/// Missing or invalid fields are rejected; unknown fields are ignored.
54TracingSettings Parse(const formats::json::Value& value, formats::parse::To<TracingSettings>);
55
56/// Combined policy accepted by @ref components::LoggingConfigurator::SetRestrictions.
57struct TelemetryConfig final {
58 /// Logger name to settings; __default__ is used only for names without explicit entries.
59 utils::DefaultDict<TelemetrySettings> loggers{};
60 /// Trace settings shared by the whole process.
61 TracingSettings tracing{};
62
63 /// Validates every logger entry, including __default__, and the trace settings.
64 /// @throws std::runtime_error if any sampling rate is non-finite or outside [0, 1].
65 void Validate() const;
66
67 /// Compares both policies.
68 bool operator==(const TelemetryConfig&) const = default;
69};
70
71/// Parses the optional logging and tracing sections of @ref USERVER_TELEMETRY_SETTINGS.
72/// Missing sections impose no restrictions. Invalid fields are rejected;
73/// unknown fields and sections are ignored.
74TelemetryConfig Parse(const formats::json::Value& value, formats::parse::To<TelemetryConfig>);
75
76} // namespace logging
77
78USERVER_NAMESPACE_END