File: span_context.h

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (98 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (10)
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <stdint.h>

#include <utility>

#include "opentelemetry/nostd/shared_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"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace 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) noexcept
      : trace_id_(),
        span_id_(),
        trace_flags_(trace::TraceFlags(static_cast<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_(std::move(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::TraceFlags &trace_flags() const noexcept { return trace_flags_; }

  // @returns the trace_id associated with this span_context
  const trace::TraceId &trace_id() const noexcept { return trace_id_; }

  // @returns the span_id associated with this span_context
  const trace::SpanId &span_id() const noexcept { return span_id_; }

  // @returns the trace_state associated with this span_context
  const nostd::shared_ptr<trace::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() noexcept { return SpanContext(false, false); }

  bool IsSampled() const noexcept { return trace_flags_.IsSampled(); }

private:
  trace::TraceId trace_id_;
  trace::SpanId span_id_;
  trace::TraceFlags trace_flags_;
  bool is_remote_;
  nostd::shared_ptr<trace::TraceState> trace_state_;
};
}  // namespace trace
OPENTELEMETRY_END_NAMESPACE