Skip to content

Commit 4e2c575

Browse files
[Tests] Fix flaky tests (#6734)
1 parent b0b4211 commit 4e2c575

File tree

2 files changed

+168
-194
lines changed

2 files changed

+168
-194
lines changed

test/OpenTelemetry.Tests/Metrics/MetricExemplarTests.cs

Lines changed: 74 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace OpenTelemetry.Metrics.Tests;
1212

1313
public class MetricExemplarTests : MetricTestsBase
1414
{
15-
private const int MaxTimeToAllowForFlush = 10000;
15+
private const int MaxTimeToAllowForFlush = 15_000;
1616

1717
[Theory]
1818
[InlineData(null, null, null)]
@@ -78,29 +78,27 @@ public void TestExemplarsCounter(MetricReaderTemporalityPreference temporality)
7878
.SetExemplarFilter(ExemplarFilterType.AlwaysOn)
7979
.AddView(i =>
8080
{
81-
if (i.Name.StartsWith("testCounter", StringComparison.Ordinal))
82-
{
83-
return new MetricStreamConfiguration
81+
return
82+
i.Name.StartsWith("testCounter", StringComparison.Ordinal) ?
83+
new MetricStreamConfiguration
8484
{
8585
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
86-
};
87-
}
88-
89-
return null;
86+
}
87+
: null;
9088
})
9189
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
9290
{
9391
metricReaderOptions.TemporalityPreference = temporality;
9492
}));
9593

9694
var measurementValues = GenerateRandomValues(2, false, null);
97-
foreach (var value in measurementValues)
95+
foreach (var (value, _) in measurementValues)
9896
{
99-
counterDouble.Add(value.Value);
100-
counterLong.Add((long)value.Value);
97+
counterDouble.Add(value);
98+
counterLong.Add((long)value);
10199
}
102100

103-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
101+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
104102

105103
ValidateFirstPhase("testCounterDouble", testStartTime, exportedItems, measurementValues, e => e.DoubleValue);
106104
ValidateFirstPhase("testCounterLong", testStartTime, exportedItems, measurementValues, e => e.LongValue);
@@ -112,15 +110,15 @@ public void TestExemplarsCounter(MetricReaderTemporalityPreference temporality)
112110
#endif
113111

114112
var secondMeasurementValues = GenerateRandomValues(1, true, measurementValues);
115-
foreach (var value in secondMeasurementValues)
113+
foreach (var (value, _) in secondMeasurementValues)
116114
{
117115
using var activity = new Activity("test");
118116
activity.Start();
119-
counterDouble.Add(value.Value);
120-
counterLong.Add((long)value.Value);
117+
counterDouble.Add(value);
118+
counterLong.Add((long)value);
121119
}
122120

123-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
121+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
124122

125123
ValidateSecondPhase("testCounterDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues, e => e.DoubleValue);
126124
ValidateSecondPhase("testCounterLong", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues, e => e.LongValue);
@@ -166,7 +164,7 @@ void ValidateSecondPhase(
166164
// First collect we saw Exemplar A & B
167165
// Second collect we saw Exemplar C but B remained in the reservoir
168166
Assert.Equal(2, exemplars.Count);
169-
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1).Take(1)).ToArray();
167+
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1).Take(1)];
170168
}
171169
else
172170
{
@@ -207,7 +205,7 @@ public void TestExemplarsObservable(MetricReaderTemporalityPreference temporalit
207205
metricReaderOptions.TemporalityPreference = temporality;
208206
}));
209207

210-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
208+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
211209

212210
ValidateFirstPhase("testGaugeDouble", testStartTime, exportedItems, measurementValues, e => e.DoubleValue);
213211
ValidateFirstPhase("testGaugeLong", testStartTime, exportedItems, measurementValues, e => e.LongValue);
@@ -222,7 +220,7 @@ public void TestExemplarsObservable(MetricReaderTemporalityPreference temporalit
222220
Thread.Sleep(10); // Compensates for low resolution timing in netfx.
223221
#endif
224222

225-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
223+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
226224

227225
ValidateSecondPhase("testGaugeDouble", testStartTime, exportedItems, measurementValues, e => e.DoubleValue);
228226
ValidateSecondPhase("testGaugeLong", testStartTime, exportedItems, measurementValues, e => e.LongValue);
@@ -306,21 +304,11 @@ public void TestExemplarsHistogramWithBuckets(MetricReaderTemporalityPreference
306304
.AddMeter(meter.Name)
307305
.AddView(i =>
308306
{
309-
if (i.Name.StartsWith("histogramWithBucketsAndMinMax", StringComparison.Ordinal))
310-
{
311-
return new ExplicitBucketHistogramConfiguration
312-
{
313-
Boundaries = buckets,
314-
};
315-
}
316-
else
307+
return new ExplicitBucketHistogramConfiguration
317308
{
318-
return new ExplicitBucketHistogramConfiguration
319-
{
320-
Boundaries = buckets,
321-
RecordMinMax = false,
322-
};
323-
}
309+
Boundaries = buckets,
310+
RecordMinMax = i.Name.StartsWith("histogramWithBucketsAndMinMax", StringComparison.Ordinal),
311+
};
324312
})
325313
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
326314
{
@@ -333,15 +321,15 @@ public void TestExemplarsHistogramWithBuckets(MetricReaderTemporalityPreference
333321
.Concat([2000.0])
334322
.Select(b => (Value: b, ExpectTraceId: false))
335323
.ToArray();
336-
foreach (var value in measurementValues)
324+
foreach (var (value, _) in measurementValues)
337325
{
338-
histogramWithBucketsAndMinMaxDouble.Record(value.Value);
339-
histogramWithBucketsDouble.Record(value.Value);
340-
histogramWithBucketsAndMinMaxLong.Record((long)value.Value);
341-
histogramWithBucketsLong.Record((long)value.Value);
326+
histogramWithBucketsAndMinMaxDouble.Record(value);
327+
histogramWithBucketsDouble.Record(value);
328+
histogramWithBucketsAndMinMaxLong.Record((long)value);
329+
histogramWithBucketsLong.Record((long)value);
342330
}
343331

344-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
332+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
345333

346334
ValidateFirstPhase("histogramWithBucketsAndMinMaxDouble", testStartTime, exportedItems, measurementValues);
347335
ValidateFirstPhase("histogramWithBucketsDouble", testStartTime, exportedItems, measurementValues);
@@ -355,17 +343,17 @@ public void TestExemplarsHistogramWithBuckets(MetricReaderTemporalityPreference
355343
#endif
356344

357345
var secondMeasurementValues = buckets.Take(1).Select(b => (Value: b, ExpectTraceId: true)).ToArray();
358-
foreach (var value in secondMeasurementValues)
346+
foreach (var (value, _) in secondMeasurementValues)
359347
{
360348
using var activity = new Activity("test");
361349
activity.Start();
362-
histogramWithBucketsAndMinMaxDouble.Record(value.Value);
363-
histogramWithBucketsDouble.Record(value.Value);
364-
histogramWithBucketsAndMinMaxLong.Record((long)value.Value);
365-
histogramWithBucketsLong.Record((long)value.Value);
350+
histogramWithBucketsAndMinMaxDouble.Record(value);
351+
histogramWithBucketsDouble.Record(value);
352+
histogramWithBucketsAndMinMaxLong.Record((long)value);
353+
histogramWithBucketsLong.Record((long)value);
366354
}
367355

368-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
356+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
369357

370358
ValidateScondPhase("histogramWithBucketsAndMinMaxDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
371359
ValidateScondPhase("histogramWithBucketsDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
@@ -408,7 +396,7 @@ static void ValidateScondPhase(
408396
if (temporality == MetricReaderTemporalityPreference.Cumulative)
409397
{
410398
Assert.Equal(11, exemplars.Count);
411-
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1)).ToArray();
399+
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1)];
412400
}
413401
else
414402
{
@@ -438,39 +426,28 @@ public void TestExemplarsHistogramWithoutBuckets(MetricReaderTemporalityPreferen
438426
.SetExemplarFilter(ExemplarFilterType.AlwaysOn)
439427
.AddView(i =>
440428
{
441-
if (i.Name.StartsWith("histogramWithoutBucketsAndMinMax", StringComparison.Ordinal))
429+
return new ExplicitBucketHistogramConfiguration
442430
{
443-
return new ExplicitBucketHistogramConfiguration
444-
{
445-
Boundaries = [],
446-
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
447-
};
448-
}
449-
else
450-
{
451-
return new ExplicitBucketHistogramConfiguration
452-
{
453-
Boundaries = [],
454-
RecordMinMax = false,
455-
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
456-
};
457-
}
431+
Boundaries = [],
432+
RecordMinMax = i.Name.StartsWith("histogramWithoutBucketsAndMinMax", StringComparison.Ordinal),
433+
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
434+
};
458435
})
459436
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
460437
{
461438
metricReaderOptions.TemporalityPreference = temporality;
462439
}));
463440

464441
var measurementValues = GenerateRandomValues(2, false, null);
465-
foreach (var value in measurementValues)
442+
foreach (var (value, _) in measurementValues)
466443
{
467-
histogramWithoutBucketsAndMinMaxDouble.Record(value.Value);
468-
histogramWithoutBucketsDouble.Record(value.Value);
469-
histogramWithoutBucketsAndMinMaxLong.Record((long)value.Value);
470-
histogramWithoutBucketsLong.Record((long)value.Value);
444+
histogramWithoutBucketsAndMinMaxDouble.Record(value);
445+
histogramWithoutBucketsDouble.Record(value);
446+
histogramWithoutBucketsAndMinMaxLong.Record((long)value);
447+
histogramWithoutBucketsLong.Record((long)value);
471448
}
472449

473-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
450+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
474451

475452
ValidateFirstPhase("histogramWithoutBucketsAndMinMaxDouble", testStartTime, exportedItems, measurementValues);
476453
ValidateFirstPhase("histogramWithoutBucketsDouble", testStartTime, exportedItems, measurementValues);
@@ -484,17 +461,17 @@ public void TestExemplarsHistogramWithoutBuckets(MetricReaderTemporalityPreferen
484461
#endif
485462

486463
var secondMeasurementValues = GenerateRandomValues(1, true, measurementValues);
487-
foreach (var value in secondMeasurementValues)
464+
foreach (var (value, _) in secondMeasurementValues)
488465
{
489466
using var activity = new Activity("test");
490467
activity.Start();
491-
histogramWithoutBucketsAndMinMaxDouble.Record(value.Value);
492-
histogramWithoutBucketsDouble.Record(value.Value);
493-
histogramWithoutBucketsAndMinMaxLong.Record((long)value.Value);
494-
histogramWithoutBucketsLong.Record((long)value.Value);
468+
histogramWithoutBucketsAndMinMaxDouble.Record(value);
469+
histogramWithoutBucketsDouble.Record(value);
470+
histogramWithoutBucketsAndMinMaxLong.Record((long)value);
471+
histogramWithoutBucketsLong.Record((long)value);
495472
}
496473

497-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
474+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
498475

499476
ValidateSecondPhase("histogramWithoutBucketsAndMinMaxDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
500477
ValidateSecondPhase("histogramWithoutBucketsDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
@@ -537,7 +514,7 @@ static void ValidateSecondPhase(
537514
if (temporality == MetricReaderTemporalityPreference.Cumulative)
538515
{
539516
Assert.Equal(2, exemplars.Count);
540-
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1)).ToArray();
517+
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1)];
541518
}
542519
else
543520
{
@@ -567,33 +544,27 @@ public void TestExemplarsExponentialHistogram(MetricReaderTemporalityPreference
567544
.SetExemplarFilter(ExemplarFilterType.AlwaysOn)
568545
.AddView(i =>
569546
{
570-
if (i.Name.StartsWith("exponentialHistogramWithMinMax", StringComparison.Ordinal))
571-
{
572-
return new Base2ExponentialBucketHistogramConfiguration();
573-
}
574-
else
547+
return new Base2ExponentialBucketHistogramConfiguration()
575548
{
576-
return new Base2ExponentialBucketHistogramConfiguration()
577-
{
578-
RecordMinMax = false,
579-
};
580-
}
549+
RecordMinMax = i.Name.StartsWith("exponentialHistogramWithMinMax", StringComparison.Ordinal),
550+
};
581551
})
582552
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
583553
{
584554
metricReaderOptions.TemporalityPreference = temporality;
585555
}));
586556

587557
var measurementValues = GenerateRandomValues(20, false, null);
588-
foreach (var value in measurementValues)
558+
559+
foreach (var (value, _) in measurementValues)
589560
{
590-
exponentialHistogramWithMinMaxDouble.Record(value.Value);
591-
exponentialHistogramDouble.Record(value.Value);
592-
exponentialHistogramWithMinMaxLong.Record((long)value.Value);
593-
exponentialHistogramLong.Record((long)value.Value);
561+
exponentialHistogramWithMinMaxDouble.Record(value);
562+
exponentialHistogramDouble.Record(value);
563+
exponentialHistogramWithMinMaxLong.Record((long)value);
564+
exponentialHistogramLong.Record((long)value);
594565
}
595566

596-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
567+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
597568

598569
ValidateFirstPhase("exponentialHistogramWithMinMaxDouble", testStartTime, exportedItems, measurementValues);
599570
ValidateFirstPhase("exponentialHistogramDouble", testStartTime, exportedItems, measurementValues);
@@ -607,17 +578,17 @@ public void TestExemplarsExponentialHistogram(MetricReaderTemporalityPreference
607578
#endif
608579

609580
var secondMeasurementValues = GenerateRandomValues(1, true, measurementValues);
610-
foreach (var value in secondMeasurementValues)
581+
foreach (var (value, _) in secondMeasurementValues)
611582
{
612583
using var activity = new Activity("test");
613584
activity.Start();
614-
exponentialHistogramWithMinMaxDouble.Record(value.Value);
615-
exponentialHistogramDouble.Record(value.Value);
616-
exponentialHistogramWithMinMaxLong.Record((long)value.Value);
617-
exponentialHistogramLong.Record((long)value.Value);
585+
exponentialHistogramWithMinMaxDouble.Record(value);
586+
exponentialHistogramDouble.Record(value);
587+
exponentialHistogramWithMinMaxLong.Record((long)value);
588+
exponentialHistogramLong.Record((long)value);
618589
}
619590

620-
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
591+
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));
621592

622593
ValidateSecondPhase("exponentialHistogramWithMinMaxDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
623594
ValidateSecondPhase("exponentialHistogramDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
@@ -660,7 +631,7 @@ static void ValidateSecondPhase(
660631
if (temporality == MetricReaderTemporalityPreference.Cumulative)
661632
{
662633
Assert.Equal(20, exemplars.Count);
663-
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1).Take(19)).ToArray();
634+
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1).Take(19)];
664635
}
665636
else
666637
{
@@ -742,12 +713,10 @@ public void TestExemplarsFilterTags(bool enableTagFiltering)
742713
TagKeys = enableTagFiltering ? ["key1"] : null,
743714
ExemplarReservoirFactory = () =>
744715
{
745-
if (testExemplarReservoir != null)
746-
{
747-
throw new InvalidOperationException();
748-
}
749-
750-
return testExemplarReservoir = new TestExemplarReservoir();
716+
return
717+
testExemplarReservoir != null ?
718+
throw new InvalidOperationException() :
719+
(ExemplarReservoir)(testExemplarReservoir = new TestExemplarReservoir());
751720
},
752721
})
753722
.AddInMemoryExporter(exportedItems));
@@ -873,8 +842,6 @@ public override void Offer(in ExemplarMeasurement<double> measurement)
873842
}
874843

875844
public override void Offer(in ExemplarMeasurement<long> measurement)
876-
{
877-
throw new NotSupportedException();
878-
}
845+
=> throw new NotSupportedException();
879846
}
880847
}

0 commit comments

Comments
 (0)