-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an example on how to config multiple span processors. (#539)
- Loading branch information
Showing
12 changed files
with
125 additions
and
19 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,10 @@ | ||
[package] | ||
name = "multiple-span-processors" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio"] } | ||
opentelemetry-zipkin = { path = "../../opentelemetry-zipkin", default-features=false, features=["reqwest-client"]} | ||
opentelemetry-jaeger = { path = "../../opentelemetry-jaeger", features = ["tokio"] } | ||
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,17 @@ | ||
# Work with multiple span processors | ||
|
||
Opentelemetry supports export spans into multiple different destinations. One way to do so is to use multiple span processors. | ||
|
||
In this example, we demonstrate how to send spans to both Jaeger and Zipkin backend. | ||
|
||
To run this example. | ||
|
||
1. Start the Jaeger and Zipkin. Run `docker-compose up` | ||
|
||
2. Use `cargo run` to run the example. | ||
|
||
3. Check the output in Jaeger and Zipkin. The console should also output the SpanData. | ||
|
||
4. Use `docker-compose down -v` to tear down the Jaeger and Zipkin backend. | ||
|
||
|
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 @@ | ||
version: "3" | ||
services: | ||
# Jaeger | ||
jaeger-all-in-one: | ||
image: jaegertracing/all-in-one:1.22 | ||
ports: | ||
- "16686:16686" | ||
- "5775:5775/udp" | ||
- "5775:5775/tcp" | ||
- "6831:6831/udp" | ||
- "6832:6832/udp" | ||
- "5778:5778" | ||
- "14628:14628" | ||
- "14250:14250" | ||
|
||
zipkin: | ||
image: openzipkin/zipkin | ||
ports: | ||
- "9411:9411" |
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,64 @@ | ||
use opentelemetry::global::{self, shutdown_tracer_provider}; | ||
use opentelemetry::sdk::export::trace::stdout::Exporter as StdoutExporter; | ||
use opentelemetry::sdk::trace::{BatchSpanProcessor, TracerProvider}; | ||
use opentelemetry::trace::{mark_span_as_active, TraceError, Tracer}; | ||
use opentelemetry::KeyValue; | ||
use std::io::stdout; | ||
use std::time::Duration; | ||
|
||
fn init_tracer() -> Result<(), TraceError> { | ||
// build a jaeger batch span processor | ||
let jaeger_processor = BatchSpanProcessor::builder( | ||
opentelemetry_jaeger::new_pipeline() | ||
.with_service_name("trace-demo") | ||
.with_tags(vec![KeyValue::new("exporter", "jaeger")]) | ||
.init_exporter()?, | ||
opentelemetry::runtime::Tokio, | ||
) | ||
.build(); | ||
|
||
// build a zipkin exporter | ||
let zipkin_exporter = opentelemetry_zipkin::new_pipeline() | ||
.with_service_name("trace-demo") | ||
.init_exporter()?; | ||
|
||
let provider = TracerProvider::builder() | ||
// We can build a span processor and pass it into provider. | ||
.with_span_processor(jaeger_processor) | ||
// For batch span processor, we can also provide the exporter and runtime and use this | ||
// helper function to build a batch span processor | ||
.with_batch_exporter(zipkin_exporter, opentelemetry::runtime::Tokio) | ||
// Same helper function is also available to build a simple span processor. | ||
.with_simple_exporter(StdoutExporter::new(stdout(), true)) | ||
.build(); | ||
|
||
let _ = global::set_tracer_provider(provider); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
init_tracer()?; | ||
|
||
let tracer = global::tracer("jaeger-and-zipkin"); | ||
|
||
{ | ||
let span = tracer.start("first span"); | ||
let _guard = mark_span_as_active(span); | ||
{ | ||
let _inner = tracer.start("first sub span"); | ||
tokio::time::sleep(Duration::from_millis(15)).await; | ||
} | ||
{ | ||
let _inner = tracer.start("second sub span"); | ||
tokio::time::sleep(Duration::from_millis(15)).await; | ||
} | ||
} | ||
|
||
tokio::time::sleep(Duration::from_millis(15)).await; | ||
|
||
shutdown_tracer_provider(); | ||
|
||
Ok(()) | ||
} |
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
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
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
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