Skip to main content

Crate opentelemetry_sdk

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 = SdkTracerProvider::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.

§Environment Variables

The SDK respects the following environment variables, as defined by the OpenTelemetry specification. Programmatic configuration via builder methods takes precedence over environment variables.

§General / Resource

VariableDescriptionDefault
OTEL_SERVICE_NAMESets the value of the service.name resource attribute. Takes priority over service.name in OTEL_RESOURCE_ATTRIBUTES.unknown_service:<process_name>
OTEL_RESOURCE_ATTRIBUTESKey-value pairs to be used as resource attributes. Format: key1=value1,key2=value2.(none)

§Trace: Sampler

VariableDescriptionDefault
OTEL_TRACES_SAMPLERSampler to use. Valid values: always_on, always_off, traceidratio, parentbased_always_on, parentbased_always_off, parentbased_traceidratio.parentbased_always_on
OTEL_TRACES_SAMPLER_ARGArgument for the sampler. Used when OTEL_TRACES_SAMPLER is traceidratio or parentbased_traceidratio. Must be a float between 0.0 and 1.0.1.0

§Trace: Span Limits

VariableDescriptionDefault
OTEL_SPAN_ATTRIBUTE_COUNT_LIMITMaximum number of attributes allowed on a span.128
OTEL_SPAN_EVENT_COUNT_LIMITMaximum number of events allowed on a span.128
OTEL_SPAN_LINK_COUNT_LIMITMaximum number of links allowed on a span.128

§Trace: Batch Span Processor (BSP)

VariableDescriptionDefault
OTEL_BSP_SCHEDULE_DELAYDelay interval (in milliseconds) between two consecutive exports.5000
OTEL_BSP_MAX_QUEUE_SIZEMaximum queue size.2048
OTEL_BSP_MAX_EXPORT_BATCH_SIZEMaximum batch size. Must be less than or equal to OTEL_BSP_MAX_QUEUE_SIZE.512
OTEL_BSP_MAX_CONCURRENT_EXPORTSMaximum number of concurrent exports. Honored by span_processor_with_async_runtime::BatchSpanProcessor; thread-based BatchSpanProcessor exports serially. For concurrent exports, enable experimental_trace_batch_span_processor_with_async_runtime and use the async-runtime processor.1

§Logs: Batch Log Record Processor (BLRP)

VariableDescriptionDefault
OTEL_BLRP_SCHEDULE_DELAYDelay interval (in milliseconds) between two consecutive exports.1000
OTEL_BLRP_MAX_QUEUE_SIZEMaximum queue size.2048
OTEL_BLRP_MAX_EXPORT_BATCH_SIZEMaximum batch size. Must be less than or equal to OTEL_BLRP_MAX_QUEUE_SIZE.512

§Metrics: Periodic Metric Reader

VariableDescriptionDefault
OTEL_METRIC_EXPORT_INTERVALInterval (in milliseconds) between metrics exports.60000

§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 (enabled by default).
  • logs: Includes the logs SDK (enabled by default).

For trace the following feature flags are available:

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.

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.