1
1
use std:: collections:: HashMap ;
2
2
#[ cfg( feature = "push-gateway" ) ]
3
3
use std:: convert:: TryFrom ;
4
- #[ cfg( feature = "http-listener" ) ]
4
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
5
5
use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
6
6
use std:: num:: NonZeroU32 ;
7
7
use std:: sync:: RwLock ;
8
- #[ cfg( any( feature = "http-listener" , feature = "push-gateway" ) ) ]
8
+ #[ cfg( all(
9
+ any( feature = "http-listener" , feature = "push-gateway" ) ,
10
+ not( target_arch = "wasm32" )
11
+ ) ) ]
9
12
use std:: thread;
10
13
use std:: time:: Duration ;
11
14
12
15
#[ cfg( feature = "push-gateway" ) ]
13
16
use hyper:: Uri ;
14
17
use indexmap:: IndexMap ;
15
- #[ cfg( feature = "http-listener" ) ]
18
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
16
19
use ipnet:: IpNet ;
17
20
use quanta:: Clock ;
18
21
@@ -37,7 +40,7 @@ use super::ExporterFuture;
37
40
pub struct PrometheusBuilder {
38
41
#[ cfg_attr( not( any( feature = "http-listener" , feature = "push-gateway" ) ) , allow( dead_code) ) ]
39
42
exporter_config : ExporterConfig ,
40
- #[ cfg( feature = "http-listener" ) ]
43
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
41
44
allowed_addresses : Option < Vec < IpNet > > ,
42
45
quantiles : Vec < Quantile > ,
43
46
bucket_duration : Option < Duration > ,
@@ -56,7 +59,7 @@ impl PrometheusBuilder {
56
59
pub fn new ( ) -> Self {
57
60
let quantiles = parse_quantiles ( & [ 0.0 , 0.5 , 0.9 , 0.95 , 0.99 , 0.999 , 1.0 ] ) ;
58
61
59
- #[ cfg( feature = "http-listener" ) ]
62
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
60
63
let exporter_config = ExporterConfig :: HttpListener {
61
64
destination : super :: ListenDestination :: Tcp ( SocketAddr :: new (
62
65
IpAddr :: V4 ( Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) ) ,
@@ -70,7 +73,7 @@ impl PrometheusBuilder {
70
73
71
74
Self {
72
75
exporter_config,
73
- #[ cfg( feature = "http-listener" ) ]
76
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
74
77
allowed_addresses : None ,
75
78
quantiles,
76
79
bucket_duration : None ,
@@ -95,7 +98,7 @@ impl PrometheusBuilder {
95
98
/// Defaults to enabled, listening at `0.0.0.0:9000`.
96
99
///
97
100
/// [scrape endpoint]: https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format
98
- #[ cfg( feature = "http-listener" ) ]
101
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
99
102
#[ cfg_attr( docsrs, doc( cfg( feature = "http-listener" ) ) ) ]
100
103
#[ must_use]
101
104
pub fn with_http_listener ( mut self , addr : impl Into < SocketAddr > ) -> Self {
@@ -180,7 +183,7 @@ impl PrometheusBuilder {
180
183
///
181
184
/// If the given address cannot be parsed into an IP address or subnet, an error variant will be returned describing
182
185
/// the error.
183
- #[ cfg( feature = "http-listener" ) ]
186
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
184
187
#[ cfg_attr( docsrs, doc( cfg( feature = "http-listener" ) ) ) ]
185
188
pub fn add_allowed_address < A > ( mut self , address : A ) -> Result < Self , BuildError >
186
189
where
@@ -383,42 +386,56 @@ impl PrometheusBuilder {
383
386
#[ cfg( any( feature = "http-listener" , feature = "push-gateway" ) ) ]
384
387
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http-listener" , feature = "push-gateway" ) ) ) ) ]
385
388
pub fn install ( self ) -> Result < ( ) , BuildError > {
386
- use tokio:: runtime;
387
-
388
- let recorder = if let Ok ( handle) = runtime:: Handle :: try_current ( ) {
389
- let ( recorder, exporter) = {
390
- let _g = handle. enter ( ) ;
391
- self . build ( ) ?
392
- } ;
393
-
394
- handle. spawn ( exporter) ;
395
-
396
- recorder
397
- } else {
398
- let thread_name =
399
- format ! ( "metrics-exporter-prometheus-{}" , self . exporter_config. as_type_str( ) ) ;
400
-
401
- let runtime = runtime:: Builder :: new_current_thread ( )
402
- . enable_all ( )
403
- . build ( )
404
- . map_err ( |e| BuildError :: FailedToCreateRuntime ( e. to_string ( ) ) ) ?;
405
-
406
- let ( recorder, exporter) = {
407
- let _g = runtime. enter ( ) ;
408
- self . build ( ) ?
389
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
390
+ {
391
+ use tokio:: runtime;
392
+
393
+ let recorder = if let Ok ( handle) = runtime:: Handle :: try_current ( ) {
394
+ let ( recorder, exporter) = {
395
+ let _g = handle. enter ( ) ;
396
+ self . build ( ) ?
397
+ } ;
398
+
399
+ handle. spawn ( exporter) ;
400
+
401
+ recorder
402
+ } else {
403
+ let thread_name =
404
+ format ! ( "metrics-exporter-prometheus-{}" , self . exporter_config. as_type_str( ) ) ;
405
+
406
+ let runtime = runtime:: Builder :: new_current_thread ( )
407
+ . enable_all ( )
408
+ . build ( )
409
+ . map_err ( |e| BuildError :: FailedToCreateRuntime ( e. to_string ( ) ) ) ?;
410
+
411
+ let ( recorder, exporter) = {
412
+ let _g = runtime. enter ( ) ;
413
+ self . build ( ) ?
414
+ } ;
415
+
416
+ thread:: Builder :: new ( )
417
+ . name ( thread_name)
418
+ . spawn ( move || runtime. block_on ( exporter) )
419
+ . map_err ( |e| BuildError :: FailedToCreateRuntime ( e. to_string ( ) ) ) ?;
420
+
421
+ recorder
409
422
} ;
410
423
411
- thread:: Builder :: new ( )
412
- . name ( thread_name)
413
- . spawn ( move || runtime. block_on ( exporter) )
414
- . map_err ( |e| BuildError :: FailedToCreateRuntime ( e. to_string ( ) ) ) ?;
424
+ metrics:: set_global_recorder ( recorder) ?;
415
425
416
- recorder
417
- } ;
426
+ Ok ( ( ) )
427
+ }
428
+ #[ cfg( target_arch = "wasm32" ) ]
429
+ {
430
+ let ( recorder, exporter) = self . build ( ) ?;
431
+ wasm_bindgen_futures:: spawn_local ( async move {
432
+ let _ = exporter. await ;
433
+ } ) ;
418
434
419
- metrics:: set_global_recorder ( recorder) ?;
435
+ metrics:: set_global_recorder ( recorder) ?;
420
436
421
- Ok ( ( ) )
437
+ Ok ( ( ) )
438
+ }
422
439
}
423
440
424
441
/// Builds the recorder and installs it globally, returning a handle to it.
@@ -461,7 +478,7 @@ impl PrometheusBuilder {
461
478
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http-listener" , feature = "push-gateway" ) ) ) ) ]
462
479
#[ cfg_attr( not( feature = "http-listener" ) , allow( unused_mut) ) ]
463
480
pub fn build ( mut self ) -> Result < ( PrometheusRecorder , ExporterFuture ) , BuildError > {
464
- #[ cfg( feature = "http-listener" ) ]
481
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
465
482
let allowed_addresses = self . allowed_addresses . take ( ) ;
466
483
let exporter_config = self . exporter_config . clone ( ) ;
467
484
let upkeep_timeout = self . upkeep_timeout ;
@@ -470,19 +487,29 @@ impl PrometheusBuilder {
470
487
let handle = recorder. handle ( ) ;
471
488
472
489
let recorder_handle = handle. clone ( ) ;
490
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
473
491
tokio:: spawn ( async move {
474
492
loop {
475
493
tokio:: time:: sleep ( upkeep_timeout) . await ;
476
494
recorder_handle. run_upkeep ( ) ;
477
495
}
478
496
} ) ;
497
+ #[ cfg( target_arch = "wasm32" ) ]
498
+ {
499
+ wasm_bindgen_futures:: spawn_local ( async move {
500
+ loop {
501
+ gloo_timers:: future:: sleep ( upkeep_timeout) . await ;
502
+ recorder_handle. run_upkeep ( ) ;
503
+ }
504
+ } ) ;
505
+ }
479
506
480
507
Ok ( (
481
508
recorder,
482
509
match exporter_config {
483
510
ExporterConfig :: Unconfigured => Err ( BuildError :: MissingExporterConfiguration ) ?,
484
511
485
- #[ cfg( feature = "http-listener" ) ]
512
+ #[ cfg( all ( feature = "http-listener" , not ( target_arch = "wasm32" ) ) ) ]
486
513
ExporterConfig :: HttpListener { destination } => match destination {
487
514
super :: ListenDestination :: Tcp ( listen_address) => {
488
515
super :: http_listener:: new_http_listener (
0 commit comments