Skip to content

Commit

Permalink
feat: add more resource detectors (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp committed Jun 16, 2021
1 parent efbc842 commit 6834b64
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 22 deletions.
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/common.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/metrics.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/metrics_service.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/resource.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/trace.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/trace_config.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/proto/grpcio/trace_service.rs
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.23.0. Do not edit
// This file is generated by rust-protobuf 2.24.1. Do not edit
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand All @@ -21,7 +21,7 @@

/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_23_0;
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1;

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(feature = "with-serde", derive(::serde::Serialize, ::serde::Deserialize))]
Expand Down
2 changes: 0 additions & 2 deletions opentelemetry/src/sdk/mod.rs
Expand Up @@ -6,7 +6,6 @@
//! facilitates the delivery of telemetry data to storage systems
//! through `Exporter`s. These can be configured on `Tracer` and
//! `Meter` creation.
pub mod env;
pub mod export;
pub mod instrumentation;
#[cfg(feature = "metrics")]
Expand All @@ -20,6 +19,5 @@ pub mod resource;
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
pub mod trace;

pub use env::EnvResourceDetector;
pub use instrumentation::InstrumentationLibrary;
pub use resource::Resource;
@@ -1,4 +1,4 @@
//! EnvResourceDetector
//! Environment variables resource detector
//!
//! Implementation of `ResourceDetector` to extract a `Resource` from environment
//! variables.
Expand Down Expand Up @@ -59,9 +59,8 @@ fn construct_otel_resources(s: String) -> Resource {

#[cfg(test)]
mod tests {
use crate::sdk::env::OTEL_RESOURCE_ATTRIBUTES;
use crate::sdk::resource::{Resource, ResourceDetector};
use crate::sdk::EnvResourceDetector;
use crate::sdk::resource::env::OTEL_RESOURCE_ATTRIBUTES;
use crate::sdk::resource::{EnvResourceDetector, Resource, ResourceDetector};
use crate::KeyValue;
use std::{env, time};

Expand Down
Expand Up @@ -13,9 +13,25 @@
//! produced by any `Tracer` from the provider are associated with this `Resource`.
//!
//! [`TracerProvider`]: crate::trace::TracerProvider
//!
//! # Resource detectors
//!
//! `ResourceDetector`s are used to detect resource from runtime or environmental variables. The
//! following `ResourceDetector`s are provided along with this SDK.
//!
//! - EnvResourceDetector, detect resource from environmental variables.
//! - OsResourceDetector, detect OS from runtime.
//! - ProcessResourceDetector, detect process information
mod env;
mod os;
mod process;

pub use env::EnvResourceDetector;
pub use os::OsResourceDetector;
pub use process::ProcessResourceDetector;

#[cfg(feature = "metrics")]
use crate::labels;
use crate::sdk::EnvResourceDetector;
use crate::{Key, KeyValue, Value};
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -187,7 +203,7 @@ pub trait ResourceDetector {
#[cfg(test)]
mod tests {
use super::*;
use crate::sdk::EnvResourceDetector;
use crate::sdk::resource::EnvResourceDetector;
use std::collections::BTreeMap;
use std::{env, time};

Expand Down
46 changes: 46 additions & 0 deletions opentelemetry/src/sdk/resource/os.rs
@@ -0,0 +1,46 @@
//! OS resource detector
//!
//! Detect the runtime operating system type.
use crate::sdk::resource::ResourceDetector;
use crate::sdk::Resource;
use crate::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(test)]
mod tests {
use crate::sdk::resource::os::OsResourceDetector;
use crate::sdk::resource::ResourceDetector;
use crate::Key;
use std::time::Duration;

#[cfg(target_os = "linux")]
#[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())
);
}
}
51 changes: 51 additions & 0 deletions opentelemetry/src/sdk/resource/process.rs
@@ -0,0 +1,51 @@
//! Process resource detector
//!
//! Detect process related information like pid, executable name.

use crate::sdk::resource::ResourceDetector;
use crate::sdk::Resource;
use crate::{Array, KeyValue, Value};
use std::borrow::Cow;
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| Cow::from(arg.to_string_lossy().into_owned()))
.collect::<Vec<Cow<'_, str>>>();
Resource::new(vec![
KeyValue::new(
"process.command_args",
Value::Array(Array::String(cmd_arg_val)),
),
KeyValue::new("process.pid", id() as i64),
])
}
}

#[cfg(test)]
mod tests {
use crate::sdk::resource::{ProcessResourceDetector, ResourceDetector};
use std::time::Duration;

#[cfg(target_os = "linux")]
#[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.
}
}

0 comments on commit 6834b64

Please sign in to comment.