-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathspan_context.h
109 lines (92 loc) · 3.65 KB
/
span_context.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/trace_flags.h"
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/trace/trace_state.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
namespace trace_api = opentelemetry::trace;
/* SpanContext contains the state that must propagate to child Spans and across
* process boundaries. It contains TraceId and SpanId, TraceFlags, TraceState
* and whether it was propagated from a remote parent.
*/
class SpanContext final
{
public:
/* A temporary constructor for an invalid SpanContext.
* Trace id and span id are set to invalid (all zeros).
*
* @param sampled_flag a required parameter specifying if child spans should be
* sampled
* @param is_remote true if this context was propagated from a remote parent.
*/
SpanContext(bool sampled_flag, bool is_remote)
: trace_id_(),
span_id_(),
trace_flags_(trace_api::TraceFlags((uint8_t)sampled_flag)),
is_remote_(is_remote),
trace_state_(TraceState::GetDefault())
{}
SpanContext(TraceId trace_id,
SpanId span_id,
TraceFlags trace_flags,
bool is_remote,
nostd::shared_ptr<TraceState> trace_state = TraceState::GetDefault()) noexcept
: trace_id_(trace_id),
span_id_(span_id),
trace_flags_(trace_flags),
is_remote_(is_remote),
trace_state_(trace_state)
{}
SpanContext(const SpanContext &ctx) = default;
// @returns whether this context is valid
bool IsValid() const noexcept { return trace_id_.IsValid() && span_id_.IsValid(); }
// @returns the trace_flags associated with this span_context
const trace_api::TraceFlags &trace_flags() const noexcept { return trace_flags_; }
// @returns the trace_id associated with this span_context
const trace_api::TraceId &trace_id() const noexcept { return trace_id_; }
// @returns the span_id associated with this span_context
const trace_api::SpanId &span_id() const noexcept { return span_id_; }
// @returns the trace_state associated with this span_context
const nostd::shared_ptr<trace_api::TraceState> trace_state() const noexcept
{
return trace_state_;
}
/*
* @param that SpanContext for comparing.
* @return true if `that` equals the current SpanContext.
* N.B. trace_state is ignored for the comparison.
*/
bool operator==(const SpanContext &that) const noexcept
{
return trace_id() == that.trace_id() && span_id() == that.span_id() &&
trace_flags() == that.trace_flags();
}
SpanContext &operator=(const SpanContext &ctx) = default;
bool IsRemote() const noexcept { return is_remote_; }
static SpanContext GetInvalid() { return SpanContext(false, false); }
bool IsSampled() const noexcept { return trace_flags_.IsSampled(); }
private:
trace_api::TraceId trace_id_;
trace_api::SpanId span_id_;
trace_api::TraceFlags trace_flags_;
bool is_remote_;
nostd::shared_ptr<trace_api::TraceState> trace_state_;
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE