11use super :: { ClassMetrics , Metrics , StatusMetrics } ;
22use crate :: { Prefixed , Registry , Report } ;
33use linkerd2_metrics:: { latency, Counter , FmtLabels , FmtMetric , FmtMetrics , Histogram , Metric } ;
4- use std:: fmt;
5- use std:: hash:: Hash ;
6- use std:: time:: Instant ;
4+ use std:: { fmt, hash:: Hash , time:: Instant } ;
75use tracing:: trace;
86
7+ #[ derive( Copy , Clone ) ]
98struct Status ( http:: StatusCode ) ;
109
1110impl < T , C > Report < T , Metrics < C > >
@@ -38,84 +37,35 @@ where
3837 }
3938}
4039
41- impl < T , C > FmtMetrics for Report < T , Metrics < C > >
42- where
43- T : FmtLabels + Hash + Eq ,
44- C : FmtLabels + Hash + Eq ,
45- {
46- fn fmt_metrics ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
47- let mut registry = match self . registry . lock ( ) {
48- Err ( _) => return Ok ( ( ) ) ,
49- Ok ( r) => r,
50- } ;
51- trace ! (
52- prefix = self . prefix,
53- targets = registry. by_target. len( ) ,
54- include_latencies = self . include_latencies,
55- "Formatting HTTP request metrics" ,
56- ) ;
57-
58- if registry. by_target . is_empty ( ) {
59- return Ok ( ( ) ) ;
60- }
61-
62- let metric = self . request_total ( ) ;
63- metric. fmt_help ( f) ?;
64- registry. fmt_by_target ( f, metric, |s| & s. total ) ?;
65-
66- if self . include_latencies {
67- let metric = self . response_latency_ms ( ) ;
68- metric. fmt_help ( f) ?;
69- registry. fmt_by_status ( f, metric, |s| & s. latency ) ?;
70- }
71-
72- let metric = self . response_total ( ) ;
73- metric. fmt_help ( f) ?;
74- registry. fmt_by_class ( f, metric, |s| & s. total ) ?;
75-
76- registry. retain_since ( Instant :: now ( ) - self . retain_idle ) ;
77-
78- Ok ( ( ) )
79- }
80- }
81-
82- impl < T , C > Registry < T , Metrics < C > >
40+ impl < T , C > Report < T , Metrics < C > >
8341where
8442 T : FmtLabels + Hash + Eq ,
8543 C : FmtLabels + Hash + Eq ,
8644{
87- fn fmt_by_target < N , V , F > (
88- & self ,
45+ fn fmt_by_target < N , M > (
46+ registry : & Registry < T , Metrics < C > > ,
8947 f : & mut fmt:: Formatter < ' _ > ,
90- metric : Metric < ' _ , N , V > ,
91- get_metric : F ,
48+ metric : Metric < ' _ , N , M > ,
49+ get_metric : impl Fn ( & Metrics < C > ) -> & M ,
9250 ) -> fmt:: Result
9351 where
9452 N : fmt:: Display ,
95- V : FmtMetric ,
96- F : Fn ( & Metrics < C > ) -> & V ,
53+ M : FmtMetric ,
9754 {
98- for ( tgt, tm) in & self . by_target {
99- if let Ok ( m) = tm. lock ( ) {
100- get_metric ( & * m) . fmt_metric_labeled ( f, & metric. name , tgt) ?;
101- }
102- }
103-
104- Ok ( ( ) )
55+ registry. fmt_by_locked ( f, metric, get_metric)
10556 }
10657
107- fn fmt_by_status < N , M , F > (
108- & self ,
58+ fn fmt_by_status < N , M > (
59+ registry : & Registry < T , Metrics < C > > ,
10960 f : & mut fmt:: Formatter < ' _ > ,
11061 metric : Metric < ' _ , N , M > ,
111- get_metric : F ,
62+ get_metric : impl Fn ( & StatusMetrics < C > ) -> & M ,
11263 ) -> fmt:: Result
11364 where
11465 N : fmt:: Display ,
11566 M : FmtMetric ,
116- F : Fn ( & StatusMetrics < C > ) -> & M ,
11767 {
118- for ( tgt, tm) in & self . by_target {
68+ for ( tgt, tm) in registry . iter ( ) {
11969 if let Ok ( tm) = tm. lock ( ) {
12070 for ( status, m) in & tm. by_status {
12171 let status = status. as_ref ( ) . map ( |s| Status ( * s) ) ;
@@ -128,18 +78,17 @@ where
12878 Ok ( ( ) )
12979 }
13080
131- fn fmt_by_class < N , M , F > (
132- & self ,
81+ fn fmt_by_class < N , M > (
82+ registry : & Registry < T , Metrics < C > > ,
13383 f : & mut fmt:: Formatter < ' _ > ,
13484 metric : Metric < ' _ , N , M > ,
135- get_metric : F ,
85+ get_metric : impl Fn ( & ClassMetrics ) -> & M ,
13686 ) -> fmt:: Result
13787 where
13888 N : fmt:: Display ,
13989 M : FmtMetric ,
140- F : Fn ( & ClassMetrics ) -> & M ,
14190 {
142- for ( tgt, tm) in & self . by_target {
91+ for ( tgt, tm) in registry . iter ( ) {
14392 if let Ok ( tm) = tm. lock ( ) {
14493 for ( status, sm) in & tm. by_status {
14594 for ( cls, m) in & sm. by_class {
@@ -155,6 +104,47 @@ where
155104 }
156105}
157106
107+ impl < T , C > FmtMetrics for Report < T , Metrics < C > >
108+ where
109+ T : FmtLabels + Hash + Eq ,
110+ C : FmtLabels + Hash + Eq ,
111+ {
112+ fn fmt_metrics ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
113+ let mut registry = match self . registry . lock ( ) {
114+ Err ( _) => return Ok ( ( ) ) ,
115+ Ok ( r) => r,
116+ } ;
117+ trace ! (
118+ prefix = self . prefix,
119+ targets = registry. len( ) ,
120+ include_latencies = self . include_latencies,
121+ "Formatting HTTP request metrics" ,
122+ ) ;
123+
124+ if registry. is_empty ( ) {
125+ return Ok ( ( ) ) ;
126+ }
127+
128+ let metric = self . request_total ( ) ;
129+ metric. fmt_help ( f) ?;
130+ Self :: fmt_by_target ( & registry, f, metric, |s| & s. total ) ?;
131+
132+ if self . include_latencies {
133+ let metric = self . response_latency_ms ( ) ;
134+ metric. fmt_help ( f) ?;
135+ Self :: fmt_by_status ( & registry, f, metric, |s| & s. latency ) ?;
136+ }
137+
138+ let metric = self . response_total ( ) ;
139+ metric. fmt_help ( f) ?;
140+ Self :: fmt_by_class ( & registry, f, metric, |s| & s. total ) ?;
141+
142+ registry. retain_since ( Instant :: now ( ) - self . retain_idle ) ;
143+
144+ Ok ( ( ) )
145+ }
146+ }
147+
158148impl FmtLabels for Status {
159149 fn fmt_labels ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
160150 write ! ( f, "status_code=\" {}\" " , self . 0 . as_u16( ) )
0 commit comments