27
27
#if defined(WITH_RUM)
28
28
#include " rum/config.h"
29
29
#endif
30
+ #include " common/variable.h"
30
31
#include " string_util.h"
31
32
#include " tracing_library.h"
32
33
#include " version.h"
@@ -314,14 +315,15 @@ static ngx_int_t datadog_module_init(ngx_conf_t *cf) noexcept {
314
315
315
316
// Add default span tags.
316
317
const auto tags = TracingLibrary::default_tags ();
317
- if (tags.empty ()) return NGX_OK;
318
- main_conf->tags =
319
- ngx_array_create (cf->pool , tags.size (), sizeof (datadog_tag_t ));
320
- if (!main_conf->tags ) return NGX_ERROR;
321
- for (const auto &tag : tags) {
322
- if (add_datadog_tag (cf, main_conf->tags , to_ngx_str (tag.first ),
323
- to_ngx_str (tag.second )) != NGX_CONF_OK) {
324
- return NGX_ERROR;
318
+ if (!tags.empty ()) {
319
+ for (const auto [key, value] : tags) {
320
+ auto ngx_value = to_ngx_str (cf->pool , value);
321
+ auto *complex_value = datadog::common::make_complex_value (cf, ngx_value);
322
+ if (complex_value == nullptr ) {
323
+ return NGX_ERROR;
324
+ }
325
+
326
+ main_conf->tags .insert_or_assign (std::string (key), complex_value);
325
327
}
326
328
}
327
329
@@ -431,33 +433,13 @@ static void *create_datadog_loc_conf(ngx_conf_t *conf) noexcept {
431
433
return loc_conf;
432
434
}
433
435
434
- static ngx_http_complex_value_t *make_default_complex_value (
435
- ngx_conf_t *cf, std::string_view default_value) {
436
- ngx_str_t value = to_ngx_str (default_value);
437
- auto *cv = (ngx_http_complex_value_t *)ngx_pcalloc (
438
- cf->pool , sizeof (ngx_http_complex_value_t ));
439
-
440
- ngx_http_compile_complex_value_t ccv;
441
- ngx_memzero (&ccv, sizeof (ngx_http_compile_complex_value_t ));
442
-
443
- ccv.cf = cf;
444
- ccv.value = &value;
445
- ccv.complex_value = cv;
446
- ccv.zero = 0 ;
447
- ccv.conf_prefix = 0 ;
448
-
449
- if (ngx_http_compile_complex_value (&ccv) != NGX_OK) {
450
- return nullptr ;
451
- }
452
-
453
- return cv;
454
- }
455
-
456
436
// ------------------------------------------------------------------------------
457
437
// merge_datadog_loc_conf
458
438
// ------------------------------------------------------------------------------
459
439
static char *merge_datadog_loc_conf (ngx_conf_t *cf, void *parent,
460
440
void *child) noexcept {
441
+ using namespace datadog ::common;
442
+
461
443
auto prev = static_cast <datadog_loc_conf_t *>(parent);
462
444
auto conf = static_cast <datadog_loc_conf_t *>(child);
463
445
@@ -474,65 +456,29 @@ static char *merge_datadog_loc_conf(ngx_conf_t *cf, void *parent,
474
456
nullptr );
475
457
ngx_conf_merge_ptr_value (
476
458
conf->operation_name_script , prev->operation_name_script ,
477
- make_default_complex_value (
459
+ make_complex_value (
478
460
cf, TracingLibrary::default_request_operation_name_pattern ()));
479
461
ngx_conf_merge_ptr_value (
480
462
conf->loc_operation_name_script , prev->loc_operation_name_script ,
481
- make_default_complex_value (
463
+ make_complex_value (
482
464
cf, TracingLibrary::default_location_operation_name_pattern ()));
483
465
ngx_conf_merge_ptr_value (
484
466
conf->resource_name_script , prev->resource_name_script ,
485
- make_default_complex_value (
486
- cf, TracingLibrary::default_resource_name_pattern ()));
467
+ make_complex_value (cf, TracingLibrary::default_resource_name_pattern ()));
487
468
ngx_conf_merge_ptr_value (
488
469
conf->loc_resource_name_script , prev->loc_resource_name_script ,
489
- make_default_complex_value (
490
- cf, TracingLibrary::default_resource_name_pattern ()));
470
+ make_complex_value (cf, TracingLibrary::default_resource_name_pattern ()));
491
471
ngx_conf_merge_value (conf->trust_incoming_span , prev->trust_incoming_span , 1 );
492
472
493
473
// Create a new array that joins `prev->tags` and `conf->tags`. Since tags
494
474
// are set consecutively and setting a tag with the same key as a previous
495
475
// one overwrites it, we need to ensure that the tags in `conf->tags` come
496
476
// after `prev->tags` so as to keep the value from the most specific
497
477
// configuration.
498
- if (prev->tags && !conf->tags ) {
499
- conf->tags = prev->tags ;
500
- } else if (prev->tags && conf->tags ) {
501
- std::unordered_map<std::string, datadog_tag_t > merged_tags;
502
-
503
- for (ngx_uint_t i = 0 ; i < prev->tags ->nelts ; i++) {
504
- datadog_tag_t *tag = &((datadog_tag_t *)prev->tags ->elts )[i];
505
- std::string key;
506
- key.assign (reinterpret_cast <const char *>(tag->key_script .pattern_ .data ),
507
- tag->key_script .pattern_ .len );
508
- merged_tags[key] = *tag;
509
- }
510
-
511
- for (ngx_uint_t i = 0 ; i < conf->tags ->nelts ; i++) {
512
- datadog_tag_t *tag = &((datadog_tag_t *)conf->tags ->elts )[i];
513
- std::string key;
514
- key.assign (reinterpret_cast <const char *>(tag->key_script .pattern_ .data ),
515
- tag->key_script .pattern_ .len );
516
- merged_tags[key] = *tag;
517
- }
518
-
519
- ngx_uint_t index = 0 ;
520
- for (const auto &kv : merged_tags) {
521
- if (index == conf->tags ->nelts ) {
522
- datadog_tag_t *tag = (datadog_tag_t *)ngx_array_push (conf->tags );
523
-
524
- if (!tag) {
525
- return (char *)NGX_CONF_ERROR;
526
- }
527
-
528
- *tag = kv.second ;
529
- } else {
530
- datadog_tag_t *tags = (datadog_tag_t *)conf->tags ->elts ;
531
- tags[index ] = kv.second ;
532
- }
533
-
534
- index ++;
535
- }
478
+ if (!prev->tags .empty ()) {
479
+ auto parent_tags =
480
+ prev->tags ; // /< Make a copy because merge steal the nodes.
481
+ conf->tags .merge (parent_tags);
536
482
}
537
483
538
484
#ifdef WITH_WAF
0 commit comments