-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprom_metrics.rs
172 lines (163 loc) · 5.61 KB
/
prom_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
use std::collections::HashMap;
use crate::cli::LogFormat;
use crate::metrics::actix_web_prometheus_metrics::{PrometheusMetrics, PrometheusMetricsBuilder};
use crate::metrics::edge_metrics::EdgeInstanceData;
#[cfg(target_os = "linux")]
use prometheus::process_collector::ProcessCollector;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{EnvFilter, Registry};
use crate::http::background_send_metrics;
fn instantiate_tracing_and_logging(log_format: &LogFormat) {
let env_filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
match log_format {
LogFormat::Plain => {
let logger = tracing_subscriber::fmt::layer();
let collector = Registry::default().with(logger).with(env_filter);
tracing::subscriber::set_global_default(collector).unwrap();
}
LogFormat::Json => {
let logger = tracing_subscriber::fmt::layer().json();
let collector = Registry::default().with(logger).with(env_filter);
tracing::subscriber::set_global_default(collector).unwrap();
}
LogFormat::Pretty => {
let logger = tracing_subscriber::fmt::layer().pretty();
let collector = Registry::default().with(logger).with(env_filter);
tracing::subscriber::set_global_default(collector).unwrap();
}
};
}
pub fn instantiate(
registry: Option<prometheus::Registry>,
disable_metrics_endpoint: bool,
log_format: &LogFormat,
instance_data: &EdgeInstanceData,
) -> PrometheusMetrics {
instantiate_tracing_and_logging(log_format);
let registry = registry.unwrap_or_else(instantiate_registry);
register_custom_metrics(®istry);
instantiate_prometheus_metrics_handler(registry, disable_metrics_endpoint, instance_data)
}
fn instantiate_prometheus_metrics_handler(
registry: prometheus::Registry,
disable_metrics_endpoint: bool,
instance_data: &EdgeInstanceData,
) -> PrometheusMetrics {
let mut extra_labels = HashMap::<String, String>::new();
extra_labels.insert(
"edge_version".to_string(),
crate::types::build::PKG_VERSION.to_string(),
);
extra_labels.insert(
"edge_githash".to_string(),
crate::types::build::SHORT_COMMIT.to_string(),
);
extra_labels.insert("app_name".to_string(), instance_data.app_name.clone());
extra_labels.insert("instance_id".to_string(), instance_data.identifier.clone());
PrometheusMetricsBuilder::new("")
.endpoint("/internal-backstage/metrics")
.const_labels(extra_labels)
.registry(registry)
.exclude("/favicon.ico")
.disable_metrics_endpoint(disable_metrics_endpoint)
.build()
.unwrap()
}
fn instantiate_registry() -> prometheus::Registry {
#[cfg(target_os = "linux")]
{
let registry = prometheus::Registry::new();
let process_collector = ProcessCollector::for_self();
let _register_result = registry.register(Box::new(process_collector));
registry
}
#[cfg(not(target_os = "linux"))]
prometheus::Registry::new()
}
fn register_custom_metrics(registry: &prometheus::Registry) {
registry
.register(Box::new(
background_send_metrics::METRICS_UNEXPECTED_ERRORS.clone(),
))
.unwrap();
registry
.register(Box::new(
background_send_metrics::METRICS_UPSTREAM_HTTP_ERRORS.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::metrics::client_metrics::METRICS_SIZE_HISTOGRAM.clone(),
))
.unwrap();
registry
.register(Box::new(
background_send_metrics::METRICS_UPSTREAM_CLIENT_BULK.clone(),
))
.unwrap();
registry
.register(Box::new(
background_send_metrics::METRICS_UPSTREAM_OUTDATED.clone(),
))
.unwrap();
registry
.register(Box::new(
background_send_metrics::METRICS_INTERVAL_BETWEEN_SEND.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::CLIENT_FEATURE_FETCH_FAILURES.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::CLIENT_REGISTER_FAILURES.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::TOKEN_VALIDATION_FAILURES.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::CLIENT_FEATURE_FETCH.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::UPSTREAM_VERSION.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::metrics::client_metrics::FEATURE_TOGGLE_USAGE_TOTAL.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::broadcaster::CONNECTED_STREAMING_CLIENTS.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::METRICS_UPLOAD.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::http::unleash_client::INSTANCE_DATA_UPLOAD.clone(),
))
.unwrap();
}
#[cfg(test)]
pub fn test_instantiate_without_tracing_and_logging(
registry: Option<prometheus::Registry>,
) -> PrometheusMetrics {
let registry = registry.unwrap_or_else(instantiate_registry);
register_custom_metrics(®istry);
instantiate_prometheus_metrics_handler(registry, false, &EdgeInstanceData::new("test app"))
}