Skip to content

Commit

Permalink
Refactor MeterService
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed May 26, 2024
1 parent 12ecf53 commit 2cec041
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class ConfigController : Controller
private readonly IUserService _userService;
private readonly IMemoryCache _cacheMemory;
private readonly ITinyEventBus _tinyEventBus;
private readonly MeterService _meterService;
private readonly IMeterService _meterService;

public ConfigController(
IConfigService configService,
IAppService appService,
IUserService userService,
IMemoryCache cacheMemory,
ITinyEventBus tinyEventBus,
MeterService meterService
IMeterService meterService
)
{
_configService = configService;
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [F
.SetAbsoluteExpiration(TimeSpan.FromSeconds(5));
_cacheMemory?.Set(cacheKey, vms, cacheOp);

_meterService.PullAppConfigCounter.Add(1, new("appId", appId), new("env", env));
_meterService.PullAppConfigCounter?.Add(1, new("appId", appId), new("env", env));

return vms;
}
Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Apisite/InitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class InitService : IHostedService
private readonly IServiceScope _localServiceScope;
public InitService(IServiceScopeFactory serviceScopeFactory,
ISystemInitializationService systemInitializationService,
MeterService meterService,
IMeterService meterService,
ILogger<InitService> logger)
{
_logger = logger;
Expand Down
16 changes: 16 additions & 0 deletions src/AgileConfig.Server.Apisite/Metrics/IMeterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Diagnostics.Metrics;

namespace AgileConfig.Server.Apisite.Metrics
{
public interface IMeterService
{
ObservableGauge<int> AppGauge { get; }
ObservableGauge<int> ClientGauge { get; }
ObservableGauge<int> ConfigGauge { get; }
ObservableGauge<int> NodeGauge { get; }
Counter<long> PullAppConfigCounter { get; }
ObservableGauge<int> ServiceGauge { get; }

void Start();
}
}
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Apisite/Metrics/MeterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace AgileConfig.Server.Apisite.Metrics
{
public class MeterService
public class MeterService : IMeterService
{
public const string MeterName = "AgileConfigMeter";

Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddOIDC();

services.AddSingleton<MeterService>();
services.AddSingleton<IMeterService, MeterService>();

services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(Program.AppName))
Expand Down
10 changes: 8 additions & 2 deletions test/ApiSiteTests/TestApiConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Caching.Memory;
using AgileConfig.Server.Apisite.Controllers.api.Models;
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.Apisite.Metrics;

namespace ApiSiteTests;

Expand Down Expand Up @@ -64,12 +65,15 @@ List<Config> newConfigs()
var appBasicAuthService = new Mock<IAppBasicAuthService>();
var userSErvice = new Mock<IUserService>();
var eventBus = new Mock<ITinyEventBus>();
var meterService = new Mock<IMeterService>();

var ctrl = new ConfigController(
configService.Object,
appService.Object,
userSErvice.Object,
memoryCache, eventBus.Object);
memoryCache, eventBus.Object,
meterService.Object
);
var act = await ctrl.GetAppConfig("001", "DEV");

Assert.IsNotNull(act);
Expand All @@ -91,7 +95,9 @@ App newApp1()
configService.Object,
appService.Object,
userSErvice.Object,
memoryCache, eventBus.Object);
memoryCache, eventBus.Object,
meterService.Object
);
act = await ctrl.GetAppConfig("001", "DEV");

Assert.IsNotNull(act);
Expand Down

0 comments on commit 2cec041

Please sign in to comment.