-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
137 additions
and
38 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "orb-telemetry" | ||
version = "0.0.0" | ||
description = "Standardized telemetry setup for the orb" | ||
authors = ["Ryan Butler <[email protected]>"] | ||
publish = false | ||
|
||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
rust-version.workspace = true | ||
|
||
[dependencies] | ||
tracing-journald.workspace = true | ||
tracing-subscriber.workspace = true | ||
tracing.workspace = true | ||
|
||
[target.'cfg(tokio_unstable)'.dependencies] | ||
console-subscriber.workspace = true | ||
|
||
[lints.rust.unexpected_cfgs] | ||
level = "warn" | ||
check-cfg = ['cfg(tokio_unstable)'] |
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,84 @@ | ||
use std::io::IsTerminal as _; | ||
|
||
use tracing::level_filters::LevelFilter; | ||
use tracing_subscriber::{ | ||
layer::SubscriberExt as _, util::SubscriberInitExt as _, EnvFilter, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct TelemetryConfig { | ||
syslog_identifier: Option<String>, | ||
global_filter: EnvFilter, | ||
} | ||
|
||
impl TelemetryConfig { | ||
/// Provides all required arguments for telemetry configuration. | ||
/// - `log_identifier` will be used for journald, if appropriate. | ||
#[expect(clippy::new_without_default, reason = "may add required args later")] | ||
pub fn new() -> Self { | ||
Self { | ||
syslog_identifier: None, | ||
global_filter: EnvFilter::builder() | ||
.with_default_directive(LevelFilter::INFO.into()) | ||
.from_env_lossy(), | ||
} | ||
} | ||
|
||
/// Enables journald, and uses the provided syslog identifier. | ||
/// | ||
/// If you run the application in a tty, stderr will be used instead. | ||
pub fn with_journald(self, syslog_identifier: &str) -> Self { | ||
Self { | ||
syslog_identifier: Some(syslog_identifier.to_owned()), | ||
..self | ||
} | ||
} | ||
|
||
/// Override the global filter to a custom filter. | ||
/// Only do this if actually necessary to deviate from the orb's defaults. | ||
pub fn with_global_filter(self, filter: EnvFilter) -> Self { | ||
Self { | ||
global_filter: filter, | ||
..self | ||
} | ||
} | ||
|
||
/// Initializes the telemetry config. Call this only once, at the beginning of the | ||
/// program. | ||
/// | ||
/// Calling this more than once or when another tracing subscriber is registered | ||
/// will cause a panic. | ||
pub fn init(self) { | ||
let registry = tracing_subscriber::registry(); | ||
// The type is only there to get it to compile. | ||
let tokio_console_layer: Option<tracing_subscriber::layer::Identity> = None; | ||
#[cfg(tokio_unstable)] | ||
let tokio_console_layer = console_subscriber::spawn(); | ||
// Checking for a terminal helps detect if we are running under systemd. | ||
let journald_layer = if !std::io::stderr().is_terminal() { | ||
self.syslog_identifier.and_then(|syslog_identifier| { | ||
tracing_journald::layer() | ||
.inspect_err(|err| { | ||
eprintln!( | ||
"failed connecting to journald socket. \ | ||
will write to stderr: {err}" | ||
); | ||
}) | ||
.map(|layer| layer.with_syslog_identifier(syslog_identifier)) | ||
.ok() | ||
}) | ||
} else { | ||
None | ||
}; | ||
let stderr_layer = journald_layer | ||
.is_none() | ||
.then(|| tracing_subscriber::fmt::layer().with_writer(std::io::stderr)); | ||
assert!(stderr_layer.is_some() || journald_layer.is_some()); | ||
registry | ||
.with(tokio_console_layer) | ||
.with(stderr_layer) | ||
.with(journald_layer) | ||
.with(self.global_filter) | ||
.init(); | ||
} | ||
} |
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