Skip to content

Commit

Permalink
fix: attempt to add trace context to logs (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored Oct 21, 2024
1 parent db7a088 commit aa3de5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,6 @@ grit_beta = [
"remote_workflows",
"workflow_server",
# "grit_timing",
# "grit_tracing",
]
grit_timing = []
28 changes: 22 additions & 6 deletions crates/cli/src/tracing_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,28 @@ 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.
// set_trace_context(&mut log_record, &_ctx);

// Not populating ObservedTimestamp, instead relying on OpenTelemetry
// API to populate it with current time.
// 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,
span_id,
opentelemetry::trace::TraceFlags::default(),
false,
opentelemetry::trace::TraceState::default(),
),
));
} else {
// eprintln!("no active span");
}

let mut visitor = EventVisitor::default();
visitor.visit_metadata(meta);
Expand Down

0 comments on commit aa3de5d

Please sign in to comment.