Expand description
§Google Cloud observability for Rust
Sends what a service emits through tracing and OpenTelemetry to Google Cloud: Cloud
Logging, Cloud Trace and Error Reporting. Formerly opentelemetry-gcloud-trace.
The guide, which covers each goal below, is at https://gcloud-observability-rust.abdolence.dev/.
§What each feature gives
logs(default): thelogsmodule, atracing-subscriberlayer that writes one structured Cloud Logging entry per event, correlated with the trace and span that emitted it. It writes JSON lines to stdout, which the Cloud Logging agent of GKE, Cloud Run and GCE collects. Error Reporting support, with backtraces and a panic hook, is part of it and works on every sink.logs-api: a sink for the layer that writes through the Cloud Logging API, for hosts with no logging agent.otlp: theotlpmodule, OpenTelemetry exporters for the Google Cloud Telemetry API (telemetry.googleapis.com), which takes OTLP, the OpenTelemetry wire protocol, andotlp::GcpAuthInterceptor, which authenticates any OTLP exporter with Google Application Default Credentials, metrics exporters included.logs-otlp: a sink for the layer that sends entries over OTLP to the Telemetry API; it includeslogsandotlp.trace-api: thetrace_apimodule, an OpenTelemetry span exporter for the Cloud Trace API v2 (cloudtrace.googleapis.com).tls-roots(default),tls-webpki-rootsandauth-default-crypto(default) choose the TLS root certificates and the rustls crypto provider.
No exporter is enabled by default. Nothing is re-exported at the crate root; errors
holds the error types, and ids the LogId the Cloud Logging API and OTLP sinks take.
§Example
Spans exported through OTLP, and every event written as a Cloud Logging JSON line carrying the trace and span it happened in:
ⓘ
use gcloud_observability::logs::GcpCloudLoggingLayerBuilder;
use gcloud_observability::otlp::GcpOtlpExporterBuilder;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{EnvFilter, Registry};
let otlp = GcpOtlpExporterBuilder::new(google_project_id.clone());
let tracer_provider = otlp.create_provider().await?;
let tracer = otlp.install(&tracer_provider);
let log_layer = GcpCloudLoggingLayerBuilder::new(google_project_id).build()?;
// The OpenTelemetry layer goes first: the logging layer reads the span context it attaches.
let subscriber = Registry::default()
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
.with(tracing_opentelemetry::layer().with_tracer(tracer))
.with(log_layer);
tracing::subscriber::set_global_default(subscriber)?;
// Exports the spans still queued; blocks, so it needs a multi-threaded Tokio runtime.
tracer_provider.shutdown()?;Full examples, one for each goal, are in the examples directory.
Modules§
- errors
- ids
- The validated identifier the Cloud Logging API and OTLP log sinks take: the log the entries go to.
- logs
- A
tracing_subscriber::Layerthat writes structured log entries for Google Cloud Logging, correlated to Cloud Trace. - otlp
- Exports through OTLP to the Google Cloud Telemetry API
(
telemetry.googleapis.com), authenticated with Google credentials. - trace_
api - Exports spans to Google Cloud Trace through the Cloud Trace API v2
(
cloudtrace.googleapis.com).