Skip to content

Commit bd7d8b0

Browse files
morrisonlevirealFlowControl
authored andcommitted
feat(prof): add DD_TAGS support (#3121)
* build: update Cargo.lock * feat(prof): add DD_TAGS support "Fun" fact: the profiler once had DD_TAGS support, and this is why there is tag parsing functions in libdatadog. I think it was lost when the profiler adopted ZAI config, which was quite a long time ago.
1 parent 6b2757b commit bd7d8b0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

profiling/src/config.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use core::mem::{swap, transmute, MaybeUninit};
1212
use core::ptr;
1313
use core::str::FromStr;
1414
pub use datadog_profiling::exporter::Uri;
15+
use ddcommon::tag::{parse_tags, Tag};
1516
use libc::c_char;
1617
use log::{warn, LevelFilter};
1718
use std::borrow::Cow;
@@ -643,6 +644,16 @@ pub(crate) unsafe fn version() -> Option<String> {
643644
get_str(Version)
644645
}
645646

647+
/// # Safety
648+
/// This function must only be called after config has been initialized in
649+
/// rinit, and before it is uninitialized in mshutdown.
650+
pub(crate) unsafe fn tags() -> (Vec<Tag>, Option<String>) {
651+
match get_str(Tags) {
652+
None => (Vec::new(), None),
653+
Some(dd_tags) => parse_tags(&dd_tags),
654+
}
655+
}
656+
646657
/// # Safety
647658
/// This function must only be called after config has been initialized in
648659
/// first rinit, and before it is uninitialized in mshutdown.
@@ -978,7 +989,11 @@ pub(crate) fn minit(module_number: libc::c_int) {
978989
zai_config_entry {
979990
id: transmute::<ConfigId, u16>(Tags),
980991
name: Tags.env_var_name(),
981-
type_: ZAI_CONFIG_TYPE_MAP,
992+
// Using a string here means we're going to parse the
993+
// string into tags over and over, but since it needs to
994+
// be a valid zval for destruction, we can't just use a
995+
// Box::leak of Vec<Tag> or something.
996+
type_: ZAI_CONFIG_TYPE_STRING,
982997
default_encoded_value: ZaiStr::new(),
983998
aliases: ptr::null_mut(),
984999
aliases_count: 0,

profiling/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ pub struct RequestLocals {
380380
pub env: Option<String>,
381381
pub service: Option<String>,
382382
pub version: Option<String>,
383+
pub tags: Vec<Tag>,
383384

384385
/// SystemSettings are global. Note that if this is being read in fringe
385386
/// conditions such as in mshutdown when there were no requests served,
@@ -408,6 +409,7 @@ impl Default for RequestLocals {
408409
env: None,
409410
service: None,
410411
version: None,
412+
tags: vec![],
411413
system_settings: ptr::NonNull::from(INITIAL_SYSTEM_SETTINGS.deref()),
412414
interrupt_count: AtomicU32::new(0),
413415
vm_interrupt_addr: ptr::null_mut(),
@@ -488,6 +490,15 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
488490
}
489491
});
490492
locals.version = config::version();
493+
494+
let (tags, maybe_err) = config::tags();
495+
if let Some(err) = maybe_err {
496+
// DD_TAGS can change on each request, so this warns on every
497+
// request. Maybe we should cache the error string and only
498+
// emit warnings for new ones?
499+
warn!("{err}");
500+
}
501+
locals.tags = tags;
491502
}
492503
locals.system_settings = system_settings;
493504
});
@@ -581,6 +592,7 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
581592
"zend-nts-ndebug"
582593
};
583594
add_tag(&mut tags, "runtime_engine", runtime_engine);
595+
tags.extend_from_slice(&locals.tags);
584596
cell.replace(Arc::new(tags));
585597
});
586598

0 commit comments

Comments
 (0)