Skip to content

Commit

Permalink
Do not record null tags to meter (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jul 27, 2023
1 parent 19c0488 commit b825c91
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,33 @@ private static void AddCommonTags(TelemetryEventArguments args, ResilienceTeleme
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.EventName, args.Event.EventName));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.EventSeverity, args.Event.Severity.AsString()));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderName, source.BuilderName));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderInstance, source.BuilderInstanceName));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.StrategyName, source.StrategyName));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.OperationKey, enrichmentContext.Context.OperationKey));

if (source.BuilderName is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderName, source.BuilderName));
}

if (source.BuilderInstanceName is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderInstance, source.BuilderInstanceName));
}

if (source.StrategyName is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.StrategyName, source.StrategyName));
}

if (enrichmentContext.Context.OperationKey is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.OperationKey, enrichmentContext.Context.OperationKey));
}

enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ResultType, args.Context.GetResultType()));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ExceptionName, args.Outcome?.Exception?.GetType().FullName));

if (args.Outcome?.Exception is Exception e)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ExceptionName, e.GetType().FullName));
}
}

private void MeterEvent(TelemetryEventArguments args)
Expand Down
40 changes: 29 additions & 11 deletions src/Polly.Extensions/Telemetry/TelemetryResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Polly.Extensions.Telemetry;
internal sealed class TelemetryResilienceStrategy : ResilienceStrategy
{
private readonly TimeProvider _timeProvider;
private readonly string _builderName;
private readonly string _builderInstance;
private readonly string? _builderName;
private readonly string? _builderInstance;
private readonly List<Action<EnrichmentContext>> _enrichers;
private readonly ILogger _logger;
private readonly Func<ResilienceContext, object?, object?> _resultFormatter;
Expand All @@ -32,8 +32,8 @@ public TelemetryResilienceStrategy(
List<Action<EnrichmentContext>> enrichers)
{
_timeProvider = timeProvider;
_builderName = builderName.GetValueOrPlaceholder();
_builderInstance = builderInstance.GetValueOrPlaceholder();
_builderName = builderName;
_builderInstance = builderInstance;
_resultFormatter = resultFormatter;
_enrichers = enrichers;
_logger = loggerFactory.CreateLogger(TelemetryUtil.PollyDiagnosticSource);
Expand All @@ -51,7 +51,7 @@ protected override async ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState
TState state)
{
var stamp = _timeProvider.GetTimestamp();
Log.ExecutingStrategy(_logger, _builderName, _builderInstance, context.OperationKey, context.GetResultType());
Log.ExecutingStrategy(_logger, _builderName.GetValueOrPlaceholder(), _builderInstance.GetValueOrPlaceholder(), context.OperationKey, context.GetResultType());

var outcome = await callback(context, state).ConfigureAwait(context.ContinueOnCapturedContext);

Expand All @@ -61,8 +61,8 @@ protected override async ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState
Log.StrategyExecuted(
_logger,
logLevel,
_builderName,
_builderInstance,
_builderName.GetValueOrPlaceholder(),
_builderInstance.GetValueOrPlaceholder(),
context.OperationKey,
context.GetResultType(),
ExpandOutcome(context, outcome),
Expand All @@ -87,11 +87,29 @@ private void RecordDuration<TResult>(ResilienceContext context, Outcome<TResult>
}

var enrichmentContext = EnrichmentContext.Get(context, null, CreateOutcome(outcome));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderName, _builderName));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderInstance, _builderInstance));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.OperationKey, context.OperationKey));

if (_builderName is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderName, _builderName));
}

if (_builderInstance is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.BuilderInstance, _builderInstance));
}

if (context.OperationKey is not null)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.OperationKey, context.OperationKey));
}

enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ResultType, context.GetResultType()));
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ExceptionName, outcome.Exception?.GetType().FullName));

if (outcome.Exception is Exception e)
{
enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ExceptionName, e.GetType().FullName));
}

enrichmentContext.Tags.Add(new(ResilienceTelemetryTags.ExecutionHealth, context.GetExecutionHealth()));
EnrichmentUtil.Enrich(enrichmentContext, _enrichers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,15 @@ public void WriteEvent_MeteringWithoutEnrichers_Ok(bool noOutcome, bool exceptio
events.Should().HaveCount(1);
var ev = events[0];

ev.Count.Should().Be(8);
if (noOutcome && exception)
{
ev.Count.Should().Be(8);
}
else
{
ev.Count.Should().Be(7);
}

ev["event-name"].Should().Be("my-event");
ev["event-severity"].Should().Be("Warning");
ev["strategy-name"].Should().Be("my-strategy");
Expand All @@ -227,7 +235,7 @@ public void WriteEvent_MeteringWithoutEnrichers_Ok(bool noOutcome, bool exceptio
}
else
{
ev["exception-name"].Should().Be(null);
ev.Should().NotContainKey("exception-name");
}
}

Expand All @@ -252,7 +260,15 @@ public void WriteExecutionAttemptEvent_Metering_Ok(bool noOutcome, bool exceptio
events.Should().HaveCount(1);
var ev = events[0];

ev.Count.Should().Be(10);
if (noOutcome && exception)
{
ev.Count.Should().Be(10);
}
else
{
ev.Count.Should().Be(9);
}

ev["event-name"].Should().Be("my-event");
ev["event-severity"].Should().Be("Warning");
ev["strategy-name"].Should().Be("my-strategy");
Expand All @@ -269,7 +285,7 @@ public void WriteExecutionAttemptEvent_Metering_Ok(bool noOutcome, bool exceptio
}
else
{
ev["exception-name"].Should().Be(null);
ev.Should().NotContainKey("exception-name");
}

_events.Single(v => v.Name == "execution-attempt-duration").Measurement.Should().Be(50000);
Expand Down Expand Up @@ -301,7 +317,7 @@ public void WriteEvent_MeteringWithEnrichers_Ok(int count)

var events = GetEvents("resilience-events");
var ev = events[0];
ev.Count.Should().Be(DefaultDimensions + count + 2);
ev.Count.Should().Be(DefaultDimensions + count + 1);
ev["other"].Should().Be("other-value");

for (int i = 0; i < count; i++)
Expand All @@ -315,7 +331,7 @@ public void WriteEvent_MeteringWithoutBuilderInstance_Ok()
{
var telemetry = Create();
ReportEvent(telemetry, null, instanceName: null);
var events = GetEvents("resilience-events")[0]["builder-instance"].Should().BeNull();
var events = GetEvents("resilience-events")[0].Should().NotContainKey("builder-instance");
}

[InlineData(true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void Execute_Enrichers_Ok()

var ev = _events.Single(v => v.Name == "strategy-execution-duration").Tags;

ev.Count.Should().Be(7);
ev.Count.Should().Be(5);
ev["my-custom-tag"].Should().Be("my-tag-value");
}

Expand All @@ -164,12 +164,12 @@ public void Execute_WithResult_EnsureMetered(bool healthy)

var ev = _events.Single(v => v.Name == "strategy-execution-duration").Tags;

ev.Count.Should().Be(6);
ev.Count.Should().Be(5);
ev["builder-instance"].Should().Be("my-instance");
ev["operation-key"].Should().Be("op-key");
ev["builder-name"].Should().Be("my-builder");
ev["result-type"].Should().Be("Boolean");
ev["exception-name"].Should().BeNull();
ev.Should().NotContainKey("exception-name");

if (healthy)
{
Expand Down

0 comments on commit b825c91

Please sign in to comment.