-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrum.cpp
More file actions
695 lines (599 loc) · 21.5 KB
/
Copy pathrum.cpp
File metadata and controls
695 lines (599 loc) · 21.5 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// 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/rum.h"
#include <cctype>
#include <memory>
#include "datadog/core.h"
#include "datadog/uuid.hpp"
#include "datadog/c/core_glue.hpp"
#include "datadog/c/rum_glue.hpp"
#include "datadog/impl/core/attribute/types.hpp"
#include "datadog/impl/core/core.hpp"
#include "datadog/impl/core/util/validation.hpp"
#include "datadog/impl/rum/rum.hpp"
#include "datadog/impl/rum/types.hpp"
static const uint32_t RUM_CONFIG_VERSION = 1;
static const dd_rum_config_t DEFAULT_RUM_CONFIG = {
RUM_CONFIG_VERSION, // version
dd_uuid_t{}, // application_id
100.0f // session_sample_rate
};
// NOLINTBEGIN(cppcoreguidelines-owning-memory)
extern "C" {
void dd_rum_config_init(dd_rum_config_t* config, const char* application_id) {
// Require a valid config struct and valid string
if (!config || !application_id) {
return;
}
// Parse application ID as a UUID value
const auto application_id_opt = datadog::UUID::Parse(application_id);
const datadog::UUID uuid_value = application_id_opt.value_or(datadog::UUID::Zero);
// Populate struct fields
*config = DEFAULT_RUM_CONFIG;
dd_uuid_set(&config->application_id, uuid_value.bytes.data());
}
void dd_rum_config_set_application_id(dd_rum_config_t* config, const char* value) {
if (config && value) {
const auto value_opt = datadog::UUID::Parse(value);
const datadog::UUID uuid_value = value_opt.value_or(datadog::UUID::Zero);
dd_uuid_set(&config->application_id, uuid_value.bytes.data());
}
}
void dd_rum_config_set_session_sample_rate(dd_rum_config_t* config, float value) {
if (config) {
config->session_sample_rate = value;
}
}
dd_rum_t* dd_rum_init(dd_core_t* core, const dd_rum_config_t* config) {
// Require a valid core: note that in the C API, where we tolerate null arguments to
// all API functions, returning NULL is effectively returning a no-op implementation
if (!core || !core->impl) {
return nullptr;
}
// Require a valid config: RUM requires an application ID at the very least, so we
// can't fall back to defaults
if (!config) {
core->diagnostic_logger.Error(
"RUM initialization failed: application must supply a valid dd_rum_config_t "
"value to dd_rum_init"
);
return nullptr;
}
// If the config has an unrecognized version, it's not properly initialized in a form
// that we can safely read
if (config->version <= 0 || config->version > RUM_CONFIG_VERSION) {
core->diagnostic_logger.Error(
"RUM initialization failed: dd_rum_config_t value must be initialized via "
"dd_rum_config_init"
);
return nullptr;
}
// If the config doesn't specify an application ID as a valid, nonzero UUID, reject it
if (dd_uuid_is_zero(&config->application_id)) {
core->diagnostic_logger.Error(
"RUM initialization failed: application_id value supplied via dd_rum_config_t "
"must be a valid, nonzero UUID"
);
return nullptr;
}
// Convert from dd_rum_config_t to datadog::RumConfig
datadog::RumConfig cpp_config = datadog::RumConfig_FromC(*config);
// Get essential state from the Core
const datadog::platform::IClock& clock = core->impl->GetClock();
// Initialize our RUM feature implementation
auto rum_impl = std::make_shared<datadog::impl::Rum>(cpp_config, clock);
// Register the feature with the core, returning a no-op interface on failure
if (!core->impl->RegisterFeature(rum_impl)) {
return nullptr;
}
// Initialize and return the API object that represents our user-facing interface
// for the RUM feature, injecting a copy of the core's DiagnosticLogger so that future
// RUM API calls can emit warnings etc.
return new dd_rum(std::move(rum_impl), core->diagnostic_logger);
}
void dd_rum_destroy(dd_rum_t* rum) { delete rum; }
void dd_rum_add_attribute(
dd_rum_t* rum, const char* name, const dd_attribute_t* value
) {
// Permit no-op calls
if (!rum || !rum->impl) {
return;
}
// Require a valid string, but allow "" as an attribute name
if (!name) {
rum->diagnostic_logger.Warning(
"dd_rum_add_attribute call ignored: application must supply an attribute name"
);
return;
}
// Require a valid attribute value
if (!value) {
rum->diagnostic_logger.Warning(
"dd_rum_add_attribute call ignored: application must supply an attribute value"
);
return;
}
// Copy to datadog::Attribute
rum->impl->AddAttribute(name, datadog::impl::AttributeConversion::CopyFromC(*value));
}
void dd_rum_remove_attribute(dd_rum_t* rum, const char* name) {
// Permit no-op calls
if (!rum || !rum->impl) {
return;
}
// Require a valid string, but allow "" as an attribute name
if (!name) {
rum->diagnostic_logger.Warning(
"dd_rum_remove_attribute call ignored: application must supply an attribute "
"name"
);
return;
}
rum->impl->RemoveAttribute(name);
}
void dd_rum_stop_session(dd_rum_t* rum) {
if (!rum || !rum->impl) {
return;
}
rum->impl->StopSession();
}
void dd_rum_start_view(
dd_rum_t* rum, const char* key, const char* name, const dd_attribute_t* attributes
) {
// Permit no-op calls
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty string for view key
if (!key || !key[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_start_view call ignored: application must supply a non-empty view key"
);
return;
}
// Construct a string_view of our name value, leaving it empty if no name string given
std::string_view cpp_name{};
if (name) {
cpp_name = name;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// Defer to the feature implementation
rum->impl->StartView(key, cpp_name, cpp_attributes);
}
void dd_rum_add_view_attribute(
dd_rum_t* rum, const char* name, const dd_attribute_t* value
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid string, but allow "" as an attribute name
if (!name) {
rum->diagnostic_logger.Warning(
"dd_rum_add_view_attribute call ignored: application must supply an attribute "
"name"
);
return;
}
// Require a valid attribute value
if (!value) {
rum->diagnostic_logger.Warning(
"dd_rum_add_view_attribute call ignored: application must supply an attribute "
"value"
);
return;
}
datadog::Attribute cpp_value = datadog::impl::AttributeConversion::CopyFromC(*value);
rum->impl->AddViewAttribute(name, cpp_value);
}
void dd_rum_remove_view_attribute(dd_rum_t* rum, const char* name) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid string, but allow "" as an attribute name
if (!name) {
rum->diagnostic_logger.Warning(
"dd_rum_remove_view_attribute call ignored: application must supply an "
"attribute name"
);
return;
}
rum->impl->RemoveViewAttribute(name);
}
void dd_rum_stop_view(
dd_rum_t* rum, const char* key, const dd_attribute_t* attributes
) {
// Permit no-op calls
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty string for view key
if (!key || !key[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_stop_view call ignored: application must supply a non-empty view key"
);
return;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// Defer to the feature implementation
rum->impl->StopView(key, cpp_attributes);
}
void dd_rum_add_action(
dd_rum_t* rum,
dd_rum_action_type_t type,
const char* name,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty action name
if (!name || !name[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_add_action call ignored: application must supply a non-empty action "
"name"
);
return;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
rum->impl->AddAction(datadog::RumActionType_FromC(type), name, cpp_attributes);
}
void dd_rum_start_action(
dd_rum_t* rum,
dd_rum_action_type_t type,
const char* name,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty action name
if (!name || !name[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_start_action call ignored: application must supply a non-empty action "
"name"
);
return;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
rum->impl->StartAction(datadog::RumActionType_FromC(type), name, cpp_attributes);
}
void dd_rum_stop_action(
dd_rum_t* rum,
dd_rum_action_type_t type,
const char* name,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// If we've been given a name, use it to rename the current action on stop; but allow
// either NULL or empty-string
std::string_view cpp_name;
if (name) {
cpp_name = name;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// The RUM implementation doesn't actually use the provided RumActionType value when
// stopping an action: it just stops the currently-active action without matching on
// action type. We accept the parameter anyway for consistency.
(void)type;
rum->impl->StopAction(cpp_name, cpp_attributes);
}
void dd_rum_start_resource(
dd_rum_t* rum,
const char* key,
dd_rum_resource_method_t method,
const char* url,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty resource key and URL
if (!key || !key[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_start_resource call ignored: application must supply a non-empty "
"resource key"
);
return;
}
if (!url || !url[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_start_resource call ignored: application must supply a non-empty URL"
);
return;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// Call StartResource on our RUM feature implementation, passing HTTP request details
const datadog::impl::RumRequestDetails request{
datadog::RumResourceMethod_FromC(method), url
};
rum->impl->StartResource(key, request, cpp_attributes);
}
void dd_rum_stop_resource(
dd_rum_t* rum,
const char* key,
int32_t status_code,
int64_t size,
dd_rum_resource_type_t type,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty resource key
if (!key || !key[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_stop_resource call ignored: application must supply a non-empty "
"resource key"
);
return;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// Call StopResource on our RUM feature implementation, passing HTTP response details
const datadog::impl::RumResponseDetails response{
status_code, size, datadog::RumResourceType_FromC(type)
};
rum->impl->StopResource(key, response, std::nullopt, cpp_attributes);
}
void dd_rum_stop_resource_with_error(
dd_rum_t* rum,
const char* key,
const char* error_message,
const char* error_type,
const char* error_stack_trace,
bool is_network_error,
int32_t status_code,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Require a valid, non-empty resource key
if (!key || !key[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_stop_resource_with_error call ignored: application must supply a "
"non-empty resource key"
);
return;
}
// If no error message is given, allow it (as the schema for RUM error events does not
// forbid empty messages) but log a warning
if (!error_message || !error_message[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_stop_resource_with_error recording an error with no message: "
"application should supply a non-empty error message"
);
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// Call StopResource on our RUM feature implementation, passing error details
const datadog::impl::RumResponseDetails response{status_code};
const datadog::impl::RumErrorDetails error{
error_message ? error_message : "",
error_type ? error_type : "",
error_stack_trace ? error_stack_trace : "",
is_network_error
};
rum->impl->StopResource(key, response, error, cpp_attributes);
}
void dd_rum_start_operation(
dd_rum_t* rum,
const char* name,
const char* operation_key,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// Backend rejects blank/empty names with its own non-empty precondition;
// drop client-side to match. Also catches the NULL pointer case.
if (datadog::impl::IsBlankCString(name)) {
rum->diagnostic_logger.Error(
"dd_rum_start_operation call ignored: application must supply a "
"non-empty operation name"
);
return;
}
// Warn if the name fails the backend's `[\w.@$-]*` pattern, but always
// emit: the backend is the source of truth on character-set policy.
if (!datadog::impl::HasOnlyAllowedOperationNameCharacters(name)) {
rum->diagnostic_logger.Warning(
"dd_rum_start_operation: operation name does not match the "
"backend-accepted pattern [\\w.@$-]* (letters, digits, _ . @ $ -). The event "
"will still be sent and may be rejected by the backend."
);
}
// If operation_key is provided (non-NULL, non-empty), it must be non-blank
if (operation_key && operation_key[0] &&
datadog::impl::IsBlankCString(operation_key)) {
rum->diagnostic_logger.Error(
"dd_rum_start_operation call ignored: operation_key, if provided, must "
"be a non-empty string"
);
return;
}
// Convert optional operation_key: NULL or empty means no key
std::optional<std::string_view> opt_key;
if (operation_key && operation_key[0]) {
opt_key = operation_key;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes;
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
rum->impl->StartOperation(name, opt_key, cpp_attributes);
}
void dd_rum_succeed_operation(
dd_rum_t* rum,
const char* name,
const char* operation_key,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
if (datadog::impl::IsBlankCString(name)) {
rum->diagnostic_logger.Error(
"dd_rum_succeed_operation call ignored: application must supply a "
"non-empty operation name"
);
return;
}
if (!datadog::impl::HasOnlyAllowedOperationNameCharacters(name)) {
rum->diagnostic_logger.Warning(
"dd_rum_succeed_operation: operation name does not match the "
"backend-accepted pattern [\\w.@$-]* (letters, digits, _ . @ $ -). The event "
"will still be sent and may be rejected by the backend."
);
}
// If operation_key is provided (non-NULL, non-empty), it must be non-blank
if (operation_key && operation_key[0] &&
datadog::impl::IsBlankCString(operation_key)) {
rum->diagnostic_logger.Error(
"dd_rum_succeed_operation call ignored: operation_key, if provided, "
"must be a non-empty string"
);
return;
}
// Convert optional operation_key: NULL or empty means no key
std::optional<std::string_view> opt_key;
if (operation_key && operation_key[0]) {
opt_key = operation_key;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes;
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
rum->impl->StopOperation(name, opt_key, std::nullopt, cpp_attributes);
}
void dd_rum_fail_operation(
dd_rum_t* rum,
const char* name,
dd_rum_failure_reason_t failure_reason,
const char* operation_key,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
if (datadog::impl::IsBlankCString(name)) {
rum->diagnostic_logger.Error(
"dd_rum_fail_operation call ignored: application must supply a "
"non-empty operation name"
);
return;
}
if (!datadog::impl::HasOnlyAllowedOperationNameCharacters(name)) {
rum->diagnostic_logger.Warning(
"dd_rum_fail_operation: operation name does not match the "
"backend-accepted pattern [\\w.@$-]* (letters, digits, _ . @ $ -). The event "
"will still be sent and may be rejected by the backend."
);
}
// If operation_key is provided (non-NULL, non-empty), it must be non-blank
if (operation_key && operation_key[0] &&
datadog::impl::IsBlankCString(operation_key)) {
rum->diagnostic_logger.Error(
"dd_rum_fail_operation call ignored: operation_key, if provided, must "
"be a non-empty string"
);
return;
}
// Convert optional operation_key: NULL or empty means no key
std::optional<std::string_view> opt_key;
if (operation_key && operation_key[0]) {
opt_key = operation_key;
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes;
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
rum->impl->StopOperation(
name,
opt_key,
datadog::RumOperationFailureReason_FromC(failure_reason),
cpp_attributes
);
}
void dd_rum_add_error(
dd_rum_t* rum,
dd_rum_error_source_t source,
const char* message,
const char* type,
const char* stack_trace,
dd_attribute_t* attributes
) {
// If the underlying feature is NULL, this call is a no-op
if (!rum || !rum->impl) {
return;
}
// If no error message is given, allow it (as the schema for RUM error events does not
// forbid empty messages) but log a warning
if (!message || !message[0]) {
rum->diagnostic_logger.Warning(
"dd_rum_add_error recording an error with no message: application should "
"supply a non-empty error message"
);
}
// If we've been given a valid object attribute, convert it to the equivalent C++ type
datadog::Attribute cpp_attributes; // Default-initialized to Attribute::Null()
if (attributes && attributes->type == DD_VALUE_TYPE_OBJECT) {
cpp_attributes = datadog::impl::AttributeConversion::CopyFromC(*attributes);
}
// Call AddError on our RUM feature implementation
const datadog::impl::RumErrorDetails error{
message ? message : "", type ? type : "", stack_trace ? stack_trace : ""
};
rum->impl->AddError(datadog::RumErrorSource_FromC(source), error, cpp_attributes);
}
}
// NOLINTEND(cppcoreguidelines-owning-memory)