Skip to content
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

[sdk-metrics] Remove ExemplarFilter classes #5408

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
248 changes: 104 additions & 144 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ internal sealed class AggregatorStore
internal readonly Func<ExemplarReservoir?>? ExemplarReservoirFactory;
internal long DroppedMeasurements = 0;

private const ExemplarFilterType DefaultExemplarFilter = ExemplarFilterType.AlwaysOff;
private static readonly string MetricPointCapHitFixMessage = "Consider opting in for the experimental SDK feature to emit all the throttled metrics under the overflow attribute by setting env variable OTEL_DOTNET_EXPERIMENTAL_METRICS_EMIT_OVERFLOW_ATTRIBUTE = true. You could also modify instrumentation to reduce the number of unique key/value pair combinations. Or use Views to drop unwanted tags. Or use MeterProviderBuilder.SetMaxMetricPointsPerMetricStream to set higher limit.";
private static readonly Comparison<KeyValuePair<string, object?>> DimensionComparisonDelegate = (x, y) => x.Key.CompareTo(y.Key);
private static readonly ExemplarFilter DefaultExemplarFilter = new AlwaysOffExemplarFilter();

private readonly object lockZeroTags = new();
private readonly object lockOverflowTag = new();
Expand All @@ -44,7 +44,7 @@ internal sealed class AggregatorStore
private readonly int exponentialHistogramMaxScale;
private readonly UpdateLongDelegate updateLongCallback;
private readonly UpdateDoubleDelegate updateDoubleCallback;
private readonly ExemplarFilter exemplarFilter;
private readonly ExemplarFilterType exemplarFilter;
private readonly Func<KeyValuePair<string, object?>[], int, int> lookupAggregatorStore;

private int metricPointIndex = 0;
Expand All @@ -60,7 +60,7 @@ internal AggregatorStore(
int cardinalityLimit,
bool emitOverflowAttribute,
bool shouldReclaimUnusedMetricPoints,
ExemplarFilter? exemplarFilter = null,
ExemplarFilterType? exemplarFilter = null,
Func<ExemplarReservoir?>? exemplarReservoirFactory = null)
{
this.name = metricStreamIdentity.InstrumentName;
Expand Down Expand Up @@ -144,17 +144,33 @@ internal bool IsExemplarEnabled()
{
// Using this filter to indicate On/Off
// instead of another separate flag.
return this.exemplarFilter is not AlwaysOffExemplarFilter;
return this.exemplarFilter != ExemplarFilterType.AlwaysOff;
}

internal void Update(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
this.updateLongCallback(value, tags);
try
{
this.updateLongCallback(value, tags);
}
catch (Exception)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
}
}

internal void Update(double value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
this.updateDoubleCallback(value, tags);
try
{
this.updateDoubleCallback(value, tags);
}
catch (Exception)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
}
}

internal int Snapshot()
Expand Down Expand Up @@ -924,177 +940,121 @@ private int RemoveStaleEntriesAndGetAvailableMetricPointRare(LookupData lookupDa

private void UpdateLong(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
var index = this.FindMetricAggregatorsDefault(tags);
if (index < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);

if (this.EmitOverflowAttribute)
{
this.InitializeOverflowTagPointIfNotInitialized();
this.metricPoints[1].Update(value);
return;
}
else
{
if (Interlocked.CompareExchange(ref this.metricCapHitMessageLogged, 1, 0) == 0)
{
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, this.metricPointCapHitMessage, MetricPointCapHitFixMessage);
}

return;
}
}
var index = this.FindMetricAggregatorsDefault(tags);

// TODO: can special case built-in filters to be bit faster.
if (this.IsExemplarEnabled())
{
var shouldSample = this.exemplarFilter.ShouldSample(value, tags);
this.metricPoints[index].UpdateWithExemplar(value, tags: default, shouldSample);
}
else
{
this.metricPoints[index].Update(value);
}
}
catch (Exception)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
}
this.UpdateLongMetricPoint(index, value, tags: default);
}

private void UpdateLongCustomTags(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
var index = this.FindMetricAggregatorsCustomTag(tags);
if (index < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);
var index = this.FindMetricAggregatorsCustomTag(tags);

if (this.EmitOverflowAttribute)
{
this.InitializeOverflowTagPointIfNotInitialized();
this.metricPoints[1].Update(value);
return;
}
else
{
if (Interlocked.CompareExchange(ref this.metricCapHitMessageLogged, 1, 0) == 0)
{
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, this.metricPointCapHitMessage, MetricPointCapHitFixMessage);
}
this.UpdateLongMetricPoint(index, value, tags);
}

return;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateLongMetricPoint(int metricPointIndex, long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
if (metricPointIndex < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);

// TODO: can special case built-in filters to be bit faster.
if (this.IsExemplarEnabled())
if (this.EmitOverflowAttribute)
{
var shouldSample = this.exemplarFilter.ShouldSample(value, tags);
this.metricPoints[index].UpdateWithExemplar(value, tags, shouldSample);
this.InitializeOverflowTagPointIfNotInitialized();
this.metricPoints[1].Update(value);
}
else
else if (Interlocked.CompareExchange(ref this.metricCapHitMessageLogged, 1, 0) == 0)
{
this.metricPoints[index].Update(value);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, this.metricPointCapHitMessage, MetricPointCapHitFixMessage);
}

return;
}
catch (Exception)

switch (this.exemplarFilter)
Copy link
Contributor

Choose a reason for hiding this comment

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

It would probably be faster to use if than switch? If yes, we should optimize the non-exemplar updates.

Copy link
Contributor

Choose a reason for hiding this comment

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

Found an old benchmark saying that switch usually will be faster cause compiler will create jump table for switch statement: https://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx.
I guess the assembly generated for if/switch here will be identical given there are only 3 cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you know your most common condition, you can make that the very first check. That way you only have to check if(true) to execute the most common path. That might be faster than the jump table lookup. In fact, you can use a mixed approach as well. Something like this:

if (mostFrequentlyHitCondition)
{
   RunSomething();
}

switch (lessFrequentlyHitConditions)
{
case Condition1:

case Condition2:

...

default:

}

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the code to use an if block. But it has been interesting to try and prove what is best.

Here are some benchmarks run without PGO enabled:

Method ExemplarFilterType Mean Error StdDev Allocated
UpdateOldStyle AlwaysOff 2.458 ns 0.0722 ns 0.0676 ns -
UpdateInlineSwitchStyle AlwaysOff 1.572 ns 0.0366 ns 0.0342 ns -
UpdateInlineIfStyle AlwaysOff 1.353 ns 0.0457 ns 0.0626 ns -
UpdateOldStyle AlwaysOn 2.938 ns 0.0206 ns 0.0172 ns -
UpdateInlineSwitchStyle AlwaysOn 1.800 ns 0.0343 ns 0.0304 ns -
UpdateInlineIfStyle AlwaysOn 1.800 ns 0.0366 ns 0.0342 ns -
UpdateOldStyle TraceBased 4.666 ns 0.0629 ns 0.0588 ns -
UpdateInlineSwitchStyle TraceBased 3.297 ns 0.0470 ns 0.0417 ns -
UpdateInlineIfStyle TraceBased 3.198 ns 0.0382 ns 0.0319 ns -

Here are some benchmarks run with PGO enabled:

Method ExemplarFilterType Mean Error StdDev Allocated
UpdateOldStyle AlwaysOff 1.299 ns 0.0410 ns 0.0383 ns -
UpdateInlineSwitchStyle AlwaysOff 1.345 ns 0.0374 ns 0.0313 ns -
UpdateInlineIfStyle AlwaysOff 1.106 ns 0.0316 ns 0.0296 ns -
UpdateOldStyle AlwaysOn 1.359 ns 0.0141 ns 0.0132 ns -
UpdateInlineSwitchStyle AlwaysOn 1.373 ns 0.0514 ns 0.0481 ns -
UpdateInlineIfStyle AlwaysOn 1.325 ns 0.0310 ns 0.0275 ns -
UpdateOldStyle TraceBased 2.756 ns 0.0709 ns 0.0663 ns -
UpdateInlineSwitchStyle TraceBased 3.392 ns 0.0452 ns 0.0422 ns -
UpdateInlineIfStyle TraceBased 2.234 ns 0.0231 ns 0.0204 ns -

PGO is smart enough to see that one value is used repeatedly and optimize the code on its own for that value. It is great for what we're doing here.

I ended up going with the if because PGO may not be there in AOT deployments/older runtimes so it seems best to optimize for it not being there.

{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
case ExemplarFilterType.AlwaysOn:
this.metricPoints[metricPointIndex].UpdateWithExemplar(
value,
tags,
isSampled: true);
break;
case ExemplarFilterType.TraceBased:
this.metricPoints[metricPointIndex].UpdateWithExemplar(
value,
tags,
isSampled: Activity.Current?.Recorded ?? false);
break;
default:
#if DEBUG
if (this.exemplarFilter != ExemplarFilterType.AlwaysOff)
{
Debug.Fail("Unexpected ExemplarFilterType");
}
#endif
this.metricPoints[metricPointIndex].Update(value);
break;
}
}

private void UpdateDouble(double value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
var index = this.FindMetricAggregatorsDefault(tags);
if (index < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);
var index = this.FindMetricAggregatorsDefault(tags);

if (this.EmitOverflowAttribute)
{
this.InitializeOverflowTagPointIfNotInitialized();
this.metricPoints[1].Update(value);
return;
}
else
{
if (Interlocked.CompareExchange(ref this.metricCapHitMessageLogged, 1, 0) == 0)
{
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, this.metricPointCapHitMessage, MetricPointCapHitFixMessage);
}

return;
}
}

// TODO: can special case built-in filters to be bit faster.
if (this.IsExemplarEnabled())
{
var shouldSample = this.exemplarFilter.ShouldSample(value, tags);
this.metricPoints[index].UpdateWithExemplar(value, tags: default, shouldSample);
}
else
{
this.metricPoints[index].Update(value);
}
}
catch (Exception)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
}
this.UpdateDoubleMetricPoint(index, value, tags: default);
}

private void UpdateDoubleCustomTags(double value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
var index = this.FindMetricAggregatorsCustomTag(tags);
if (index < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);
var index = this.FindMetricAggregatorsCustomTag(tags);

if (this.EmitOverflowAttribute)
{
this.InitializeOverflowTagPointIfNotInitialized();
this.metricPoints[1].Update(value);
return;
}
else
{
if (Interlocked.CompareExchange(ref this.metricCapHitMessageLogged, 1, 0) == 0)
{
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, this.metricPointCapHitMessage, MetricPointCapHitFixMessage);
}
this.UpdateDoubleMetricPoint(index, value, tags);
}

return;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateDoubleMetricPoint(int metricPointIndex, double value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
if (metricPointIndex < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);

// TODO: can special case built-in filters to be bit faster.
if (this.IsExemplarEnabled())
if (this.EmitOverflowAttribute)
{
var shouldSample = this.exemplarFilter.ShouldSample(value, tags);
this.metricPoints[index].UpdateWithExemplar(value, tags, shouldSample);
this.InitializeOverflowTagPointIfNotInitialized();
this.metricPoints[1].Update(value);
}
else
else if (Interlocked.CompareExchange(ref this.metricCapHitMessageLogged, 1, 0) == 0)
{
this.metricPoints[index].Update(value);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, this.metricPointCapHitMessage, MetricPointCapHitFixMessage);
}

return;
}
catch (Exception)

switch (this.exemplarFilter)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
case ExemplarFilterType.AlwaysOn:
this.metricPoints[metricPointIndex].UpdateWithExemplar(
value,
tags,
isSampled: true);
break;
case ExemplarFilterType.TraceBased:
this.metricPoints[metricPointIndex].UpdateWithExemplar(
value,
tags,
isSampled: Activity.Current?.Recorded ?? false);
break;
default:
#if DEBUG
if (this.exemplarFilter != ExemplarFilterType.AlwaysOff)
{
Debug.Fail("Unexpected ExemplarFilterType");
}
#endif
this.metricPoints[metricPointIndex].Update(value);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ static MeterProviderBuilder SetExemplarFilter(
switch (exemplarFilter)
{
case ExemplarFilterType.AlwaysOn:
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
meterProviderBuilderSdk.SetExemplarFilter(new AlwaysOnExemplarFilter());
meterProviderBuilderSdk.SetExemplarFilter(exemplarFilter);
break;
case ExemplarFilterType.AlwaysOff:
meterProviderBuilderSdk.SetExemplarFilter(new AlwaysOffExemplarFilter());
meterProviderBuilderSdk.SetExemplarFilter(exemplarFilter);
break;
case ExemplarFilterType.TraceBased:
meterProviderBuilderSdk.SetExemplarFilter(new TraceBasedExemplarFilter());
meterProviderBuilderSdk.SetExemplarFilter(exemplarFilter);
break;
default:
throw new NotSupportedException($"SdkExemplarFilter '{exemplarFilter}' is not supported.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public MeterProviderBuilderSdk(IServiceProvider serviceProvider)

public ResourceBuilder? ResourceBuilder { get; private set; }

public ExemplarFilter? ExemplarFilter { get; private set; }
public ExemplarFilterType? ExemplarFilter { get; private set; }

public MeterProvider? Provider => this.meterProvider;

Expand Down Expand Up @@ -145,10 +145,8 @@ public MeterProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder)
return this;
}

public MeterProviderBuilder SetExemplarFilter(ExemplarFilter exemplarFilter)
public MeterProviderBuilder SetExemplarFilter(ExemplarFilterType exemplarFilter)
{
Debug.Assert(exemplarFilter != null, "exemplarFilter was null");

this.ExemplarFilter = exemplarFilter;

return this;
Expand Down
27 changes: 0 additions & 27 deletions src/OpenTelemetry/Metrics/Exemplar/AlwaysOffExemplarFilter.cs

This file was deleted.

Loading