Skip to content

Commit

Permalink
Minor update to stdout exporter example to show log correlation
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Dec 20, 2024
1 parent f911ed5 commit d6e474c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
51 changes: 11 additions & 40 deletions opentelemetry-stdout/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
use opentelemetry::{global, KeyValue};

#[cfg(feature = "trace")]
use opentelemetry::trace::{Span, Tracer};
use opentelemetry::trace::Tracer;

#[cfg(feature = "metrics")]
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
Expand Down Expand Up @@ -60,52 +60,23 @@ fn init_logs() -> opentelemetry_sdk::logs::LoggerProvider {

#[cfg(feature = "trace")]
fn emit_span() {
use opentelemetry::{
trace::{SpanContext, SpanId, TraceFlags, TraceId, TraceState},
InstrumentationScope,
};
use opentelemetry::{trace::TraceContextExt, InstrumentationScope};

let scope = InstrumentationScope::builder("stdout-example")
.with_version("v1")
.with_attributes([KeyValue::new("scope_key", "scope_value")])
.build();

let tracer = global::tracer_with_scope(scope);
let mut span = tracer.start("example-span");
span.set_attribute(KeyValue::new("attribute_key1", "attribute_value1"));
span.set_attribute(KeyValue::new("attribute_key2", "attribute_value2"));
span.add_event(
"example-event-name",
vec![KeyValue::new("event_attribute1", "event_value1")],
);
span.add_link(
SpanContext::new(
TraceId::from_hex("58406520a006649127e371903a2de979").expect("invalid"),
SpanId::from_hex("b6d7d7f6d7d6d7f6").expect("invalid"),
TraceFlags::default(),
false,
TraceState::NONE,
),
vec![
KeyValue::new("link_attribute1", "link_value1"),
KeyValue::new("link_attribute2", "link_value2"),
],
);

span.add_link(
SpanContext::new(
TraceId::from_hex("23401120a001249127e371903f2de971").expect("invalid"),
SpanId::from_hex("cd37d765d743d7f6").expect("invalid"),
TraceFlags::default(),
false,
TraceState::NONE,
),
vec![
KeyValue::new("link_attribute1", "link_value1"),
KeyValue::new("link_attribute2", "link_value2"),
],
);
span.end();
tracer.in_span("example-span", |cx| {
let span = cx.span();
span.set_attribute(KeyValue::new("my-attribute", "my-value"));
span.add_event(
"example-event-name",
vec![KeyValue::new("event_attribute1", "event_value1")],
);
emit_log();
})
}

#[cfg(feature = "metrics")]
Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-stdout/src/logs/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ fn print_logs(batch: LogBatch<'_>) {
if let Some(trace_context) = record.trace_context() {
println!("\t TraceId: {:?}", trace_context.trace_id);
println!("\t SpanId: {:?}", trace_context.span_id);
if let Some(trace_flags) = trace_context.trace_flags {
println!("\t TraceFlags: {:?}", trace_flags);
}

Check warning on line 87 in opentelemetry-stdout/src/logs/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/logs/exporter.rs#L85-L87

Added lines #L85 - L87 were not covered by tests
}
if let Some(timestamp) = record.timestamp() {
let datetime: DateTime<Utc> = timestamp.into();
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-stdout/src/trace/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn print_spans(batch: Vec<export::trace::SpanData>) {
println!("\tName : {}", &span.name);
println!("\tTraceId : {}", &span.span_context.trace_id());
println!("\tSpanId : {}", &span.span_context.span_id());
println!("\tTraceFlags : {:?}", &span.span_context.trace_flags());

Check warning on line 99 in opentelemetry-stdout/src/trace/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/trace/exporter.rs#L99

Added line #L99 was not covered by tests
println!("\tParentSpanId: {}", &span.parent_span_id);
println!("\tKind : {:?}", &span.span_kind);

Expand Down

0 comments on commit d6e474c

Please sign in to comment.