Skip to main content

gcloud_observability/
lib.rs

1//! # Google Cloud observability for Rust
2//!
3//! Sends what a service emits through `tracing` and OpenTelemetry to Google Cloud: Cloud
4//! Logging, Cloud Trace and Error Reporting. Formerly `opentelemetry-gcloud-trace`.
5//!
6//! The guide, which covers each goal below, is at
7//! <https://gcloud-observability-rust.abdolence.dev/>.
8//!
9//! ## What each feature gives
10//!
11//! - `logs` (default): the [`logs`] module, a `tracing-subscriber` layer that writes one
12//!   structured Cloud Logging entry per event, correlated with the trace and span that
13//!   emitted it. It writes JSON lines to stdout, which the Cloud Logging agent of GKE,
14//!   Cloud Run and GCE collects. Error Reporting support, with backtraces and a panic hook,
15//!   is part of it and works on every sink.
16//! - `logs-api`: a sink for the layer that writes through the Cloud Logging API, for hosts with
17//!   no logging agent.
18//! - `otlp`: the [`otlp`] module, OpenTelemetry exporters for the Google Cloud Telemetry API
19//!   (`telemetry.googleapis.com`), which takes OTLP, the OpenTelemetry wire protocol, and
20//!   [`otlp::GcpAuthInterceptor`], which authenticates any OTLP exporter with Google
21//!   Application Default Credentials, metrics exporters included.
22//! - `logs-otlp`: a sink for the layer that sends entries over OTLP to the Telemetry API; it
23//!   includes `logs` and `otlp`.
24//! - `trace-api`: the [`trace_api`] module, an OpenTelemetry span exporter for the Cloud Trace
25//!   API v2 (`cloudtrace.googleapis.com`).
26//! - `tls-roots` (default), `tls-webpki-roots` and `auth-default-crypto` (default) choose the
27//!   TLS root certificates and the rustls crypto provider.
28//!
29//! No exporter is enabled by default. Nothing is re-exported at the crate root; [`errors`]
30//! holds the error types, and [`ids`] the `LogId` the Cloud Logging API and OTLP sinks take.
31//!
32//! ## Example
33//!
34//! Spans exported through OTLP, and every event written as a Cloud Logging JSON line carrying
35//! the trace and span it happened in:
36//!
37//! ```ignore
38//! use gcloud_observability::logs::GcpCloudLoggingLayerBuilder;
39//! use gcloud_observability::otlp::GcpOtlpExporterBuilder;
40//! use tracing_subscriber::layer::SubscriberExt;
41//! use tracing_subscriber::{EnvFilter, Registry};
42//!
43//! let otlp = GcpOtlpExporterBuilder::new(google_project_id.clone());
44//! let tracer_provider = otlp.create_provider().await?;
45//! let tracer = otlp.install(&tracer_provider);
46//!
47//! let log_layer = GcpCloudLoggingLayerBuilder::new(google_project_id).build()?;
48//!
49//! // The OpenTelemetry layer goes first: the logging layer reads the span context it attaches.
50//! let subscriber = Registry::default()
51//!     .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
52//!     .with(tracing_opentelemetry::layer().with_tracer(tracer))
53//!     .with(log_layer);
54//! tracing::subscriber::set_global_default(subscriber)?;
55//!
56//! // Exports the spans still queued; blocks, so it needs a multi-threaded Tokio runtime.
57//! tracer_provider.shutdown()?;
58//! ```
59//!
60//! Full examples, one for each goal, are in the `examples` directory.
61
62#![allow(unused_parens, clippy::new_without_default, clippy::needless_update)]
63
64#[cfg(any(feature = "logs", feature = "trace-api", feature = "otlp"))]
65mod common;
66pub mod errors;
67#[cfg(any(feature = "logs-api", feature = "logs-otlp"))]
68pub mod ids;
69
70#[cfg(feature = "logs")]
71pub mod logs;
72
73#[cfg(feature = "otlp")]
74pub mod otlp;
75
76#[cfg(feature = "trace-api")]
77pub mod trace_api;