Expand description
The OTLP Exporter supports exporting logs, metrics and traces in the OTLP format to the OpenTelemetry collector or other compatible backend.
The OpenTelemetry Collector offers a vendor-agnostic implementation on how to receive, process, and export telemetry data. In addition, it removes the need to run, operate, and maintain multiple agents/collectors in order to support open-source telemetry data formats (e.g. Jaeger, Prometheus, etc.) sending to multiple open-source or commercial back-ends.
Currently, this crate supports sending telemetry in OTLP via gRPC and http (binary and json).
§Quickstart
First make sure you have a running version of the opentelemetry collector you want to send data to:
$ docker run -p 4317:4317 otel/opentelemetry-collector:latest
Then create a new Exporter
, and Provider
with the recommended defaults to start exporting
telemetry.
You will have to build a OTLP exporter first. Create the correct exporter based on the signal
you are looking to export SpanExporter::builder()
, MetricExporter::builder()
,
LogExporter::builder()
respectively for traces, metrics, and logs.
Once you have the exporter, you can create your Provider
by starting with TracerProvider::builder()
,
SdkMeterProvider::builder()
, and LoggerProvider::builder()
respectively for traces, metrics, and logs.
use opentelemetry::global;
use opentelemetry::trace::Tracer;
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
// First, create a OTLP exporter builder. Configure it as you need.
let otlp_exporter = opentelemetry_otlp::SpanExporter::builder().with_tonic().build()?;
// Then pass it into provider builder
let _ = opentelemetry_sdk::trace::SdkTracerProvider::builder()
.with_simple_exporter(otlp_exporter)
.build();
let tracer = global::tracer("my_tracer");
tracer.in_span("doing_work", |cx| {
// Traced app logic here...
});
Ok(())
}
§Performance
For optimal performance, a batch exporting processor is recommended as the simple processor will export each span synchronously on dropping, and is only good for test/debug purposes.
[dependencies]
opentelemetry-otlp = { version = "*", features = ["grpc-tonic"] }
use opentelemetry::global;
use opentelemetry::trace::Tracer;
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
// First, create a OTLP exporter builder. Configure it as you need.
let otlp_exporter = opentelemetry_otlp::SpanExporter::builder().with_tonic().build()?;
// Then pass it into provider builder
let _ = opentelemetry_sdk::trace::SdkTracerProvider::builder()
.with_batch_exporter(otlp_exporter)
.build();
let tracer = global::tracer("my_tracer");
tracer.in_span("doing_work", |cx| {
// Traced app logic here...
});
Ok(())
}
§Feature Flags
The following feature flags can enable exporters for different telemetry signals:
trace
: Includes the trace exporters.metrics
: Includes the metrics exporters.logs
: Includes the logs exporters.
The following feature flags generate additional code and types:
serialize
: Enables serialization support for type defined in this create viaserde
.
The following feature flags offer additional configurations on gRPC:
For users using tonic
as grpc layer:
grpc-tonic
: Usetonic
as grpc layer.gzip-tonic
: Use gzip compression fortonic
grpc layer.zstd-tonic
: Use zstd compression fortonic
grpc layer.tls-roots
: Adds system trust roots to rustls-based gRPC clients using the rustls-native-certs cratetls-webpki-roots
: Embeds Mozilla’s trust roots to rustls-based gRPC clients using the webpki-roots crate
The following feature flags offer additional configurations on http:
http-proto
: Use http as transport layer, protobuf as body format. This feature is enabled by default.reqwest-blocking-client
: Use reqwest blocking http client. This feature is enabled by default.reqwest-client
: Use reqwest http client.reqwest-rustls
: Use reqwest with TLS with system trust roots viarustls-native-certs
crate.reqwest-rustls-webpki-roots
: Use reqwest with TLS with Mozilla’s trust roots viawebpki-roots
crate.
§Kitchen Sink Full Configuration
Example showing how to override all configuration options.
Generally there are two parts of configuration. One is the exporter, the other is the provider. Users can configure the exporter using SpanExporter::builder() for traces, and MetricExporter::builder() + opentelemetry_sdk::metrics::PeriodicReader::builder() for metrics. Once you have an exporter, you can add it to either a opentelemetry_sdk::trace::SdkTracerProvider::builder() for traces, or opentelemetry_sdk::metrics::SdkMeterProvider::builder() for metrics.
use opentelemetry::{global, KeyValue, trace::Tracer};
use opentelemetry_sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
use opentelemetry_sdk::metrics::Temporality;
use opentelemetry_otlp::{Protocol, WithExportConfig, WithTonicConfig};
use std::time::Duration;
use tonic::metadata::*;
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut map = MetadataMap::with_capacity(3);
map.insert("x-host", "example.com".parse().unwrap());
map.insert("x-number", "123".parse().unwrap());
map.insert_bin("trace-proto-bin", MetadataValue::from_bytes(b"[binary data]"));
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_endpoint("http://localhost:4317")
.with_timeout(Duration::from_secs(3))
.with_metadata(map)
.build()?;
let tracer_provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
.with_batch_exporter(exporter)
.with_config(
trace::Config::default()
.with_sampler(Sampler::AlwaysOn)
.with_id_generator(RandomIdGenerator::default())
.with_max_events_per_span(64)
.with_max_attributes_per_span(16)
.with_max_events_per_span(16)
.with_resource(Resource::builder_empty().with_attributes([KeyValue::new("service.name", "example")]).build()),
).build();
global::set_tracer_provider(tracer_provider.clone());
let tracer = global::tracer("tracer-name");
let exporter = opentelemetry_otlp::MetricExporter::builder()
.with_tonic()
.with_endpoint("http://localhost:4318/v1/metrics")
.with_protocol(Protocol::Grpc)
.with_timeout(Duration::from_secs(3))
.build()
.unwrap();
let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder()
.with_periodic_exporter(exporter)
.with_resource(Resource::builder_empty().with_attributes([KeyValue::new("service.name", "example")]).build())
.build();
tracer.in_span("doing_work", |cx| {
// Traced app logic here...
});
Ok(())
}
Structs§
- Export
Config - Configuration for the OTLP exporter.
- Http
Exporter Builder http-proto
orhttp-json
- Configuration for the OTLP HTTP exporter.
- Http
Exporter Builder Set http-proto
orhttp-json
- Type to hold the HttpExporterBuilder and indicate it has been set.
- LogExporter
logs
and (http-proto
orhttp-json
orgrpc-tonic
) - OTLP exporter that sends log data
- Metric
Exporter metrics
and (http-proto
orhttp-json
orgrpc-tonic
) - Export metrics in OTEL format.
- NoExporter
Builder Set - Type to indicate the builder does not have a client set.
- Span
Exporter trace
and (http-proto
orhttp-json
orgrpc-tonic
) - OTLP exporter that sends tracing information
- Tonic
Config grpc-tonic
- Configuration for tonic
- Tonic
Exporter Builder grpc-tonic
- Configuration for the tonic OTLP GRPC exporter.
- Tonic
Exporter Builder Set grpc-tonic
- Type to hold the TonicExporterBuilder and indicate it has been set.
Enums§
- Compression
- The compression algorithm to use when sending data.
- Error
- Wrap type for errors from this crate.
- Protocol
- The communication protocol to use when exporting data.
Constants§
- OTEL_
EXPORTER_ OTLP_ COMPRESSION - Compression algorithm to use, defaults to none.
- OTEL_
EXPORTER_ OTLP_ ENDPOINT - Target to which the exporter is going to send signals, defaults to https://localhost:4317. Learn about the relationship between this constant and metrics/spans/logs at https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp
- OTEL_
EXPORTER_ OTLP_ ENDPOINT_ DEFAULT - Default target to which the exporter is going to send signals.
- OTEL_
EXPORTER_ OTLP_ HEADERS - Key-value pairs to be used as headers associated with gRPC or HTTP requests
Example:
k1=v1,k2=v2
Note: as of now, this is only supported for HTTP requests. - OTEL_
EXPORTER_ OTLP_ LOGS_ COMPRESSION logs
and (http-proto
orhttp-json
orgrpc-tonic
) - Compression algorithm to use, defaults to none.
- OTEL_
EXPORTER_ OTLP_ LOGS_ ENDPOINT logs
and (http-proto
orhttp-json
orgrpc-tonic
) - Target to which the exporter is going to send logs
- OTEL_
EXPORTER_ OTLP_ LOGS_ HEADERS logs
and (http-proto
orhttp-json
orgrpc-tonic
) - Key-value pairs to be used as headers associated with gRPC or HTTP requests
for sending logs.
Example:
k1=v1,k2=v2
Note: this is only supported for HTTP. - OTEL_
EXPORTER_ OTLP_ LOGS_ TIMEOUT logs
and (http-proto
orhttp-json
orgrpc-tonic
) - Maximum time the OTLP exporter will wait for each batch logs export.
- OTEL_
EXPORTER_ OTLP_ METRICS_ COMPRESSION metrics
and (http-proto
orhttp-json
orgrpc-tonic
) - Compression algorithm to use, defaults to none.
- OTEL_
EXPORTER_ OTLP_ METRICS_ ENDPOINT metrics
and (http-proto
orhttp-json
orgrpc-tonic
) - Target to which the exporter is going to send metrics, defaults to https://localhost:4317/v1/metrics. Learn about the relationship between this constant and default/spans/logs at https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp
- OTEL_
EXPORTER_ OTLP_ METRICS_ HEADERS metrics
and (http-proto
orhttp-json
orgrpc-tonic
) - Key-value pairs to be used as headers associated with gRPC or HTTP requests
for sending metrics.
Example:
k1=v1,k2=v2
Note: this is only supported for HTTP. - OTEL_
EXPORTER_ OTLP_ METRICS_ TIMEOUT metrics
and (http-proto
orhttp-json
orgrpc-tonic
) - Max waiting time for the backend to process each metrics batch, defaults to 10s.
- OTEL_
EXPORTER_ OTLP_ PROTOCOL - Protocol the exporter will use. Either
http/protobuf
orgrpc
. - OTEL_
EXPORTER_ OTLP_ PROTOCOL_ DEFAULT - Default protocol, using http-json.
- OTEL_
EXPORTER_ OTLP_ TIMEOUT - Max waiting time for the backend to process each signal batch, defaults to 10 seconds.
- OTEL_
EXPORTER_ OTLP_ TIMEOUT_ DEFAULT - Default max waiting time for the backend to process each signal batch.
- OTEL_
EXPORTER_ OTLP_ TRACES_ COMPRESSION trace
and (http-proto
orhttp-json
orgrpc-tonic
) - Compression algorithm to use, defaults to none.
- OTEL_
EXPORTER_ OTLP_ TRACES_ ENDPOINT trace
and (http-proto
orhttp-json
orgrpc-tonic
) - Target to which the exporter is going to send spans, defaults to https://localhost:4317/v1/traces. Learn about the relationship between this constant and default/metrics/logs at https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp
- OTEL_
EXPORTER_ OTLP_ TRACES_ HEADERS trace
and (http-proto
orhttp-json
orgrpc-tonic
) - Key-value pairs to be used as headers associated with gRPC or HTTP requests
for sending spans.
Example:
k1=v1,k2=v2
Note: this is only supported for HTTP. - OTEL_
EXPORTER_ OTLP_ TRACES_ TIMEOUT trace
and (http-proto
orhttp-json
orgrpc-tonic
) - Max waiting time for the backend to process each spans batch, defaults to 10s.
Traits§
- HasExport
Config - Provide access to the ExportConfig field within the exporter builders.
- HasHttp
Config http-proto
orhttp-json
- Expose interface for modifying builder config.
- HasTonic
Config grpc-tonic
- Expose interface for modifying TonicConfig fields within the exporter builders.
- With
Export Config - Expose methods to override ExportConfig.
- With
Http Config http-proto
orhttp-json
- This trait will be implemented for every struct that implemented
HasHttpConfig
trait. - With
Tonic Config grpc-tonic
- Expose methods to override TonicConfig.