Skip to content

Commit 4856754

Browse files
authored
Merge pull request #89 from prom-client-net/refactor/rename-service
rename service & internal refactoring
2 parents 3a6e375 + d08b1a9 commit 4856754

7 files changed

+25
-35
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
[![nuget](https://img.shields.io/nuget/v/Prometheus.Client.MetricPusher.HostedService?logo=nuget&style=flat-square)](https://www.nuget.org/packages/Prometheus.Client.MetricPusher.HostedService)
55
[![nuget](https://img.shields.io/nuget/dt/Prometheus.Client.MetricPusher.HostedService?logo=nuget&style=flat-square)](https://www.nuget.org/packages/Prometheus.Client.MetricPusher.HostedService)
66
[![codecov](https://img.shields.io/codecov/c/github/prom-client-net/prom-client-metricpusher-hostedservice?logo=codecov&style=flat-square)](https://app.codecov.io/gh/prom-client-net/prom-client-metricpusher-hostedservice)
7-
[![codefactor](https://img.shields.io/codefactor/grade/github/prom-client-net/prom-client-metricpusher-hostedservice?logo=codefactor&style=flat-square)](https://www.codefactor.io/repository/github/prom-client-net/prom-client-metricpusher-hostedservice)
87
[![license](https://img.shields.io/github/license/prom-client-net/prom-client-metricpusher-hostedservice?style=flat-square)](https://github.com/prom-client-net/prom-client-metricpusher-hostedservice/blob/main/LICENSE)
98

109
Extension for [Prometheus.Client](https://github.com/prom-client-net/prom-client)
1110

12-
## Installation
11+
## Install
1312

1413
```sh
1514
dotnet add package Prometheus.Client.MetricPusher.HostedService
@@ -29,7 +28,7 @@ var metricPusher = new MetricPusher(new MetricPusherOptions
2928
```
3029

3130
```c#
32-
services.AddMetricPusherService(metricPusher);
31+
services.AddMetricPusherHostedService(metricPusher);
3332
```
3433

3534
## Contribute

src/Defaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace Prometheus.Client.MetricPusher.HostedService;
44

55
internal static class Defaults
66
{
7-
internal static TimeSpan Interval = TimeSpan.FromMilliseconds(1000);
7+
internal static TimeSpan PushInterval = TimeSpan.FromMilliseconds(1000);
88
}

src/MetricPusherService.cs renamed to src/MetricPusherHostedService.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@
55

66
namespace Prometheus.Client.MetricPusher.HostedService;
77

8-
public class MetricPusherService : BackgroundService
8+
public class MetricPusherHostedService(IMetricPusher pusher, TimeSpan pushInterval) : BackgroundService
99
{
10-
private readonly IMetricPusher _pusher;
11-
private readonly TimeSpan _interval;
12-
13-
public MetricPusherService(IMetricPusher pusher)
14-
: this(pusher, Defaults.Interval)
15-
{
16-
}
17-
18-
public MetricPusherService(IMetricPusher pusher, TimeSpan interval)
10+
public MetricPusherHostedService(IMetricPusher pusher)
11+
: this(pusher, Defaults.PushInterval)
1912
{
20-
_pusher = pusher;
21-
_interval = interval;
2213
}
2314

2415
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -28,7 +19,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2819
await DoPushAsync();
2920
try
3021
{
31-
await Task.Delay(_interval, stoppingToken);
22+
await Task.Delay(pushInterval, stoppingToken);
3223
}
3324
catch (TaskCanceledException)
3425
{
@@ -43,7 +34,7 @@ async Task DoPushAsync()
4334
{
4435
try
4536
{
46-
await _pusher.PushAsync();
37+
await pusher.PushAsync();
4738
}
4839
catch (Exception)
4940
{

src/Prometheus.Client.MetricPusher.HostedService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
4-
<Description>Prometheus.Client.MetricPusher as HostedService</Description>
4+
<Description>MetricPusher as HostedService</Description>
55
<RepositoryUrl>https://github.com/prom-client-net/prom-client-metricpusher-hostedservice</RepositoryUrl>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
77
</PropertyGroup>

src/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ namespace Prometheus.Client.MetricPusher.HostedService;
66

77
public static class ServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddMetricPusherService(this IServiceCollection services, IMetricPusher pusher)
9+
public static IServiceCollection AddMetricPusherHostedService(this IServiceCollection services, IMetricPusher pusher)
1010
{
11-
return AddMetricPusherService(services, pusher, Defaults.Interval);
11+
return AddMetricPusherHostedService(services, pusher, Defaults.PushInterval);
1212
}
1313

14-
public static IServiceCollection AddMetricPusherService(this IServiceCollection services, IMetricPusher pusher, TimeSpan interval)
14+
public static IServiceCollection AddMetricPusherHostedService(this IServiceCollection services, IMetricPusher pusher, TimeSpan pushInterval)
1515
{
1616
if (pusher == null)
1717
throw new ArgumentNullException(nameof(pusher));
18-
if (interval == TimeSpan.Zero)
19-
throw new ArgumentOutOfRangeException(nameof(interval));
18+
if (pushInterval == TimeSpan.Zero)
19+
throw new ArgumentOutOfRangeException(nameof(pushInterval));
2020

21-
services.AddSingleton<IHostedService, MetricPusherService>(_ => new MetricPusherService(pusher, interval));
21+
services.AddSingleton<IHostedService, MetricPusherHostedService>(_ => new MetricPusherHostedService(pusher, pushInterval));
2222

2323
return services;
2424
}

tests/MetricPusherServiceTests.cs renamed to tests/MetricPusherHostedServiceTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
namespace Prometheus.Client.MetricPusher.HostedService.Tests;
55

6-
public class MetricPusherServiceTests
6+
public class MetricPusherHostedServiceTests
77
{
88
[Fact]
99
public async Task WithDefaultInterval_PushMetricPeriodically()
1010
{
1111
var metricPusherMock = Substitute.For<IMetricPusher>();
12-
var metricPusherService = new MetricPusherService(metricPusherMock);
12+
var metricPusherService = new MetricPusherHostedService(metricPusherMock);
1313
var canellationToken = Arg.Any<CancellationToken>();
1414

1515
await metricPusherService.StartAsync(canellationToken);
@@ -27,7 +27,7 @@ public async Task WithDefaultInterval_PushMetricPeriodically()
2727
public async Task WithGivenInterval_PushMetricPeriodically(int seconds)
2828
{
2929
var metricPusherMock = Substitute.For<IMetricPusher>();
30-
var metricPusherService = new MetricPusherService(metricPusherMock, TimeSpan.FromSeconds(seconds));
30+
var metricPusherService = new MetricPusherHostedService(metricPusherMock, TimeSpan.FromSeconds(seconds));
3131
var canellationToken = Arg.Any<CancellationToken>();
3232

3333
await metricPusherService.StartAsync(canellationToken);

tests/ServiceCollecttionExtensionsTests.cs renamed to tests/ServiceCollectionExtensionsTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
namespace Prometheus.Client.MetricPusher.HostedService.Tests;
55

6-
public class ServiceCollecttionExtensionsTests
6+
public class ServiceCollectionExtensionsTests
77
{
88
[Fact]
99
public void AddMetricPusherService_WithNullMetricPusher_ThrowsArgumentNullException()
1010
{
1111
var servicesCollection = Substitute.For<IServiceCollection>();
1212

13-
Action act = () => servicesCollection.AddMetricPusherService(null, TimeSpan.FromSeconds(1));
13+
Action act = () => servicesCollection.AddMetricPusherHostedService(null, TimeSpan.FromSeconds(1));
1414

1515
act.Should().Throw<ArgumentNullException>();
1616
}
@@ -21,7 +21,7 @@ public void AddMetricPusherService_WithZeroTimeInterval_ThrowsArgumentOutOfRange
2121
var servicesCollection = Substitute.For<IServiceCollection>();
2222
var metricPusher = Substitute.For<IMetricPusher>();
2323

24-
Action act = () => servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.Zero);
24+
Action act = () => servicesCollection.AddMetricPusherHostedService(metricPusher, TimeSpan.Zero);
2525

2626
act.Should().Throw<ArgumentOutOfRangeException>();
2727
}
@@ -32,11 +32,11 @@ public void AddMetricPusherService_WithDefaultInterval_AddsMetricPusherServiceIn
3232
var servicesCollection = new ServiceCollection();
3333
var metricPusher = Substitute.For<IMetricPusher>();
3434

35-
servicesCollection.AddMetricPusherService(metricPusher);
35+
servicesCollection.AddMetricPusherHostedService(metricPusher);
3636
var provider = servicesCollection.BuildServiceProvider();
3737
var service = provider.GetRequiredService<IHostedService>();
3838

39-
service.Should().BeOfType<MetricPusherService>();
39+
service.Should().BeOfType<MetricPusherHostedService>();
4040
}
4141

4242
[Fact]
@@ -45,10 +45,10 @@ public void AddMetricPusherService_WithValidParameterValues_AddsMetricPusherServ
4545
var servicesCollection = new ServiceCollection();
4646
var metricPusher = Substitute.For<IMetricPusher>();
4747

48-
servicesCollection.AddMetricPusherService(metricPusher, TimeSpan.FromSeconds(1));
48+
servicesCollection.AddMetricPusherHostedService(metricPusher, TimeSpan.FromSeconds(1));
4949
var provider = servicesCollection.BuildServiceProvider();
5050
var service = provider.GetRequiredService<IHostedService>();
5151

52-
service.Should().BeOfType<MetricPusherService>();
52+
service.Should().BeOfType<MetricPusherHostedService>();
5353
}
5454
}

0 commit comments

Comments
 (0)