-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
390 lines (344 loc) · 11.2 KB
/
Copy pathcore.cpp
File metadata and controls
390 lines (344 loc) · 11.2 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#include "datadog/core.h"
#include <iostream>
#include <memory>
#include "datadog/c/core_glue.hpp"
#include "datadog/impl/core/attribute/types.hpp"
#include "datadog/impl/core/core.hpp"
#include "datadog/impl/core/types.hpp"
// NOLINTBEGIN(cppcoreguidelines-owning-memory)
static const uint32_t CORE_CONFIG_VERSION = 1;
static const dd_core_config_t DEFAULT_CORE_CONFIG = {
CORE_CONFIG_VERSION, // version (for ABI future-proofing)
dd_stderr_diagnostic_handler, // diagnostic_handler
nullptr, // diagnostic_handler_userdata
DD_DIAGNOSTIC_LEVEL_WARNING, // diagnostic_threshold
DD_TRACKING_CONSENT_PENDING, // tracking_consent
{0}, // application_storage_path
DD_SITE_US1, // site
nullptr, // client_token
nullptr, // service
nullptr, // env
nullptr, // application_version
nullptr, // variant
DD_BATCH_SIZE_MEDIUM, // batch_size
DD_UPLOAD_FREQUENCY_AVERAGE, // upload_frequency
DD_BATCH_PROCESSING_LEVEL_MEDIUM, // batch_processing_level
{
false, // internal_options.flush_http_requests_on_stop
nullptr, // internal_options.custom_endpoint_url
nullptr, // internal_options.source
nullptr // internal_options.sdk_version
}
};
extern "C" {
void dd_stderr_diagnostic_handler(const dd_diagnostic_message_t* message, void*) {
static const char* level_names[] = {"DEBUG", "STATUS", "WARNING", "ERROR"};
const size_t i = static_cast<size_t>(message->level);
const char* level_name = i < std::size(level_names) ? level_names[i] : ""; // NOLINT
std::cerr << "[DATADOG " << level_name << "] " << message->text << "\n";
}
void dd_core_config_init(
dd_core_config_t* config,
const char* client_token,
const char* service,
const char* env
) {
if (!config) {
return;
}
*config = DEFAULT_CORE_CONFIG;
config->client_token = client_token;
config->service = service;
config->env = env;
}
void dd_core_config_set_diagnostic_handler(
dd_core_config_t* config, dd_diagnostic_handler_t value
) {
if (!config) {
return;
}
config->diagnostic_handler = value;
}
void dd_core_config_set_diagnostic_threshold(
dd_core_config_t* config, dd_diagnostic_level_t value
) {
if (!config) {
return;
}
config->diagnostic_threshold = value;
}
void dd_core_config_set_diagnostic_handler_userdata(
dd_core_config_t* config, void* value
) {
if (!config) {
return;
}
config->diagnostic_handler_userdata = value;
}
void dd_core_config_set_application_storage_path(
dd_core_config_t* config, const char* value
) {
if (!config || !value) {
return;
}
const size_t capacity = std::size(config->application_storage_path);
const std::size_t max_len = capacity - 1;
const void* p_null{
std::memchr(value, '\0', max_len + 1)
}; // NOLINT(cppcoreguidelines-init-variables)
if (!p_null) {
auto diagnostic_logger = datadog::impl::DiagnosticLogger::FromC(
config->diagnostic_handler,
config->diagnostic_handler_userdata,
config->diagnostic_threshold
);
diagnostic_logger.Error(
"Unable to accept value passed to dd_core_config_set_application_storage_path: "
"length limit exceeded"
);
return;
}
const size_t len = static_cast<const char*>(p_null) - value;
std::memcpy(static_cast<char*>(config->application_storage_path), value, len);
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
config->application_storage_path[len] = '\0';
}
void dd_core_config_set_site(dd_core_config_t* config, dd_site_t value) {
if (!config) {
return;
}
config->site = value;
}
void dd_core_config_set_client_token(dd_core_config_t* config, const char* value) {
if (!config) {
return;
}
config->client_token = value;
}
void dd_core_config_set_service(dd_core_config_t* config, const char* value) {
if (!config) {
return;
}
config->service = value;
}
void dd_core_config_set_env(dd_core_config_t* config, const char* value) {
if (!config) {
return;
}
config->env = value;
}
void dd_core_config_set_version(dd_core_config_t* config, const char* value) {
if (!config) {
return;
}
config->application_version = value;
}
void dd_core_config_set_variant(dd_core_config_t* config, const char* value) {
if (!config) {
return;
}
config->variant = value;
}
void dd_core_config_set_batch_size(dd_core_config_t* config, dd_batch_size_t value) {
if (!config) {
return;
}
config->batch_size = value;
}
void dd_core_config_set_upload_frequency(
dd_core_config_t* config, dd_upload_frequency_t value
) {
if (!config) {
return;
}
config->upload_frequency = value;
}
void dd_core_config_set_batch_processing_level(
dd_core_config_t* config, dd_batch_processing_level_t value
) {
if (!config) {
return;
}
config->batch_processing_level = value;
}
void dd_core_config_internal_flush_http_requests_on_stop(dd_core_config_t* config) {
if (!config) {
return;
}
config->internal_options.flush_http_requests_on_stop = true;
}
void dd_core_config_internal_set_custom_endpoint_url(
dd_core_config_t* config, const char* value
) {
if (!config) {
return;
}
config->internal_options.custom_endpoint_url = value;
}
void dd_core_config_internal_set_source(dd_core_config_t* config, const char* value) {
if (!config) {
return;
}
config->internal_options.source = value;
}
void dd_core_config_internal_set_sdk_version(
dd_core_config_t* config, const char* value
) {
if (!config) {
return;
}
config->internal_options.sdk_version = value;
}
dd_core_t* dd_core_create(
const dd_core_config_t* config, dd_tracking_consent_t tracking_consent
) {
// If we've not been given a valid config, return null (which effectively serves as a
// no-op interface, since the C API tolerates null arguments)
if (!config) {
return nullptr;
}
// If the config struct was not properly initialized, reject it and make no further
// attempts to read from the provided struct
if (config->version <= 0 || config->version > CORE_CONFIG_VERSION) {
return nullptr;
}
// Prepare a diagnostic logging interface that we can use to emit messages to be
// handled by the application (or written to stderr by default)
auto diagnostic_logger = datadog::impl::DiagnosticLogger::FromC(
config->diagnostic_handler,
config->diagnostic_handler_userdata,
config->diagnostic_threshold
);
// Likewise, if the config is missing any required values, reject it
if (!config->client_token || !config->client_token[0]) {
diagnostic_logger.Error(
"SDK initialization failed: application must supply a non-empty 'client_token' "
"value in dd_core_config_t"
);
return nullptr;
}
if (!config->service || !config->service[0]) {
diagnostic_logger.Error(
"SDK initialization failed: application must supply a non-empty 'service' "
"value in dd_core_config_t"
);
return nullptr;
}
if (!config->env || !config->env[0]) {
diagnostic_logger.Error(
"SDK initialization failed: application must supply a non-empty 'env' value in "
"dd_core_config_t"
);
return nullptr;
}
// Initialize core subsystems using the platform-specific implementations compiled
// in this build
auto subsystems = datadog::impl::CoreSubsystems::Init(diagnostic_logger);
if (!subsystems.has_value()) {
return nullptr;
}
// Convert from dd_core_config_t to datadog::CoreConfig, as the implementation layer
// uses the latter
datadog::CoreConfig cpp_config = datadog::CoreConfig_FromC(*config);
// Create the impl::Core object
auto impl = std::make_unique<datadog::impl::Core>(
cpp_config,
datadog::TrackingConsent_FromC(tracking_consent),
std::move(*subsystems)
);
// Perform mandatory initialization routines that might fail: this may include
// migration of event data from <old-pid>/ to <new-pid>/
if (!impl->Init()) {
return nullptr;
}
// Wrap the core in a dynamically-allocated dd_core struct, which will own our
// implementation via unique_ptr, ensuring cleanup as long as we delete the dd_core
return new dd_core(std::move(impl), std::move(diagnostic_logger));
}
void dd_core_destroy(dd_core_t* core) { delete core; }
void dd_core_set_tracking_consent(dd_core_t* core, dd_tracking_consent_t value) {
if (core && core->impl) {
core->impl->SetTrackingConsent(datadog::TrackingConsent_FromC(value));
}
}
void dd_core_set_user_info(
dd_core_t* core,
const char* id,
const char* name,
const char* email,
const dd_attribute_t* extra_info
) {
if (core && core->impl) {
datadog::Attribute cpp_extra_info;
if (extra_info && extra_info->type == DD_VALUE_TYPE_OBJECT) {
cpp_extra_info = datadog::impl::AttributeConversion::CopyFromC(*extra_info);
}
core->impl->SetUserInfo(
id ? std::string_view{id} : std::string_view{},
name ? std::string_view{name} : std::string_view{},
email ? std::string_view{email} : std::string_view{},
cpp_extra_info
);
}
}
void dd_core_add_user_extra_info(dd_core_t* core, const dd_attribute_t* extra_info) {
if (core && core->impl) {
datadog::Attribute cpp_extra_info;
if (extra_info && extra_info->type == DD_VALUE_TYPE_OBJECT) {
cpp_extra_info = datadog::impl::AttributeConversion::CopyFromC(*extra_info);
}
core->impl->AddUserExtraInfo(cpp_extra_info);
}
}
void dd_core_clear_user_info(dd_core_t* core) {
if (core && core->impl) {
core->impl->ClearUserInfo();
}
}
void dd_core_set_account_info(
dd_core_t* core, const char* id, const char* name, const dd_attribute_t* extra_info
) {
if (core && core->impl) {
datadog::Attribute cpp_extra_info;
if (extra_info && extra_info->type == DD_VALUE_TYPE_OBJECT) {
cpp_extra_info = datadog::impl::AttributeConversion::CopyFromC(*extra_info);
}
core->impl->SetAccountInfo(
id ? std::string_view{id} : std::string_view{},
name ? std::string_view{name} : std::string_view{},
cpp_extra_info
);
}
}
void dd_core_add_account_extra_info(dd_core_t* core, const dd_attribute_t* extra_info) {
if (core && core->impl) {
datadog::Attribute cpp_extra_info;
if (extra_info && extra_info->type == DD_VALUE_TYPE_OBJECT) {
cpp_extra_info = datadog::impl::AttributeConversion::CopyFromC(*extra_info);
}
core->impl->AddAccountExtraInfo(cpp_extra_info);
}
}
void dd_core_clear_account_info(dd_core_t* core) {
if (core && core->impl) {
core->impl->ClearAccountInfo();
}
}
bool dd_core_start(dd_core_t* core) {
if (core && core->impl) {
return core->impl->Start();
}
return false;
}
void dd_core_stop(dd_core_t* core) {
if (core && core->impl) {
core->impl->Stop();
}
}
}
// NOLINTEND(cppcoreguidelines-owning-memory)