-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathspan.cpp
More file actions
182 lines (142 loc) · 4.94 KB
/
span.cpp
File metadata and controls
182 lines (142 loc) · 4.94 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <datadog/dict_writer.h>
#include <datadog/optional.h>
#include <datadog/span.h>
#include <datadog/span_config.h>
#include <datadog/string_view.h>
#include <datadog/trace_segment.h>
#include <datadog/tracer.h>
#include <cassert>
#include <string>
#include "otel_identifiers.h"
#include "span_data.h"
#include "tags.h"
namespace datadog {
namespace tracing {
Span::Span(SpanData* data, const std::shared_ptr<TraceSegment>& trace_segment,
const std::function<std::uint64_t()>& generate_span_id,
const Clock& clock)
: trace_segment_(trace_segment),
data_(data),
generate_span_id_(generate_span_id),
clock_(clock) {
assert(trace_segment_);
assert(data_);
assert(generate_span_id_);
assert(clock_);
}
Span::~Span() {
if (!trace_segment_) {
// We were moved from.
return;
}
if (end_time_) {
data_->duration = *end_time_ - data_->start.tick;
} else {
const auto now = clock_();
data_->duration = now - data_->start;
}
#ifdef __linux__
// When a span is finished, we must update the span_id to its parent's.
if (process_storage != nullptr && parent_id().has_value()) {
auto span_id = std::to_string(parent_id().value());
custom_labels_labelset_set(
custom_labels_current_set,
{sizeof(OTEL_SPAN_ID_IDENTIFIER),
reinterpret_cast<const unsigned char*>(OTEL_SPAN_ID_IDENTIFIER)},
{span_id.size(),
reinterpret_cast<const unsigned char*>(span_id.c_str())});
}
#endif
trace_segment_->span_finished();
}
Span Span::create_child(const SpanConfig& config) const {
auto span_data = std::make_unique<SpanData>();
span_data->apply_config(trace_segment_->defaults(), config, clock_);
span_data->trace_id = data_->trace_id;
span_data->parent_id = data_->span_id;
span_data->span_id = generate_span_id_();
const auto span_data_ptr = span_data.get();
trace_segment_->register_span(std::move(span_data));
return Span(span_data_ptr, trace_segment_, generate_span_id_, clock_);
}
Span Span::create_child() const { return create_child(SpanConfig{}); }
void Span::inject(DictWriter& writer) const {
trace_segment_->inject(writer, *data_);
}
void Span::inject(DictWriter& writer, const InjectionOptions& options) const {
trace_segment_->inject(writer, *data_, options);
}
std::uint64_t Span::id() const { return data_->span_id; }
TraceID Span::trace_id() const { return data_->trace_id; }
Optional<std::uint64_t> Span::parent_id() const {
if (data_->parent_id == 0) {
return nullopt;
}
return data_->parent_id;
}
TimePoint Span::start_time() const { return data_->start; }
bool Span::error() const { return data_->error; }
const std::string& Span::service_name() const { return data_->service; }
const std::string& Span::service_type() const { return data_->service_type; }
const std::string& Span::name() const { return data_->name; }
const std::string& Span::resource_name() const { return data_->resource; }
Optional<StringView> Span::lookup_tag(StringView name) const {
const auto found = data_->tags.find(std::string(name));
if (found == data_->tags.end()) {
return nullopt;
}
return found->second;
}
Optional<double> Span::lookup_metric(StringView name) const {
const auto found = data_->numeric_tags.find(std::string(name));
if (found == data_->numeric_tags.end()) {
return nullopt;
}
return found->second;
}
void Span::set_tag(StringView name, StringView value) {
data_->tags.insert_or_assign(std::string(name), std::string(value));
}
void Span::set_metric(StringView name, double value) {
data_->numeric_tags.insert_or_assign(std::string(name), value);
}
void Span::remove_tag(StringView name) { data_->tags.erase(std::string(name)); }
void Span::remove_metric(StringView name) {
data_->numeric_tags.erase(std::string(name));
}
void Span::set_service_name(StringView service) {
assign(data_->service, service);
}
void Span::set_service_type(StringView type) {
assign(data_->service_type, type);
}
void Span::set_resource_name(StringView resource) {
assign(data_->resource, resource);
}
void Span::set_error(bool is_error) {
data_->error = is_error;
if (!is_error) {
data_->tags.erase("error.message");
data_->tags.erase("error.type");
}
}
void Span::set_error_message(StringView message) {
data_->error = true;
data_->tags.insert_or_assign("error.message", std::string(message));
}
void Span::set_error_type(StringView type) {
data_->error = true;
data_->tags.insert_or_assign("error.type", std::string(type));
}
void Span::set_error_stack(StringView type) {
data_->error = true;
data_->tags.insert_or_assign("error.stack", std::string(type));
}
void Span::set_name(StringView value) { assign(data_->name, value); }
void Span::set_end_time(std::chrono::steady_clock::time_point end_time) {
end_time_ = end_time;
}
TraceSegment& Span::trace_segment() { return *trace_segment_; }
const TraceSegment& Span::trace_segment() const { return *trace_segment_; }
} // namespace tracing
} // namespace datadog