diff --git a/Cargo.toml b/Cargo.toml index 39ee5c00b0..52eec49037 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ members = [ "examples/logs-basic", "examples/traceresponse", "examples/tracing-grpc", + "examples/tracing-jaeger", "stress", ] resolver = "2" diff --git a/examples/tracing-jaeger/Cargo.toml b/examples/tracing-jaeger/Cargo.toml new file mode 100644 index 0000000000..afafd3fdb1 --- /dev/null +++ b/examples/tracing-jaeger/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "tracing-jaeger" +version = "0.1.0" +edition = "2021" +license = "Apache-2.0" +publish = false + +[dependencies] +opentelemetry = { path = "../../opentelemetry" } +opentelemetry-jaeger = { path = "../../opentelemetry-jaeger" } +opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["rt-tokio"] } +opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = ["tonic"] } +tokio = { version = "1.0", features = ["full"] } diff --git a/examples/tracing-jaeger/README.md b/examples/tracing-jaeger/README.md new file mode 100644 index 0000000000..20adc19f17 --- /dev/null +++ b/examples/tracing-jaeger/README.md @@ -0,0 +1,19 @@ +# Exporting traces to Jaeger + +This example shows how to export spans to Jaeger agent using OTLPExporter. + +## Usage + +Launch the example app with Jaeger running in background via docker: + +```shell + +# Run jaeger in background with native OTLP Ingestion +$ docker run -d -p16686:16686 -p4317:4317 -e COLLECTOR_OTLP_ENABLED=true jaegertracing/all-in-one:latest + +# Run the app +$ cargo run + +# View spans +$ firefox http://localhost:16686/ +``` diff --git a/examples/tracing-jaeger/src/main.rs b/examples/tracing-jaeger/src/main.rs new file mode 100644 index 0000000000..e80f028390 --- /dev/null +++ b/examples/tracing-jaeger/src/main.rs @@ -0,0 +1,48 @@ +use opentelemetry::global::shutdown_tracer_provider; +use opentelemetry::{ + global, + trace::{TraceContextExt, TraceError, Tracer}, + KeyValue, +}; +use opentelemetry_otlp::WithExportConfig; +use opentelemetry_sdk::{runtime, trace as sdktrace, Resource}; +use std::error::Error; + +fn init_tracer() -> Result { + opentelemetry_otlp::new_pipeline() + .tracing() + .with_exporter( + opentelemetry_otlp::new_exporter() + .tonic() + .with_endpoint("http://localhost:4317"), + ) + .with_trace_config( + sdktrace::config().with_resource(Resource::new(vec![KeyValue::new( + "service.name", + "tracing-jaeger", + )])), + ) + .install_batch(runtime::Tokio) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let _tracer = init_tracer().expect("Failed to initialize tracer."); + + let tracer = global::tracer("tracing-jaeger"); + tracer.in_span("main-operation", |cx| { + let span = cx.span(); + span.set_attribute(KeyValue::new("my-attribute", "my-value")); + span.add_event( + "Main span event".to_string(), + vec![KeyValue::new("foo", "1")], + ); + tracer.in_span("child-operation...", |cx| { + let span = cx.span(); + span.add_event("Sub span event", vec![KeyValue::new("bar", "1")]); + }); + }); + + shutdown_tracer_provider(); + Ok(()) +}