otlp
ModuleOpenTelemetry: aggregates measurements in memory and exports them as
OTLP/protobuf through a Transport.
Contents
2 submodules, and what the module defines itself.
use utopia_telemetry::OpenTelemetry;pub struct OpenTelemetryThe OpenTelemetry backend: instruments aggregate in memory (cumulative
sums, explicit-bucket histograms, last-value gauges) and
Telemetry::collect exports everything as one OTLP request.
Instruments are registered on their first measurement, so an instrument that never records is never exported, and metrics without data points (an observable gauge whose callbacks reported nothing) are left out: a collector such as Prometheus rejects a whole request over one of them. When nothing is left, nothing is sent. Gauges carry no start time.
use std::sync::{Arc, Mutex};
use utopia_telemetry::{BoxFuture, Error, OpenTelemetry, Telemetry, Transport, Value};
/// Keeps the payloads instead of sending them.
#[derive(Default)]
struct Capture(Mutex<Vec<Vec<u8>>>);
impl Transport for Capture {
fn content_type(&self) -> &str {
"application/x-protobuf"
}
fn send(&self, payload: Vec<u8>) -> BoxFuture<'_, Result<Vec<u8>, Error>> {
self.0.lock().unwrap().push(payload);
Box::pin(async { Ok(Vec::new()) })
}
fn shutdown(&self) -> bool {
true
}
}
let capture = Arc::new(Capture::default());
let telemetry = OpenTelemetry::with_transport("edge", "edge-databases", "instance", capture.clone());
telemetry.counter("http.requests", Some("{request}"), None).add(1, &[("route", Value::from("/v1/health"))]);
let exported = tokio::runtime::Runtime::new()?.block_on(telemetry.collect());
assert!(exported);
assert_eq!(capture.0.lock().unwrap().len(), 1);otlp::proto
Own pageThe OTLP metrics protobuf messages this backend writes
(opentelemetry/proto/collector/metrics/v1), encoded by hand: the
messages are few and fixed, and a schema compiler would cost more than it
saves.
otlp::transport
Own page[Transport]: how OTLP payloads reach a collector, and HttpTransport,
the default.
use utopia_telemetry::HttpTransport;pub struct HttpTransportOTLP over HTTP/1.1 (TLS for https), reusing keep-alive connections from
a pool.
A request that fails on a reused connection before any answer (the collector dropped the idle connection) is sent again once on a new one; a timeout is not, since the collector may have accepted it. A connection that failed, or got a 5xx answer, is closed.
use utopia_telemetry::{HttpTransport, Transport};
let transport = HttpTransport::new("http://collector:4318/v1/metrics")?
.with_header("Authorization", "Bearer token");
assert_eq!(transport.content_type(), "application/x-protobuf");Debug.use utopia_telemetry::Transport;pub trait Transport: Send + SyncSends encoded OTLP requests to a collector. Implement it to export another way (a test capture, a queue).