Skip to content

Commit f81a47b

Browse files
authored
feat: detect process.runtime.name and process.runtime.version (#251)
Signed-off-by: bestgopher <[email protected]>
1 parent 167d82f commit f81a47b

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

opentelemetry-resource-detectors/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## vNext
44

5+
### Added
6+
7+
- Add "process.runtime.name/process.runtime.version/process.runtime.description" attributes into the `ProcessResourceDetector`.
8+
59
## v0.8.0
610

711
### Changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::process::Command;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=build.rs");
5+
6+
let Ok(output) = Command::new("rustc").arg("-V").output() else {
7+
return;
8+
};
9+
10+
let Ok(stdout) = String::from_utf8(output.stdout) else {
11+
return;
12+
};
13+
14+
println!("cargo:rustc-env=RUSTC_VERSION_DESCRIPTION={}", stdout);
15+
16+
// rustc -V: rustc 1.76.0 (07dca489a 2024-02-04)
17+
// version is 1.76.0
18+
if let Some(version) = stdout.split_whitespace().nth(1) {
19+
println!("cargo:rustc-env=RUSTC_VERSION={}", version);
20+
}
21+
}

opentelemetry-resource-detectors/src/process.rs

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use std::process::id;
1515
/// - process command line arguments(`process.command_args`), the full command arguments of this
1616
/// application.
1717
/// - OS assigned process id(`process.pid`).
18+
/// - process runtime version(`process.runtime.version`).
19+
/// - process runtime name(`process.runtime.name`).
20+
/// - process runtime description(`process.runtime.description`).
1821
pub struct ProcessResourceDetector;
1922

2023
impl ResourceDetector for ProcessResourceDetector {
@@ -25,29 +28,76 @@ impl ResourceDetector for ProcessResourceDetector {
2528
.map(|arg| arg.to_string_lossy().into_owned().into())
2629
.collect::<Vec<StringValue>>();
2730
Resource::builder_empty()
28-
.with_attributes(vec![
29-
KeyValue::new(
30-
opentelemetry_semantic_conventions::attribute::PROCESS_COMMAND_ARGS,
31-
Value::Array(cmd_arg_val.into()),
32-
),
33-
KeyValue::new(
34-
opentelemetry_semantic_conventions::attribute::PROCESS_PID,
35-
id() as i64,
36-
),
37-
])
31+
.with_attributes(
32+
vec![
33+
Some(KeyValue::new(
34+
opentelemetry_semantic_conventions::attribute::PROCESS_COMMAND_ARGS,
35+
Value::Array(cmd_arg_val.into()),
36+
)),
37+
Some(KeyValue::new(
38+
opentelemetry_semantic_conventions::attribute::PROCESS_PID,
39+
id() as i64,
40+
)),
41+
Some(KeyValue::new(
42+
opentelemetry_semantic_conventions::attribute::PROCESS_RUNTIME_NAME,
43+
"rustc",
44+
)),
45+
// Set from build.rs
46+
option_env!("RUSTC_VERSION").map(|rustc_version| {
47+
KeyValue::new(
48+
opentelemetry_semantic_conventions::attribute::PROCESS_RUNTIME_VERSION,
49+
rustc_version,
50+
)
51+
}),
52+
// Set from build.rs
53+
option_env!("RUSTC_VERSION_DESCRIPTION").map(|rustc_version_desc| {
54+
KeyValue::new(
55+
opentelemetry_semantic_conventions::attribute::PROCESS_RUNTIME_DESCRIPTION,
56+
rustc_version_desc,
57+
)
58+
}),
59+
]
60+
.into_iter()
61+
.flatten(),
62+
)
3863
.build()
3964
}
4065
}
4166

42-
#[cfg(target_os = "linux")]
4367
#[cfg(test)]
4468
mod tests {
4569
use super::ProcessResourceDetector;
4670
use opentelemetry_sdk::resource::ResourceDetector;
71+
use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_DESCRIPTION;
4772

73+
#[cfg(target_os = "linux")]
4874
#[test]
4975
fn test_processor_resource_detector() {
5076
let resource = ProcessResourceDetector.detect();
51-
assert_eq!(resource.len(), 2); // we cannot assert on the values because it changes along with runtime.
77+
assert_eq!(resource.len(), 5); // we cannot assert on the values because it changes along with runtime.
78+
}
79+
80+
#[test]
81+
fn test_processor_resource_detector_runtime() {
82+
use opentelemetry_semantic_conventions::attribute::{
83+
PROCESS_RUNTIME_NAME, PROCESS_RUNTIME_VERSION,
84+
};
85+
86+
let resource = ProcessResourceDetector.detect();
87+
88+
assert_eq!(
89+
resource.get(&PROCESS_RUNTIME_NAME.into()),
90+
Some("rustc".into())
91+
);
92+
93+
assert_eq!(
94+
resource.get(&PROCESS_RUNTIME_VERSION.into()),
95+
Some(env!("RUSTC_VERSION").into())
96+
);
97+
98+
assert_eq!(
99+
resource.get(&PROCESS_RUNTIME_DESCRIPTION.into()),
100+
Some(env!("RUSTC_VERSION_DESCRIPTION").into())
101+
);
52102
}
53103
}

0 commit comments

Comments
 (0)