Skip to content

Commit

Permalink
Update default attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Nov 12, 2024
1 parent 0471c25 commit 048057c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ zstd = "=0.12.4"
opentelemetry = "0.27.0"
opentelemetry-http = "0.27.0"
opentelemetry-otlp = { version = "0.27.0", features = ["logs", "http-proto", "http-json"] }
opentelemetry-semantic-conventions = "0.27.0"
opentelemetry-semantic-conventions = { version = "0.27.0", features = ["semconv_experimental"] }
opentelemetry_sdk = "0.27.0"

# crypto
Expand Down
6 changes: 2 additions & 4 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,8 @@ impl CliOptions {
.contains(&String::from("otel"))
{
Some(OtelConfig {
default_service_name: Cow::Borrowed("deno"),
default_service_version: Cow::Borrowed(
crate::version::DENO_VERSION_INFO.deno,
),
runtime_name: Cow::Borrowed("deno"),
runtime_version: Cow::Borrowed(crate::version::DENO_VERSION_INFO.deno),
..Default::default()
})
} else {
Expand Down
61 changes: 38 additions & 23 deletions runtime/ops/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ use opentelemetry_sdk::logs::LogRecord;
use opentelemetry_sdk::trace::BatchSpanProcessor;
use opentelemetry_sdk::trace::SpanProcessor as SpanProcessorTrait;
use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;
use opentelemetry_semantic_conventions::resource::SERVICE_VERSION;
use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_NAME;
use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_VERSION;
use opentelemetry_semantic_conventions::resource::TELEMETRY_SDK_LANGUAGE;
use opentelemetry_semantic_conventions::resource::TELEMETRY_SDK_NAME;
use opentelemetry_semantic_conventions::resource::TELEMETRY_SDK_VERSION;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
Expand Down Expand Up @@ -68,8 +71,8 @@ deno_core::extension!(

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OtelConfig {
pub default_service_name: Cow<'static, str>,
pub default_service_version: Cow<'static, str>,
pub runtime_name: Cow<'static, str>,
pub runtime_version: Cow<'static, str>,
pub console: OtelConsoleConfig,
}

Expand All @@ -84,8 +87,8 @@ pub enum OtelConsoleConfig {
impl Default for OtelConfig {
fn default() -> Self {
Self {
default_service_name: Cow::Borrowed(env!("CARGO_PKG_NAME")),
default_service_version: Cow::Borrowed(env!("CARGO_PKG_VERSION")),
runtime_name: Cow::Borrowed(env!("CARGO_PKG_NAME")),
runtime_version: Cow::Borrowed(env!("CARGO_PKG_VERSION")),
console: OtelConsoleConfig::Capture,
}
}
Expand Down Expand Up @@ -326,23 +329,35 @@ fn otel_create_globals(
// * Default attribute values defined here.
// TODO(piscisaureus): add more default attributes (e.g. script path).
let mut resource = Resource::default();
// The default service name assigned by `Resource::default()`, if not
// otherwise specified via environment variables, is "unknown_service".
// Override this with the current crate name and version.
if resource
.get(Key::from_static_str(SERVICE_NAME))
.filter(|service_name| service_name.as_str() != "unknown_service")
.is_none()
{
resource = resource.merge(&Resource::new(
[
(SERVICE_NAME, config.default_service_name),
(SERVICE_VERSION, config.default_service_version),
]
.into_iter()
.map(|(k, v)| KeyValue::new(k, v)),
))
}

// Add the runtime name and version to the resource attributes. Also override
// the `telemetry.sdk` attributes to include the Deno runtime.
resource = resource.merge(&Resource::new(vec![
KeyValue::new(PROCESS_RUNTIME_NAME, config.runtime_name),
KeyValue::new(PROCESS_RUNTIME_VERSION, config.runtime_version.clone()),
KeyValue::new(
TELEMETRY_SDK_LANGUAGE,
format!(
"deno-{}",
resource.get(Key::new(TELEMETRY_SDK_LANGUAGE)).unwrap()
),
),
KeyValue::new(
TELEMETRY_SDK_NAME,
format!(
"deno-{}",
resource.get(Key::new(TELEMETRY_SDK_NAME)).unwrap()
),
),
KeyValue::new(
TELEMETRY_SDK_VERSION,
format!(
"{}-{}",
config.runtime_version,
resource.get(Key::new(TELEMETRY_SDK_VERSION)).unwrap()
),
),
]));

// The OTLP endpoint is automatically picked up from the
// `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable. Additional headers can
Expand Down

0 comments on commit 048057c

Please sign in to comment.