From 88101a41801236dff6783438888335b04ce88218 Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Fri, 15 Mar 2024 09:00:59 +0100 Subject: [PATCH 1/5] remove Os and Process resource detectors Detectors have been migrated to opentelemetry-rust-contrib repository --- opentelemetry-sdk/src/resource/mod.rs | 8 +--- opentelemetry-sdk/src/resource/os.rs | 46 ---------------------- opentelemetry-sdk/src/resource/process.rs | 47 ----------------------- 3 files changed, 2 insertions(+), 99 deletions(-) delete mode 100644 opentelemetry-sdk/src/resource/os.rs delete mode 100644 opentelemetry-sdk/src/resource/process.rs diff --git a/opentelemetry-sdk/src/resource/mod.rs b/opentelemetry-sdk/src/resource/mod.rs index f43fe1c1e0..ac6850609d 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -16,18 +16,14 @@ //! SDK. //! //! - [`EnvResourceDetector`] - detect resource from environmental variables. -//! - [`OsResourceDetector`] - detect OS from runtime. -//! - [`ProcessResourceDetector`] - detect process information. //! - [`TelemetryResourceDetector`] - detect telemetry SDK's information. +//! +//! The OS and Process resource detectors are now packaged separately in the `opentelemetry-resource-detector` [contrib crate](https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-resource-detectors), due to their experimental nature in semantic conventions. mod env; -mod os; -mod process; mod telemetry; pub use env::EnvResourceDetector; pub use env::SdkProvidedResourceDetector; -pub use os::OsResourceDetector; -pub use process::ProcessResourceDetector; pub use telemetry::TelemetryResourceDetector; use opentelemetry::{Key, KeyValue, Value}; diff --git a/opentelemetry-sdk/src/resource/os.rs b/opentelemetry-sdk/src/resource/os.rs deleted file mode 100644 index 6009ce6cb8..0000000000 --- a/opentelemetry-sdk/src/resource/os.rs +++ /dev/null @@ -1,46 +0,0 @@ -//! OS resource detector -//! -//! Detect the runtime operating system type. -use crate::resource::ResourceDetector; -use crate::Resource; -use opentelemetry::KeyValue; -use std::env::consts::OS; -use std::time::Duration; - -/// Detect runtime operating system information. -/// -/// This detector uses Rust's [`OS constant`] to detect the operating system type and -/// maps the result to the supported value defined in [`OpenTelemetry spec`]. -/// -/// [`OS constant`]: https://doc.rust-lang.org/std/env/consts/constant.OS.html -/// [`OpenTelemetry spec`]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/os.md -#[derive(Debug)] -pub struct OsResourceDetector; - -impl ResourceDetector for OsResourceDetector { - fn detect(&self, _timeout: Duration) -> Resource { - Resource::new(vec![KeyValue::new("os.type", OS)]) - } -} - -#[cfg(target_os = "linux")] -#[cfg(test)] -mod tests { - use crate::resource::os::OsResourceDetector; - use crate::resource::ResourceDetector; - use opentelemetry::Key; - use std::time::Duration; - - #[test] - fn test_os_resource_detector() { - let resource = OsResourceDetector.detect(Duration::from_secs(0)); - assert_eq!( - resource - .iter() - .0 - .find(|(k, _v)| **k == Key::from_static_str("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 deleted file mode 100644 index 7c1e22d09d..0000000000 --- a/opentelemetry-sdk/src/resource/process.rs +++ /dev/null @@ -1,47 +0,0 @@ -//! Process resource detector -//! -//! Detect process related information like pid, executable name. - -use crate::resource::ResourceDetector; -use crate::Resource; -use opentelemetry::{KeyValue, StringValue, Value}; -use std::env::args_os; -use std::process::id; -use std::time::Duration; - -/// Detect process information. -/// -/// This resource detector returns the following information: -/// -/// - process command line arguments(`process.command_args`), the full command arguments of this -/// application. -/// - OS assigned process id(`process.pid`). -#[derive(Debug)] -pub struct ProcessResourceDetector; - -impl ResourceDetector for ProcessResourceDetector { - fn detect(&self, _timeout: Duration) -> Resource { - let arguments = args_os(); - let cmd_arg_val = arguments - .into_iter() - .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), - ]) - } -} - -#[cfg(target_os = "linux")] -#[cfg(test)] -mod tests { - use crate::resource::{ProcessResourceDetector, ResourceDetector}; - use std::time::Duration; - - #[test] - fn test_processor_resource_detector() { - let resource = ProcessResourceDetector.detect(Duration::from_secs(0)); - assert_eq!(resource.len(), 2); // we cannot assert on the values because it changes along with runtime. - } -} From 565c3a6d3c5c5f8db4141aa76ed01b0066aef1dc Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Fri, 15 Mar 2024 09:08:27 +0100 Subject: [PATCH 2/5] feat: use public crate constants for resource keys --- opentelemetry-sdk/src/resource/attributes.rs | 32 ++++++++++++++++++++ opentelemetry-sdk/src/resource/env.rs | 13 ++++---- opentelemetry-sdk/src/resource/mod.rs | 3 ++ opentelemetry-sdk/src/resource/telemetry.rs | 6 ++-- 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 opentelemetry-sdk/src/resource/attributes.rs diff --git a/opentelemetry-sdk/src/resource/attributes.rs b/opentelemetry-sdk/src/resource/attributes.rs new file mode 100644 index 0000000000..c2273e07a5 --- /dev/null +++ b/opentelemetry-sdk/src/resource/attributes.rs @@ -0,0 +1,32 @@ +/// 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 ac6850609d..50b8f44a54 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -22,6 +22,9 @@ mod env; mod telemetry; +mod attributes; +pub(crate) use attributes::*; + pub use env::EnvResourceDetector; pub use env::SdkProvidedResourceDetector; pub use telemetry::TelemetryResourceDetector; 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")), ]) } } From 9df54949c269666583b76d3cd81b50843d37f75f Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Sat, 16 Mar 2024 11:02:29 +0100 Subject: [PATCH 3/5] Update opentelemetry-sdk/src/resource/mod.rs Co-authored-by: Cijo Thomas --- opentelemetry-sdk/src/resource/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/resource/mod.rs b/opentelemetry-sdk/src/resource/mod.rs index 50b8f44a54..83a137e039 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -18,7 +18,7 @@ //! - [`EnvResourceDetector`] - detect resource from environmental variables. //! - [`TelemetryResourceDetector`] - detect telemetry SDK's information. //! -//! The OS and Process resource detectors are now packaged separately in the `opentelemetry-resource-detector` [contrib crate](https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-resource-detectors), due to their experimental nature in semantic conventions. +//! The OS and Process resource detectors are now packaged separately in the `opentelemetry-resource-detector` [crate](https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-resource-detectors). mod env; mod telemetry; From ed9c4dd190cc76e3124e06443b157b30067060dd Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Sun, 17 Mar 2024 12:41:50 +0000 Subject: [PATCH 4/5] Update opentelemetry-sdk/src/resource/attributes.rs Co-authored-by: Zhongyang Wu --- opentelemetry-sdk/src/resource/attributes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/resource/attributes.rs b/opentelemetry-sdk/src/resource/attributes.rs index c2273e07a5..41ab4a8d86 100644 --- a/opentelemetry-sdk/src/resource/attributes.rs +++ b/opentelemetry-sdk/src/resource/attributes.rs @@ -14,7 +14,7 @@ pub(crate) const TELEMETRY_SDK_LANGUAGE: &str = "telemetry.sdk.language"; /// /// 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 +/// `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. From 28c90b3e0fefa789b4a2119f41a0b94f64db008f Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Sun, 17 Mar 2024 13:56:31 +0000 Subject: [PATCH 5/5] add breaking changelog entry --- opentelemetry-sdk/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index e249982872..8b8da7500b 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -9,6 +9,7 @@ Fix metrics aggregation bug when using Views to drop attributes. - [#1623](https://github.com/open-telemetry/opentelemetry-rust/pull/1623) Add Drop implementation for SdkMeterProvider, which shuts down metricreaders, thereby allowing metrics still in memory to be flushed out. +- **Breaking** [#1624](https://github.com/open-telemetry/opentelemetry-rust/pull/1624) Remove `OsResourceDetector` and `ProcessResourceDetector` resource detectors, use the `opentelemetry-resource-detector` [crate](https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-resource-detectors) instead. ## v0.22.1