-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: produce some telemetry spans and events
Install an OpenTelemetryLayer configured with the OTLP exporter. The tracing spans and events to export are selected by target "movement_telemetry".
- Loading branch information
Showing
12 changed files
with
132 additions
and
73 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
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
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,29 @@ | ||
use anyhow::anyhow; | ||
use std::env; | ||
|
||
const OTLP_TRACING_ENV: &str = "MOVEMENT_OTLP"; | ||
|
||
/// Options for tracing configuration. | ||
#[derive(Debug, Default)] | ||
pub struct Config { | ||
/// URL of the OpenTelemetry collector endpoint using the OTLP gRPC protocol. | ||
/// If the value is `None`, telemetry is not exported. | ||
pub otlp_grpc_url: Option<String>, | ||
} | ||
|
||
impl Config { | ||
/// Get the tracing configuration from well-known environment variables. | ||
pub fn from_env() -> Result<Self, anyhow::Error> { | ||
let otlp_grpc_url = match env::var(OTLP_TRACING_ENV) { | ||
Ok(url) => Some(url), | ||
Err(env::VarError::NotPresent) => None, | ||
Err(env::VarError::NotUnicode(s)) => { | ||
return Err(anyhow!( | ||
"value of environment variable {OTLP_TRACING_ENV} is not valid UTF-8: {}", | ||
s.to_string_lossy() | ||
)); | ||
} | ||
}; | ||
Ok(Self { otlp_grpc_url }) | ||
} | ||
} |
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,14 @@ | ||
pub mod telemetry; | ||
//! Tracing setup for Movement services. | ||
//! | ||
//! Exporting of tracing data via [OpenTelemetry] is optionally supported | ||
//! by setting "movement_telemetry" as the target in tracing spans and events. | ||
//! | ||
//! [OpenTelemetry]: https://opentelemetry.io/ | ||
|
||
mod config; | ||
mod telemetry; | ||
mod tracing; | ||
|
||
pub use config::Config; | ||
pub use telemetry::ScopeGuard; | ||
pub use tracing::init_tracing_subscriber; |
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,11 +1,27 @@ | ||
use crate::telemetry::{self, ScopeGuard}; | ||
use crate::Config; | ||
use tracing_subscriber::filter::{EnvFilter, LevelFilter}; | ||
use tracing_subscriber::prelude::*; | ||
use tracing_subscriber::{fmt, Registry}; | ||
|
||
/// Sets up the tracing subscribers for a Movement process. This should be | ||
/// called at the beginning of a process' `main` function. | ||
pub fn init_tracing_subscriber() { | ||
/// | ||
/// If successful, returns a guard object that should be dropped at the end | ||
/// of the process' `main` function scope. | ||
pub fn init_tracing_subscriber( | ||
service_name: &'static str, | ||
service_version: &'static str, | ||
config: &Config, | ||
) -> Result<ScopeGuard, anyhow::Error> { | ||
// TODO: compose console_subscriber as a layer | ||
let env_filter = EnvFilter::builder() | ||
.with_default_directive(LevelFilter::INFO.into()) | ||
.from_env_lossy(); | ||
tracing_subscriber::fmt().with_env_filter(env_filter).init(); | ||
let fmt_layer = fmt::layer().with_filter(env_filter); | ||
let subscriber = Registry::default().with(fmt_layer); | ||
let (scope_guard, subscriber) = | ||
telemetry::init_tracing_layer(subscriber, service_name, service_version, config)?; | ||
subscriber.init(); | ||
Ok(scope_guard) | ||
} |