Skip to content

Commit f32784c

Browse files
feat(prof): add source code integration (#3418)
* add basic SCI support via env variables * move SCI env vars to config * leave protocol in URL * refactor: rename var since meaning changed --------- Co-authored-by: Levi Morrison <[email protected]>
1 parent c70ff24 commit f32784c

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

profiling/src/config.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ pub(crate) enum ConfigId {
379379
TraceAgentPort,
380380
TraceAgentUrl,
381381
Version,
382+
GitCommitSha,
383+
GitRepositoryUrl,
382384
}
383385

384386
use ConfigId::*;
@@ -411,6 +413,8 @@ impl ConfigId {
411413
TraceAgentPort => b"DD_TRACE_AGENT_PORT\0",
412414
TraceAgentUrl => b"DD_TRACE_AGENT_URL\0",
413415
Version => b"DD_VERSION\0",
416+
GitCommitSha => b"DD_GIT_COMMIT_SHA\0",
417+
GitRepositoryUrl => b"DD_GIT_REPOSITORY_URL\0",
414418
};
415419

416420
// Safety: all these byte strings are [CStr::from_bytes_with_nul_unchecked] compatible.
@@ -673,6 +677,20 @@ pub(crate) unsafe fn version() -> Option<String> {
673677
get_str(Version)
674678
}
675679

680+
/// # Safety
681+
/// This function must only be called after config has been initialized in
682+
/// rinit, and before it is uninitialized in mshutdown.
683+
pub(crate) unsafe fn git_commit_sha() -> Option<String> {
684+
get_str(GitCommitSha)
685+
}
686+
687+
/// # Safety
688+
/// This function must only be called after config has been initialized in
689+
/// rinit, and before it is uninitialized in mshutdown.
690+
pub(crate) unsafe fn git_repository_url() -> Option<String> {
691+
get_str(GitRepositoryUrl)
692+
}
693+
676694
/// # Safety
677695
/// This function must only be called after config has been initialized in
678696
/// rinit, and before it is uninitialized in mshutdown.
@@ -1164,6 +1182,30 @@ pub(crate) fn minit(module_number: libc::c_int) {
11641182
displayer: None,
11651183
env_config_fallback: None,
11661184
},
1185+
zai_config_entry {
1186+
id: transmute::<ConfigId, u16>(GitCommitSha),
1187+
name: GitCommitSha.env_var_name(),
1188+
type_: ZAI_CONFIG_TYPE_STRING,
1189+
default_encoded_value: ZaiStr::new(),
1190+
aliases: ptr::null_mut(),
1191+
aliases_count: 0,
1192+
ini_change: None,
1193+
parser: Some(parse_utf8_string),
1194+
displayer: None,
1195+
env_config_fallback: None,
1196+
},
1197+
zai_config_entry {
1198+
id: transmute::<ConfigId, u16>(GitRepositoryUrl),
1199+
name: GitRepositoryUrl.env_var_name(),
1200+
type_: ZAI_CONFIG_TYPE_STRING,
1201+
default_encoded_value: ZaiStr::new(),
1202+
aliases: ptr::null_mut(),
1203+
aliases_count: 0,
1204+
ini_change: None,
1205+
parser: Some(parse_utf8_string),
1206+
displayer: None,
1207+
env_config_fallback: None,
1208+
},
11671209
]
11681210
};
11691211

@@ -1208,6 +1250,8 @@ mod tests {
12081250
(b"DD_SERVICE\0", "datadog.service"),
12091251
(b"DD_ENV\0", "datadog.env"),
12101252
(b"DD_VERSION\0", "datadog.version"),
1253+
(b"DD_GIT_COMMIT_SHA\0", "datadog.git_commit_sha"),
1254+
(b"DD_GIT_REPOSITORY_URL\0", "datadog.git_repository_url"),
12111255
(b"DD_TRACE_AGENT_URL\0", "datadog.trace.agent_url"),
12121256
(b"DD_TRACE_AGENT_PORT\0", "datadog.trace.agent_port"),
12131257
(b"DD_AGENT_HOST\0", "datadog.agent_host"),

profiling/src/lib.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ pub struct RequestLocals {
413413
pub env: Option<String>,
414414
pub service: Option<String>,
415415
pub version: Option<String>,
416+
pub git_commit_sha: Option<String>,
417+
pub git_repository_url: Option<String>,
416418
pub tags: Vec<Tag>,
417419

418420
/// SystemSettings are global. Note that if this is being read in fringe
@@ -442,6 +444,8 @@ impl Default for RequestLocals {
442444
env: None,
443445
service: None,
444446
version: None,
447+
git_commit_sha: None,
448+
git_repository_url: None,
445449
tags: vec![],
446450
system_settings: ptr::NonNull::from(INITIAL_SYSTEM_SETTINGS.deref()),
447451
interrupt_count: AtomicU32::new(0),
@@ -590,6 +594,21 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
590594
}
591595
});
592596
locals.version = config::version();
597+
locals.git_commit_sha = config::git_commit_sha();
598+
locals.git_repository_url = config::git_repository_url().map(|val| {
599+
// Remove potential credentials, customers are encouraged to not send those anyway.
600+
if let Some(at_pos) = val.find("@") {
601+
if let Some(proto_pos) = val.find("://") {
602+
// Keep protocol, but remove credentials
603+
format!("{}{}", &val[..(proto_pos + 3)], &val[(at_pos + 1)..])
604+
} else {
605+
// No protocol, just remove everything before @
606+
val[(at_pos + 1)..].to_string()
607+
}
608+
} else {
609+
val
610+
}
611+
});
593612

594613
let (tags, maybe_err) = config::tags();
595614
if let Some(err) = maybe_err {
@@ -682,16 +701,20 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
682701
TAGS.set({
683702
// SAFETY: accessing in RINIT after config is initialized.
684703
let globals = GLOBAL_TAGS.deref();
685-
let unified_service_tags_len = locals.service.is_some() as usize
704+
let extra_tags_len = locals.service.is_some() as usize
686705
+ locals.env.is_some() as usize
687-
+ locals.version.is_some() as usize;
706+
+ locals.version.is_some() as usize
707+
+ locals.git_commit_sha.is_some() as usize
708+
+ locals.git_repository_url.is_some() as usize;
688709

689710
let mut tags = Vec::new();
690-
tags.reserve_exact(globals.len() + unified_service_tags_len + locals.tags.len());
711+
tags.reserve_exact(globals.len() + extra_tags_len + locals.tags.len());
691712
tags.extend_from_slice(globals.as_slice());
692713
add_optional_tag(&mut tags, "service", &locals.service);
693714
add_optional_tag(&mut tags, "env", &locals.env);
694715
add_optional_tag(&mut tags, "version", &locals.version);
716+
add_optional_tag(&mut tags, "git.commit.sha", &locals.git_commit_sha);
717+
add_optional_tag(&mut tags, "git.repository_url", &locals.git_repository_url);
695718
tags.extend_from_slice(locals.tags.as_slice());
696719
Arc::new(tags)
697720
});

0 commit comments

Comments
 (0)