asupersync-conformance 0.3.3

Conformance test suite for async runtime specifications
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
//! OpenTelemetry Sum Aggregator Conformance Test (Tick #127)
//!
//! This conformance test verifies that our metric Sum aggregator produces
//! identical Sum values and preserves the monotonicity flag compared to
//! the reference opentelemetry-sdk implementation.

use asupersync::observability::otel::MetricsSnapshot;
use opentelemetry::KeyValue;
use opentelemetry::metrics::MeterProvider as _;
use opentelemetry_sdk::metrics::{
    InMemoryMetricExporter, PeriodicReader, SdkMeterProvider,
    data::{AggregatedMetrics, MetricData, ResourceMetrics},
};
use std::collections::BTreeMap;

/// Test cases for Sum aggregator conformance.
struct SumAggregatorTestCase {
    name: &'static str,
    counter_name: &'static str,
    data_points: Vec<(Vec<(&'static str, &'static str)>, i64)>, // (labels, value)
    is_monotonic: bool,
    description: &'static str,
}

fn main() {
    println!("🔍 OpenTelemetry Sum Aggregator Conformance Test");
    println!("Verifying Sum aggregator produces identical values and preserves monotonicity");

    let test_cases = vec![
        SumAggregatorTestCase {
            name: "monotonic_counter_basic",
            counter_name: "requests_total",
            data_points: vec![
                (vec![("method", "GET"), ("status", "200")], 10),
                (vec![("method", "POST"), ("status", "201")], 5),
                (vec![("method", "GET"), ("status", "404")], 2),
            ],
            is_monotonic: true,
            description: "Basic monotonic counter with multiple label sets",
        },
        SumAggregatorTestCase {
            name: "monotonic_counter_single_series",
            counter_name: "events_processed",
            data_points: vec![
                (vec![("service", "api")], 100),
                (vec![("service", "api")], 50), // Same labels, accumulated
            ],
            is_monotonic: true,
            description: "Single time series with accumulation",
        },
        SumAggregatorTestCase {
            name: "updown_counter_positive_negative",
            counter_name: "active_connections",
            data_points: vec![
                (vec![("region", "us-east")], 10),
                (vec![("region", "us-east")], -3), // Negative increment
                (vec![("region", "us-west")], 5),
                (vec![("region", "us-west")], -1),
            ],
            is_monotonic: false,
            description: "UpDownCounter with positive and negative increments",
        },
        SumAggregatorTestCase {
            name: "zero_values",
            counter_name: "zero_test",
            data_points: vec![
                (vec![("type", "zero")], 0),
                (vec![("type", "positive")], 5),
                (vec![("type", "zero")], 0), // More zeros
            ],
            is_monotonic: true,
            description: "Counter with zero value increments",
        },
        SumAggregatorTestCase {
            name: "large_values",
            counter_name: "large_counter",
            data_points: vec![
                (vec![("size", "large")], i64::MAX / 2),
                (vec![("size", "small")], 1),
                (vec![("size", "large")], 1000), // Should not overflow
            ],
            is_monotonic: true,
            description: "Large values near i64::MAX",
        },
    ];

    println!(
        "📋 Running {} Sum aggregator conformance tests",
        test_cases.len()
    );

    let mut failed_tests = Vec::new();

    for test_case in &test_cases {
        println!("  Testing {}: {}", test_case.name, test_case.description);

        // Test our implementation
        let our_sum_data = test_our_sum_aggregator(test_case);

        // Test reference implementation
        let reference_sum_data = test_reference_sum_aggregator(test_case);

        // Compare results
        if let Err(error) = compare_sum_data(&our_sum_data, &reference_sum_data, test_case) {
            failed_tests.push((test_case.name.to_string(), error));
        } else {
            println!("{}", test_case.name);
        }
    }

    // Test edge cases
    println!("\n📋 Testing Sum aggregator edge cases");
    test_sum_aggregator_edge_cases(&mut failed_tests);

    // Report results
    println!("\n📊 Sum Aggregator Conformance Test Results");
    if failed_tests.is_empty() {
        println!("✅ ALL TESTS PASSED - Sum aggregator is conformant");
        println!("🎯 Sum values and monotonicity flags match opentelemetry-sdk exactly");
    } else {
        println!("{} TESTS FAILED:", failed_tests.len());
        for (test_name, error) in &failed_tests {
            println!("   {} - {}", test_name, error);
        }
        std::process::exit(1);
    }
}

/// Test our Sum aggregator implementation.
fn test_our_sum_aggregator(test_case: &SumAggregatorTestCase) -> Vec<SumDataPoint> {
    let mut series = BTreeMap::<Vec<(String, String)>, i64>::new();
    for (labels, value) in &test_case.data_points {
        let label_set = labels
            .iter()
            .map(|(key, value)| ((*key).to_string(), (*value).to_string()))
            .collect::<Vec<_>>();
        *series.entry(label_set).or_default() += *value;
    }

    let mut snapshot = MetricsSnapshot::new();
    for (labels, value) in series {
        if test_case.is_monotonic {
            snapshot.add_counter(test_case.counter_name, labels, value as u64);
        } else {
            snapshot.add_gauge(test_case.counter_name, labels, value);
        }
    }

    extract_sum_data_from_snapshot(&snapshot, test_case.counter_name, test_case.is_monotonic)
}

/// Test reference opentelemetry-sdk Sum aggregator.
fn test_reference_sum_aggregator(test_case: &SumAggregatorTestCase) -> Vec<SumDataPoint> {
    let exporter = InMemoryMetricExporter::default();
    let reader = PeriodicReader::builder(exporter.clone()).build();
    let provider = SdkMeterProvider::builder().with_reader(reader).build();

    let meter = provider.meter("test");

    // Create counter or updown counter based on monotonicity
    if test_case.is_monotonic {
        let counter = meter.u64_counter(test_case.counter_name).build();

        for (labels, value) in &test_case.data_points {
            let kvs: Vec<_> = labels
                .iter()
                .map(|(key, value)| KeyValue::new(*key, *value))
                .collect();
            counter.add(*value as u64, &kvs);
        }
    } else {
        let updown_counter = meter.i64_up_down_counter(test_case.counter_name).build();

        for (labels, value) in &test_case.data_points {
            let kvs: Vec<_> = labels
                .iter()
                .map(|(key, value)| KeyValue::new(*key, *value))
                .collect();
            updown_counter.add(*value, &kvs);
        }
    }

    provider.force_flush().expect("force flush metrics");
    let resource_metrics = exporter
        .get_finished_metrics()
        .expect("in-memory metrics export");

    // Extract Sum data
    extract_sum_data_from_sdk(
        &resource_metrics,
        test_case.counter_name,
        test_case.is_monotonic,
    )
}

/// Our test representation of Sum data point.
#[derive(Debug, Clone, PartialEq)]
struct SumDataPoint {
    labels: Vec<(String, String)>,
    value: i64,
    is_monotonic: bool,
}

/// Extract Sum data points from our metrics snapshot.
fn extract_sum_data_from_snapshot(
    snapshot: &MetricsSnapshot,
    counter_name: &str,
    is_monotonic: bool,
) -> Vec<SumDataPoint> {
    let mut data_points = Vec::new();

    // Check counters (monotonic)
    if is_monotonic {
        for (name, labels, value) in &snapshot.counters {
            if name == counter_name {
                let sorted_labels: Vec<_> =
                    labels.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
                data_points.push(SumDataPoint {
                    labels: sorted_labels,
                    value: *value as i64,
                    is_monotonic: true,
                });
            }
        }
    } else {
        // For UpDownCounters, we'd need to check gauges or a separate field
        // This is a simplification for the conformance test
        for (name, labels, value) in &snapshot.gauges {
            if name == counter_name {
                let sorted_labels: Vec<_> =
                    labels.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
                data_points.push(SumDataPoint {
                    labels: sorted_labels,
                    value: *value,
                    is_monotonic: false,
                });
            }
        }
    }

    // Sort for deterministic comparison
    data_points.sort_by(|a, b| a.labels.cmp(&b.labels));
    data_points
}

/// Extract Sum data points from opentelemetry-sdk ResourceMetrics.
fn extract_sum_data_from_sdk(
    resource_metrics: &[ResourceMetrics],
    counter_name: &str,
    expected_monotonic: bool,
) -> Vec<SumDataPoint> {
    let mut data_points = Vec::new();

    for resource_metric in resource_metrics {
        for scope_metric in resource_metric.scope_metrics() {
            for metric in scope_metric.metrics() {
                if metric.name() != counter_name {
                    continue;
                }

                match metric.data() {
                    AggregatedMetrics::U64(MetricData::Sum(sum_data)) => {
                        push_sum_data_points(&mut data_points, sum_data, |value| {
                            i64::try_from(value).ok()
                        });
                    }
                    AggregatedMetrics::I64(MetricData::Sum(sum_data)) => {
                        push_sum_data_points(&mut data_points, sum_data, Some);
                    }
                    AggregatedMetrics::F64(MetricData::Sum(sum_data)) => {
                        push_sum_data_points(&mut data_points, sum_data, f64_to_i64);
                    }
                    _ => {
                        let _ = expected_monotonic;
                    }
                }
            }
        }
    }

    // Sort for deterministic comparison
    data_points.sort_by(|a, b| a.labels.cmp(&b.labels));
    data_points
}

fn push_sum_data_points<T>(
    out: &mut Vec<SumDataPoint>,
    sum_data: &opentelemetry_sdk::metrics::data::Sum<T>,
    convert: impl Fn(T) -> Option<i64>,
) where
    T: Copy,
{
    for data_point in sum_data.data_points() {
        let labels: Vec<_> = data_point
            .attributes()
            .map(|kv| (kv.key.to_string(), kv.value.to_string()))
            .collect();

        if let Some(value) = convert(data_point.value()) {
            out.push(SumDataPoint {
                labels,
                value,
                is_monotonic: sum_data.is_monotonic(),
            });
        }
    }
}

fn f64_to_i64(value: f64) -> Option<i64> {
    if value.is_finite()
        && value.fract() == 0.0
        && value >= i64::MIN as f64
        && value <= i64::MAX as f64
    {
        Some(value as i64)
    } else {
        None
    }
}

/// Compare Sum data from our implementation vs reference.
fn compare_sum_data(
    our_data: &[SumDataPoint],
    reference_data: &[SumDataPoint],
    test_case: &SumAggregatorTestCase,
) -> Result<(), String> {
    if our_data.len() != reference_data.len() {
        return Err(format!(
            "Data point count mismatch: our={}, reference={}",
            our_data.len(),
            reference_data.len()
        ));
    }

    for (i, (our_point, ref_point)) in our_data.iter().zip(reference_data.iter()).enumerate() {
        // Check monotonicity flag
        if our_point.is_monotonic != ref_point.is_monotonic {
            return Err(format!(
                "Monotonicity flag mismatch at index {}: our={}, reference={}",
                i, our_point.is_monotonic, ref_point.is_monotonic
            ));
        }

        // Check expected monotonicity
        if our_point.is_monotonic != test_case.is_monotonic {
            return Err(format!(
                "Monotonicity flag wrong at index {}: expected={}, actual={}",
                i, test_case.is_monotonic, our_point.is_monotonic
            ));
        }

        // Check labels
        if our_point.labels != ref_point.labels {
            return Err(format!(
                "Labels mismatch at index {}: our={:?}, reference={:?}",
                i, our_point.labels, ref_point.labels
            ));
        }

        // Check values
        if our_point.value != ref_point.value {
            return Err(format!(
                "Value mismatch at index {}: our={}, reference={}",
                i, our_point.value, ref_point.value
            ));
        }
    }

    Ok(())
}

/// Test edge cases for Sum aggregator.
fn test_sum_aggregator_edge_cases(failed_tests: &mut Vec<(String, String)>) {
    // Test empty counter
    let empty_case = SumAggregatorTestCase {
        name: "empty_counter",
        counter_name: "empty_test",
        data_points: vec![],
        is_monotonic: true,
        description: "Empty counter with no data points",
    };

    let our_data = test_our_sum_aggregator(&empty_case);
    let reference_data = test_reference_sum_aggregator(&empty_case);

    if let Err(error) = compare_sum_data(&our_data, &reference_data, &empty_case) {
        failed_tests.push(("empty_counter".to_string(), error));
    } else {
        println!("    ✅ empty_counter");
    }

    // Test accumulation consistency
    let accumulation_case = SumAggregatorTestCase {
        name: "accumulation_consistency",
        counter_name: "accumulation_test",
        data_points: vec![
            (vec![("key", "same")], 10),
            (vec![("key", "same")], 20),
            (vec![("key", "same")], 5),
        ],
        is_monotonic: true,
        description: "Multiple increments to same label set",
    };

    let our_data = test_our_sum_aggregator(&accumulation_case);
    let reference_data = test_reference_sum_aggregator(&accumulation_case);

    if let Err(error) = compare_sum_data(&our_data, &reference_data, &accumulation_case) {
        failed_tests.push(("accumulation_consistency".to_string(), error));
    } else {
        println!("    ✅ accumulation_consistency");
    }
}