diff --git a/opentelemetry-sdk/src/resource/attributes.rs b/opentelemetry-sdk/src/resource/attributes.rs new file mode 100644 index 0000000000..5f934eae79 --- /dev/null +++ b/opentelemetry-sdk/src/resource/attributes.rs @@ -0,0 +1,61 @@ +//! # Resource attributes module +//! +//! This module defines constants related to telemetry metadata. +//! +//! # TODO +//! +//! This file should be removed once the `opentelemetry-semantic-conventions` crate is stable. +//! Currently, these constants are needed for the operation of different parts of the sdk, but +//! once the official crate is stable, it is more efficient and maintainable to use the definitions +//! from there. + +/// The operating system type. +pub(crate) const OS_TYPE: &str = "os.type"; + +/// All the command arguments (including the command/executable itself) as received by the process. On Linux-based systems (and some other Unixoid systems supporting procfs), can be set according to the list of null-delimited strings extracted from `proc/[pid]/cmdline`. For libc-based executables, this would be the full argv vector passed to `main`. +/// +/// # Examples +/// +/// - `cmd/otecol` +/// - `--config=config.yaml` +pub(crate) const PROCESS_COMMAND_ARGS: &str = "process.command_args"; + +/// Process identifier (PID). +/// +/// # Examples +/// +/// - `1234` +pub(crate) const PROCESS_PID: &str = "process.pid"; + +/// Logical name of the service. +/// +/// MUST be the same for all instances of horizontally scaled services. If the value was not specified, SDKs MUST fallback to `unknown_service:` concatenated with [`process.executable.name`](process.md#process), e.g. `unknown_service:bash`. If `process.executable.name` is not available, the value MUST be set to `unknown_service`. +/// +/// # Examples +/// +/// - `shoppingcart` +pub(crate) const SERVICE_NAME: &str = "service.name"; + +/// The language of the telemetry SDK. +pub(crate) const TELEMETRY_SDK_LANGUAGE: &str = "telemetry.sdk.language"; + +/// The name of the telemetry SDK as defined above. +/// +/// The OpenTelemetry SDK MUST set the `telemetry.sdk.name` attribute to `opentelemetry`. +/// If another SDK, like a fork or a vendor-provided implementation, is used, this SDK MUST set the +/// `telemetry.sdk.name` attribute to the fully-qualified class or module name of this SDK's main entry point +/// or another suitable identifier depending on the language. +/// The identifier `opentelemetry` is reserved and MUST NOT be used in this case. +/// All custom identifiers SHOULD be stable across different versions of an implementation. +/// +/// # Examples +/// +/// - `opentelemetry` +pub(crate) const TELEMETRY_SDK_NAME: &str = "telemetry.sdk.name"; + +/// The version string of the telemetry SDK. +/// +/// # Examples +/// +/// - `1.2.3` +pub(crate) const TELEMETRY_SDK_VERSION: &str = "telemetry.sdk.version"; diff --git a/opentelemetry-sdk/src/resource/env.rs b/opentelemetry-sdk/src/resource/env.rs index feb3ac22c2..64960bd4af 100644 --- a/opentelemetry-sdk/src/resource/env.rs +++ b/opentelemetry-sdk/src/resource/env.rs @@ -76,7 +76,7 @@ pub struct SdkProvidedResourceDetector; impl ResourceDetector for SdkProvidedResourceDetector { fn detect(&self, _timeout: Duration) -> Resource { Resource::new(vec![KeyValue::new( - "service.name", + super::SERVICE_NAME, env::var(OTEL_SERVICE_NAME) .ok() .filter(|s| !s.is_empty()) @@ -84,7 +84,7 @@ impl ResourceDetector for SdkProvidedResourceDetector { .or_else(|| { EnvResourceDetector::new() .detect(Duration::from_secs(0)) - .get(Key::new("service.name")) + .get(Key::new(super::SERVICE_NAME)) }) .unwrap_or_else(|| "unknown_service".into()), )]) @@ -132,18 +132,17 @@ 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(crate::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(crate::resource::SERVICE_NAME)), Some(Value::from("test service")), ) }); @@ -154,7 +153,7 @@ 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(crate::resource::SERVICE_NAME)), Some(Value::from("test service1")), ) }, @@ -169,7 +168,7 @@ 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(crate::resource::SERVICE_NAME)), Some(Value::from("test service")) ); }, diff --git a/opentelemetry-sdk/src/resource/mod.rs b/opentelemetry-sdk/src/resource/mod.rs index f43fe1c1e0..f336fd78a7 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -24,6 +24,9 @@ mod os; mod process; mod telemetry; +mod attributes; +pub(crate) use attributes::*; + pub use env::EnvResourceDetector; pub use env::SdkProvidedResourceDetector; pub use os::OsResourceDetector; diff --git a/opentelemetry-sdk/src/resource/os.rs b/opentelemetry-sdk/src/resource/os.rs index 6009ce6cb8..b04d9533b0 100644 --- a/opentelemetry-sdk/src/resource/os.rs +++ b/opentelemetry-sdk/src/resource/os.rs @@ -19,7 +19,7 @@ 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(super::OS_TYPE, OS)]) } } @@ -38,7 +38,7 @@ mod tests { resource .iter() .0 - .find(|(k, _v)| **k == Key::from_static_str("os.type")) + .find(|(k, _v)| **k == Key::from_static_str(crate::resource::OS_TYPE)) .map(|(_k, v)| v.to_string()), Some("linux".to_string()) ); diff --git a/opentelemetry-sdk/src/resource/process.rs b/opentelemetry-sdk/src/resource/process.rs index 7c1e22d09d..8eaed34ff1 100644 --- a/opentelemetry-sdk/src/resource/process.rs +++ b/opentelemetry-sdk/src/resource/process.rs @@ -27,8 +27,11 @@ impl ResourceDetector for ProcessResourceDetector { .map(|arg| arg.to_string_lossy().into_owned().into()) .collect::>(); Resource::new(vec![ - KeyValue::new("process.command_args", Value::Array(cmd_arg_val.into())), - KeyValue::new("process.pid", id() as i64), + KeyValue::new( + super::PROCESS_COMMAND_ARGS, + Value::Array(cmd_arg_val.into()), + ), + KeyValue::new(super::PROCESS_PID, id() as i64), ]) } } diff --git a/opentelemetry-sdk/src/resource/telemetry.rs b/opentelemetry-sdk/src/resource/telemetry.rs index 9948f2953b..932870012c 100644 --- a/opentelemetry-sdk/src/resource/telemetry.rs +++ b/opentelemetry-sdk/src/resource/telemetry.rs @@ -19,9 +19,9 @@ 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(super::TELEMETRY_SDK_NAME, "opentelemetry"), + KeyValue::new(super::TELEMETRY_SDK_LANGUAGE, "rust"), + KeyValue::new(super::TELEMETRY_SDK_VERSION, env!("CARGO_PKG_VERSION")), ]) } }