|
12 | 12 | #![deny(unused_must_use, rust_2018_idioms)] |
13 | 13 | #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] |
14 | 14 |
|
15 | | -use crate::utils::{metrics::MetricsConfig, otlp::OtelGuard, tracing::TracingConfig}; |
| 15 | +use crate::utils::{ |
| 16 | + from_env::{FromEnv, FromEnvErr}, |
| 17 | + metrics::MetricsConfig, |
| 18 | + otlp::OtelGuard, |
| 19 | + tracing::TracingConfig, |
| 20 | +}; |
16 | 21 |
|
17 | 22 | #[cfg(feature = "perms")] |
18 | 23 | /// Permissioning and authorization utilities for Signet builders. |
@@ -100,34 +105,71 @@ pub fn init4() -> Option<OtelGuard> { |
100 | 105 | guard |
101 | 106 | } |
102 | 107 |
|
103 | | -/// Init metrics and tracing, including OTLP if enabled. |
| 108 | +/// Trait for config types that can be used with [`init`]. |
| 109 | +/// |
| 110 | +/// Implementors must provide access to [`TracingConfig`] and [`MetricsConfig`], |
| 111 | +/// and must be loadable from the environment via [`FromEnv`]. |
| 112 | +/// |
| 113 | +/// # Example |
| 114 | +/// |
| 115 | +/// ```ignore |
| 116 | +/// #[derive(Debug, FromEnv)] |
| 117 | +/// pub struct MyConfig { |
| 118 | +/// pub tracing: TracingConfig, |
| 119 | +/// pub metrics: MetricsConfig, |
| 120 | +/// #[from_env(var = "MY_THING", desc = "some app-specific value")] |
| 121 | +/// pub my_thing: String, |
| 122 | +/// } |
| 123 | +/// |
| 124 | +/// impl Init4Config for MyConfig { |
| 125 | +/// fn tracing(&self) -> &TracingConfig { &self.tracing } |
| 126 | +/// fn metrics(&self) -> &MetricsConfig { &self.metrics } |
| 127 | +/// } |
| 128 | +/// ``` |
| 129 | +pub trait Init4Config: FromEnv { |
| 130 | + /// Get the tracing configuration. |
| 131 | + fn tracing(&self) -> &TracingConfig; |
| 132 | + /// Get the metrics configuration. |
| 133 | + fn metrics(&self) -> &MetricsConfig; |
| 134 | +} |
| 135 | + |
| 136 | +/// The result of [`init`]: the loaded config and an optional OTLP guard. |
| 137 | +/// |
| 138 | +/// The [`OtelGuard`] (if present) must be kept alive for the lifetime of the |
| 139 | +/// program to ensure the OTLP exporter continues to send data. |
| 140 | +#[derive(Debug)] |
| 141 | +pub struct ConfigAndGuard<T> { |
| 142 | + /// The loaded configuration. |
| 143 | + pub config: T, |
| 144 | + /// The OTLP guard, if OTLP was enabled. |
| 145 | + pub guard: Option<OtelGuard>, |
| 146 | +} |
| 147 | + |
| 148 | +/// Load config from the environment and initialize metrics and tracing. |
104 | 149 | /// |
105 | 150 | /// This will perform the following: |
106 | | -/// - Read environment configuration for tracing |
| 151 | +/// - Load `T` from environment variables via [`FromEnv`] |
| 152 | +/// - Read tracing configuration from the loaded config |
107 | 153 | /// - Determine whether to enable OTLP |
108 | 154 | /// - Install a global tracing subscriber, using the OTLP provider if enabled |
109 | | -/// - Read environment configuration for metrics |
| 155 | +/// - Read metrics configuration from the loaded config |
110 | 156 | /// - Install a global metrics recorder and serve it over HTTP on 0.0.0.0 |
111 | 157 | /// |
112 | 158 | /// See [`init_tracing`] and [`init_metrics`] for more |
113 | 159 | /// details on specific actions taken and env vars read. |
114 | 160 | /// |
115 | | -/// # Returns |
116 | | -/// |
117 | | -/// The OpenTelemetry guard, if OTLP is enabled. This guard should be kept alive |
118 | | -/// for the lifetime of the program to ensure the exporter continues to send |
119 | | -/// data to the remote API. |
120 | | -/// |
121 | 161 | /// [`init_tracing`]: utils::tracing::init_tracing |
122 | 162 | /// [`init_metrics`]: utils::metrics::init_metrics |
123 | | -pub fn init(tracing_config: TracingConfig, metrics_config: MetricsConfig) -> Option<OtelGuard> { |
124 | | - let guard = utils::tracing::init_tracing_with_config(tracing_config); |
125 | | - utils::metrics::init_metrics_with_config(metrics_config); |
| 163 | +pub fn init<T: Init4Config>() -> Result<ConfigAndGuard<T>, FromEnvErr> { |
| 164 | + let config = T::from_env()?; |
| 165 | + |
| 166 | + let guard = utils::tracing::init_tracing_with_config(config.tracing().clone()); |
| 167 | + utils::metrics::init_metrics_with_config(*config.metrics()); |
126 | 168 |
|
127 | 169 | // This will install the AWS-LC-Rust TLS provider for rustls, if no other |
128 | 170 | // provider has been installed yet |
129 | 171 | #[cfg(feature = "rustls")] |
130 | 172 | let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); |
131 | 173 |
|
132 | | - guard |
| 174 | + Ok(ConfigAndGuard { config, guard }) |
133 | 175 | } |
0 commit comments