Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Google Logging support #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
568 changes: 523 additions & 45 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ documentation = "https://github.com/bolcom/unFTP"
path = "crates/redislog"
version = "0.1.2"

[dependencies.googlelog]
path = "crates/googlelog"
features = ["shipper"]
version = "0.1.0"

[dependencies]
async-trait = "0.1.80"
base64 = "0.22.1"
Expand Down
19 changes: 19 additions & 0 deletions crates/googlelog/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "googlelog"
version = "0.1.0"
edition = "2021"
authors = [
"Rob klein Gunnewiek <[email protected]>"
]

[features]
shipper = []

[dependencies]
google-logging2 = "5.0.5"
serde_json = "1.0.117"
chrono = "0.4.38"
tokio = { version = "1.38.0", features = ["macros", "time", "rt-multi-thread"] }
slog = "2.7.0"
thiserror = "1.0.61"
reqwest = { version = "0.12.5", default-features = false, features = ["rustls-tls", "json"] }
40 changes: 40 additions & 0 deletions crates/googlelog/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use thiserror;

use reqwest::{self, StatusCode};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to read the 'default_labels' object from the JSON file, does it exist by this name?. Parse error: {0}")]
DefaultLabelsError(serde_json::Error),
#[error("Failed to read the 'resource_labels' object from the JSON file, does it exist by this name?. Parse error: {0}")]
ResourceLabelsError(serde_json::Error),
#[error("Serde JSON serialization failed with context '{context}'. Error: {source}")]
ShipperSerializeError {
context: String,
source: serde_json::Error,
},
#[error("Reqwest error with context '{context}'. Error: {source}")]
ShipperReqwestError {
context: String,
source: reqwest::Error,
},
#[error("No 'access_token' found in the metadata server response body")]
ShipperTokenNotFound,
#[error("No 'expires_in' found in the metadata server response body")]
ShipperTokenExpiryNotFound,
#[error("unsuccessful HTTP response error with context '{context}'. HTTP status code: '{status}', body: '{body}'")]
HttpResponseError {
context: String,
status: StatusCode,
body: String,
},
}

impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Self::ShipperReqwestError {
context: "Error sending HTTP request".to_string(),
source: err,
}
}
}
27 changes: 27 additions & 0 deletions crates/googlelog/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! An implemention of [`slog::Drain`](https://slog-rs.github.io/slog/slog/trait.Drain.html) for logging to [Google Cloud](https://cloud.google.com/logging).
//!
//! # Usage
//!
//! Warning: Currently, this library only works in the context of [workload identity](https://cloud.google.com/iam/docs/workload-identity-federation).
//!
//! The `googlelog` drain creates log entries compatible with [Google Cloud Logging](https://cloud.google.com/logging).
//! Depending on how you want to ship these logs to the Google Logging API, you can choose from one of the available build methods.
//!
//! Start by configuring the Logger with the builder ([`Builder`](logger::Builder::new)) and selecting the appropriate build method based on your shipping requirements:
//!
//! 1. [`build()`](logger::Builder::build): Receives [`WriteLogEntries`](https://cloud.google.com/logging/docs/reference/v2/rpc/google.logging.v2#google.logging.v2.LoggingServiceV2.WriteLogEntries) over a channel and allows you to handle the transportation manually.
//! 2. [`build_with_async_shipper()`](logger::Builder::build_with_async_shipper): Offloads transportation to the [`Shipper`](shipper::Shipper) and its sync-async Bridge in an async context. (Requires the `shipper` feature.)

//!
//! The [`builder`](struct@logger::Builder) supports several `with_*` methods to customize the log message format,
//! particularly the default labels attached to [log entries](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
//!
/// Googlelog Error types
pub mod error;

/// The [`slog::Drain`](https://slog-rs.github.io/slog/slog/trait.Drain.html) Implementation of the slog Drain for [Google Cloud Logging](https://cloud.google.com/logging)
pub mod logger;

/// An optional async process to ship the log for you
#[cfg(feature = "shipper")]
pub mod shipper;
Loading