@@ -11,6 +11,16 @@ use tracing::info;
1111use crate :: exporters:: Exporter ;
1212use crate :: { Error , Report } ;
1313
14+ /// A helper trait for defining the type for hooks that are called when the metrics are being
15+ /// collected by the server.
16+ trait Hook : Fn ( ) + Send + Sync { }
17+ impl < T : Fn ( ) + Send + Sync > Hook for T { }
18+
19+ /// A shared hook that can be cloned.
20+ type SharedHook = Arc < dyn Hook < Output = ( ) > > ;
21+ /// A list of shared hooks.
22+ type Hooks = Vec < SharedHook > ;
23+
1424/// A handle to the metrics server.
1525#[ derive( Debug ) ]
1626pub struct MetricsServerHandle {
@@ -45,40 +55,27 @@ impl MetricsServerHandle {
4555 }
4656}
4757
48- /// A helper trait for defining the type for hooks that are called when the metrics are being
49- /// collected by the server.
50- trait Hook : Fn ( ) + Send + Sync { }
51- impl < T : Fn ( ) + Send + Sync > Hook for T { }
52-
53- /// A shared hook that can be cloned.
54- type SharedHook = Arc < dyn Hook < Output = ( ) > > ;
55- /// A list of shared hooks.
56- type Hooks = Vec < SharedHook > ;
57-
5858/// Server for serving metrics.
5959// TODO: allow configuring the server executor to allow cancelling on invidiual connection tasks.
6060// See, [hyper::server::server::Builder::executor]
61- pub struct Server < MetricsExporter > {
61+ pub struct MetricsServer < E > {
6262 /// Hooks or callable functions for collecting metrics in the cases where
6363 /// the metrics are not being collected in the main program flow.
6464 ///
6565 /// These are called when metrics are being served through the server.
6666 hooks : Hooks ,
6767 /// The exporter that is used to export the collected metrics.
68- exporter : MetricsExporter ,
68+ exporter : E ,
6969}
7070
71- impl < MetricsExporter > Server < MetricsExporter >
72- where
73- MetricsExporter : Exporter + ' static ,
74- {
71+ impl < E : Exporter + ' static > MetricsServer < E > {
7572 /// Creates a new metrics server using the given exporter.
76- pub fn new ( exporter : MetricsExporter ) -> Self {
73+ pub fn new ( exporter : E ) -> Self {
7774 Self { exporter, hooks : Vec :: new ( ) }
7875 }
7976
8077 /// Add new metrics reporter to the server.
81- pub fn with_reports < I > ( mut self , reports : I ) -> Self
78+ pub fn reports < I > ( mut self , reports : I ) -> Self
8279 where
8380 I : IntoIterator < Item = Box < dyn Report > > ,
8481 {
@@ -105,7 +102,7 @@ where
105102 /// Starts an endpoint at the given address to serve Prometheus metrics.
106103 ///
107104 /// Returns a handle that can be used to stop the server and wait for it to finish.
108- pub async fn start ( & self , addr : SocketAddr ) -> Result < MetricsServerHandle , Error > {
105+ pub fn start ( & self , addr : SocketAddr ) -> Result < MetricsServerHandle , Error > {
109106 // Clone the hooks (clones the Arc references, not the closures themselves)
110107 let hooks = self . hooks . clone ( ) ;
111108 let exporter = self . exporter . clone ( ) ;
@@ -137,6 +134,7 @@ where
137134 let actual_addr = server. local_addr ( ) ;
138135
139136 // Spawn the server with graceful shutdown
137+ // TODO: spawn on a task manager
140138 let task_handle = tokio:: spawn ( async move {
141139 server
142140 . with_graceful_shutdown ( async {
@@ -152,7 +150,7 @@ where
152150 }
153151}
154152
155- impl < MetricsExporter > fmt:: Debug for Server < MetricsExporter > {
153+ impl < E > fmt:: Debug for MetricsServer < E > {
156154 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
157155 f. debug_struct ( "Server" )
158156 . field ( "hooks" , & format_args ! ( "{} hook(s)" , self . hooks. len( ) ) )
0 commit comments