| Crates.io | nano-gcp-logging |
| lib.rs | nano-gcp-logging |
| version | 0.1.0 |
| created_at | 2025-09-13 06:53:33.246921+00 |
| updated_at | 2025-09-13 06:53:33.246921+00 |
| description | A custom logging layer for sending logs to Google Cloud Logging using tracing and tracing-subscriber. |
| homepage | |
| repository | https://github.com/aovestdipaperino/nano-gcp-logging |
| max_upload_size | |
| id | 1837357 |
| size | 79,635 |
A compact tracing Layer that captures structured logs and ships them to Google Cloud Logging (Stackdriver). It enriches events with instance and container metadata when available and sends JSON payloads to the Logging API.
(C) 2025 Enzo Lombardi enzinol@gmail.com
Quick links
Features
tracing / tracing-subscriber ecosystemTable of contents
Quickstart
Add to your Cargo.toml:
nano-gcp-logging = "0.1"
Create and initialize the layer, then attach it to a tracing subscriber.
Project ID and metadata
On a Google Cloud VM or GKE node you can query the metadata server to obtain the current project id:
PROJECT_ID=$(curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/project/project-id) echo $PROJECT_ID
In environments without access to the metadata server (local dev, CI), export PROJECT_ID in your environment before starting your application:
export PROJECT_ID="my-project-id"
The crate attempts to auto-discover instance and container metadata by querying the GCE metadata server. If that fails, the library falls back to reasonable defaults (so your app can run locally or in CI without failing on startup).
Usage example
Below is a minimal example demonstrating initialization and attaching the layer to a tracing subscriber. Replace your-gcp-project-id with your project id or set the PROJECT_ID env var.
use nano_gcp_logging::GcpLoggingLayer;
use tracing_subscriber::Registry;
use tracing_subscriber::layer::SubscriberExt;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let project_id = env::var("PROJECT_ID").unwrap_or_else(|_| "your-gcp-project-id".into());
let gcp_layer = GcpLoggingLayer::new(project_id).await?;
let subscriber = Registry::default().with(gcp_layer);
tracing::subscriber::set_global_default(subscriber)?;
tracing::info!("Hello from nano-gcp-logging!");
Ok(())
}
Behavior and local development notes
Authentication
gcp_auth) to obtain an OAuth token with the logging.write scope.Metadata discovery
project_id (from env or constructor) is preserved.Background sending
Examples & tests
examples/basic.rs demonstrates basic initialization and emits a few log events. For local runs, setting PROJECT_ID is recommended.tests/. The crate is designed so tests can run without real GCP credentials — initialized layers will warn and use fallbacks.Local testing tips
To run tests locally without GCP credentials:
export PROJECT_ID="local-project" export HOSTNAME="$(hostname)" cargo test
To run the example locally:
export PROJECT_ID="local-project" cargo run --example basic
To test sending logs to GCP in a development environment:
roles/logging.logWriter or equivalent.Design notes & rationale
Fail-fast vs. permissive initialization
Back-pressure and buffering
Configuration & extension points
GcpLoggingLayer::new(project_id: String) — construct the layer with an explicit project id.Contributing
Contributions, bug reports and PRs are welcome. Please follow the repository's contribution guidelines and coding style.
Run tests with:
cargo test
License
Author