Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 74 additions & 107 deletions test/OpenTelemetry.Tests/Metrics/MetricExemplarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenTelemetry.Metrics.Tests;

public class MetricExemplarTests : MetricTestsBase
{
private const int MaxTimeToAllowForFlush = 10000;
private const int MaxTimeToAllowForFlush = 15_000;

[Theory]
[InlineData(null, null, null)]
Expand Down Expand Up @@ -78,29 +78,27 @@ public void TestExemplarsCounter(MetricReaderTemporalityPreference temporality)
.SetExemplarFilter(ExemplarFilterType.AlwaysOn)
.AddView(i =>
{
if (i.Name.StartsWith("testCounter", StringComparison.Ordinal))
{
return new MetricStreamConfiguration
return
i.Name.StartsWith("testCounter", StringComparison.Ordinal) ?
new MetricStreamConfiguration
{
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
};
}

return null;
}
: null;
})
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.TemporalityPreference = temporality;
}));

var measurementValues = GenerateRandomValues(2, false, null);
foreach (var value in measurementValues)
foreach (var (value, _) in measurementValues)
{
counterDouble.Add(value.Value);
counterLong.Add((long)value.Value);
counterDouble.Add(value);
counterLong.Add((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

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

var secondMeasurementValues = GenerateRandomValues(1, true, measurementValues);
foreach (var value in secondMeasurementValues)
foreach (var (value, _) in secondMeasurementValues)
{
using var activity = new Activity("test");
activity.Start();
counterDouble.Add(value.Value);
counterLong.Add((long)value.Value);
counterDouble.Add(value);
counterLong.Add((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

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

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

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

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateSecondPhase("testGaugeDouble", testStartTime, exportedItems, measurementValues, e => e.DoubleValue);
ValidateSecondPhase("testGaugeLong", testStartTime, exportedItems, measurementValues, e => e.LongValue);
Expand Down Expand Up @@ -306,21 +304,11 @@ public void TestExemplarsHistogramWithBuckets(MetricReaderTemporalityPreference
.AddMeter(meter.Name)
.AddView(i =>
{
if (i.Name.StartsWith("histogramWithBucketsAndMinMax", StringComparison.Ordinal))
{
return new ExplicitBucketHistogramConfiguration
{
Boundaries = buckets,
};
}
else
return new ExplicitBucketHistogramConfiguration
{
return new ExplicitBucketHistogramConfiguration
{
Boundaries = buckets,
RecordMinMax = false,
};
}
Boundaries = buckets,
RecordMinMax = i.Name.StartsWith("histogramWithBucketsAndMinMax", StringComparison.Ordinal),
};
})
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
Expand All @@ -333,15 +321,15 @@ public void TestExemplarsHistogramWithBuckets(MetricReaderTemporalityPreference
.Concat([2000.0])
.Select(b => (Value: b, ExpectTraceId: false))
.ToArray();
foreach (var value in measurementValues)
foreach (var (value, _) in measurementValues)
{
histogramWithBucketsAndMinMaxDouble.Record(value.Value);
histogramWithBucketsDouble.Record(value.Value);
histogramWithBucketsAndMinMaxLong.Record((long)value.Value);
histogramWithBucketsLong.Record((long)value.Value);
histogramWithBucketsAndMinMaxDouble.Record(value);
histogramWithBucketsDouble.Record(value);
histogramWithBucketsAndMinMaxLong.Record((long)value);
histogramWithBucketsLong.Record((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateFirstPhase("histogramWithBucketsAndMinMaxDouble", testStartTime, exportedItems, measurementValues);
ValidateFirstPhase("histogramWithBucketsDouble", testStartTime, exportedItems, measurementValues);
Expand All @@ -355,17 +343,17 @@ public void TestExemplarsHistogramWithBuckets(MetricReaderTemporalityPreference
#endif

var secondMeasurementValues = buckets.Take(1).Select(b => (Value: b, ExpectTraceId: true)).ToArray();
foreach (var value in secondMeasurementValues)
foreach (var (value, _) in secondMeasurementValues)
{
using var activity = new Activity("test");
activity.Start();
histogramWithBucketsAndMinMaxDouble.Record(value.Value);
histogramWithBucketsDouble.Record(value.Value);
histogramWithBucketsAndMinMaxLong.Record((long)value.Value);
histogramWithBucketsLong.Record((long)value.Value);
histogramWithBucketsAndMinMaxDouble.Record(value);
histogramWithBucketsDouble.Record(value);
histogramWithBucketsAndMinMaxLong.Record((long)value);
histogramWithBucketsLong.Record((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateScondPhase("histogramWithBucketsAndMinMaxDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
ValidateScondPhase("histogramWithBucketsDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
Expand Down Expand Up @@ -408,7 +396,7 @@ static void ValidateScondPhase(
if (temporality == MetricReaderTemporalityPreference.Cumulative)
{
Assert.Equal(11, exemplars.Count);
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1)).ToArray();
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1)];
}
else
{
Expand Down Expand Up @@ -438,39 +426,28 @@ public void TestExemplarsHistogramWithoutBuckets(MetricReaderTemporalityPreferen
.SetExemplarFilter(ExemplarFilterType.AlwaysOn)
.AddView(i =>
{
if (i.Name.StartsWith("histogramWithoutBucketsAndMinMax", StringComparison.Ordinal))
return new ExplicitBucketHistogramConfiguration
{
return new ExplicitBucketHistogramConfiguration
{
Boundaries = [],
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
};
}
else
{
return new ExplicitBucketHistogramConfiguration
{
Boundaries = [],
RecordMinMax = false,
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
};
}
Boundaries = [],
RecordMinMax = i.Name.StartsWith("histogramWithoutBucketsAndMinMax", StringComparison.Ordinal),
ExemplarReservoirFactory = () => new SimpleFixedSizeExemplarReservoir(3),
};
})
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.TemporalityPreference = temporality;
}));

var measurementValues = GenerateRandomValues(2, false, null);
foreach (var value in measurementValues)
foreach (var (value, _) in measurementValues)
{
histogramWithoutBucketsAndMinMaxDouble.Record(value.Value);
histogramWithoutBucketsDouble.Record(value.Value);
histogramWithoutBucketsAndMinMaxLong.Record((long)value.Value);
histogramWithoutBucketsLong.Record((long)value.Value);
histogramWithoutBucketsAndMinMaxDouble.Record(value);
histogramWithoutBucketsDouble.Record(value);
histogramWithoutBucketsAndMinMaxLong.Record((long)value);
histogramWithoutBucketsLong.Record((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateFirstPhase("histogramWithoutBucketsAndMinMaxDouble", testStartTime, exportedItems, measurementValues);
ValidateFirstPhase("histogramWithoutBucketsDouble", testStartTime, exportedItems, measurementValues);
Expand All @@ -484,17 +461,17 @@ public void TestExemplarsHistogramWithoutBuckets(MetricReaderTemporalityPreferen
#endif

var secondMeasurementValues = GenerateRandomValues(1, true, measurementValues);
foreach (var value in secondMeasurementValues)
foreach (var (value, _) in secondMeasurementValues)
{
using var activity = new Activity("test");
activity.Start();
histogramWithoutBucketsAndMinMaxDouble.Record(value.Value);
histogramWithoutBucketsDouble.Record(value.Value);
histogramWithoutBucketsAndMinMaxLong.Record((long)value.Value);
histogramWithoutBucketsLong.Record((long)value.Value);
histogramWithoutBucketsAndMinMaxDouble.Record(value);
histogramWithoutBucketsDouble.Record(value);
histogramWithoutBucketsAndMinMaxLong.Record((long)value);
histogramWithoutBucketsLong.Record((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateSecondPhase("histogramWithoutBucketsAndMinMaxDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
ValidateSecondPhase("histogramWithoutBucketsDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
Expand Down Expand Up @@ -537,7 +514,7 @@ static void ValidateSecondPhase(
if (temporality == MetricReaderTemporalityPreference.Cumulative)
{
Assert.Equal(2, exemplars.Count);
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1)).ToArray();
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1)];
}
else
{
Expand Down Expand Up @@ -567,33 +544,27 @@ public void TestExemplarsExponentialHistogram(MetricReaderTemporalityPreference
.SetExemplarFilter(ExemplarFilterType.AlwaysOn)
.AddView(i =>
{
if (i.Name.StartsWith("exponentialHistogramWithMinMax", StringComparison.Ordinal))
{
return new Base2ExponentialBucketHistogramConfiguration();
}
else
return new Base2ExponentialBucketHistogramConfiguration()
{
return new Base2ExponentialBucketHistogramConfiguration()
{
RecordMinMax = false,
};
}
RecordMinMax = i.Name.StartsWith("exponentialHistogramWithMinMax", StringComparison.Ordinal),
};
})
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.TemporalityPreference = temporality;
}));

var measurementValues = GenerateRandomValues(20, false, null);
foreach (var value in measurementValues)

foreach (var (value, _) in measurementValues)
{
exponentialHistogramWithMinMaxDouble.Record(value.Value);
exponentialHistogramDouble.Record(value.Value);
exponentialHistogramWithMinMaxLong.Record((long)value.Value);
exponentialHistogramLong.Record((long)value.Value);
exponentialHistogramWithMinMaxDouble.Record(value);
exponentialHistogramDouble.Record(value);
exponentialHistogramWithMinMaxLong.Record((long)value);
exponentialHistogramLong.Record((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateFirstPhase("exponentialHistogramWithMinMaxDouble", testStartTime, exportedItems, measurementValues);
ValidateFirstPhase("exponentialHistogramDouble", testStartTime, exportedItems, measurementValues);
Expand All @@ -607,17 +578,17 @@ public void TestExemplarsExponentialHistogram(MetricReaderTemporalityPreference
#endif

var secondMeasurementValues = GenerateRandomValues(1, true, measurementValues);
foreach (var value in secondMeasurementValues)
foreach (var (value, _) in secondMeasurementValues)
{
using var activity = new Activity("test");
activity.Start();
exponentialHistogramWithMinMaxDouble.Record(value.Value);
exponentialHistogramDouble.Record(value.Value);
exponentialHistogramWithMinMaxLong.Record((long)value.Value);
exponentialHistogramLong.Record((long)value.Value);
exponentialHistogramWithMinMaxDouble.Record(value);
exponentialHistogramDouble.Record(value);
exponentialHistogramWithMinMaxLong.Record((long)value);
exponentialHistogramLong.Record((long)value);
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.True(meterProvider.ForceFlush(MaxTimeToAllowForFlush));

ValidateSecondPhase("exponentialHistogramWithMinMaxDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
ValidateSecondPhase("exponentialHistogramDouble", temporality, testStartTime, exportedItems, measurementValues, secondMeasurementValues);
Expand Down Expand Up @@ -660,7 +631,7 @@ static void ValidateSecondPhase(
if (temporality == MetricReaderTemporalityPreference.Cumulative)
{
Assert.Equal(20, exemplars.Count);
secondMeasurementValues = secondMeasurementValues.Concat(firstMeasurementValues.Skip(1).Take(19)).ToArray();
secondMeasurementValues = [.. secondMeasurementValues, .. firstMeasurementValues.Skip(1).Take(19)];
}
else
{
Expand Down Expand Up @@ -742,12 +713,10 @@ public void TestExemplarsFilterTags(bool enableTagFiltering)
TagKeys = enableTagFiltering ? ["key1"] : null,
ExemplarReservoirFactory = () =>
{
if (testExemplarReservoir != null)
{
throw new InvalidOperationException();
}

return testExemplarReservoir = new TestExemplarReservoir();
return
testExemplarReservoir != null ?
throw new InvalidOperationException() :
(ExemplarReservoir)(testExemplarReservoir = new TestExemplarReservoir());
},
})
.AddInMemoryExporter(exportedItems));
Expand Down Expand Up @@ -873,8 +842,6 @@ public override void Offer(in ExemplarMeasurement<double> measurement)
}

public override void Offer(in ExemplarMeasurement<long> measurement)
{
throw new NotSupportedException();
}
=> throw new NotSupportedException();
}
}
Loading
Loading