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
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix issue where a measurement would be dropped when recording it with a
null-valued tag.
([#3325](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3325))

## 1.3.0

Released 2022-Jun-03
Expand Down
28 changes: 14 additions & 14 deletions src/OpenTelemetry/Metrics/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,23 @@ namespace OpenTelemetry.Metrics
{
internal readonly struct Tags : IEquatable<Tags>
{
private readonly int hashCode;

public Tags(string[] keys, object[] values)
{
this.Keys = keys;
this.Values = values;

unchecked
{
var hash = 17;
for (int i = 0; i < this.Keys.Length; i++)
{
hash = (hash * 31) + this.Keys[i].GetHashCode() + this.Values[i]?.GetHashCode() ?? 0;
}

this.hashCode = hash;
}
Comment on lines +30 to +39

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?

Copy link
Member Author

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

private readonly ConcurrentDictionary<Tags, int> tagsToMetricPointIndexDictionary =
.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadOnlySpan is a ref struct and cannot be used a valid type for a dictionary as it has to live on stack.

}

public readonly string[] Keys { get; }
Expand Down Expand Up @@ -78,19 +91,6 @@ public readonly bool Equals(Tags other)
return true;
}

public override readonly int GetHashCode()
{
int hash = 17;

unchecked
{
for (int i = 0; i < this.Keys.Length; i++)
{
hash = (hash * 31) + this.Keys[i].GetHashCode() + this.Values[i].GetHashCode();
}
}

return hash;
}
public override readonly int GetHashCode() => this.hashCode;
}
}
33 changes: 33 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ public MetricApiTest(ITestOutputHelper output)
this.output = output;
}

[Fact]
public void MeasurementWithNullValuedTag()
Copy link
Member

Choose a reason for hiding this comment

The 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()
{
Expand Down