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

Propagate span_id & trace_id in logs records when converting trace events #1394

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
8 changes: 4 additions & 4 deletions examples/tracing-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ name = "grpc-client"
path = "src/client.rs"

[dependencies]
opentelemetry = { version = "0.20" }
opentelemetry_sdk = { version = "0.20", features = ["rt-tokio"] }
opentelemetry-jaeger = { version = "0.19", 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"
tracing = "0.1"
tracing-futures = "0.2"
tracing-opentelemetry = "0.20"
tracing-opentelemetry = "0.22"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[build-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-appender-tracing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## vNext

- Support trace correlation by setting the trace_id and span_id on the log records

## v0.2.0

### Changed
Expand Down
8 changes: 5 additions & 3 deletions opentelemetry-appender-tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ rust-version = "1.65"
[dependencies]
opentelemetry = { version = "0.21", path = "../opentelemetry", features = ["logs"] }
opentelemetry_sdk = { version = "0.21", path = "../opentelemetry-sdk", features = ["logs"] }
tracing = {version = "0.1", default-features = false, features = ["std"]}
tracing = { version = "0.1", default-features = false, features = ["std"] }
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", path = "../opentelemetry-stdout", 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"]
testing = ["opentelemetry_sdk/testing"]
6 changes: 5 additions & 1 deletion opentelemetry-appender-tracing/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -23,6 +23,10 @@ fn main() {
let layer = layer::OpenTelemetryTracingBridge::new(&provider);
tracing_subscriber::registry().with(layer).init();

info_span!("my-span").in_scope(|| {
info!(target: "my-system", "an info log");
});

error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]");
drop(provider);
}
35 changes: 32 additions & 3 deletions opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use opentelemetry::logs::{LogRecord, Logger, LoggerProvider, Severity};
use std::borrow::Cow;
use tracing_core::Level;
use tracing_subscriber::Layer;
use tracing_core::{Level, Subscriber};
use tracing_subscriber::{registry::LookupSpan, Layer};

const INSTRUMENTATION_LIBRARY_NAME: &str = "opentelemetry-appender-tracing";

Expand Down Expand Up @@ -90,7 +90,7 @@

impl<S, P, L> Layer<S> for OpenTelemetryTracingBridge<P, L>
where
S: tracing::Subscriber,
S: Subscriber + for<'a> LookupSpan<'a>,
P: LoggerProvider<Logger = L> + Send + Sync + 'static,
L: Logger + Send + Sync + 'static,
{
Expand All @@ -104,6 +104,10 @@
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.
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "tracing")]
set_trace_context(&mut log_record, &_ctx);

// add the `name` metadata to attributes
// TBD - Propose this to be part of log_record metadata.
let vec = vec![("name", meta.name())];
Expand Down Expand Up @@ -131,6 +135,31 @@
}
}

#[cfg(feature = "tracing")]
fn set_trace_context<S>(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::<tracing_opentelemetry::OtelData>()
.and_then(|ext| ext.builder.trace_id.zip(ext.builder.span_id))
}) {
log_record.trace_context = Some(TraceContext::from(&SpanContext::new(

Check failure on line 153 in opentelemetry-appender-tracing/src/layer.rs

View workflow job for this annotation

GitHub Actions / lint

arguments to this function are incorrect
trace_id,
span_id,
TraceFlags::default(),
false,
TraceState::default(),
)));
}
}

const fn severity_of_level(level: &Level) -> Severity {
match *level {
Level::TRACE => Severity::Trace,
Expand Down
Loading