1212#![ deny( unused_must_use, rust_2018_idioms) ]
1313#![ cfg_attr( docsrs, feature( doc_cfg, doc_auto_cfg) ) ]
1414
15+ use crate :: utils:: {
16+ from_env:: { FromEnv , FromEnvErr } ,
17+ metrics:: MetricsConfig ,
18+ otlp:: OtelGuard ,
19+ tracing:: TracingConfig ,
20+ } ;
21+
1522#[ cfg( feature = "perms" ) ]
1623/// Permissioning and authorization utilities for Signet builders.
1724pub mod perms;
@@ -85,7 +92,8 @@ pub mod deps {
8592///
8693/// [`init_tracing`]: utils::tracing::init_tracing
8794/// [`init_metrics`]: utils::metrics::init_metrics
88- pub fn init4 ( ) -> Option < utils:: otlp:: OtelGuard > {
95+ #[ deprecated( since = "0.18.0-rc.11" , note = "use `init` instead" ) ]
96+ pub fn init4 ( ) -> Option < OtelGuard > {
8997 let guard = utils:: tracing:: init_tracing ( ) ;
9098 utils:: metrics:: init_metrics ( ) ;
9199
@@ -96,3 +104,72 @@ pub fn init4() -> Option<utils::otlp::OtelGuard> {
96104
97105 guard
98106}
107+
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.
149+ ///
150+ /// This will perform the following:
151+ /// - Load `T` from environment variables via [`FromEnv`]
152+ /// - Read tracing configuration from the loaded config
153+ /// - Determine whether to enable OTLP
154+ /// - Install a global tracing subscriber, using the OTLP provider if enabled
155+ /// - Read metrics configuration from the loaded config
156+ /// - Install a global metrics recorder and serve it over HTTP on 0.0.0.0
157+ ///
158+ /// See [`init_tracing`] and [`init_metrics`] for more
159+ /// details on specific actions taken and env vars read.
160+ ///
161+ /// [`init_tracing`]: utils::tracing::init_tracing
162+ /// [`init_metrics`]: utils::metrics::init_metrics
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 ( ) ) ;
168+
169+ // This will install the AWS-LC-Rust TLS provider for rustls, if no other
170+ // provider has been installed yet
171+ #[ cfg( feature = "rustls" ) ]
172+ let _ = rustls:: crypto:: aws_lc_rs:: default_provider ( ) . install_default ( ) ;
173+
174+ Ok ( ConfigAndGuard { config, guard } )
175+ }
0 commit comments