Skip to content

Commit b8bf258

Browse files
authored
refactor(prof): tag handling (#3423)
Tags are now divided into three groups: 1. Globals which are language, profiler version, process ID, etc. 2. Unified service tags which are service, env, and version. 3. Locals, which are request-local tags where DD_TAGS are stored. Unified service tag are also request-local but stored separately. Globals are initialized lazily. This moves more tags into global. The warning for an invalid tag now includes the key name. All our tag keys are static, so there's not privacy concern to include it. The `Vec<Tag>` now reserves exactly how much memory it needs. I don't expect this to be a meaningful performance change, just doing it while I'm here.
1 parent 4c233be commit b8bf258

1 file changed

Lines changed: 35 additions & 26 deletions

File tree

profiling/src/lib.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,35 @@ static mut RUNTIME_PHP_VERSION: &str = {
7676
};
7777

7878
lazy_static! {
79-
static ref LAZY_STATICS_TAGS: Vec<Tag> = {
80-
vec![
79+
/// # Safety
80+
/// The first time this is accessed must be after config is initialized in
81+
/// the first RINIT and before mshutdown!
82+
static ref GLOBAL_TAGS: Vec<Tag> = {
83+
let mut tags = vec![
8184
tag!("language", "php"),
8285
tag!("profiler_version", env!("PROFILER_VERSION")),
8386
// SAFETY: calling getpid() is safe.
8487
Tag::new("process_id", unsafe { libc::getpid() }.to_string())
8588
.expect("process_id tag to be valid"),
8689
Tag::new("runtime-id", runtime_id().to_string()).expect("runtime-id tag to be valid"),
87-
]
90+
];
91+
92+
// This should probably be "language_version", but this is the
93+
// standardized tag name.
94+
// SAFETY: PHP_VERSION is safe to access in rinit (only
95+
// mutated during minit).
96+
add_tag(&mut tags, "runtime_version", unsafe { RUNTIME_PHP_VERSION });
97+
add_tag(&mut tags, "php.sapi", SAPI.as_ref());
98+
// In case we ever add PHP debug build support, we should add `zend-zts-debug` and
99+
// `zend-nts-debug`. For the time being we only support `zend-zts-ndebug` and
100+
// `zend-nts-ndebug`
101+
let runtime_engine = if cfg!(php_zts) {
102+
"zend-zts-ndebug"
103+
} else {
104+
"zend-nts-ndebug"
105+
};
106+
add_tag(&mut tags, "runtime_engine", runtime_engine);
107+
tags
88108
};
89109

90110
/// The Server API the profiler is running under.
@@ -660,26 +680,19 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
660680
CLOCKS.with_borrow_mut(|clocks| clocks.initialize(cpu_time_enabled));
661681

662682
TAGS.set({
663-
let mut tags = LAZY_STATICS_TAGS.clone();
683+
// SAFETY: accessing in RINIT after config is initialized.
684+
let globals = GLOBAL_TAGS.deref();
685+
let unified_service_tags_len = locals.service.is_some() as usize
686+
+ locals.env.is_some() as usize
687+
+ locals.version.is_some() as usize;
688+
689+
let mut tags = Vec::new();
690+
tags.reserve_exact(globals.len() + unified_service_tags_len + locals.tags.len());
691+
tags.extend_from_slice(globals.as_slice());
664692
add_optional_tag(&mut tags, "service", &locals.service);
665693
add_optional_tag(&mut tags, "env", &locals.env);
666694
add_optional_tag(&mut tags, "version", &locals.version);
667-
// This should probably be "language_version", but this is the
668-
// standardized tag name.
669-
// SAFETY: PHP_VERSION is safe to access in rinit (only
670-
// mutated during minit).
671-
add_tag(&mut tags, "runtime_version", unsafe { RUNTIME_PHP_VERSION });
672-
add_tag(&mut tags, "php.sapi", SAPI.as_ref());
673-
// In case we ever add PHP debug build support, we should add `zend-zts-debug` and
674-
// `zend-nts-debug`. For the time being we only support `zend-zts-ndebug` and
675-
// `zend-nts-ndebug`
676-
let runtime_engine = if cfg!(php_zts) {
677-
"zend-zts-ndebug"
678-
} else {
679-
"zend-nts-ndebug"
680-
};
681-
add_tag(&mut tags, "runtime_engine", runtime_engine);
682-
tags.extend_from_slice(&locals.tags);
695+
tags.extend_from_slice(locals.tags.as_slice());
683696
Arc::new(tags)
684697
});
685698

@@ -721,12 +734,8 @@ fn add_optional_tag<T: AsRef<str>>(tags: &mut Vec<Tag>, key: &str, value: &Optio
721734
fn add_tag(tags: &mut Vec<Tag>, key: &str, value: &str) {
722735
assert!(!value.is_empty());
723736
match Tag::new(key, value) {
724-
Ok(tag) => {
725-
tags.push(tag);
726-
}
727-
Err(err) => {
728-
warn!("invalid tag: {err}");
729-
}
737+
Ok(tag) => tags.push(tag),
738+
Err(err) => warn!("invalid {key} tag: {err}"),
730739
}
731740
}
732741

0 commit comments

Comments
 (0)