@@ -341,6 +341,56 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
341341 self . metrics . write ( ) . clear ( )
342342 }
343343
344+ /// Returns `true` if the given label set exists within the metric family.
345+ ///
346+ /// ```
347+ /// # use prometheus_client::metrics::counter::{Atomic, Counter};
348+ /// # use prometheus_client::metrics::family::Family;
349+ /// #
350+ /// let family = Family::<Vec<(String, String)>, Counter>::default();
351+ /// let get = vec![("method".to_owned(), "GET".to_owned())];
352+ /// let post = vec![("method".to_owned(), "POST".to_owned())];
353+ ///
354+ /// // Create the metric with labels `method="GET"`.
355+ /// family.get_or_create(&get).inc();
356+ ///
357+ /// assert!(family.contains(&get), "a `method=\"GET\"`-labeled metric exists");
358+ /// assert!(!family.contains(&post), "a `method=\"POST\"`-labeled metric does NOT exist");
359+ /// ```
360+ #[ cfg( any( test, feature = "test-util" ) ) ]
361+ pub fn contains ( & self , label_set : & S ) -> bool {
362+ self . metrics . read ( ) . get ( label_set) . is_some ( )
363+ }
364+
365+ /// Returns the number of metrics in this family.
366+ ///
367+ /// ```
368+ /// # use prometheus_client::metrics::counter::{Atomic, Counter};
369+ /// # use prometheus_client::metrics::family::Family;
370+ /// #
371+ /// let family = Family::<Vec<(String, String)>, Counter>::default();
372+ /// assert_eq!(family.len(), 0);
373+ ///
374+ /// // Will create the metric with label `method="GET"` on first call and
375+ /// // return a reference.
376+ /// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
377+ /// assert_eq!(family.len(), 1);
378+ ///
379+ /// // Clear the family of all label sets.
380+ /// family.clear();
381+ /// assert_eq!(family.len(), 0);
382+ /// ```
383+ #[ cfg( any( test, feature = "test-util" ) ) ]
384+ pub fn len ( & self ) -> usize {
385+ self . metrics . read ( ) . len ( )
386+ }
387+
388+ /// Returns `true` if the family contains no metrics.
389+ #[ cfg( any( test, feature = "test-util" ) ) ]
390+ pub fn is_empty ( & self ) -> bool {
391+ self . metrics . read ( ) . is_empty ( )
392+ }
393+
344394 pub ( crate ) fn read ( & self ) -> RwLockReadGuard < ' _ , HashMap < S , M > > {
345395 self . metrics . read ( )
346396 }
@@ -377,6 +427,10 @@ where
377427 fn metric_type ( & self ) -> MetricType {
378428 M :: TYPE
379429 }
430+
431+ fn is_empty ( & self ) -> bool {
432+ self . metrics . read ( ) . is_empty ( )
433+ }
380434}
381435
382436#[ cfg( test) ]
0 commit comments