Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/MetricPusherHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ async Task DoPushAsync()
{
await pusher.PushAsync();
}
catch (Exception)
catch (Exception ex)
{
// TODO: report error to DiagnosticSource?
OnPushError(pusher, ex);
}
}
}

protected virtual void OnPushError(IMetricPusher metricPusher, Exception exception)
{
}
}
15 changes: 15 additions & 0 deletions tests/MetricPusherHostedServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public async Task WithGivenInterval_PushMetricPeriodically(int seconds)
await metricPusherMock.Received(3).PushAsync();
}

[Fact]
public async Task OnPushError_HandlesException()
{
var pusher = Substitute.For<IMetricPusher>();
pusher.PushAsync().Returns(Task.FromException(new Exception("Simulated Push Exception")));
var ct = Arg.Any<CancellationToken>();

var hostedService = new TestableMetricPusherHostedService(pusher);
await hostedService.StartAsync(ct);
await Task.Delay(1000, ct);
await hostedService.StopAsync(ct);

Assert.True(hostedService.ErrorHandled);
}

private static TimeSpan GetDelay(int seconds)
{
var milliseconds = seconds * 1000 + 100;
Expand Down
11 changes: 11 additions & 0 deletions tests/TestableMetricPusherHostedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Prometheus.Client.MetricPusher.HostedService.Tests;

public class TestableMetricPusherHostedService(IMetricPusher pusher) : MetricPusherHostedService(pusher)
{
public bool ErrorHandled { get; private set; }

protected override void OnPushError(IMetricPusher metricPusher, Exception exception)
{
ErrorHandled = true;
}
}
Loading