userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
span_log_context.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/tracing/span_log_context.hpp
4
/// @brief @copybrief tracing::SpanLogContext
5
6
#
include
<
cstddef
>
7
#
include
<
iosfwd
>
8
#
include
<
optional
>
9
#
include
<
string_view
>
10
11
#
include
<
userver
/
compiler
/
impl
/
lifetime
.
hpp
>
12
#
include
<
userver
/
logging
/
log_extra
.
hpp
>
13
#
include
<
userver
/
tracing
/
fwd
.
hpp
>
14
#
include
<
userver
/
utils
/
small_string
.
hpp
>
15
16
USERVER_NAMESPACE_BEGIN
17
18
namespace
tracing {
19
20
/// @brief Owns a snapshot of a span's tracing identifiers and sampling state for logging.
21
///
22
/// Useful in rare cases when logs need to be associated with a span that has
23
/// already finished, such as request or response send errors, or logs from a
24
/// background task spawned by a request handler.
25
///
26
/// Capture the context with SetFromSpan() while the span is alive, then append
27
/// GetLogExtra() to a log record to attach the captured tracing fields.
28
class
SpanLogContext
final
{
29
public
:
30
/// Replaces the captured context with the span's current logging context.
31
/// The trace ID and the link are always captured.
32
/// Uses Span::GetSpanIdForChildLogs() to select the span ID, which may belong
33
/// to a parent span if the supplied span is not loggable, or be absent
34
/// if no span in the chain is loggable.
35
void
SetFromSpan
(
const
Span& span);
36
37
/// Returns the captured trace ID, or an empty string if no context was captured.
38
std::string_view
GetTraceId
()
const
noexcept
USERVER_IMPL_LIFETIME_BOUND;
39
/// Returns the captured logging span ID, or an empty string if no span in the chain was loggable.
40
std::string_view
GetSpanId
()
const
noexcept
USERVER_IMPL_LIFETIME_BOUND;
41
/// Returns the captured link, or an empty string if no context was captured.
42
std::string_view
GetLink
()
const
noexcept
USERVER_IMPL_LIFETIME_BOUND;
43
44
/// Returns true if no context has been captured with SetFromSpan().
45
bool
IsEmpty
()
const
noexcept
;
46
/// Returns the captured sampling state, or false for an empty context.
47
bool
IsSampled
()
const
noexcept
;
48
49
/// Returns the captured tracing fields for logging, or an empty LogExtra
50
/// if no context has been captured.
51
logging
::LogExtra
GetLogExtra
()
const
;
52
53
bool
operator==(
const
SpanLogContext&)
const
=
default
;
54
55
private
:
56
static
constexpr
std::size_t kTraceIdInlineSize = 32;
57
static
constexpr
std::size_t kSpanIdInlineSize = 16;
58
static
constexpr
std::size_t kLinkInlineSize = 32;
59
60
utils
::SmallString<kTraceIdInlineSize> trace_id_;
61
utils
::SmallString<kSpanIdInlineSize> span_id_;
62
utils
::SmallString<kLinkInlineSize> link_;
63
std::optional<
bool
> trace_sampled_;
64
};
65
66
void
PrintTo(
const
SpanLogContext& context, std::ostream* os);
67
68
}
// namespace tracing
69
70
USERVER_NAMESPACE_END
userver
tracing
span_log_context.hpp
Generated on
for userver by
Doxygen
1.17.0