-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtracer.cpp
More file actions
348 lines (294 loc) · 9.92 KB
/
tracer.cpp
File metadata and controls
348 lines (294 loc) · 9.92 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "datadog/c/tracer.h"
#include <datadog/hex.h>
#include <datadog/trace_segment.h>
#include <datadog/tracer.h>
#include <cstddef>
#include <cstring>
#include <string>
namespace dd = datadog::tracing;
namespace {
class ContextReader : public dd::DictReader {
dd_context_read_callback read_;
public:
explicit ContextReader(dd_context_read_callback read_callback)
: read_(read_callback) {}
dd::Optional<dd::StringView> lookup(dd::StringView key) const override {
std::string key_str(key);
if (auto value = read_(key_str.c_str())) {
return value;
}
return dd::nullopt;
}
void visit(const std::function<void(dd::StringView key, dd::StringView value)>
& /* visitor */) const override {}
};
class ContextWriter : public dd::DictWriter {
dd_context_write_callback write_;
public:
explicit ContextWriter(dd_context_write_callback func) : write_(func) {}
void set(dd::StringView key, dd::StringView value) override {
std::string key_str(key);
std::string value_str(value);
write_(key_str.c_str(), value_str.c_str());
}
};
std::chrono::system_clock::time_point wall_ns_to_system_time(int64_t wall_ns) {
return std::chrono::system_clock::time_point(
std::chrono::round<std::chrono::system_clock::duration>(
std::chrono::nanoseconds(wall_ns)));
}
// Create a TimePoint from a wall-clock timestamp in nanoseconds. The tick is
// derived from the wall clock using the current offset between the system and
// the steady clocks. This keeps duration computed from ticks accurate when
// the start time is supplied via FFI.
dd::TimePoint wall_ns_to_timepoint(int64_t wall_ns) {
const auto wall = wall_ns_to_system_time(wall_ns);
const auto now_wall = std::chrono::system_clock::now();
const auto now_tick = std::chrono::steady_clock::now();
return {wall,
now_tick - std::chrono::duration_cast<dd::Duration>(now_wall - wall)};
}
dd::SpanConfig make_span_config(dd_span_options_t options) {
dd::SpanConfig span_config;
if (options.name != nullptr) {
span_config.name = options.name;
}
if (options.resource != nullptr) {
span_config.resource = options.resource;
}
if (options.service != nullptr) {
span_config.service = options.service;
}
if (options.service_type != nullptr) {
span_config.service_type = options.service_type;
}
if (options.environment != nullptr) {
span_config.environment = options.environment;
}
if (options.version != nullptr) {
span_config.version = options.version;
}
if (options.start_time_ns != DD_TRACE_CURRENT_TIME) {
span_config.start = wall_ns_to_timepoint(options.start_time_ns);
}
return span_config;
}
void set_error(dd_error_t *error, dd_error_code code, const char *message) {
if (error == nullptr) {
return;
}
error->code = code;
std::strncpy(error->message, message, sizeof(error->message) - 1);
error->message[sizeof(error->message) - 1] = '\0';
}
} // namespace
extern "C" {
dd_conf_t *dd_tracer_conf_new(void) {
try {
return reinterpret_cast<dd_conf_t *>(new dd::TracerConfig);
} catch (...) {
return nullptr;
}
}
void dd_tracer_conf_free(dd_conf_t *handle) {
if (handle == nullptr) {
return;
}
delete reinterpret_cast<dd::TracerConfig *>(handle);
}
void dd_tracer_conf_set(dd_conf_t *handle, dd_tracer_option option,
const void *value) {
if (handle == nullptr || value == nullptr) {
return;
}
auto *cfg = reinterpret_cast<dd::TracerConfig *>(handle);
switch (option) {
case DD_OPT_SERVICE_NAME:
cfg->service = static_cast<const char *>(value);
break;
case DD_OPT_ENV:
cfg->environment = static_cast<const char *>(value);
break;
case DD_OPT_VERSION:
cfg->version = static_cast<const char *>(value);
break;
case DD_OPT_AGENT_URL:
cfg->agent.url = static_cast<const char *>(value);
break;
case DD_OPT_INTEGRATION_NAME:
cfg->integration_name = static_cast<const char *>(value);
break;
case DD_OPT_INTEGRATION_VERSION:
cfg->integration_version = static_cast<const char *>(value);
break;
}
}
dd_tracer_t *dd_tracer_new(const dd_conf_t *conf_handle, dd_error_t *error) {
if (conf_handle == nullptr) {
set_error(error, DD_ERROR_NULL_ARGUMENT, "conf_handle is NULL");
return nullptr;
}
const auto *config = reinterpret_cast<const dd::TracerConfig *>(conf_handle);
const auto validated_config = dd::finalize_config(*config);
if (!validated_config) {
set_error(error, DD_ERROR_INVALID_CONFIG,
validated_config.error().message.c_str());
return nullptr;
}
try {
return reinterpret_cast<dd_tracer_t *>(new dd::Tracer{*validated_config});
} catch (...) {
set_error(error, DD_ERROR_ALLOCATION_FAILURE, "failed to allocate tracer");
return nullptr;
}
}
void dd_tracer_free(dd_tracer_t *tracer_handle) {
if (tracer_handle == nullptr) {
return;
}
delete reinterpret_cast<dd::Tracer *>(tracer_handle);
}
dd_span_t *dd_tracer_create_span(dd_tracer_t *tracer_handle,
dd_span_options_t options) {
if (tracer_handle == nullptr || options.name == nullptr) {
return nullptr;
}
auto *tracer = reinterpret_cast<dd::Tracer *>(tracer_handle);
auto span_config = make_span_config(options);
try {
return reinterpret_cast<dd_span_t *>(
new dd::Span(tracer->create_span(span_config)));
} catch (...) {
return nullptr;
}
}
dd_span_t *dd_tracer_extract_or_create_span(
dd_tracer_t *tracer_handle, dd_context_read_callback on_context_read,
dd_span_options_t options) {
if (tracer_handle == nullptr || on_context_read == nullptr ||
options.name == nullptr) {
return nullptr;
}
auto *tracer = reinterpret_cast<dd::Tracer *>(tracer_handle);
auto span_config = make_span_config(options);
ContextReader reader(on_context_read);
try {
return reinterpret_cast<dd_span_t *>(
new dd::Span(tracer->extract_or_create_span(reader, span_config)));
} catch (...) {
return nullptr;
}
}
void dd_span_free(dd_span_t *span_handle) {
if (span_handle == nullptr) {
return;
}
delete reinterpret_cast<dd::Span *>(span_handle);
}
void dd_span_set_tag(dd_span_t *span_handle, const char *key,
const char *value) {
if (span_handle == nullptr || key == nullptr || value == nullptr) {
return;
}
reinterpret_cast<dd::Span *>(span_handle)->set_tag(key, value);
}
void dd_span_set_error(dd_span_t *span_handle, int error_value) {
if (span_handle == nullptr) {
return;
}
reinterpret_cast<dd::Span *>(span_handle)->set_error(error_value != 0);
}
void dd_span_set_error_message(dd_span_t *span_handle,
const char *error_message) {
if (span_handle == nullptr || error_message == nullptr) {
return;
}
reinterpret_cast<dd::Span *>(span_handle)->set_error_message(error_message);
}
void dd_span_inject(dd_span_t *span_handle,
dd_context_write_callback on_context_write) {
if (span_handle == nullptr || on_context_write == nullptr) {
return;
}
auto *span = reinterpret_cast<dd::Span *>(span_handle);
ContextWriter writer(on_context_write);
span->inject(writer);
}
dd_span_t *dd_span_create_child(dd_span_t *span_handle,
dd_span_options_t options) {
if (span_handle == nullptr || options.name == nullptr) {
return nullptr;
}
auto *span = reinterpret_cast<dd::Span *>(span_handle);
auto span_config = make_span_config(options);
try {
return reinterpret_cast<dd_span_t *>(
new dd::Span(span->create_child(span_config)));
} catch (...) {
return nullptr;
}
}
void dd_span_finish(dd_span_t *span_handle) {
if (span_handle == nullptr) {
return;
}
reinterpret_cast<dd::Span *>(span_handle)
->set_end_time(std::chrono::steady_clock::now());
}
void dd_span_set_end_time(dd_span_t *span_handle, int64_t end_time_ns) {
if (span_handle == nullptr) {
return;
}
auto *span = reinterpret_cast<dd::Span *>(span_handle);
// Anchor end.tick to start.tick + wall delta so NTP adjustments during the
// span's lifetime don't skew duration. Clamp to start.tick on negative
// deltas — serialization casts duration to uint64_t.
const auto start = span->start_time();
const auto end_wall = wall_ns_to_system_time(end_time_ns);
const auto duration =
end_wall > start.wall
? std::chrono::duration_cast<dd::Duration>(end_wall - start.wall)
: dd::Duration::zero();
span->set_end_time(start.tick + duration);
}
int dd_span_get_trace_id(dd_span_t *span_handle, char *buffer,
size_t buffer_size) {
if (span_handle == nullptr || buffer == nullptr || buffer_size == 0) {
return -1;
}
auto *span = reinterpret_cast<dd::Span *>(span_handle);
std::string hex = span->trace_id().hex_padded();
if (hex.size() >= buffer_size) {
return -1;
}
std::strncpy(buffer, hex.c_str(), buffer_size);
// Safe narrowing: hex trace IDs are at most 32 characters.
return static_cast<int>(hex.size());
}
int dd_span_get_span_id(dd_span_t *span_handle, char *buffer,
size_t buffer_size) {
if (span_handle == nullptr || buffer == nullptr || buffer_size == 0) {
return -1;
}
auto *span = reinterpret_cast<dd::Span *>(span_handle);
std::string hex = dd::hex_padded(span->id());
if (hex.size() >= buffer_size) {
return -1;
}
std::strncpy(buffer, hex.c_str(), buffer_size);
// Safe narrowing: hex span IDs are 16 characters.
return static_cast<int>(hex.size());
}
void dd_span_set_resource(dd_span_t *span_handle, const char *resource) {
if (span_handle == nullptr || resource == nullptr) {
return;
}
reinterpret_cast<dd::Span *>(span_handle)->set_resource_name(resource);
}
void dd_span_set_service(dd_span_t *span_handle, const char *service) {
if (span_handle == nullptr || service == nullptr) {
return;
}
reinterpret_cast<dd::Span *>(span_handle)->set_service_name(service);
}
} // extern "C"