8 releases
Uses new Rust 2024
| 0.2.6 | Sep 10, 2025 |
|---|---|
| 0.2.5 | Sep 8, 2025 |
| 0.1.0 | Aug 4, 2025 |
#592 in Debugging
345 downloads per month
33KB
655 lines
tracing-gcloud-layer
A robust and configurable tracing layer for Rust, delivering logs from the tracing ecosystem directly to Google Cloud Logging (Stackdriver).
🚀 Features
- Seamless integration with Rust’s tracing ecosystem.
- Structured logs delivered to Google Cloud Logging (Stackdriver) using service account authentication.
- Asynchronous, batched log delivery for improved efficiency.
- Log formatting and enrichment customizable via the LogMapper trait.
- Basic support for trace ID and severity metadata propagation.
📦 Installation
Add to your Cargo.toml:
cargo add tracing-gcloud-layer
🛠️ Quickstart
-
Generate a Google Cloud service account with the "Logs Writer" role and download the JSON key.
-
Initialize the tracing layer:
use tracing_gcloud_layer::DefaultGCloudLayerConfigBuilder;
use tracing_subscriber::Registry;
let (layer, _runtime) = DefaultGCloudLayerConfigBuilder::default()
.log_name("my-service")
.logger_credential(include_bytes!("../gcp-service-account.json"))
.build()
.expect("Invalid config")
.build_layer();
tracing_subscriber::registry()
.with(layer)
.init();
- Emit logs in your application:
tracing::info!(user_id = 42, "User logged in");
Logs will appear in Google Cloud Logging under the configured log name
⚙️ Configuration
log_name: Log stream name in GCP (e.g., "stdout", "my-app").logger_credential: Service account credentials (as bytes).config: Batching, timeouts, and writer options.log_mapper: Plug in your ownLogMapperto customize log transformation.
Example: Custom Log Mapper
use tracing_gcloud_layer::{GCloudLayerConfigBuilder, LogContext, LogMapper};
#[derive(Clone, Default)]
pub struct CustomLogMapper;
impl LogMapper for CustomLogMapper {
fn map(&self, context: LogContext, log_entry: serde_json::Value) -> serde_json::Value {
todo!("Custom mapping logic here")
}
}
let (layer, _runtime) = GCloudLayerConfigBuilder::<CustomLogMapper>::default()
.log_name("custom-logs")
.logger_credential(include_bytes!("../gcp.json"))
.log_mapper(CustomLogMapper)
.build()
.expect("Invalid config")
.build_layer();
Dependencies
~19–36MB
~520K SLoC