11//! Prometheus counters for request and response bodies.
22
3- use linkerd_metrics:: prom:: { self , Counter , Family , Registry } ;
3+ use linkerd_metrics:: prom:: {
4+ self , metrics:: family:: MetricConstructor , Family , Histogram , Registry , Unit ,
5+ } ;
46
57/// Counters for response body frames.
68#[ derive( Clone , Debug ) ]
79pub struct ResponseBodyFamilies < L > {
8- /// Counts the number of response body frames.
9- resp_body_frames_total : Family < L , Counter > ,
10- /// Counts the total number of bytes in response body frames.
11- resp_body_frames_bytes : Family < L , Counter > ,
10+ /// Counts the number of response body frames by size.
11+ frame_sizes : Family < L , Histogram , NewHisto > ,
1212}
1313
1414/// Counters to instrument a request or response body.
15- #[ derive( Clone , Debug , Default ) ]
15+ #[ derive( Clone , Debug ) ]
1616pub struct BodyDataMetrics {
1717 /// Counts the number of request body frames.
18- pub frames_total : Counter ,
19- /// Counts the total number of bytes in request body frames.
20- pub frames_bytes : Counter ,
18+ pub frame_size : Histogram ,
19+ }
20+
21+ #[ derive( Clone , Copy ) ]
22+ struct NewHisto ;
23+
24+ impl MetricConstructor < Histogram > for NewHisto {
25+ fn new_metric ( & self ) -> Histogram {
26+ Histogram :: new ( [ 128.0 , 1024.0 , 10240.0 ] . into_iter ( ) )
27+ }
2128}
2229
2330// === impl ResponseBodyFamilies ===
2835{
2936 fn default ( ) -> Self {
3037 Self {
31- resp_body_frames_total : Default :: default ( ) ,
32- resp_body_frames_bytes : Default :: default ( ) ,
38+ frame_sizes : Family :: new_with_constructor ( NewHisto ) ,
3339 }
3440 }
3541}
@@ -45,50 +51,25 @@ where
4551 + Sync
4652 + ' static ,
4753{
48- const RESP_BODY_FRAMES_TOTAL_NAME : & ' static str = "resp_body_frames_total" ;
49- const RESP_BODY_FRAMES_TOTAL_HELP : & ' static str =
50- "Counts the number of frames in response bodies." ;
51-
52- const RESP_BODY_FRAMES_BYTES_NAME : & ' static str = "resp_body_frames_bytes" ;
53- const RESP_BODY_FRAMES_BYTES_HELP : & ' static str =
54- "Counts the total number of bytes in response bodies." ;
55-
5654 /// Registers and returns a new family of body data metrics.
5755 pub fn register ( registry : & mut Registry ) -> Self {
58- let resp_body_frames_total = Family :: default ( ) ;
59- registry. register (
60- Self :: RESP_BODY_FRAMES_TOTAL_NAME ,
61- Self :: RESP_BODY_FRAMES_TOTAL_HELP ,
62- resp_body_frames_total. clone ( ) ,
63- ) ;
64-
65- let resp_body_frames_bytes = Family :: default ( ) ;
56+ let frame_sizes = Family :: new_with_constructor ( NewHisto ) ;
6657 registry. register_with_unit (
67- Self :: RESP_BODY_FRAMES_BYTES_NAME ,
68- Self :: RESP_BODY_FRAMES_BYTES_HELP ,
69- prom :: Unit :: Bytes ,
70- resp_body_frames_bytes . clone ( ) ,
58+ "response_frame_size" ,
59+ "Response data frame sizes" ,
60+ Unit :: Bytes ,
61+ frame_sizes . clone ( ) ,
7162 ) ;
7263
73- Self {
74- resp_body_frames_total,
75- resp_body_frames_bytes,
76- }
64+ Self { frame_sizes }
7765 }
7866
7967 /// Returns the [`BodyDataMetrics`] for the given label set.
8068 pub fn metrics ( & self , labels : & L ) -> BodyDataMetrics {
81- let Self {
82- resp_body_frames_total,
83- resp_body_frames_bytes,
84- } = self ;
69+ let Self { frame_sizes } = self ;
8570
86- let frames_total = resp_body_frames_total. get_or_create ( labels) . clone ( ) ;
87- let frames_bytes = resp_body_frames_bytes. get_or_create ( labels) . clone ( ) ;
71+ let frame_size = frame_sizes. get_or_create ( labels) . clone ( ) ;
8872
89- BodyDataMetrics {
90- frames_total,
91- frames_bytes,
92- }
73+ BodyDataMetrics { frame_size }
9374 }
9475}
0 commit comments