-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jaeger tracing example to main examples (#1345)
- Loading branch information
1 parent
ab969bf
commit 6c3dc78
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |