1
#pragma once
2

            
3
#include <cstdint>
4
#include <string>
5

            
6
namespace Envoy {
7
namespace Extensions {
8
namespace Tracers {
9
namespace Zipkin {
10

            
11
/**
12
 * This class represents the context of a Zipkin span. It embodies the following
13
 * span characteristics: trace id, span id, parent id, and basic annotations.
14
 */
15
class SpanContext {
16
public:
17
  /**
18
   * Default constructor. Creates an empty context.
19
   */
20
58
  SpanContext() = default;
21

            
22
  /**
23
   * Constructor that creates a context object from the supplied trace, span and
24
   * parent ids, and sampled flag.
25
   *
26
   * @param trace_id_high The high 64 bits of the trace id.
27
   * @param trace_id The low 64 bits of the trace id.
28
   * @param id The span id.
29
   * @param parent_id The parent span id.
30
   * @param sampled The sampled flag.
31
   * @param inner_context If this context is created base on the inner span.
32
   */
33
  SpanContext(const uint64_t trace_id_high, const uint64_t trace_id, const uint64_t id,
34
              const uint64_t parent_id, bool sampled, bool inner_context = false)
35
31
      : trace_id_high_(trace_id_high), trace_id_(trace_id), id_(id), parent_id_(parent_id),
36
31
        sampled_(sampled), inner_context_(inner_context) {}
37

            
38
  /**
39
   * @return the span id as an integer
40
   */
41
39
  uint64_t id() const { return id_; }
42

            
43
  /**
44
   * @return the span's parent id as an integer.
45
   */
46
35
  uint64_t parentId() const { return parent_id_; }
47

            
48
  /**
49
   * @return the high 64 bits of the trace id as an integer.
50
   */
51
16
  uint64_t traceIdHigh() const { return trace_id_high_; }
52

            
53
  /**
54
   * @return the low 64 bits of the trace id as an integer.
55
   */
56
39
  uint64_t traceId() const { return trace_id_; }
57

            
58
  /**
59
   * @return whether using 128 bit trace id.
60
   */
61
34
  bool is128BitTraceId() const { return trace_id_high_ != 0; }
62

            
63
  /**
64
   * @return the sampled flag.
65
   */
66
33
  bool sampled() const { return sampled_; }
67

            
68
  /**
69
   * @return the inner context flag. True if this context is created base on the inner span.
70
   */
71
21
  bool innerContext() const { return inner_context_; }
72
2
  void setInnerContextForTest(bool inner_context) { inner_context_ = inner_context; }
73

            
74
private:
75
  const uint64_t trace_id_high_{0};
76
  const uint64_t trace_id_{0};
77
  const uint64_t id_{0};
78
  const uint64_t parent_id_{0};
79
  const bool sampled_{false};
80
  bool inner_context_{false};
81
};
82

            
83
} // namespace Zipkin
84
} // namespace Tracers
85
} // namespace Extensions
86
} // namespace Envoy