-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Reduce allocations in telemetry #1321
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.