mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-13 08:22:55 +00:00
89 lines
2.9 KiB
Rust
89 lines
2.9 KiB
Rust
use tracing::Subscriber;
|
|
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
|
|
use tracing_subscriber::fmt::format::{Format, Full};
|
|
use tracing_subscriber::fmt::time::SystemTime;
|
|
use tracing_subscriber::fmt::{FormatEvent, FormatFields};
|
|
use tracing_subscriber::prelude::*;
|
|
use tracing_subscriber::registry::LookupSpan;
|
|
|
|
/// Initialize logging and OpenTelemetry tracing and exporter.
|
|
///
|
|
/// Logging can be configured using `RUST_LOG` environment variable.
|
|
///
|
|
/// OpenTelemetry is configured with OTLP/HTTP exporter. It picks up
|
|
/// configuration from environment variables. For example, to change the
|
|
/// destination, set `OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318`.
|
|
/// See <https://opentelemetry.io/docs/reference/specification/sdk-environment-variables>
|
|
pub async fn init() -> anyhow::Result<LoggingGuard> {
|
|
let env_filter = EnvFilter::builder()
|
|
.with_default_directive(LevelFilter::INFO.into())
|
|
.from_env_lossy()
|
|
.add_directive("aws_config=info".parse().unwrap())
|
|
.add_directive("azure_core::policies::transport=off".parse().unwrap());
|
|
|
|
let fmt_layer = tracing_subscriber::fmt::layer()
|
|
.with_ansi(false)
|
|
.with_writer(std::io::stderr)
|
|
.with_target(false);
|
|
|
|
let otlp_layer = tracing_utils::init_tracing("proxy").await;
|
|
|
|
tracing_subscriber::registry()
|
|
.with(env_filter)
|
|
.with(otlp_layer)
|
|
.with(fmt_layer)
|
|
.try_init()?;
|
|
|
|
Ok(LoggingGuard)
|
|
}
|
|
|
|
/// Initialize logging for local_proxy with log prefix and no opentelemetry.
|
|
///
|
|
/// Logging can be configured using `RUST_LOG` environment variable.
|
|
pub fn init_local_proxy() -> anyhow::Result<LoggingGuard> {
|
|
let env_filter = EnvFilter::builder()
|
|
.with_default_directive(LevelFilter::INFO.into())
|
|
.from_env_lossy();
|
|
|
|
let fmt_layer = tracing_subscriber::fmt::layer()
|
|
.with_ansi(false)
|
|
.with_writer(std::io::stderr)
|
|
.event_format(LocalProxyFormatter(Format::default().with_target(false)));
|
|
|
|
tracing_subscriber::registry()
|
|
.with(env_filter)
|
|
.with(fmt_layer)
|
|
.try_init()?;
|
|
|
|
Ok(LoggingGuard)
|
|
}
|
|
|
|
pub struct LocalProxyFormatter(Format<Full, SystemTime>);
|
|
|
|
impl<S, N> FormatEvent<S, N> for LocalProxyFormatter
|
|
where
|
|
S: Subscriber + for<'a> LookupSpan<'a>,
|
|
N: for<'a> FormatFields<'a> + 'static,
|
|
{
|
|
fn format_event(
|
|
&self,
|
|
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
|
|
mut writer: tracing_subscriber::fmt::format::Writer<'_>,
|
|
event: &tracing::Event<'_>,
|
|
) -> std::fmt::Result {
|
|
writer.write_str("[local_proxy] ")?;
|
|
self.0.format_event(ctx, writer, event)
|
|
}
|
|
}
|
|
|
|
pub struct LoggingGuard;
|
|
|
|
impl Drop for LoggingGuard {
|
|
fn drop(&mut self) {
|
|
// Shutdown trace pipeline gracefully, so that it has a chance to send any
|
|
// pending traces before we exit.
|
|
tracing::info!("shutting down the tracing machinery");
|
|
tracing_utils::shutdown_tracing();
|
|
}
|
|
}
|