Skip to content

Commit

Permalink
Merge branch 'log-async-trait-impl' of github.com:lalitb/opentelemetr…
Browse files Browse the repository at this point in the history
…y-rust into log-async-trait-impl
  • Loading branch information
lalitb committed Dec 20, 2024
2 parents ddb15bf + 09f7cb8 commit 5e025d2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 61 deletions.
6 changes: 2 additions & 4 deletions opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ license = "Apache-2.0"
publish = false

[features]
default = ["reqwest", "experimental_metrics_periodicreader_with_async_runtime"]
reqwest = ["opentelemetry-otlp/reqwest-client"]
default = ["reqwest-blocking"]
reqwest-blocking = ["opentelemetry-otlp/reqwest-blocking-client"]
hyper = ["opentelemetry-otlp/hyper-client"]
experimental_metrics_periodicreader_with_async_runtime = ["opentelemetry_sdk/experimental_metrics_periodicreader_with_async_runtime"]


[dependencies]
once_cell = { workspace = true }
Expand Down
6 changes: 2 additions & 4 deletions opentelemetry-otlp/examples/basic-otlp-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ applications, these filters should be adjusted appropriately.

The example employs a `BatchExporter` for logs and traces, which is the
recommended approach when using OTLP exporters. While it can be modified to use
a `SimpleExporter`, this requires enabling feature flag `reqwest-blocking-client` and
making the `main()` a normal main and *not* `tokio::main`
a `SimpleExporter`, this requires making the main function a regular main and
*not* tokio main.

// TODO: Metrics does not work with non tokio main when using `reqwest-blocking-client` today, fix that when switching
// default to use own thread.
// TODO: Document `hyper` feature flag when using SimpleProcessor.

## Usage
Expand Down
20 changes: 7 additions & 13 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use opentelemetry_otlp::{LogExporter, MetricExporter, Protocol, SpanExporter};
use opentelemetry_sdk::{
logs::LoggerProvider,
metrics::{MetricError, SdkMeterProvider},
runtime,
trace::{self as sdktrace, TracerProvider},
};
use opentelemetry_sdk::{
Expand Down Expand Up @@ -50,7 +49,9 @@ fn init_traces() -> Result<sdktrace::TracerProvider, TraceError> {
.build()?;

Ok(TracerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
// TODO: Enable BatchExporter after
// https://github.com/open-telemetry/opentelemetry-rust/pull/2456
.with_simple_exporter(exporter)
.with_resource(RESOURCE.clone())
.build())
}
Expand All @@ -62,15 +63,6 @@ fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, Metric
.with_endpoint("http://localhost:4318/v1/metrics")
.build()?;

#[cfg(feature = "experimental_metrics_periodicreader_with_async_runtime")]
let reader =
opentelemetry_sdk::metrics::periodic_reader_with_async_runtime::PeriodicReader::builder(
exporter,
runtime::Tokio,
)
.build();
// TODO: This does not work today. See https://github.com/open-telemetry/opentelemetry-rust/issues/2400
#[cfg(not(feature = "experimental_metrics_periodicreader_with_async_runtime"))]
let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter).build();

Ok(SdkMeterProvider::builder()
Expand All @@ -79,8 +71,10 @@ fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, Metric
.build())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// #[tokio::main]
// TODO: Re-enable tokio::main, if needed, after
// https://github.com/open-telemetry/opentelemetry-rust/pull/2456
fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let logger_provider = init_logs()?;

// Create a new OpenTelemetryTracingBridge using the above LoggerProvider.
Expand Down
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 @@ -85,6 +85,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);
}
}
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());
println!("\tParentSpanId: {}", &span.parent_span_id);
println!("\tKind : {:?}", &span.span_kind);

Expand Down

0 comments on commit 5e025d2

Please sign in to comment.