Skip to content

Commit

Permalink
Introduce TelemetryOptions.OnTelemetryEvent (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Jul 4, 2023
1 parent ed4c480 commit 20598ad
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 0 additions & 3 deletions src/Polly.Core/Telemetry/ResilienceTelemetrySource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.ComponentModel;

namespace Polly.Telemetry;

/// <summary>
Expand All @@ -12,7 +10,6 @@ namespace Polly.Telemetry;
/// <remarks>
/// This class is used by the telemetry infrastructure and should not be used directly by user code.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed record class ResilienceTelemetrySource(
string? BuilderName,
ResilienceProperties BuilderProperties,
Expand Down
3 changes: 0 additions & 3 deletions src/Polly.Core/Telemetry/TelemetryEventArguments.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.ComponentModel;

namespace Polly.Telemetry;

/// <summary>
/// The arguments of the telemetry event.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed partial record class TelemetryEventArguments
{
private TelemetryEventArguments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ internal class ResilienceTelemetryDiagnosticSource : DiagnosticSource

private readonly ILogger _logger;
private readonly Func<ResilienceContext, object?, object?> _resultFormatter;
private readonly Action<TelemetryEventArguments>? _onEvent;
private readonly List<Action<EnrichmentContext>> _enrichers;

public ResilienceTelemetryDiagnosticSource(TelemetryOptions options)
{
_enrichers = options.Enrichers.ToList();
_logger = options.LoggerFactory.CreateLogger(TelemetryUtil.PollyDiagnosticSource);
_resultFormatter = options.ResultFormatter;
_onEvent = options.OnTelemetryEvent;

Counter = Meter.CreateCounter<int>(
"resilience-events",
Expand All @@ -41,6 +43,8 @@ public override void Write(string name, object? value)
return;
}

_onEvent?.Invoke(args);

LogEvent(args);
MeterEvent(args);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Polly.Extensions/Telemetry/TelemetryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Polly.Telemetry;

namespace Polly.Extensions.Telemetry;

Expand All @@ -10,6 +11,14 @@ namespace Polly.Extensions.Telemetry;
/// </summary>
public class TelemetryOptions
{
/// <summary>
/// Gets or sets the callback that is raised when <see cref="TelemetryEventArguments"/> is received from Polly.
/// </summary>
/// <remarks>
/// Defaults to <see langword="null"/>.
/// </remarks>
public Action<TelemetryEventArguments>? OnTelemetryEvent { get; set; }

/// <summary>
/// Gets or sets the logger factory.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ResilienceTelemetryDiagnosticSourceTests : IDisposable
private readonly ILoggerFactory _loggerFactory;
private readonly List<MeteringEvent> _events = new();
private readonly IDisposable _metering;
private Action<TelemetryEventArguments>? _onEvent;

public ResilienceTelemetryDiagnosticSourceTests()
{
Expand Down Expand Up @@ -319,13 +320,32 @@ public void WriteEvent_MeteringWithoutStrategyKey_Ok()
var events = GetEvents("resilience-events")[0]["strategy-key"].Should().BeNull();
}

[InlineData(true)]
[InlineData(false)]
[Theory]
public void OnTelemetryEvent_Ok(bool hasCallback)
{
var called = false;

if (hasCallback)
{
_onEvent = e => called = true;
}

var telemetry = Create();
ReportEvent(telemetry, null, strategyKey: null);

called.Should().Be(hasCallback);
}

private List<Dictionary<string, object?>> GetEvents(string eventName) => _events.Where(e => e.Name == eventName).Select(v => v.Tags).ToList();

private ResilienceTelemetryDiagnosticSource Create(Action<ICollection<Action<EnrichmentContext>>>? configureEnrichers = null)
{
var options = new TelemetryOptions
{
LoggerFactory = _loggerFactory
LoggerFactory = _loggerFactory,
OnTelemetryEvent = _onEvent
};

configureEnrichers?.Invoke(options.Enrichers);
Expand Down

0 comments on commit 20598ad

Please sign in to comment.