forked from open-telemetry/otel-arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollector.rs
More file actions
332 lines (288 loc) · 11 KB
/
collector.rs
File metadata and controls
332 lines (288 loc) · 11 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//! Task periodically collecting the internal signals emitted by the engine and the pipelines.
use std::sync::Arc;
use otap_df_config::pipeline::telemetry::TelemetryConfig;
use tokio_util::sync::CancellationToken;
use crate::error::Error;
use crate::metrics::MetricSetSnapshot;
use crate::registry::TelemetryRegistryHandle;
use crate::reporter::MetricsReporter;
/// Internal collector responsible for gathering internal telemetry signals (fow now only metric
/// sets or multivariate metrics).
pub struct InternalCollector {
/// The registry where entities and metrics are declared and aggregated.
registry: TelemetryRegistryHandle,
/// Receiver for incoming metrics.
/// The message is a combination of a MetricSetKey and collection of MetricValues.
/// The metrics key is the aggregation key for the metrics,
metrics_receiver: flume::Receiver<MetricSetSnapshot>,
}
impl InternalCollector {
/// Creates a new `InternalCollector` with a pipeline.
pub(crate) fn new(
config: &TelemetryConfig,
registry: TelemetryRegistryHandle,
) -> (Self, MetricsReporter) {
let (metrics_sender, metrics_receiver) =
flume::bounded::<MetricSetSnapshot>(config.reporting_channel_size);
(
Self {
registry,
metrics_receiver,
},
MetricsReporter::new(metrics_sender),
)
}
/// Collects metrics from the reporting channel and aggregates them into the `registry`.
/// The collection runs indefinitely until the metrics channel is closed.
/// Returns the pipeline instance when the loop ends (or None if no pipeline was configured).
pub async fn run_collection_loop(self: Arc<Self>) -> Result<(), Error> {
loop {
match self.metrics_receiver.recv_async().await {
Ok(metrics) => {
self.registry
.accumulate_metric_set_snapshot(metrics.key, &metrics.metrics);
}
Err(_) => {
// Channel closed, exit the loop
return Ok(());
}
}
}
}
/// Runs the collection loop until cancellation is requested.
///
/// This method starts the internal signal collection loop and listens for a shutdown signal.
/// It returns when either the collection loop ends (Ok/Err) or the shutdown signal fires.
pub async fn run(self: Arc<Self>, cancel: CancellationToken) -> Result<(), Error> {
tokio::select! {
biased;
_ = cancel.cancelled() => {
// Shutdown requested; cancel the collection loop by dropping its future.
Ok(())
}
res = self.run_collection_loop() => {
res
}
}
}
}
#[cfg(test)]
mod tests {
use otap_df_config::pipeline::telemetry::metrics::MetricsConfig;
use otap_df_config::settings::telemetry::logs::LogsConfig;
use super::*;
use crate::attributes::{AttributeSetHandler, AttributeValue};
use crate::descriptor::{
AttributeField, AttributeValueType, AttributesDescriptor, Instrument, MetricValueType,
MetricsDescriptor, MetricsField, Temporality,
};
use crate::metrics::MetricSetHandler;
use crate::metrics::MetricValue;
use crate::registry::MetricSetKey;
use std::collections::HashMap;
use std::fmt::Debug;
use std::time::Duration;
// --- Test-only mock metric/attributes definitions (no pipeline required) ---
#[derive(Debug)]
struct MockMetricSet {
values: Vec<MetricValue>,
}
impl MockMetricSet {
fn new() -> Self {
Self {
values: vec![MetricValue::from(0u64), MetricValue::from(0u64)],
}
}
}
impl Default for MockMetricSet {
fn default() -> Self {
Self::new()
}
}
static MOCK_METRICS_DESCRIPTOR: MetricsDescriptor = MetricsDescriptor {
name: "test_metrics",
metrics: &[
MetricsField {
name: "counter1",
unit: "1",
brief: "Test counter 1",
instrument: Instrument::Counter,
temporality: Some(Temporality::Delta),
value_type: MetricValueType::U64,
},
MetricsField {
name: "counter2",
unit: "1",
brief: "Test counter 2",
instrument: Instrument::Counter,
temporality: Some(Temporality::Delta),
value_type: MetricValueType::U64,
},
],
};
static MOCK_ATTRIBUTES_DESCRIPTOR: AttributesDescriptor = AttributesDescriptor {
name: "test_attributes",
fields: &[AttributeField {
key: "test_key",
r#type: AttributeValueType::String,
brief: "Test attribute",
}],
};
impl MetricSetHandler for MockMetricSet {
fn descriptor(&self) -> &'static MetricsDescriptor {
&MOCK_METRICS_DESCRIPTOR
}
fn snapshot_values(&self) -> Vec<MetricValue> {
self.values.clone()
}
fn clear_values(&mut self) {
self.values.iter_mut().for_each(MetricValue::reset);
}
fn needs_flush(&self) -> bool {
self.values.iter().any(|&v| !v.is_zero())
}
}
#[derive(Debug)]
struct MockAttributeSet {
_value: String,
attribute_values: Vec<AttributeValue>,
}
impl MockAttributeSet {
fn new(value: impl Into<String>) -> Self {
let v = value.into();
Self {
_value: v.clone(),
attribute_values: vec![AttributeValue::String(v)],
}
}
}
impl AttributeSetHandler for MockAttributeSet {
fn descriptor(&self) -> &'static AttributesDescriptor {
&MOCK_ATTRIBUTES_DESCRIPTOR
}
fn iter_attributes<'a>(&'a self) -> crate::attributes::AttributeIterator<'a> {
crate::attributes::AttributeIterator::new(
MOCK_ATTRIBUTES_DESCRIPTOR.fields,
&self.attribute_values,
)
}
fn attribute_values(&self) -> &[AttributeValue] {
&self.attribute_values
}
}
fn create_test_config(reporting_interval_ms: u64) -> TelemetryConfig {
// Flush interval is irrelevant when no pipeline is configured; keep field for completeness.
TelemetryConfig {
reporting_channel_size: 10,
reporting_interval: Duration::from_millis(reporting_interval_ms),
metrics: MetricsConfig::default(),
logs: LogsConfig::default(),
resource: HashMap::new(),
}
}
fn create_test_snapshot(key: MetricSetKey, values: Vec<MetricValue>) -> MetricSetSnapshot {
MetricSetSnapshot {
key,
metrics: values,
}
}
fn create_test_registry() -> TelemetryRegistryHandle {
TelemetryRegistryHandle::new()
}
// --- Tests without any pipeline, asserting on the registry state ---
#[tokio::test]
async fn test_collector_without_pipeline_returns_none_on_channel_close() {
let config = create_test_config(100);
let telemetry_registry = create_test_registry();
let (collector, _reporter) = InternalCollector::new(&config, telemetry_registry);
// Close immediately
drop(_reporter);
Arc::new(collector).run_collection_loop().await.unwrap();
}
#[tokio::test]
async fn test_accumulates_snapshots_into_registry() {
let config = create_test_config(10);
let telemetry_registry = create_test_registry();
// Register a metric set to get a valid key
let metric_set: crate::metrics::MetricSet<MockMetricSet> =
telemetry_registry.register_metric_set(MockAttributeSet::new("attr"));
let key = metric_set.key;
let (collector, reporter) = InternalCollector::new(&config, telemetry_registry.clone());
let handle = tokio::spawn(async move { Arc::new(collector).run_collection_loop().await });
// Send two snapshots that should be accumulated: [10,20] + [5,15] => [15,35]
reporter
.report_snapshot(create_test_snapshot(
key,
vec![MetricValue::from(10u64), MetricValue::from(20u64)],
))
.await
.unwrap();
reporter
.report_snapshot(create_test_snapshot(
key,
vec![MetricValue::from(5u64), MetricValue::from(15u64)],
))
.await
.unwrap();
// Give the collector a brief moment to process
tokio::time::sleep(Duration::from_millis(30)).await;
// Inspect current metrics without resetting
let mut collected = Vec::new();
telemetry_registry.visit_current_metrics(|_desc, _attrs, iter| {
for (field, value) in iter {
collected.push((field.name, value));
}
});
assert_eq!(collected.len(), 2);
// Order follows descriptor order
assert_eq!(collected[0].0, "counter1");
assert_eq!(collected[0].1, MetricValue::from(15u64));
assert_eq!(collected[1].0, "counter2");
assert_eq!(collected[1].1, MetricValue::from(35u64));
// Close the channel and ensure loop ends returning None
drop(reporter);
handle.await.unwrap().unwrap();
}
#[tokio::test]
async fn test_visit_then_reset_via_registry_api() {
let config = create_test_config(10);
let telemetry_registry = create_test_registry();
let metric_set: crate::metrics::MetricSet<MockMetricSet> =
telemetry_registry.register_metric_set(MockAttributeSet::new("attr"));
let key = metric_set.key;
let (collector, reporter) = InternalCollector::new(&config, telemetry_registry.clone());
let handle = tokio::spawn(async move { Arc::new(collector).run_collection_loop().await });
reporter
.report_snapshot(create_test_snapshot(
key,
vec![MetricValue::from(7u64), MetricValue::from(0u64)],
))
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
// First visit should see the non-zero and then reset
let mut first = Vec::new();
telemetry_registry.visit_metrics_and_reset(|_d, _a, iter| {
for (f, v) in iter {
first.push((f.name, v));
}
});
assert_eq!(
first,
vec![
("counter1", MetricValue::from(7u64)),
("counter2", MetricValue::from(0u64))
]
);
// Second visit should see nothing
let mut count = 0;
telemetry_registry.visit_metrics_and_reset(|_, _, _| {
count += 1;
});
assert_eq!(count, 0);
drop(reporter);
handle.await.unwrap().unwrap();
}
}