-
Notifications
You must be signed in to change notification settings - Fork 746
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
Fix NullReferenceException caught by SDK when metric has a tag with a null value #3325
Merged
cijothomas
merged 8 commits into
open-telemetry:main
from
alanwest:alanwest/null-valued-dimensions
Jun 3, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fafaeed
Add test recording a metric with a null-valued dimension
alanwest da6b615
Fix null-valued tags
alanwest 4a421ec
Assert.Null instead of Assert.Equal
alanwest bfe2c3e
Compute hash code in constructor
alanwest 3c38408
Update changelog
alanwest 82f20af
Merge branch 'main' into alanwest/null-valued-dimensions
alanwest 3d57791
Merge branch 'main' into alanwest/null-valued-dimensions
alanwest a155e08
Merge branch 'main' into alanwest/null-valued-dimensions
cijothomas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,39 @@ public MetricApiTest(ITestOutputHelper output) | |
this.output = output; | ||
} | ||
|
||
[Fact] | ||
public void MeasurementWithNullValuedTag() | ||
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. 💯 |
||
{ | ||
using var meter = new Meter(Utils.GetCurrentMethodName()); | ||
var exportedItems = new List<Metric>(); | ||
using var meterProvider = Sdk.CreateMeterProviderBuilder() | ||
.AddMeter(meter.Name) | ||
.AddInMemoryExporter(exportedItems) | ||
.Build(); | ||
|
||
var counter = meter.CreateCounter<long>("myCounter"); | ||
counter.Add(100, new KeyValuePair<string, object>("tagWithNullValue", null)); | ||
|
||
meterProvider.ForceFlush(MaxTimeToAllowForFlush); | ||
Assert.Single(exportedItems); | ||
var metric = exportedItems[0]; | ||
Assert.Equal("myCounter", metric.Name); | ||
List<MetricPoint> metricPoints = new List<MetricPoint>(); | ||
foreach (ref readonly var mp in metric.GetMetricPoints()) | ||
{ | ||
metricPoints.Add(mp); | ||
} | ||
|
||
Assert.Single(metricPoints); | ||
var metricPoint = metricPoints[0]; | ||
Assert.Equal(100, metricPoint.GetSumLong()); | ||
Assert.Equal(1, metricPoint.Tags.Count); | ||
var tagEnumerator = metricPoint.Tags.GetEnumerator(); | ||
tagEnumerator.MoveNext(); | ||
Assert.Equal("tagWithNullValue", tagEnumerator.Current.Key); | ||
Assert.Null(tagEnumerator.Current.Value); | ||
} | ||
|
||
[Fact] | ||
public void ObserverCallbackTest() | ||
{ | ||
|
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.
Is there any concern that the underlying values of Keys or Values will get changed since the array indexes can still be written to but the hash won't be updated?
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.
@tgrieger-sf No because the the keys and values never do change. This struct is internal and instances of it are only used within our AggregatorStore for looking up metric points
opentelemetry-dotnet/src/OpenTelemetry/Metrics/AggregatorStore.cs
Line 33 in a19d106
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.
If that's the case, is there any benefit in using
ReadOnlySpan<string/object>
for Keys and Values to make it explicit?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.
ReadOnlySpan
is aref struct
and cannot be used a valid type for a dictionary as it has to live on stack.