-
-
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.
Added func to add trace parent to span from http headers, and reverse…
… to add current span/context to http response
- Loading branch information
Showing
10 changed files
with
201 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[toolchain] | ||
channel = "nightly" | ||
channel = "stable" |
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,54 @@ | ||
use std::borrow::Cow; | ||
|
||
use parking_lot::{MappedMutexGuard, MutexGuard}; | ||
|
||
use super::{out::GLOBAL_LOG, GlobalLog}; | ||
use crate::prelude::*; | ||
|
||
#[cfg(feature = "opentelemetry")] | ||
/// Returns a new [Meter] with the provided name and default configuration. | ||
/// | ||
/// A [Meter] should be scoped at most to a single application or crate. The | ||
/// name needs to be unique so it does not collide with other names used by | ||
/// an application, nor other applications. | ||
/// | ||
/// If the name is empty, then an implementation defined default name will | ||
/// be used instead. | ||
pub fn meter(name: impl Into<Cow<'static, str>>) -> Result<opentelemetry::metrics::Meter, AnyErr> { | ||
get_global()?.meter(name) | ||
} | ||
|
||
#[cfg(feature = "opentelemetry")] | ||
/// Connect this program's span to the trace that is represented by the provided HTTP headers. | ||
/// E.g. connect an axum handler's trace/span to the nginx trace/span. | ||
pub fn set_span_parent_from_http_headers( | ||
span: &tracing::Span, | ||
headers: &http::HeaderMap, | ||
) -> Result<(), AnyErr> { | ||
get_global()?.set_span_parent_from_http_headers(span, headers) | ||
} | ||
|
||
#[cfg(feature = "opentelemetry")] | ||
/// Set the response headers from the current span context. So downstream services can continue the current trace. | ||
pub fn set_response_headers_from_ctx<B>(response: &mut http::Response<B>) -> Result<(), AnyErr> { | ||
get_global()?.set_response_headers_from_ctx(response) | ||
} | ||
|
||
/// Force through logs, traces and metrics, useful in e.g. testing. | ||
/// | ||
/// Note there doesn't seem to be an underlying interface to force through metrics. | ||
pub fn flush() -> Result<(), AnyErr> { | ||
get_global()?.flush() | ||
} | ||
|
||
/// Shutdown the logger, traces and metrics, should be called when the program is about to exit. | ||
pub fn shutdown() -> Result<(), AnyErr> { | ||
get_global()?.shutdown() | ||
} | ||
|
||
fn get_global<'a>() -> Result<MappedMutexGuard<'a, GlobalLog>, AnyErr> { | ||
if GLOBAL_LOG.lock().is_none() { | ||
return Err(anyerr!("GlobalLog not registered!")); | ||
} | ||
Ok(MutexGuard::map(GLOBAL_LOG.lock(), |x| x.as_mut().unwrap())) | ||
} |
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,32 @@ | ||
use opentelemetry::propagation::{Extractor, Injector}; | ||
|
||
// copy from crate opentelemetry-http (to not be dependants of on 3rd: http, ...) | ||
pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap); | ||
|
||
impl<'a> Injector for HeaderInjector<'a> { | ||
/// Set a key and value in the `HeaderMap`. Does nothing if the key or value are not valid inputs. | ||
fn set(&mut self, key: &str, value: String) { | ||
if let Ok(name) = http::header::HeaderName::from_bytes(key.as_bytes()) { | ||
if let Ok(val) = http::header::HeaderValue::from_str(&value) { | ||
self.0.insert(name, val); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct HeaderExtractor<'a>(pub &'a http::HeaderMap); | ||
|
||
impl<'a> Extractor for HeaderExtractor<'a> { | ||
/// Get a value for a key from the `HeaderMap`. If the value is not valid ASCII, returns None. | ||
fn get(&self, key: &str) -> Option<&str> { | ||
self.0.get(key).and_then(|value| value.to_str().ok()) | ||
} | ||
|
||
/// Collect all the keys from the `HeaderMap`. | ||
fn keys(&self) -> Vec<&str> { | ||
self.0 | ||
.keys() | ||
.map(http::HeaderName::as_str) | ||
.collect::<Vec<_>>() | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
mod builder; | ||
pub mod global_fns; | ||
#[cfg(feature = "opentelemetry")] | ||
mod http_headers; | ||
mod out; | ||
mod setup; | ||
|
||
|
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.