Skip to content
Merged
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
23 changes: 2 additions & 21 deletions src/bridges/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use opentelemetry::{
global::ObjectSafeSpan,
trace::{SamplingDecision, TraceContextExt},
};
use tracing::{Subscriber, span};
use tracing::Subscriber;
use tracing_opentelemetry::{OtelData, PreSampledTracer};
use tracing_subscriber::{Layer, registry::LookupSpan};

Expand Down Expand Up @@ -31,28 +31,9 @@ where
"logfire.level_num",
level_to_level_number(*attrs.metadata().level()),
));

attributes.push(KeyValue::new("logfire.span_type", "span"));
}
}

// Emit a pending span when this span is first entered, if this span will be sampled.
fn on_enter(&self, id: &span::Id, ctx: tracing_subscriber::layer::Context<'_, S>) {
/// Dummy struct to mark that we've already entered this span.
struct LogfireSpanEntered;

let span = ctx.span(id).expect("span not found");
let mut extensions = span.extensions_mut();

if extensions.get_mut::<LogfireSpanEntered>().is_some() {
return;
}

extensions.insert(LogfireSpanEntered);

// Guaranteed to be on first entering of the span

if let Some(otel_data) = extensions.get_mut::<OtelData>() {
// Emit a pending span, if this span will be sampled.
let context = self.0.sampled_context(otel_data);
let sampling_result = otel_data
.builder
Expand Down
7 changes: 4 additions & 3 deletions src/internal/exporters/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl SimpleConsoleSpanExporter {
.unwrap_or(Cow::Borrowed("span"));

// only print for pending span and logs
if span_type == "pending_span" {
if span_type == "span" {
return Ok(());
}

Expand All @@ -117,6 +117,7 @@ impl SimpleConsoleSpanExporter {
// Filter out known values
ATTRIBUTES_SPAN_TYPE_KEY
| "logfire.json_schema"
| "logfire.pending_parent_id"
| "code.filepath"
| "code.lineno"
| "thread.id"
Expand Down Expand Up @@ -218,12 +219,12 @@ mod tests {
let output = std::str::from_utf8(&output).unwrap();

assert_snapshot!(output, @r#"
1970-01-01T00:00:00.000000Z INFO logfire::internal::exporters::console::tests root span
1970-01-01T00:00:01.000000Z INFO logfire::internal::exporters::console::tests hello world span
1970-01-01T00:00:03.000000Z DEBUG logfire::internal::exporters::console::tests debug span
1970-01-01T00:00:05.000000Z DEBUG logfire::internal::exporters::console::tests debug span with explicit parent
1970-01-01T00:00:07.000000Z INFO logfire::internal::exporters::console::tests hello world log
1970-01-01T00:00:08.000000Z ERROR logfire panic: oh no! location=src/internal/exporters/console.rs:210:17, backtrace=disabled backtrace
1970-01-01T00:00:00.000000Z INFO logfire::internal::exporters::console::tests root span
1970-01-01T00:00:08.000000Z ERROR logfire panic: oh no! location=src/internal/exporters/console.rs:211:17, backtrace=disabled backtrace
"#);
}
}
278 changes: 275 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,142 @@ mod tests {
attributes: [],
},
},
SpanData {
span_context: SpanContext {
trace_id: 000000000000000000000000000000f0,
span_id: 00000000000000f5,
trace_flags: TraceFlags(
1,
),
is_remote: false,
trace_state: TraceState(
None,
),
},
parent_span_id: 00000000000000f4,
span_kind: Internal,
name: "debug span",
start_time: SystemTime {
tv_sec: 3,
tv_nsec: 0,
},
end_time: SystemTime {
tv_sec: 3,
tv_nsec: 0,
},
attributes: [
KeyValue {
key: Static(
"code.filepath",
),
value: String(
Static(
"src/lib.rs",
),
),
},
KeyValue {
key: Static(
"code.namespace",
),
value: String(
Static(
"logfire::tests",
),
),
},
KeyValue {
key: Static(
"code.lineno",
),
value: I64(
831,
),
},
KeyValue {
key: Static(
"thread.id",
),
value: I64(
0,
),
},
KeyValue {
key: Static(
"thread.name",
),
value: String(
Owned(
"tests::test_basic_span",
),
),
},
KeyValue {
key: Static(
"logfire.msg",
),
value: String(
Owned(
"debug span",
),
),
},
KeyValue {
key: Static(
"logfire.json_schema",
),
value: String(
Owned(
"{\"type\":\"object\",\"properties\":{}}",
),
),
},
KeyValue {
key: Static(
"logfire.level_num",
),
value: I64(
5,
),
},
KeyValue {
key: Static(
"logfire.span_type",
),
value: String(
Static(
"pending_span",
),
),
},
KeyValue {
key: Static(
"logfire.pending_parent_id",
),
value: String(
Owned(
"00000000000000f0",
),
),
},
],
dropped_attributes_count: 0,
events: SpanEvents {
events: [],
dropped_count: 0,
},
links: SpanLinks {
links: [],
dropped_count: 0,
},
status: Unset,
instrumentation_scope: InstrumentationScope {
name: "logfire",
version: None,
schema_url: None,
attributes: [],
},
},
SpanData {
span_context: SpanContext {
trace_id: 000000000000000000000000000000f0,
Expand Down Expand Up @@ -1389,7 +1525,143 @@ mod tests {
SpanData {
span_context: SpanContext {
trace_id: 000000000000000000000000000000f0,
span_id: 00000000000000f5,
span_id: 00000000000000f7,
trace_flags: TraceFlags(
1,
),
is_remote: false,
trace_state: TraceState(
None,
),
},
parent_span_id: 00000000000000f6,
span_kind: Internal,
name: "debug span with explicit parent",
start_time: SystemTime {
tv_sec: 5,
tv_nsec: 0,
},
end_time: SystemTime {
tv_sec: 5,
tv_nsec: 0,
},
attributes: [
KeyValue {
key: Static(
"code.filepath",
),
value: String(
Static(
"src/lib.rs",
),
),
},
KeyValue {
key: Static(
"code.namespace",
),
value: String(
Static(
"logfire::tests",
),
),
},
KeyValue {
key: Static(
"code.lineno",
),
value: I64(
833,
),
},
KeyValue {
key: Static(
"thread.id",
),
value: I64(
0,
),
},
KeyValue {
key: Static(
"thread.name",
),
value: String(
Owned(
"tests::test_basic_span",
),
),
},
KeyValue {
key: Static(
"logfire.msg",
),
value: String(
Owned(
"debug span with explicit parent",
),
),
},
KeyValue {
key: Static(
"logfire.json_schema",
),
value: String(
Owned(
"{\"type\":\"object\",\"properties\":{}}",
),
),
},
KeyValue {
key: Static(
"logfire.level_num",
),
value: I64(
5,
),
},
KeyValue {
key: Static(
"logfire.span_type",
),
value: String(
Static(
"pending_span",
),
),
},
KeyValue {
key: Static(
"logfire.pending_parent_id",
),
value: String(
Owned(
"00000000000000f0",
),
),
},
],
dropped_attributes_count: 0,
events: SpanEvents {
events: [],
dropped_count: 0,
},
links: SpanLinks {
links: [],
dropped_count: 0,
},
status: Unset,
instrumentation_scope: InstrumentationScope {
name: "logfire",
version: None,
schema_url: None,
attributes: [],
},
},
SpanData {
span_context: SpanContext {
trace_id: 000000000000000000000000000000f0,
span_id: 00000000000000f6,
trace_flags: TraceFlags(
1,
),
Expand Down Expand Up @@ -1531,7 +1803,7 @@ mod tests {
SpanData {
span_context: SpanContext {
trace_id: 000000000000000000000000000000f0,
span_id: 00000000000000f6,
span_id: 00000000000000f8,
trace_flags: TraceFlags(
1,
),
Expand Down Expand Up @@ -1657,7 +1929,7 @@ mod tests {
SpanData {
span_context: SpanContext {
trace_id: 000000000000000000000000000000f0,
span_id: 00000000000000f7,
span_id: 00000000000000f9,
trace_flags: TraceFlags(
1,
),
Expand Down
Loading