-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to start tracing to `stdout` with the following options: - tracing target: provided by default with `RUST_LOG` directives. Falls back to `hera=info` if not provided. - whether to show the target in stdout: `RUST_LOG_TARGET` (don't show it only when set to `0`) - whether to use ANSI formatting and colors: `RUST_LOG_STYLE` (don't use it only if set to `never`) Also initialized a Prometheus subscriber on the provided port. Tested on the Hera binary. closes #33
- Loading branch information
1 parent
13278aa
commit b0e797d
Showing
6 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::{io::IsTerminal, net::SocketAddr}; | ||
|
||
use eyre::{bail, Result}; | ||
use metrics_exporter_prometheus::PrometheusBuilder; | ||
use tracing::{info, Level}; | ||
use tracing_subscriber::{ | ||
fmt::Layer as FmtLayer, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, | ||
}; | ||
|
||
/// Initialize the tracing stack and Prometheus metrics recorder. | ||
/// | ||
/// This function should be called at the beginning of the program. | ||
pub fn init_telemetry_stack(metrics_port: u16) -> Result<()> { | ||
let filter = EnvFilter::builder().with_default_directive("hera=info".parse()?).from_env_lossy(); | ||
|
||
// Whether to use ANSI formatting and colors in the console output. | ||
// If unset, always use colors if stdout is a tty. | ||
// If set to "never", just disable all colors. | ||
let should_use_colors = match std::env::var("RUST_LOG_STYLE") { | ||
Ok(val) => val != "never", | ||
Err(_) => std::io::stdout().is_terminal(), | ||
}; | ||
|
||
// Whether to show the tracing target in the console output. | ||
// If set, always show the target unless explicitly set to "0". | ||
// If unset, show target only if the filter is more verbose than INFO. | ||
let should_show_target = match std::env::var("RUST_LOG_TARGET") { | ||
Ok(val) => val != "0", | ||
Err(_) => filter.max_level_hint().map_or(true, |max_level| max_level > Level::INFO), | ||
}; | ||
|
||
let std_layer = FmtLayer::new() | ||
.with_ansi(should_use_colors) | ||
.with_target(should_show_target) | ||
.with_writer(std::io::stdout) | ||
.with_filter(filter); | ||
|
||
tracing_subscriber::registry().with(std_layer).try_init()?; | ||
|
||
let prometheus_addr = SocketAddr::from(([0, 0, 0, 0], metrics_port)); | ||
let builder = PrometheusBuilder::new().with_http_listener(prometheus_addr); | ||
|
||
if let Err(e) = builder.install() { | ||
bail!("failed to install Prometheus recorder: {:?}", e); | ||
} else { | ||
info!("Telemetry initialized. Serving Prometheus metrics at: http://{}", prometheus_addr); | ||
} | ||
|
||
Ok(()) | ||
} |