Expand description
OpenTelemetry instrumentation for the stream processor.
This module provides comprehensive distributed tracing and metrics export using OpenTelemetry. It includes:
- Tracing: Distributed tracing with OTLP exporter, span instrumentation, and multiple sampling strategies.
- Metrics: OTLP metrics export with support for counters, histograms, and gauges.
- Context Propagation: W3C Trace Context and Baggage propagation for distributed tracing across service boundaries.
- Resource Attributes: Automatic detection and configuration of service metadata following OpenTelemetry semantic conventions.
§Usage
§Initializing Tracing
ⓘ
use processor::telemetry::{TraceConfig, init_tracer_provider};
let config = TraceConfig {
enabled: true,
service_name: "my-processor".to_string(),
otlp_endpoint: "http://localhost:4317".to_string(),
..Default::default()
};
let provider = init_tracer_provider(config)?;§Creating Spans
ⓘ
use processor::telemetry::SpanBuilder;
use opentelemetry::trace::SpanKind;
let span = SpanBuilder::new("process_event")
.with_kind(SpanKind::Internal)
.with_attribute("event.type", "metric")
.start();Or using the macro:
ⓘ
use processor::span;
let span = span!("process_event", "event.type" => "metric");§Context Propagation
ⓘ
use processor::telemetry::{inject_into_headers, extract_from_headers};
use opentelemetry::Context;
// Inject context into outgoing request
let mut headers = HashMap::new();
inject_into_headers(&Context::current(), &mut headers);
// Extract context from incoming request
let context = extract_from_headers(&headers);§Recording Metrics
ⓘ
use processor::telemetry::{MetricsRecorder, metric_names};
use opentelemetry::KeyValue;
let recorder = MetricsRecorder::new()
.with_attribute("service", "processor");
recorder.record_counter(
"processor",
metric_names::EVENTS_PROCESSED,
1,
vec![KeyValue::new("event.type", "metric")],
);§Integration with Existing Code
The telemetry module integrates seamlessly with the existing tracing crate
used throughout the processor. Use tracing-opentelemetry to bridge the two:
ⓘ
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
use tracing_opentelemetry::OpenTelemetryLayer;
let tracer = global::tracer("processor");
let telemetry = OpenTelemetryLayer::new(tracer);
let subscriber = Registry::default().with(telemetry);
tracing::subscriber::set_global_default(subscriber)?;Re-exports§
pub use context::extract_context;pub use context::extract_from_headers;pub use context::inject_context;pub use context::inject_into_headers;pub use context::context_with_span;pub use context::get_baggage;pub use context::propagate_context;pub use context::with_baggage;pub use context::with_context;pub use context::BaggageItem;pub use context::HeaderCarrier;pub use metrics::init_meter_provider;pub use metrics::shutdown_meter_provider;pub use metrics::metric_attributes;pub use metrics::metric_names;pub use metrics::ExportConfig;pub use metrics::MetricsConfig;pub use metrics::MetricsRecorder;pub use metrics::Temporality;pub use resource::create_processor_resource;pub use resource::ResourceBuilder;pub use tracing::init_tracer_provider;pub use tracing::record_exception;pub use tracing::shutdown_tracer_provider;pub use tracing::BatchConfiguration;pub use tracing::OtlpProtocol;pub use tracing::SamplingStrategy;pub use tracing::SpanBuilder;pub use tracing::TraceConfig;
Modules§
- context
- Context propagation for distributed tracing.
- global
- Utilities for working with global telemetry primitives
- metrics
- OTLP metrics export for OpenTelemetry.
- resource
- Resource attributes and detection for OpenTelemetry.
- tracing
- Distributed tracing with OpenTelemetry.
Structs§
- Context
- An execution-scoped collection of values.
- KeyValue
- A key-value pair describing an attribute.
- Telemetry
Config - Combined configuration for both tracing and metrics.
- Telemetry
Providers - Container for initialized telemetry providers.
Enums§
- Span
Kind SpanKinddescribes the relationship between theSpan, its parents, and its children in a trace.- Status
- The status of a
Span.
Traits§
Functions§
- init_
telemetry - Initializes both tracing and metrics providers.