-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathtelemetry.rs
More file actions
302 lines (272 loc) · 9.72 KB
/
Copy pathtelemetry.rs
File metadata and controls
302 lines (272 loc) · 9.72 KB
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
use crate::client::protocol::{SidecarSettings, TelemetrySettings};
use crate::ffi::sidecar_ffi::{
ddog_MetricType, ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
ddog_MetricType_DDOG_METRIC_TYPE_DISTRIBUTION, ddog_MetricType_DDOG_METRIC_TYPE_GAUGE,
};
use std::cell::Cell;
use std::collections::HashMap;
use std::sync::Mutex;
pub mod error_tel_ctx;
mod sidecar;
mod tel_aware_logger;
pub use sidecar::{resolve_symbols, TelemetrySidecarLogSubmitter, TelemetrySidecarMetricSubmitter};
pub use sidecar::{SidecarReadyFuture, SidecarStatus};
pub use tel_aware_logger::TelemetryAwareLogger;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MetricName(pub &'static str);
#[derive(Debug, Clone, Copy)]
pub struct SpanMetricName(pub &'static str);
#[derive(Debug, Clone, Copy)]
pub struct SpanMetaName(pub &'static str);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum LogLevel {
Error,
Warn,
#[allow(dead_code)]
Debug,
}
pub trait SpanMetricsSubmitter {
fn submit_metric(&mut self, key: SpanMetricName, value: f64);
fn submit_meta(&mut self, key: SpanMetaName, value: String);
fn submit_meta_dyn_key(&mut self, key: String, value: String);
fn submit_metric_dyn_key(&mut self, key: String, value: f64);
}
pub trait SpanMetricsGenerator {
fn generate_span_metrics(&'_ self, submitter: &mut dyn SpanMetricsSubmitter);
}
pub trait SpanMetaGenerator {
fn generate_meta(&'_ self, submitter: &mut dyn SpanMetricsSubmitter);
}
pub trait TelemetryMetricSubmitter {
fn submit_metric(&mut self, key: MetricName, value: f64, tags: TelemetryTags);
}
pub trait TelemetryMetricsGenerator {
fn generate_telemetry_metrics(&self, submitter: &mut dyn TelemetryMetricSubmitter);
}
pub trait TelemetryLogSubmitter {
fn submit_log(&mut self, log: TelemetryLog);
}
pub trait TelemetryLogsGenerator {
fn generate_telemetry_logs(&'_ self, submitter: &mut dyn TelemetryLogSubmitter);
}
#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct TelemetryTags {
data: String,
}
impl TelemetryTags {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, key: impl AsRef<str>, value: impl AsRef<str>) -> &mut Self {
if !self.data.is_empty() {
self.data.push(',');
}
self.data.push_str(key.as_ref());
self.data.push(':');
self.data.push_str(value.as_ref());
self
}
pub fn into_string(self) -> String {
self.data
}
}
impl From<TelemetryTags> for String {
fn from(tags: TelemetryTags) -> String {
tags.data
}
}
pub const WAF_INIT: MetricName = MetricName("waf.init");
pub const WAF_UPDATES: MetricName = MetricName("waf.updates");
pub const WAF_REQUESTS: MetricName = MetricName("waf.requests");
pub const WAF_CONFIG_ERRORS: MetricName = MetricName("waf.config_errors");
pub const WAF_ERROR: MetricName = MetricName("waf.error");
pub const WAF_DURATION_DIST: MetricName = MetricName("waf.duration");
pub const RASP_DURATION_DIST: MetricName = MetricName("rasp.duration");
pub const RASP_RULE_EVAL: MetricName = MetricName("rasp.rule.eval");
pub const RASP_RULE_DURATION_DIST: MetricName = MetricName("rasp.rule.duration");
pub const RASP_RULE_MATCH: MetricName = MetricName("rasp.rule.match");
pub const RASP_TIMEOUT: MetricName = MetricName("rasp.timeout");
pub const RASP_ERROR: MetricName = MetricName("rasp.error");
pub const HELPER_WORKER_COUNT: MetricName = MetricName("helper.service_worker_count");
#[derive(Debug, Clone, Copy)]
pub struct KnownMetric {
pub name: MetricName,
pub metric_type: ddog_MetricType,
}
pub const KNOWN_METRICS: &[KnownMetric] = &[
KnownMetric {
name: WAF_REQUESTS,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: WAF_UPDATES,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: WAF_INIT,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: WAF_CONFIG_ERRORS,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: WAF_ERROR,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: WAF_DURATION_DIST,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_DISTRIBUTION,
},
KnownMetric {
name: RASP_DURATION_DIST,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_DISTRIBUTION,
},
KnownMetric {
name: RASP_RULE_DURATION_DIST,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_DISTRIBUTION,
},
KnownMetric {
name: RASP_TIMEOUT,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: RASP_RULE_MATCH,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: RASP_RULE_EVAL,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: RASP_ERROR,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_COUNT,
},
KnownMetric {
name: HELPER_WORKER_COUNT,
metric_type: ddog_MetricType_DDOG_METRIC_TYPE_GAUGE,
},
];
pub fn register_known_metrics(
sidecar_settings: &SidecarSettings,
telemetry_settings: &TelemetrySettings,
) -> anyhow::Result<()> {
for metric in KNOWN_METRICS {
sidecar::register_metric_ffi(sidecar_settings, telemetry_settings, metric)?;
}
Ok(())
}
// not implemented (difficult to count requests on the helper)
#[allow(dead_code)]
pub const RC_REQUESTS_BEFORE_RUNNING: MetricName =
MetricName("remote_config.requests_before_running");
pub const EVENT_RULES_LOADED: SpanMetricName = SpanMetricName("_dd.appsec.event_rules.loaded");
pub const EVENT_RULES_FAILED: SpanMetricName = SpanMetricName("_dd.appsec.event_rules.error_count");
pub const EVENT_RULES_ERRORS: SpanMetaName = SpanMetaName("_dd.appsec.event_rules.errors");
pub const EVENT_RULES_VERSION: SpanMetaName = SpanMetaName("_dd.appsec.event_rules.version");
pub const WAF_VERSION: SpanMetaName = SpanMetaName("_dd.appsec.waf.version");
pub const WAF_DURATION: SpanMetricName = SpanMetricName("_dd.appsec.waf.duration");
pub const RAST_DURATION: SpanMetricName = SpanMetricName("_dd.appsec.rasp.duration");
pub const RAST_RULE_EVALS: SpanMetricName = SpanMetricName("_dd.appsec.rasp.rule.eval");
pub const RAST_TIMEOUTS: SpanMetricName = SpanMetricName("_dd.appsec.rasp.timeout");
// A "Collector" is a type of fake submitter that instead of submitting telemetry
// directly, stores it inside. It can then be converted into a generator (or implements it directly)
// for submission into the real submitter.
// See TelemetryMetricsCollector and TelemetryLogsCollector.
#[derive(Default, Debug)]
pub struct TelemetryMetricsCollector {
metrics: HashMap<MetricName, Vec<(f64, TelemetryTags)>>,
}
impl TelemetryMetricsCollector {
pub fn into_generator(self) -> impl TelemetryMetricsGenerator {
struct TelemetryMetricsGeneratorImpl {
metrics: Cell<TelemetryMetricsCollector>,
}
impl TelemetryMetricsGenerator for TelemetryMetricsGeneratorImpl {
fn generate_telemetry_metrics(&self, submitter: &mut dyn TelemetryMetricSubmitter) {
for (key, values) in self.metrics.take().metrics.into_iter() {
for (value, tags) in values {
submitter.submit_metric(key, value, tags);
}
}
}
}
TelemetryMetricsGeneratorImpl {
metrics: Cell::new(self),
}
}
}
impl TelemetryMetricSubmitter for TelemetryMetricsCollector {
fn submit_metric(&mut self, key: MetricName, value: f64, tags: TelemetryTags) {
self.metrics.entry(key).or_default().push((value, tags));
}
}
#[derive(Debug, Clone)]
pub struct TelemetryLog {
pub level: LogLevel,
pub identifier: String,
pub message: String,
pub stack_trace: Option<String>,
pub tags: Option<TelemetryTags>,
pub is_sensitive: bool,
}
#[allow(dead_code)] // Used in TelemetryLogSubmitter impl
const MAX_PENDING_LOGS: usize = 100;
pub struct TelemetryLogsCollector {
logs: Mutex<Vec<TelemetryLog>>,
}
impl TelemetryLogsCollector {
pub fn new() -> Self {
Self {
logs: Mutex::new(Vec::new()),
}
}
pub fn submit_log(&self, log: TelemetryLog) {
let mut logs = self.logs.lock().unwrap();
if logs.len() >= MAX_PENDING_LOGS {
log::warn!("Pending logs queue is full, dropping log");
return;
}
log::trace!(
"submit_log [{:?}][{}]: {}",
log.level,
log.identifier,
log.message
);
logs.push(log);
}
}
impl Default for TelemetryLogsCollector {
fn default() -> Self {
Self::new()
}
}
impl TelemetryLogSubmitter for TelemetryLogsCollector {
fn submit_log(&mut self, log: TelemetryLog) {
let mut logs = self.logs.lock().unwrap();
if logs.len() >= MAX_PENDING_LOGS {
log::warn!("Pending logs queue is full, dropping log");
return;
}
log::trace!(
"submit_log [{:?}][{}]: {}",
log.level,
log.identifier,
log.message
);
logs.push(log);
}
}
impl TelemetryLogsGenerator for TelemetryLogsCollector {
fn generate_telemetry_logs(&'_ self, submitter: &mut dyn TelemetryLogSubmitter) {
let mut logs = self.logs.lock().unwrap();
log::debug!("Draining {} telemetry logs from collector", logs.len());
for (i, log) in logs.drain(..).enumerate() {
log::debug!("Submitting log {} of batch", i + 1);
submitter.submit_log(log);
log::debug!("Successfully submitted log {}", i + 1);
}
log::debug!("Finished draining all logs");
}
}