generated from famedly/rust-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add OpenTelemetry setup function
- Loading branch information
Showing
6 changed files
with
580 additions
and
0 deletions.
There are no files selected for viewing
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,103 @@ | ||
//! OpenTelemetry configuration | ||
//! | ||
//! Module containing the configuration struct for the OpenTelemetry | ||
use std::str::FromStr as _; | ||
|
||
use serde::Deserialize; | ||
use url::Url; | ||
|
||
use crate::LevelFilter; | ||
|
||
const DEFAULT_FILTER: &str = "opentelemetry=warn,tonic=warn,h2=warn,reqwest=info,axum=info,hyper=info,hyper-tls=info,tokio=info,tower=info,josekit=info,openssl=info"; | ||
const DEFAULT_LEVEL: &str = "info"; | ||
const DEFAULT_ENDPOINT: &str = "http://localhost:4317"; | ||
|
||
/// OpenTelemetry configuration | ||
#[derive(Debug, Deserialize, Clone)] | ||
pub struct OtelConfig { | ||
/// Enables logs on stdout | ||
pub stdout: Option<StdoutLogsConfig>, | ||
/// Configurations for exporting traces, metrics and logs | ||
pub exporter: Option<ExporterConfig>, | ||
} | ||
|
||
/// Configuration for exporting OpenTelemetry data | ||
#[derive(Debug, Deserialize, Clone, Default)] | ||
pub struct ExporterConfig { | ||
/// gRPC endpoint for exporting using OTELP | ||
pub endpoint: Option<Url>, | ||
/// Application service name | ||
pub service_name: String, | ||
/// Application version | ||
pub version: String, | ||
|
||
/// Logs exporting config | ||
pub logger: Option<ProviderConfig>, | ||
/// Traces exporting config | ||
pub tracer: Option<ProviderConfig>, | ||
/// Metrics exporting config | ||
pub meter: Option<ProviderConfig>, | ||
} | ||
|
||
/// Stdout logs configuration | ||
#[derive(Debug, Deserialize, Clone)] | ||
pub struct StdoutLogsConfig { | ||
/// Enables the stdout logs | ||
pub enabled: bool, | ||
/// Level for the filter | ||
pub level: Option<LevelFilter>, | ||
/// Filter directives to change the level on other crates | ||
pub filter_directives: Option<String>, | ||
} | ||
|
||
/// Provider configuration for OpenTelemetry export | ||
#[derive(Debug, Deserialize, Clone, Default)] | ||
pub struct ProviderConfig { | ||
/// Enables provider | ||
pub enabled: bool, | ||
/// Level for the filter | ||
pub level: Option<LevelFilter>, | ||
/// Filter directives to change the level on other crates | ||
pub filter_directives: Option<String>, | ||
} | ||
|
||
impl ProviderConfig { | ||
#[allow(clippy::expect_used)] | ||
pub(crate) fn get_filter(&self) -> String { | ||
format!( | ||
"{},{}", | ||
self.level.unwrap_or( | ||
LevelFilter::from_str(DEFAULT_LEVEL).expect("Error parsing default level") | ||
), | ||
self.filter_directives.as_ref().unwrap_or(&DEFAULT_FILTER.to_owned()) | ||
) | ||
} | ||
} | ||
|
||
impl StdoutLogsConfig { | ||
#[allow(clippy::expect_used)] | ||
pub(crate) fn get_filter(&self) -> String { | ||
format!( | ||
"{},{}", | ||
self.level.unwrap_or( | ||
LevelFilter::from_str(DEFAULT_LEVEL).expect("Error parsing default level") | ||
), | ||
self.filter_directives.as_ref().unwrap_or(&DEFAULT_FILTER.to_owned()) | ||
) | ||
} | ||
} | ||
|
||
impl Default for StdoutLogsConfig { | ||
fn default() -> Self { | ||
Self { enabled: true, level: None, filter_directives: None } | ||
} | ||
} | ||
|
||
impl ExporterConfig { | ||
#[allow(clippy::expect_used)] | ||
pub(crate) fn get_endpoint(&self) -> Url { | ||
self.endpoint | ||
.clone() | ||
.unwrap_or(Url::from_str(DEFAULT_ENDPOINT).expect("Error parsing default endpoint")) | ||
} | ||
} |
Oops, something went wrong.