Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use constants for resource detectors keys #1610

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust-version = "1.65"
[dependencies]
opentelemetry = { version = "0.22", path = "../opentelemetry/" }
opentelemetry-http = { version = "0.11", path = "../opentelemetry-http", optional = true }
opentelemetry-semantic-conventions = { version = "0.14", path = "../opentelemetry-semantic-conventions"}
async-std = { workspace = true, features = ["unstable"], optional = true }
async-trait = { workspace = true, optional = true }
futures-channel = "0.3"
Expand Down
23 changes: 16 additions & 7 deletions opentelemetry-sdk/src/resource/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ pub struct SdkProvidedResourceDetector;
impl ResourceDetector for SdkProvidedResourceDetector {
fn detect(&self, _timeout: Duration) -> Resource {
Resource::new(vec![KeyValue::new(
"service.name",
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
env::var(OTEL_SERVICE_NAME)
.ok()
.filter(|s| !s.is_empty())
.map(Value::from)
.or_else(|| {
EnvResourceDetector::new()
.detect(Duration::from_secs(0))
.get(Key::new("service.name"))
.get(Key::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
))
})
.unwrap_or_else(|| "unknown_service".into()),
)])
Expand Down Expand Up @@ -132,18 +134,21 @@ mod tests {

#[test]
fn test_sdk_provided_resource_detector() {
const SERVICE_NAME: &str = "service.name";
// Ensure no env var set
let no_env = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
assert_eq!(
no_env.get(Key::from_static_str(SERVICE_NAME)),
no_env.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::SERVICE_NAME
)),
Some(Value::from("unknown_service")),
);

temp_env::with_var(OTEL_SERVICE_NAME, Some("test service"), || {
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
assert_eq!(
with_service.get(Key::from_static_str(SERVICE_NAME)),
with_service.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::SERVICE_NAME
)),
Some(Value::from("test service")),
)
});
Expand All @@ -154,7 +159,9 @@ mod tests {
|| {
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
assert_eq!(
with_service.get(Key::from_static_str(SERVICE_NAME)),
with_service.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::SERVICE_NAME
)),
Some(Value::from("test service1")),
)
},
Expand All @@ -169,7 +176,9 @@ mod tests {
|| {
let with_service = SdkProvidedResourceDetector.detect(Duration::from_secs(1));
assert_eq!(
with_service.get(Key::from_static_str(SERVICE_NAME)),
with_service.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::SERVICE_NAME
)),
Some(Value::from("test service"))
);
},
Expand Down
8 changes: 6 additions & 2 deletions opentelemetry-sdk/src/resource/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub struct OsResourceDetector;

impl ResourceDetector for OsResourceDetector {
fn detect(&self, _timeout: Duration) -> Resource {
Resource::new(vec![KeyValue::new("os.type", OS)])
Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::OS_TYPE,
OS,
)])
}
}

Expand All @@ -38,7 +41,8 @@ mod tests {
resource
.iter()
.0
.find(|(k, _v)| **k == Key::from_static_str("os.type"))
.find(|(k, _v)| **k
== Key::from_static_str(opentelemetry_semantic_conventions::resource::OS_TYPE))
.map(|(_k, v)| v.to_string()),
Some("linux".to_string())
);
Expand Down
10 changes: 8 additions & 2 deletions opentelemetry-sdk/src/resource/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ impl ResourceDetector for ProcessResourceDetector {
.map(|arg| arg.to_string_lossy().into_owned().into())
.collect::<Vec<StringValue>>();
Resource::new(vec![
KeyValue::new("process.command_args", Value::Array(cmd_arg_val.into())),
KeyValue::new("process.pid", id() as i64),
KeyValue::new(
opentelemetry_semantic_conventions::resource::PROCESS_COMMAND_ARGS,
Value::Array(cmd_arg_val.into()),
),
KeyValue::new(
opentelemetry_semantic_conventions::resource::PROCESS_PID,
id() as i64,
),
])
}
}
Expand Down
15 changes: 12 additions & 3 deletions opentelemetry-sdk/src/resource/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ pub struct TelemetryResourceDetector;
impl ResourceDetector for TelemetryResourceDetector {
fn detect(&self, _timeout: Duration) -> Resource {
Resource::new(vec![
KeyValue::new("telemetry.sdk.name", "opentelemetry"),
KeyValue::new("telemetry.sdk.language", "rust"),
KeyValue::new("telemetry.sdk.version", env!("CARGO_PKG_VERSION")),
KeyValue::new(
opentelemetry_semantic_conventions::resource::TELEMETRY_SDK_NAME,
"opentelemetry",
),
KeyValue::new(
opentelemetry_semantic_conventions::resource::TELEMETRY_SDK_LANGUAGE,
"rust",
),
KeyValue::new(
opentelemetry_semantic_conventions::resource::TELEMETRY_SDK_VERSION,
env!("CARGO_PKG_VERSION"),
),
])
}
}
Loading