-
Notifications
You must be signed in to change notification settings - Fork 37
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
telemetry: introduce orb-telemetry crate and use it in orb-ui #258
Merged
TheButlah
merged 1 commit into
main
from
ryanbutler-orbp-289-orb-ui-should-use-journald
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we prevent the panic? return an error instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the panic is preferred. This type of code is once-only initialization code. If we called it more than once we should abort.
Generally errors are more for things that actually are expected to happen and need to be robustly handled. But someone calling this twice is better immediately slapped on the wrist and told to change their code.