pub trait WithTonicConfig {
// Required methods
fn with_metadata(self, metadata: MetadataMap) -> Self;
fn with_compression(self, compression: Compression) -> Self;
fn with_channel(self, channel: Channel) -> Self;
fn with_interceptor<I>(self, interceptor: I) -> Self
where I: Interceptor + Clone + Send + Sync + 'static;
}Expand description
Expose methods to override TonicConfig.
This trait will be implemented for every struct that implemented HasTonicConfig trait.
§Examples
use opentelemetry_otlp::{WithExportConfig, WithTonicConfig};
let exporter_builder = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_compression(opentelemetry_otlp::Compression::Gzip);Required Methods§
Sourcefn with_metadata(self, metadata: MetadataMap) -> Self
fn with_metadata(self, metadata: MetadataMap) -> Self
Set custom metadata entries to send to the collector.
Note: This method is additive - calling it multiple times will merge the metadata entries. If the same key is provided in multiple calls, the last value will override previous ones.
§Example
use tonic::metadata::MetadataMap;
use opentelemetry_otlp::WithTonicConfig;
let mut metadata1 = MetadataMap::new();
metadata1.insert("key1", "value1".parse().unwrap());
let mut metadata2 = MetadataMap::new();
metadata2.insert("key2", "value2".parse().unwrap());
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_metadata(metadata1) // Adds key1=value1
.with_metadata(metadata2) // Adds key2=value2 (both are present)
.build()?;Sourcefn with_compression(self, compression: Compression) -> Self
fn with_compression(self, compression: Compression) -> Self
Set the compression algorithm to use when communicating with the collector.
Sourcefn with_channel(self, channel: Channel) -> Self
fn with_channel(self, channel: Channel) -> Self
Use channel as tonic’s transport channel.
this will override tls config and should only be used
when working with non-HTTP transports.
Users MUST make sure the ExportConfig::timeout is
the same as the channel’s timeout.
Sourcefn with_interceptor<I>(self, interceptor: I) -> Self
fn with_interceptor<I>(self, interceptor: I) -> Self
Use a custom interceptor to modify each outbound request.
This can be used to modify the gRPC metadata, for example
to inject auth tokens.
Note: Calling this method multiple times will replace the previous interceptor. If you need multiple interceptors, chain them together before passing to this method.
§Examples
§Single interceptor
use tonic::{Request, Status};
use opentelemetry_otlp::WithTonicConfig;
fn auth_interceptor(mut req: Request<()>) -> Result<Request<()>, Status> {
req.metadata_mut().insert("authorization", "Bearer token".parse().unwrap());
Ok(req)
}
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_interceptor(auth_interceptor)
.build()?;§Multiple interceptors (chaining)
use tonic::{Request, Status};
use opentelemetry_otlp::WithTonicConfig;
fn auth_interceptor(mut req: Request<()>) -> Result<Request<()>, Status> {
req.metadata_mut().insert("authorization", "Bearer token".parse().unwrap());
Ok(req)
}
fn logging_interceptor(req: Request<()>) -> Result<Request<()>, Status> {
println!("Sending gRPC request with metadata: {:?}", req.metadata());
Ok(req)
}
// Chain interceptors by wrapping them
fn combined_interceptor(req: Request<()>) -> Result<Request<()>, Status> {
let req = logging_interceptor(req)?;
auth_interceptor(req)
}
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_interceptor(combined_interceptor)
.build()?;Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.