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 all 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
61 changes: 61 additions & 0 deletions opentelemetry-sdk/src/resource/attributes.rs
Original file line number Diff line number Diff line change
@@ -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";
13 changes: 6 additions & 7 deletions opentelemetry-sdk/src/resource/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ 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())
.map(Value::from)
.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()),
)])
Expand Down Expand Up @@ -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")),
)
});
Expand All @@ -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")),
)
},
Expand All @@ -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"))
);
},
Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-sdk/src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-sdk/src/resource/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
}
}

Expand All @@ -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())
);
Expand Down
7 changes: 5 additions & 2 deletions opentelemetry-sdk/src/resource/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ 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(
super::PROCESS_COMMAND_ARGS,
Value::Array(cmd_arg_val.into()),
),
KeyValue::new(super::PROCESS_PID, id() as i64),
])
}
}
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/src/resource/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
])
}
}
Loading