[][src]Crate opentelemetry_zipkin

OpenTelemetry Zipkin Exporter

Collects OpenTelemetry spans and reports them to a given Zipkin collector endpoint. See the Zipkin Docs for details and deployment information.

Quickstart

First make sure you have a running version of the zipkin process you want to send data to:

$ docker run -d -p 9411:9411 openzipkin/zipkin

Then install a new pipeline with the recommended defaults to start exporting telemetry:

use opentelemetry::api::trace::Tracer;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tracer, _uninstall) = opentelemetry_zipkin::new_pipeline().install()?;

    tracer.in_span("doing_work", |cx| {
        // Traced app logic here...
    });

    Ok(())
}

Performance

For optimal performance, a batch exporter is recommended as the simple exporter will export each span synchronously on drop. You can enable the tokio or async-std features to have a batch exporter configured for you automatically for either executor when you install the pipeline.

[dependencies]
opentelemetry = { version = "*", features = ["tokio"] }
opentelemetry-zipkin = "*"

Bring your own http clients

Users can choose appropriate http clients to align with their runtime.

Based on the feature enabled. The default http client will be different. If user doesn't specific features or enabled reqwest-blocking-client feature. The blocking reqwest http client will be used as default client. If reqwest-client feature is enabled. The async reqwest http client will be used. If surf-client feature is enabled. The surf http client will be used.

Note that async http clients may need specific runtime otherwise it will panic. User should make sure the http client is running in appropriate runime.

Users can always use their own http clients by implementing HttpClient trait.

Kitchen Sink Full Configuration

Example showing how to override all configuration options. See the ZipkinPipelineBuilder docs for details of each option.

use opentelemetry::api::{KeyValue, trace::Tracer};
use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
use opentelemetry::exporter::trace::ExportResult;
use opentelemetry::exporter::trace::HttpClient;
use async_trait::async_trait;
use std::error::Error;

// `reqwest` and `surf` are supported through features, if you prefer an
// alternate http client you can add support by implementing `HttpClient` as
// shown here.
#[derive(Debug)]
struct IsahcClient(isahc::HttpClient);

#[async_trait]
impl HttpClient for IsahcClient {
  async fn send(&self, request: http::Request<Vec<u8>>) -> Result<ExportResult, Box<dyn Error>> {
    let result = self.0.send_async(request).await?;

    if result.status().is_success() {
      Ok(ExportResult::Success)
    } else {
      Ok(ExportResult::FailedNotRetryable)
    }
  }
}

fn main() -> Result<(), Box<dyn Error>> {
    let (tracer, _uninstall) = opentelemetry_zipkin::new_pipeline()
        .with_http_client(IsahcClient(isahc::HttpClient::new()?))
        .with_service_name("my_app")
        .with_service_address("127.0.0.1:8080".parse()?)
        .with_collector_endpoint("http://localhost:9411/api/v2/spans")
        .with_trace_config(
            trace::config()
                .with_default_sampler(Sampler::AlwaysOn)
                .with_id_generator(IdGenerator::default())
                .with_max_events_per_span(64)
                .with_max_attributes_per_span(16)
                .with_max_events_per_span(16)
                .with_resource(Resource::new(vec![KeyValue::new("key", "value")])),
        )
        .install()?;

    tracer.in_span("doing_work", |cx| {
        // Traced app logic here...
    });

    Ok(())
}

Structs

Exporter

Zipkin span exporter

Uninstall

Uninstalls the Zipkin pipeline on drop.

ZipkinPipelineBuilder

Builder for ExporterConfig struct.

Functions

new_pipeline

Create a new Zipkin exporter pipeline builder.