Skip to content
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions crates/trusted-server-adapter-fastly/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use chrono::{SecondsFormat, Utc};
use log_fastly::Logger;

/// Extracts the final `::` segment from a Rust module path for use as a log label.
///
/// Falls back to the full target string when the input contains no separator or
/// when the separator appears at the trailing position (e.g. `"foo::"`), which
/// would otherwise produce an empty label in log output.
fn target_label(target: &str) -> &str {
match target.rsplit_once("::") {
Some((head, "")) => head,
Some((_, last)) => last,
None => target,
}
}

/// Initialises the Fastly-backed `fern` logger and installs it as the global logger.
///
/// Log records are forwarded to the `tslog` Fastly endpoint and echoed to stdout.
/// Each line is prefixed with an RFC 3339 timestamp, level, and the final segment
/// of the record's target module path.
///
/// # Panics
///
/// Panics if the logger cannot be built or if a global logger has already been set.
pub(crate) fn init_logger() {
let logger = Logger::builder()
.default_endpoint("tslog")
.echo_stdout(true)
.max_level(log::LevelFilter::Info)
Comment thread
prk-Jr marked this conversation as resolved.
.build()
.expect("should build Logger");

fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{} {} [{}] {}",
Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true),
record.level(),
target_label(record.target()),
message
));
})
.chain(Box::new(logger) as Box<dyn log::Log>)
.apply()
.expect("should initialize logger");
}
Comment thread
prk-Jr marked this conversation as resolved.

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn target_label_extracts_correct_segment() {
assert_eq!(
Comment thread
prk-Jr marked this conversation as resolved.
target_label("trusted_server_adapter_fastly::proxy"),
"proxy",
"should handle standard single-separator case"
);
assert_eq!(
target_label("foo::bar::baz"),
"baz",
"should handle multiple separators"
);
assert_eq!(
target_label("no_separators_here"),
"no_separators_here",
"should handle inputs without ::"
);
assert_eq!(target_label(""), "", "should handle empty strings");
assert_eq!(
target_label("trailing::"),
"trailing",
"should strip separator when trailing segment is empty"
);
}
}
31 changes: 2 additions & 29 deletions crates/trusted-server-adapter-fastly/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use error_stack::Report;
use fastly::http::Method;
use fastly::{Error, Request, Response};
use log_fastly::Logger;

use trusted_server_core::auction::endpoints::handle_auction;
use trusted_server_core::auction::{build_orchestrator, AuctionOrchestrator};
Expand All @@ -28,6 +27,7 @@ use trusted_server_core::settings::Settings;
use trusted_server_core::settings_data::get_settings;

mod error;
mod logging;
mod management_api;
mod platform;

Expand All @@ -36,7 +36,7 @@ use crate::platform::{build_runtime_services, open_kv_store, UnavailableKvStore}

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
init_logger();
logging::init_logger();

// Keep the health probe independent from settings loading and routing so
// readiness checks still get a cheap liveness response during startup.
Expand Down Expand Up @@ -241,30 +241,3 @@ fn finalize_response(settings: &Settings, geo_info: Option<&GeoInfo>, response:
response.set_header(key, value);
}
}

fn init_logger() {
let logger = Logger::builder()
.default_endpoint("tslog")
.echo_stdout(true)
.max_level(log::LevelFilter::Info)
.build()
.expect("should build Logger");

fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{} {} [{}] {}",
chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
record.level(),
record
.target()
.split("::")
.last()
.unwrap_or(record.target()),
message
))
})
.chain(Box::new(logger) as Box<dyn log::Log>)
.apply()
.expect("should initialize logger");
}
1 change: 0 additions & 1 deletion crates/trusted-server-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ iab_gpp = { workspace = true }
jose-jwk = { workspace = true }
log = { workspace = true }
rand = { workspace = true }
log-fastly = { workspace = true }
lol_html = { workspace = true }
matchit = { workspace = true }
pin-project-lite = { workspace = true }
Expand Down
Loading
Loading