/
metrics.rs
454 lines (383 loc) · 12.3 KB
/
metrics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! This module provides most of the metrics infrastructure for both inbound &
//! outbound proxies.
//!
//! This is less than ideal. Instead of having common metrics with differing
//! labels for inbound & outbound, we should instead have distinct metrics for
//! each case. And the metric registries should be instantiated in the
//! inbound/outbound crates, etc.
pub use crate::transport::labels::{TargetAddr, TlsAccept};
use crate::{
classify::Class,
control, http_metrics, opencensus, profiles, stack_metrics,
svc::Param,
tls,
transport::{self, labels::TlsConnect},
};
use linkerd_addr::Addr;
pub use linkerd_metrics::*;
use linkerd_proxy_server_policy as policy;
use std::{
fmt::{self, Write},
net::SocketAddr,
sync::Arc,
time::Duration,
};
pub type ControlHttp = http_metrics::Requests<ControlLabels, Class>;
pub type HttpEndpoint = http_metrics::Requests<EndpointLabels, Class>;
pub type HttpProfileRoute = http_metrics::Requests<ProfileRouteLabels, Class>;
pub type HttpProfileRouteRetry = http_metrics::Retries<ProfileRouteLabels>;
pub type Stack = stack_metrics::Registry<StackLabels>;
#[derive(Clone, Debug)]
pub struct Metrics {
pub proxy: Proxy,
pub control: ControlHttp,
pub opencensus: opencensus::metrics::Registry,
}
#[derive(Clone, Debug)]
pub struct Proxy {
pub http_profile_route: HttpProfileRoute,
pub http_profile_route_actual: HttpProfileRoute,
pub http_profile_route_retry: HttpProfileRouteRetry,
pub http_endpoint: HttpEndpoint,
pub transport: transport::Metrics,
pub stack: Stack,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ControlLabels {
addr: Addr,
server_id: tls::ConditionalClientTls,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum EndpointLabels {
Inbound(InboundEndpointLabels),
Outbound(OutboundEndpointLabels),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct InboundEndpointLabels {
pub tls: tls::ConditionalServerTls,
pub authority: Option<http::uri::Authority>,
pub target_addr: SocketAddr,
pub policy: RouteAuthzLabels,
}
/// A label referencing an inbound `Server` (i.e. for policy).
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ServerLabel(pub Arc<policy::Meta>);
/// Labels referencing an inbound server and authorization.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ServerAuthzLabels {
pub server: ServerLabel,
pub authz: Arc<policy::Meta>,
}
/// Labels referencing an inbound server and route.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct RouteLabels {
pub server: ServerLabel,
pub route: Arc<policy::Meta>,
}
/// Labels referencing an inbound server, route, and authorization.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct RouteAuthzLabels {
pub route: RouteLabels,
pub authz: Arc<policy::Meta>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct OutboundEndpointLabels {
pub server_id: tls::ConditionalClientTls,
pub authority: Option<http::uri::Authority>,
pub labels: Option<String>,
pub target_addr: SocketAddr,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct StackLabels {
pub direction: Direction,
pub protocol: &'static str,
pub name: &'static str,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ProfileRouteLabels {
direction: Direction,
addr: profiles::LogicalAddr,
labels: Option<String>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
In,
Out,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct Authority<'a>(&'a http::uri::Authority);
pub fn prefix_labels<'i, I>(prefix: &str, mut labels_iter: I) -> Option<String>
where
I: Iterator<Item = (&'i String, &'i String)>,
{
let (k0, v0) = labels_iter.next()?;
let mut out = format!("{}_{}=\"{}\"", prefix, k0, v0);
for (k, v) in labels_iter {
write!(out, ",{}_{}=\"{}\"", prefix, k, v).expect("label concat must succeed");
}
Some(out)
}
// === impl Metrics ===
impl Metrics {
pub fn new(retain_idle: Duration) -> (Self, impl FmtMetrics + Clone + Send + 'static) {
let (control, control_report) = {
let m = http_metrics::Requests::<ControlLabels, Class>::default();
let r = m.clone().into_report(retain_idle).with_prefix("control");
(m, r)
};
let (http_endpoint, endpoint_report) = {
let m = http_metrics::Requests::<EndpointLabels, Class>::default();
let r = m.clone().into_report(retain_idle);
(m, r)
};
let (http_profile_route, profile_route_report) = {
let m = http_metrics::Requests::<ProfileRouteLabels, Class>::default();
let r = m.clone().into_report(retain_idle).with_prefix("route");
(m, r)
};
let (http_profile_route_retry, retry_report) = {
let m = http_metrics::Retries::<ProfileRouteLabels>::default();
let r = m.clone().into_report(retain_idle).with_prefix("route");
(m, r)
};
let (http_profile_route_actual, actual_report) = {
let m = http_metrics::Requests::<ProfileRouteLabels, Class>::default();
let r = m
.clone()
.into_report(retain_idle)
.with_prefix("route_actual");
(m, r.without_latencies())
};
let stack = stack_metrics::Registry::default();
let (transport, transport_report) = transport::Metrics::new(retain_idle);
let proxy = Proxy {
http_endpoint,
http_profile_route,
http_profile_route_retry,
http_profile_route_actual,
stack: stack.clone(),
transport,
};
let (opencensus, opencensus_report) = opencensus::metrics::new();
let metrics = Metrics {
proxy,
control,
opencensus,
};
let report = endpoint_report
.and_report(profile_route_report)
.and_report(retry_report)
.and_report(actual_report)
.and_report(control_report)
.and_report(transport_report)
.and_report(opencensus_report)
.and_report(stack);
(metrics, report)
}
}
// === impl CtlLabels ===
impl Param<ControlLabels> for control::ControlAddr {
fn param(&self) -> ControlLabels {
ControlLabels {
addr: self.addr.clone(),
server_id: self.identity.clone(),
}
}
}
impl FmtLabels for ControlLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "addr=\"{}\",", self.addr)?;
TlsConnect::from(&self.server_id).fmt_labels(f)?;
Ok(())
}
}
// === impl ProfileRouteLabels ===
impl ProfileRouteLabels {
pub fn inbound(addr: profiles::LogicalAddr, route: &profiles::http::Route) -> Self {
let labels = prefix_labels("rt", route.labels().iter());
Self {
addr,
labels,
direction: Direction::In,
}
}
pub fn outbound(addr: profiles::LogicalAddr, route: &profiles::http::Route) -> Self {
let labels = prefix_labels("rt", route.labels().iter());
Self {
addr,
labels,
direction: Direction::Out,
}
}
}
impl FmtLabels for ProfileRouteLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.direction.fmt_labels(f)?;
write!(f, ",dst=\"{}\"", self.addr)?;
if let Some(labels) = self.labels.as_ref() {
write!(f, ",{}", labels)?;
}
Ok(())
}
}
// === impl EndpointLabels ===
impl From<InboundEndpointLabels> for EndpointLabels {
fn from(i: InboundEndpointLabels) -> Self {
Self::Inbound(i)
}
}
impl From<OutboundEndpointLabels> for EndpointLabels {
fn from(i: OutboundEndpointLabels) -> Self {
Self::Outbound(i)
}
}
impl FmtLabels for EndpointLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Inbound(i) => (Direction::In, i).fmt_labels(f),
Self::Outbound(o) => (Direction::Out, o).fmt_labels(f),
}
}
}
impl FmtLabels for InboundEndpointLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(a) = self.authority.as_ref() {
Authority(a).fmt_labels(f)?;
write!(f, ",")?;
}
(
(TargetAddr(self.target_addr), TlsAccept::from(&self.tls)),
&self.policy,
)
.fmt_labels(f)?;
Ok(())
}
}
impl FmtLabels for ServerLabel {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"srv_group=\"{}\",srv_kind=\"{}\",srv_name=\"{}\"",
self.0.group(),
self.0.kind(),
self.0.name()
)
}
}
impl FmtLabels for ServerAuthzLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.server.fmt_labels(f)?;
write!(
f,
",authz_group=\"{}\",authz_kind=\"{}\",authz_name=\"{}\"",
self.authz.group(),
self.authz.kind(),
self.authz.name()
)
}
}
impl FmtLabels for RouteLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.server.fmt_labels(f)?;
write!(
f,
",route_group=\"{}\",route_kind=\"{}\",route_name=\"{}\"",
self.route.group(),
self.route.kind(),
self.route.name(),
)
}
}
impl FmtLabels for RouteAuthzLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.route.fmt_labels(f)?;
write!(
f,
",authz_group=\"{}\",authz_kind=\"{}\",authz_name=\"{}\"",
self.authz.group(),
self.authz.kind(),
self.authz.name(),
)
}
}
impl FmtLabels for OutboundEndpointLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(a) = self.authority.as_ref() {
Authority(a).fmt_labels(f)?;
write!(f, ",")?;
}
let ta = TargetAddr(self.target_addr);
let tls = TlsConnect::from(&self.server_id);
(ta, tls).fmt_labels(f)?;
if let Some(labels) = self.labels.as_ref() {
write!(f, ",{}", labels)?;
}
Ok(())
}
}
impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::In => write!(f, "inbound"),
Self::Out => write!(f, "outbound"),
}
}
}
impl FmtLabels for Direction {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "direction=\"{}\"", self)
}
}
impl<'a> FmtLabels for Authority<'a> {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "authority=\"{}\"", self.0)
}
}
impl FmtLabels for Class {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let class = |ok: bool| if ok { "success" } else { "failure" };
match self {
Class::Http(res) => write!(
f,
"classification=\"{}\",grpc_status=\"\",error=\"\"",
class(res.is_ok())
),
Class::Grpc(res) => write!(
f,
"classification=\"{}\",grpc_status=\"{}\",error=\"\"",
class(res.is_ok()),
match res {
Ok(code) | Err(code) => <i32>::from(*code),
}
),
Class::Error(msg) => write!(
f,
"classification=\"failure\",grpc_status=\"\",error=\"{}\"",
msg
),
}
}
}
// === impl StackLabels ===
impl StackLabels {
pub fn inbound(protocol: &'static str, name: &'static str) -> Self {
Self {
name,
protocol,
direction: Direction::In,
}
}
pub fn outbound(protocol: &'static str, name: &'static str) -> Self {
Self {
name,
protocol,
direction: Direction::Out,
}
}
}
impl FmtLabels for StackLabels {
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.direction.fmt_labels(f)?;
write!(f, ",protocol=\"{}\",name=\"{}\"", self.protocol, self.name)
}
}