From af1f7500be34ed604d9aa7d00946c34b9c08ca75 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 6 Nov 2023 16:25:28 -0800 Subject: [PATCH 1/2] Add jaeger tracing example to main examples --- Cargo.toml | 1 + examples/tracing-jaeger/Cargo.toml | 13 ++++++++ examples/tracing-jaeger/README.md | 19 ++++++++++++ examples/tracing-jaeger/src/main.rs | 47 +++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 examples/tracing-jaeger/Cargo.toml create mode 100644 examples/tracing-jaeger/README.md create mode 100644 examples/tracing-jaeger/src/main.rs 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..8145175752 --- /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", "metrics", "logs"] } +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..13fa54493f --- /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 OTLP. + +## Usage + +Launch the application: + +```shell + +# Run jaeger in background +$ 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..9aebbd0ec6 --- /dev/null +++ b/examples/tracing-jaeger/src/main.rs @@ -0,0 +1,47 @@ +use opentelemetry::{ + global, + trace::{TraceContextExt, TraceError, Tracer}, + Key, KeyValue, +}; +use opentelemetry_sdk::{runtime, trace as sdktrace, Resource}; +use opentelemetry_otlp::WithExportConfig; +use std::error::Error; +use opentelemetry::global::shutdown_tracer_provider; + +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("ex.com/basic"); + tracer.in_span("operation", |cx| { + let span = cx.span(); + span.add_event( + "Nice operation!".to_string(), + vec![Key::new("bogons").i64(100)], + ); + tracer.in_span("Sub operation...", |cx| { + let span = cx.span(); + span.add_event("Sub span event", vec![]); + }); + }); + + shutdown_tracer_provider(); + Ok(()) +} From e438b5a984b0ec7a9214814de93489f5915737b3 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 6 Nov 2023 17:08:13 -0800 Subject: [PATCH 2/2] slight modification to examples --- examples/tracing-jaeger/Cargo.toml | 2 +- examples/tracing-jaeger/README.md | 6 +++--- examples/tracing-jaeger/src/main.rs | 19 ++++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/examples/tracing-jaeger/Cargo.toml b/examples/tracing-jaeger/Cargo.toml index 8145175752..afafd3fdb1 100644 --- a/examples/tracing-jaeger/Cargo.toml +++ b/examples/tracing-jaeger/Cargo.toml @@ -9,5 +9,5 @@ publish = false opentelemetry = { path = "../../opentelemetry" } opentelemetry-jaeger = { path = "../../opentelemetry-jaeger" } opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["rt-tokio"] } -opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = ["tonic", "metrics", "logs"] } +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 index 13fa54493f..20adc19f17 100644 --- a/examples/tracing-jaeger/README.md +++ b/examples/tracing-jaeger/README.md @@ -1,14 +1,14 @@ # Exporting traces to Jaeger -This example shows how to export spans to Jaeger agent using OTLP. +This example shows how to export spans to Jaeger agent using OTLPExporter. ## Usage -Launch the application: +Launch the example app with Jaeger running in background via docker: ```shell -# Run jaeger in background +# 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 diff --git a/examples/tracing-jaeger/src/main.rs b/examples/tracing-jaeger/src/main.rs index 9aebbd0ec6..e80f028390 100644 --- a/examples/tracing-jaeger/src/main.rs +++ b/examples/tracing-jaeger/src/main.rs @@ -1,12 +1,12 @@ +use opentelemetry::global::shutdown_tracer_provider; use opentelemetry::{ global, trace::{TraceContextExt, TraceError, Tracer}, - Key, KeyValue, + KeyValue, }; -use opentelemetry_sdk::{runtime, trace as sdktrace, Resource}; use opentelemetry_otlp::WithExportConfig; +use opentelemetry_sdk::{runtime, trace as sdktrace, Resource}; use std::error::Error; -use opentelemetry::global::shutdown_tracer_provider; fn init_tracer() -> Result { opentelemetry_otlp::new_pipeline() @@ -29,16 +29,17 @@ fn init_tracer() -> Result { async fn main() -> Result<(), Box> { let _tracer = init_tracer().expect("Failed to initialize tracer."); - let tracer = global::tracer("ex.com/basic"); - tracer.in_span("operation", |cx| { + 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( - "Nice operation!".to_string(), - vec![Key::new("bogons").i64(100)], + "Main span event".to_string(), + vec![KeyValue::new("foo", "1")], ); - tracer.in_span("Sub operation...", |cx| { + tracer.in_span("child-operation...", |cx| { let span = cx.span(); - span.add_event("Sub span event", vec![]); + span.add_event("Sub span event", vec![KeyValue::new("bar", "1")]); }); });