1
- //! Module implementing an Open Metrics histogram .
1
+ //! Module implementing an Open Metrics summary .
2
2
//!
3
3
//! See [`Summary`] for details.
4
4
5
5
use crate :: encoding:: { EncodeMetric , MetricEncoder } ;
6
6
7
7
use super :: { MetricType , TypedMetric } ;
8
- //use owning_ref::OwningRef;
9
- //use std::iter::{self, once};
10
- use std:: sync:: { Arc , Mutex } ;
8
+ use parking_lot:: RwLock ;
9
+ use std:: sync:: Arc ;
11
10
use std:: time:: { Duration , Instant } ;
12
11
13
12
use quantiles:: ckms:: CKMS ;
@@ -20,7 +19,7 @@ pub struct Summary {
20
19
max_age_buckets : u64 ,
21
20
max_age_seconds : u64 ,
22
21
stream_duration : Duration ,
23
- inner : Arc < Mutex < InnerSummary > > ,
22
+ inner : Arc < RwLock < InnerSummary > > ,
24
23
}
25
24
26
25
impl Clone for Summary {
@@ -69,7 +68,7 @@ impl Summary {
69
68
stream_duration,
70
69
target_quantile,
71
70
target_error,
72
- inner : Arc :: new ( Mutex :: new ( InnerSummary {
71
+ inner : Arc :: new ( RwLock :: new ( InnerSummary {
73
72
sum : Default :: default ( ) ,
74
73
count : Default :: default ( ) ,
75
74
quantile_streams : streams,
@@ -83,7 +82,7 @@ impl Summary {
83
82
pub fn observe ( & self , v : f64 ) {
84
83
self . rotate_buckets ( ) ;
85
84
86
- let mut inner = self . inner . lock ( ) . unwrap ( ) ;
85
+ let mut inner = self . inner . write ( ) ;
87
86
inner. sum += v;
88
87
inner. count += 1 ;
89
88
@@ -97,7 +96,7 @@ impl Summary {
97
96
pub fn get ( & self ) -> ( f64 , u64 , Vec < ( f64 , f64 ) > ) {
98
97
self . rotate_buckets ( ) ;
99
98
100
- let inner = self . inner . lock ( ) . unwrap ( ) ;
99
+ let inner = self . inner . read ( ) ;
101
100
let sum = inner. sum ;
102
101
let count = inner. count ;
103
102
let mut quantile_values: Vec < ( f64 , f64 ) > = Vec :: new ( ) ;
@@ -112,7 +111,7 @@ impl Summary {
112
111
}
113
112
114
113
fn rotate_buckets ( & self ) {
115
- let mut inner = self . inner . lock ( ) . unwrap ( ) ;
114
+ let mut inner = self . inner . write ( ) ;
116
115
if inner. last_rotated_timestamp . elapsed ( ) >= self . stream_duration {
117
116
inner. last_rotated_timestamp = Instant :: now ( ) ;
118
117
if inner. head_stream_idx == self . max_age_buckets {
0 commit comments