Skip to content

Commit

Permalink
feat(tracing): adding apikey header and value configuration for loki …
Browse files Browse the repository at this point in the history
…logging

Signed-off-by: gabrik <[email protected]>
  • Loading branch information
gabrik committed Apr 8, 2024
1 parent 405c602 commit b21c054
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions zenohd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ use url::Url;
#[cfg(feature = "loki")]
const LOKI_ENDPOINT_VAR: &str = "LOKI_ENDPOINT";

#[cfg(feature = "loki")]
const LOKI_API_KEY_VAR: &str = "LOKI_API_KEY";

#[cfg(feature = "loki")]
const LOKI_API_KEY_HEADER_VAR: &str = "LOKI_API_KEY_HEADER";

const GIT_VERSION: &str = git_version!(prefix = "v", cargo_prefix = "v");

lazy_static::lazy_static!(
Expand Down Expand Up @@ -384,15 +390,26 @@ fn init_logging() -> Result<()> {
.with(fmt_layer);

#[cfg(feature = "loki")]
if let Some(loki_url) = get_loki_endpoint() {
let (loki_layer, task) = tracing_loki::builder()
.label("service", "zenoh")?
.build_url(Url::parse(&loki_url)?)?;
match (
get_loki_endpoint(),
get_loki_apikey(),
get_loki_apikey_header(),
) {
(Some(loki_url), Some(header), Some(apikey)) => {
let (loki_layer, task) = tracing_loki::builder()
.label("service", "zenoh")?
.http_header(header, apikey)?
.build_url(Url::parse(&loki_url)?)?;

tracing_sub.with(loki_layer).init();
tokio::spawn(task);
return Ok(());
}
_ => {
tracing::warn!("Missing one of the required header for Loki!")
}
};

tracing_sub.with(loki_layer).init();
tokio::spawn(task);
return Ok(());
}
tracing_sub.init();
Ok(())
}
Expand All @@ -402,6 +419,16 @@ pub fn get_loki_endpoint() -> Option<String> {
std::env::var(LOKI_ENDPOINT_VAR).ok()
}

#[cfg(feature = "loki")]
pub fn get_loki_apikey() -> Option<String> {
std::env::var(LOKI_API_KEY_VAR).ok()
}

#[cfg(feature = "loki")]
pub fn get_loki_apikey_header() -> Option<String> {
std::env::var(LOKI_API_KEY_HEADER_VAR).ok()
}

#[test]
#[cfg(feature = "default")]
fn test_default_features() {
Expand Down

0 comments on commit b21c054

Please sign in to comment.