Crate opentelemetry_sdk

Source
Expand description

Implements the SDK component of OpenTelemetry.

Supported Rust Versions

§Getting Started

use opentelemetry::{global, trace::{Tracer, TracerProvider}};
use opentelemetry_sdk::trace::SdkTracerProvider;

fn main() {
    // Choose an exporter like `opentelemetry_stdout::SpanExporter`
    let exporter = new_exporter();

    // Create a new trace pipeline that prints to stdout
    let provider = TracerProvider::builder()
        .with_simple_exporter(exporter)
        .build();
    let tracer = provider.tracer("readme_example");

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

    // Shutdown trace pipeline
    provider.shutdown().expect("TracerProvider should shutdown successfully")
}

See the examples directory for different integration patterns.

See the API trace module docs for more information on creating and managing spans.

§Metrics

§Creating instruments and recording measurements

use opentelemetry::{global, KeyValue};

// get a meter from a provider
let meter = global::meter("my_service");

// create an instrument
let counter = meter.u64_counter("my_counter").build();

// record a measurement
counter.add(1, &[KeyValue::new("http.client_ip", "83.164.160.102")]);

See the examples directory for different integration patterns.

See the API metrics module docs for more information on creating and managing instruments.

§Crate Feature Flags

The following feature flags can used to control the telemetry signals to use:

  • trace: Includes the trace SDK (enabled by default).
  • metrics: Includes the metrics SDK.
  • logs: Includes the logs SDK.

For trace the following feature flags are available:

For logs the following feature flags are available:

  • spec_unstable_logs_enabled: control the log level

Support for recording and exporting telemetry asynchronously and perform metrics aggregation can be added via the following flags:

  • experimental_async_runtime: Enables the experimental Runtime trait and related functionality.
  • rt-tokio: Spawn telemetry tasks using tokio’s multi-thread runtime.
  • rt-tokio-current-thread: Spawn telemetry tasks on a separate runtime so that the main runtime won’t be blocked.
  • rt-async-std: Spawn telemetry tasks using async-std’s runtime.

Re-exports§

pub use error::ExportError;

Modules§

error
Wrapper for error from trace, logs and metrics part of open telemetry.
logslogs
OpenTelemetry Log SDK
metricsmetrics
The crust of the OpenTelemetry metrics SDK.
propagationtrace
OpenTelemetry Propagators
resource
Representations of entities producing telemetry.
runtimeexperimental_async_runtime
Provides an abstraction of several async runtimes
testingtesting or test
In-Memory exporters for testing purpose.
tracetrace
OpenTelemetry Trace SDK

Structs§

Resource
An immutable representation of the entity producing telemetry as attributes. Utilizes Arc for efficient sharing and cloning.