-
Notifications
You must be signed in to change notification settings - Fork 772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support UpDownCounter and ObservableUpDownCounter #3606
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9a8e4a1
Support UpDownCounter
alanwest e2fb633
Merge remote-tracking branch 'upstream/main' into alanwest/updown
alanwest e254f4d
Update public API
alanwest ce5f7eb
Add Metric API tests
alanwest 9e190a5
Add OTLP exporter tests
alanwest e339d3e
Merge branch 'main' into alanwest/updown
alanwest 4934584
Fix asserts
alanwest 9e9c623
Merge branch 'main' into alanwest/updown
alanwest b605ddf
UpDownCounter support for Prometheus
alanwest 0872c6d
Minor assert cleanup
alanwest 891d553
Remove suspicious compiler directive
alanwest f5a0e25
Update public API
alanwest 23b2db5
Update changelog
alanwest cf9d50c
Not suspicious I guess
alanwest 75104a9
Merge branch 'main' into alanwest/updown
alanwest 0518539
Rename consts
alanwest 65fd1ee
Be more bitwise
alanwest 3688d7e
:-1:
alanwest 2bd8778
Time is :moneybag:
alanwest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,12 +271,12 @@ public void TestGaugeToOtlpMetric(string name, string description, string unit, | |
} | ||
|
||
[Theory] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative, true)] | ||
[InlineData("test_counter", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative, true)] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, true)] | ||
[InlineData("test_counter", "description", "unit", 123L, null, MetricReaderTemporalityPreference.Cumulative, true)] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, true, "key1", "value1", "key2", 123)] | ||
public void TestCounterToOtlpMetric(string name, string description, string unit, long? longValue, double? doubleValue, MetricReaderTemporalityPreference aggregationTemporality, bool isMonotonic, params object[] keysValues) | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_counter", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta)] | ||
[InlineData("test_counter", "description", "unit", 123L, null, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, "key1", "value1", "key2", 123)] | ||
public void TestCounterToOtlpMetric(string name, string description, string unit, long? longValue, double? doubleValue, MetricReaderTemporalityPreference aggregationTemporality, params object[] keysValues) | ||
{ | ||
var metrics = new List<Metric>(); | ||
|
||
|
@@ -324,7 +324,7 @@ public void TestCounterToOtlpMetric(string name, string description, string unit | |
Assert.Null(actual.ExponentialHistogram); | ||
Assert.Null(actual.Summary); | ||
|
||
Assert.Equal(isMonotonic, actual.Sum.IsMonotonic); | ||
Assert.True(actual.Sum.IsMonotonic); | ||
|
||
var otlpAggregationTemporality = aggregationTemporality == MetricReaderTemporalityPreference.Cumulative | ||
? OtlpMetrics.AggregationTemporality.Cumulative | ||
|
@@ -359,6 +359,95 @@ public void TestCounterToOtlpMetric(string name, string description, string unit | |
Assert.Empty(dataPoint.Exemplars); | ||
} | ||
|
||
[Theory] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_counter", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta)] | ||
[InlineData("test_counter", "description", "unit", 123L, null, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, "key1", "value1", "key2", 123)] | ||
public void TestUpDownCounterToOtlpMetric(string name, string description, string unit, long? longValue, double? doubleValue, MetricReaderTemporalityPreference aggregationTemporality, params object[] keysValues) | ||
{ | ||
var metrics = new List<Metric>(); | ||
|
||
using var meter = new Meter(Utils.GetCurrentMethodName()); | ||
using var provider = Sdk.CreateMeterProviderBuilder() | ||
.AddMeter(meter.Name) | ||
.AddInMemoryExporter(metrics, metricReaderOptions => | ||
{ | ||
metricReaderOptions.TemporalityPreference = aggregationTemporality; | ||
}) | ||
.Build(); | ||
|
||
var attributes = ToAttributes(keysValues).ToArray(); | ||
if (longValue.HasValue) | ||
{ | ||
var counter = meter.CreateUpDownCounter<long>(name, unit, description); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: var counterUpDownCounter |
||
counter.Add(longValue.Value, attributes); | ||
} | ||
else | ||
{ | ||
var counter = meter.CreateUpDownCounter<double>(name, unit, description); | ||
counter.Add(doubleValue.Value, attributes); | ||
} | ||
|
||
provider.ForceFlush(); | ||
|
||
var batch = new Batch<Metric>(metrics.ToArray(), metrics.Count); | ||
|
||
var request = new OtlpCollector.ExportMetricsServiceRequest(); | ||
request.AddMetrics(ResourceBuilder.CreateEmpty().Build().ToOtlpResource(), batch); | ||
|
||
var resourceMetric = request.ResourceMetrics.Single(); | ||
var scopeMetrics = resourceMetric.ScopeMetrics.Single(); | ||
var actual = scopeMetrics.Metrics.Single(); | ||
|
||
Assert.Equal(name, actual.Name); | ||
Assert.Equal(description ?? string.Empty, actual.Description); | ||
Assert.Equal(unit ?? string.Empty, actual.Unit); | ||
|
||
Assert.Equal(OtlpMetrics.Metric.DataOneofCase.Sum, actual.DataCase); | ||
|
||
Assert.Null(actual.Gauge); | ||
Assert.NotNull(actual.Sum); | ||
Assert.Null(actual.Histogram); | ||
Assert.Null(actual.ExponentialHistogram); | ||
Assert.Null(actual.Summary); | ||
|
||
Assert.False(actual.Sum.IsMonotonic); | ||
|
||
var otlpAggregationTemporality = aggregationTemporality == MetricReaderTemporalityPreference.Cumulative | ||
? OtlpMetrics.AggregationTemporality.Cumulative | ||
: OtlpMetrics.AggregationTemporality.Cumulative; | ||
Assert.Equal(otlpAggregationTemporality, actual.Sum.AggregationTemporality); | ||
|
||
Assert.Single(actual.Sum.DataPoints); | ||
var dataPoint = actual.Sum.DataPoints.First(); | ||
Assert.True(dataPoint.StartTimeUnixNano > 0); | ||
Assert.True(dataPoint.TimeUnixNano > 0); | ||
|
||
if (longValue.HasValue) | ||
{ | ||
Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsInt, dataPoint.ValueCase); | ||
Assert.Equal(longValue, dataPoint.AsInt); | ||
} | ||
else | ||
{ | ||
Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsDouble, dataPoint.ValueCase); | ||
Assert.Equal(doubleValue, dataPoint.AsDouble); | ||
} | ||
|
||
if (attributes.Length > 0) | ||
{ | ||
OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes); | ||
} | ||
else | ||
{ | ||
Assert.Empty(dataPoint.Attributes); | ||
} | ||
|
||
Assert.Empty(dataPoint.Exemplars); | ||
} | ||
|
||
[Theory] | ||
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative)] | ||
[InlineData("test_histogram", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: it might be good add a comment here to explain why. (like UpDownCounters are exported as Prometheus Gauge etc.)