From 762b99a02888ad200faaeefbdc7d7ce3f30fd503 Mon Sep 17 00:00:00 2001 From: Maxime Bedard Date: Tue, 5 Dec 2023 14:03:37 -0500 Subject: [PATCH] Use released versions + gate bethind tracing feature flag --- examples/tracing-grpc/Cargo.toml | 6 +-- opentelemetry-appender-tracing/Cargo.toml | 9 ++-- .../examples/basic.rs | 2 +- opentelemetry-appender-tracing/src/layer.rs | 54 ++++++++++++------- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/examples/tracing-grpc/Cargo.toml b/examples/tracing-grpc/Cargo.toml index aefe94a404..fa0443c15c 100644 --- a/examples/tracing-grpc/Cargo.toml +++ b/examples/tracing-grpc/Cargo.toml @@ -14,9 +14,9 @@ name = "grpc-client" path = "src/client.rs" [dependencies] -opentelemetry = { path = "../../opentelemetry" } -opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["rt-tokio"] } -opentelemetry-jaeger = { path = "../../opentelemetry-jaeger", features = ["rt-tokio"] } +opentelemetry = { version = "0.21" } +opentelemetry_sdk = { version = "0.21", features = ["rt-tokio"] } +opentelemetry-jaeger = { version = "0.20", features = ["rt-tokio"] } prost = "0.11" tokio = { version = "1.28", features = ["full"] } tonic = "0.9.2" diff --git a/opentelemetry-appender-tracing/Cargo.toml b/opentelemetry-appender-tracing/Cargo.toml index 603dec6e16..d21075a2a2 100644 --- a/opentelemetry-appender-tracing/Cargo.toml +++ b/opentelemetry-appender-tracing/Cargo.toml @@ -11,18 +11,19 @@ license = "Apache-2.0" rust-version = "1.65" [dependencies] -opentelemetry = { path = "../opentelemetry", features = ["logs"] } -opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["logs"] } +opentelemetry = { version = "0.21", features = ["logs"] } +opentelemetry_sdk = { version = "0.21", features = ["logs"] } tracing = { version = "0.1", default-features = false, features = ["std"] } -tracing-opentelemetry = "0.22" +tracing-opentelemetry = { version = "0.22", optional = true } tracing-core = "0.1.31" tracing-subscriber = { version = "0.3.0", default-features = false, features = ["registry", "std"] } once_cell = "1.13.0" [dev-dependencies] -opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"] } +opentelemetry-stdout = { version = "0.2", features = ["logs"] } [features] logs_level_enabled = ["opentelemetry/logs_level_enabled", "opentelemetry_sdk/logs_level_enabled"] +tracing = ["tracing-opentelemetry"] default = ["logs_level_enabled"] testing = ["opentelemetry_sdk/testing"] \ No newline at end of file diff --git a/opentelemetry-appender-tracing/examples/basic.rs b/opentelemetry-appender-tracing/examples/basic.rs index 4e0349ec2a..3b90c7498f 100644 --- a/opentelemetry-appender-tracing/examples/basic.rs +++ b/opentelemetry-appender-tracing/examples/basic.rs @@ -6,7 +6,7 @@ use opentelemetry_sdk::{ logs::{Config, LoggerProvider}, Resource, }; -use tracing::error; +use tracing::{error, info, info_span}; use tracing_subscriber::prelude::*; fn main() { diff --git a/opentelemetry-appender-tracing/src/layer.rs b/opentelemetry-appender-tracing/src/layer.rs index 67b77bb967..ad95023729 100644 --- a/opentelemetry-appender-tracing/src/layer.rs +++ b/opentelemetry-appender-tracing/src/layer.rs @@ -1,10 +1,6 @@ -use opentelemetry::{ - logs::{LogRecord, Logger, LoggerProvider, Severity, TraceContext}, - trace::{SpanContext, TraceFlags, TraceState}, -}; +use opentelemetry::logs::{LogRecord, Logger, LoggerProvider, Severity}; use std::borrow::Cow; use tracing_core::{Level, Subscriber}; -use tracing_opentelemetry::OtelData; use tracing_subscriber::{registry::LookupSpan, Layer}; const INSTRUMENTATION_LIBRARY_NAME: &str = "opentelemetry-appender-tracing"; @@ -98,26 +94,19 @@ where P: LoggerProvider + Send + Sync + 'static, L: Logger + Send + Sync + 'static, { - fn on_event(&self, event: &tracing::Event<'_>, ctx: tracing_subscriber::layer::Context<'_, S>) { + fn on_event( + &self, + event: &tracing::Event<'_>, + _ctx: tracing_subscriber::layer::Context<'_, S>, + ) { let meta = event.metadata(); let mut log_record: LogRecord = LogRecord::default(); log_record.severity_number = Some(severity_of_level(meta.level())); log_record.severity_text = Some(meta.level().to_string().into()); // Extract the trace_id & span_id from the opentelemetry extension. - if let Some((trace_id, span_id)) = ctx.lookup_current().and_then(|span| { - span.extensions() - .get::() - .and_then(|ext| ext.builder.trace_id.zip(ext.builder.span_id)) - }) { - log_record.trace_context = Some(TraceContext::from(&SpanContext::new( - trace_id, - span_id, - TraceFlags::default(), - false, - TraceState::default(), - ))); - } + #[cfg(feature = "tracing")] + inject_trace_context(&mut log_record, &_ctx); // add the `name` metadata to attributes // TBD - Propose this to be part of log_record metadata. @@ -146,6 +135,33 @@ where } } +#[cfg(feature = "tracing")] +fn inject_trace_context( + log_record: &mut LogRecord, + ctx: &tracing_subscriber::layer::Context<'_, S>, +) where + S: Subscriber + for<'a> LookupSpan<'a>, +{ + use opentelemetry::{ + logs::TraceContext, + trace::{SpanContext, TraceFlags, TraceState}, + }; + + if let Some((trace_id, span_id)) = ctx.lookup_current().and_then(|span| { + span.extensions() + .get::() + .and_then(|ext| ext.builder.trace_id.zip(ext.builder.span_id)) + }) { + log_record.trace_context = Some(TraceContext::from(&SpanContext::new( + trace_id, + span_id, + TraceFlags::default(), + false, + TraceState::default(), + ))); + } +} + const fn severity_of_level(level: &Level) -> Severity { match *level { Level::TRACE => Severity::Trace,