-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce allocations in telemetry (#1321)
- Loading branch information
Showing
19 changed files
with
290 additions
and
101 deletions.
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
16 changes: 14 additions & 2 deletions
16
bench/Polly.Core.Benchmarks/MultipleStrategiesBenchmark.cs
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 |
---|---|---|
@@ -1,20 +1,32 @@ | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
public class MultipleStrategiesBenchmark | ||
{ | ||
private MeterListener? _meterListener; | ||
private object? _strategyV7; | ||
private object? _strategyV8; | ||
private object? _strategyTelemetryV8; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_strategyV7 = Helper.CreateStrategyPipeline(PollyVersion.V7); | ||
_strategyV8 = Helper.CreateStrategyPipeline(PollyVersion.V8); | ||
_meterListener = MeteringUtil.ListenPollyMetrics(); | ||
_strategyV7 = Helper.CreateStrategyPipeline(PollyVersion.V7, false); | ||
_strategyV8 = Helper.CreateStrategyPipeline(PollyVersion.V8, false); | ||
_strategyTelemetryV8 = Helper.CreateStrategyPipeline(PollyVersion.V8, true); | ||
} | ||
|
||
[GlobalCleanup] | ||
public void Cleanup() => _meterListener?.Dispose(); | ||
|
||
[Benchmark(Baseline = true)] | ||
public ValueTask ExecuteStrategyPipeline_V7() => _strategyV7!.ExecuteAsync(PollyVersion.V7); | ||
|
||
[Benchmark] | ||
public ValueTask ExecuteStrategyPipeline_V8() => _strategyV8!.ExecuteAsync(PollyVersion.V8); | ||
|
||
[Benchmark] | ||
public ValueTask ExecuteStrategyPipeline_Telemetry_V8() => _strategyTelemetryV8!.ExecuteAsync(PollyVersion.V8); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace Polly.Core.Benchmarks.Utils; | ||
|
||
internal class MeteringUtil | ||
{ | ||
public static MeterListener ListenPollyMetrics() | ||
{ | ||
var meterListener = new MeterListener | ||
{ | ||
InstrumentPublished = (instrument, listener) => | ||
{ | ||
if (instrument.Meter.Name is "Polly") | ||
{ | ||
listener.EnableMeasurementEvents(instrument); | ||
} | ||
} | ||
}; | ||
|
||
meterListener.SetMeasurementEventCallback<int>(OnMeasurementRecorded); | ||
meterListener.Start(); | ||
|
||
static void OnMeasurementRecorded<T>( | ||
Instrument instrument, | ||
T measurement, | ||
ReadOnlySpan<KeyValuePair<string, object?>> tags, | ||
object? state) | ||
{ | ||
// do nothing | ||
} | ||
|
||
return meterListener; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Polly.Telemetry; | ||
|
||
public sealed partial record class TelemetryEventArguments | ||
{ | ||
private static readonly ObjectPool<TelemetryEventArguments> Pool = new(() => new TelemetryEventArguments(), args => | ||
{ | ||
args.Source = null!; | ||
args.EventName = null!; | ||
args.Context = null!; | ||
args.Outcome = default; | ||
args.Arguments = null!; | ||
}); | ||
|
||
internal static TelemetryEventArguments Get( | ||
ResilienceTelemetrySource source, | ||
string eventName, | ||
ResilienceContext context, | ||
Outcome<object>? outcome, | ||
object arguments) | ||
{ | ||
var args = Pool.Get(); | ||
|
||
args.Source = source; | ||
args.EventName = eventName; | ||
args.Context = context; | ||
args.Outcome = outcome; | ||
args.Arguments = arguments; | ||
|
||
return args; | ||
} | ||
|
||
internal static void Return(TelemetryEventArguments args) => Pool.Return(args); | ||
} |
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 |
---|---|---|
@@ -1,31 +1,19 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Polly.Extensions.Telemetry; | ||
|
||
internal static class EnrichmentUtil | ||
{ | ||
public static void Enrich( | ||
ref TagList tags, | ||
List<Action<EnrichmentContext>> enrichers, | ||
ResilienceContext resilienceContext, | ||
Outcome<object>? outcome, | ||
object? resilienceArguments) | ||
public static void Enrich(EnrichmentContext context, List<Action<EnrichmentContext>> enrichers) | ||
{ | ||
if (enrichers.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
var context = EnrichmentContext.Get(resilienceContext, resilienceArguments, outcome); | ||
|
||
foreach (var enricher in enrichers) | ||
{ | ||
enricher(context); | ||
} | ||
|
||
foreach (var pair in context.Tags) | ||
{ | ||
tags.Add(pair.Key, pair.Value); | ||
} | ||
|
||
EnrichmentContext.Return(context); | ||
} | ||
} |
Oops, something went wrong.