Skip to content

Commit

Permalink
Add jaeger tracing example to main examples (#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Nov 7, 2023
1 parent ab969bf commit 6c3dc78
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"examples/logs-basic",
"examples/traceresponse",
"examples/tracing-grpc",
"examples/tracing-jaeger",
"stress",
]
resolver = "2"
Expand Down
13 changes: 13 additions & 0 deletions examples/tracing-jaeger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
19 changes: 19 additions & 0 deletions examples/tracing-jaeger/README.md
Original file line number Diff line number Diff line change
@@ -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/
```
48 changes: 48 additions & 0 deletions examples/tracing-jaeger/src/main.rs
Original file line number Diff line number Diff line change
@@ -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_sdk::trace::Tracer, TraceError> {
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<dyn Error + Send + Sync + 'static>> {
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(())
}

0 comments on commit 6c3dc78

Please sign in to comment.