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

fix: attempt to add trace context to logs #547

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ok, this works
morgante committed Oct 21, 2024
commit 6151193d456500ceb3c9ede2d4625197f9f9e328
29 changes: 16 additions & 13 deletions crates/cli/src/tracing_bridge.rs
Original file line number Diff line number Diff line change
@@ -167,7 +167,11 @@ where
P: LoggerProvider<Logger = L> + 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>,
) {
#[cfg(feature = "experimental_metadata_attributes")]
let normalized_meta = event.normalized_metadata();
#[cfg(feature = "experimental_metadata_attributes")]
@@ -180,13 +184,16 @@ where
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.
// This isn't really working for us.
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))
}) {
// Extract the trace_id & span_id from *tracing*, which propagates across async boundaries better
use opentelemetry::trace::TraceContextExt;
use tracing_opentelemetry::OpenTelemetrySpanExt;
let s = tracing::Span::current().context();

if s.has_active_span() {
let sr = s.span();
let sc = sr.span_context();
let trace_id = sc.trace_id();
let span_id = sc.span_id();
log_record.trace_context = Some(opentelemetry::logs::TraceContext::from(
&opentelemetry::trace::SpanContext::new(
trace_id,
@@ -196,14 +203,10 @@ where
opentelemetry::trace::TraceState::default(),
),
));
eprintln!("trace_id: {:?}, span_id: {:?}", trace_id, span_id);
} else {
eprintln!("No trace_id or span_id found");
// eprintln!("no active span");
}

// Not populating ObservedTimestamp, instead relying on OpenTelemetry
// API to populate it with current time.

let mut visitor = EventVisitor::default();
visitor.visit_metadata(meta);
// Visit fields.