Skip to content

Commit

Permalink
Add some metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed May 26, 2024
1 parent 3ac6e13 commit b98ad79
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using AgileConfig.Server.Common;
using System.Dynamic;
using AgileConfig.Server.Apisite.Utilites;
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.Event;
Expand Down
9 changes: 7 additions & 2 deletions src/AgileConfig.Server.Apisite/InitService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AgileConfig.Server.Apisite.Metrics;
using AgileConfig.Server.Apisite.Utilites;
using AgileConfig.Server.IService;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -20,7 +20,10 @@ public class InitService : IHostedService
private readonly ISystemInitializationService _systemInitializationService;
private readonly ILogger _logger;
private readonly IServiceScope _localServiceScope;
public InitService(IServiceScopeFactory serviceScopeFactory, ISystemInitializationService systemInitializationService, ILogger<InitService> logger)
public InitService(IServiceScopeFactory serviceScopeFactory,
ISystemInitializationService systemInitializationService,
MeterService meterService,
ILogger<InitService> logger)
{
_logger = logger;
_systemInitializationService = systemInitializationService;
Expand All @@ -29,6 +32,8 @@ public InitService(IServiceScopeFactory serviceScopeFactory, ISystemInitializati
_eventRegister = _localServiceScope.ServiceProvider.GetService<IEventHandlerRegister>();
_serverNodeService = _localServiceScope.ServiceProvider.GetService<IServerNodeService>();
_serviceHealthCheckService = _localServiceScope.ServiceProvider.GetService<IServiceHealthCheckService>();

meterService.Start();
}

public async Task StartAsync(CancellationToken cancellationToken)
Expand Down
143 changes: 143 additions & 0 deletions src/AgileConfig.Server.Apisite/Metrics/MeterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.Metrics;
using System.Threading.Tasks;

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

public static Meter AgileConfigMeter { get; }

public ObservableGauge<int> AppGauge { get; }
public ObservableGauge<int> ConfigGauge { get; }
public ObservableGauge<int> ServiceGauge { get; }
public ObservableGauge<int> ClientGauge { get; }
public ObservableGauge<int> NodeGauge { get; }

private readonly IAppService _appService;
private readonly IConfigService _configService;
private readonly IServerNodeService _serverNodeService;
private readonly IRemoteServerNodeProxy _remoteServer;
private readonly IServiceInfoService _serviceInfoService;

private int _appCount = 0;
private int _configCount = 0;
private int _clientCount = 0;
private int _serverNodeCount = 0;
private int _serviceCount = 0;

private const int _checkInterval = 30;

static MeterService()
{
AgileConfigMeter = new(MeterName, "1.0");
}

public MeterService(IServiceScopeFactory sf)
{
var sp = sf.CreateScope().ServiceProvider;
_appService = sp.GetService<IAppService>();
_configService = sp.GetService<IConfigService>();
_serverNodeService = sp.GetService<IServerNodeService>();
_remoteServer = sp.GetService<IRemoteServerNodeProxy>();
_serviceInfoService = sp.GetService<IServiceInfoService>();

AppGauge = AgileConfigMeter.CreateObservableGauge<int>("AppCount", () =>
{
return _appCount;
}, "", "The number of enabled apps");
ConfigGauge = AgileConfigMeter.CreateObservableGauge<int>("ConfigCount", () =>
{
return _configCount;
}, "", "The number of enabled configuration items");
ServiceGauge = AgileConfigMeter.CreateObservableGauge<int>("ServiceCount", () =>
{
return _serviceCount;
}, "", "The number of registered services");
ClientGauge = AgileConfigMeter.CreateObservableGauge<int>("ClientCount", () =>
{
return _clientCount;
}, "", "The number of connected clients");
NodeGauge = AgileConfigMeter.CreateObservableGauge<int>("NodeCount", () =>
{
return _serverNodeCount;
}, "", "The number of nodes");
}

public void Start()
{
_ = StartCheckAppCountAsync();
_ = StartCheckConfigCountAsync();
_ = StartCheckNodeCountAsync();
_ = StartCheckNodeCountAsync();
_ = StartCheckServiceCountAsync();
}

private Task StartCheckAppCountAsync()
{
return Task.Run(async () =>
{
while (true)
{
_appCount = await _appService.CountEnabledAppsAsync();
await Task.Delay(1000 * _checkInterval);
}
});
}

private Task StartCheckConfigCountAsync()
{
return Task.Run(async () =>
{
while (true)
{
_configCount = await _configService.CountEnabledConfigsAsync();
await Task.Delay(1000 * _checkInterval);
}
});
}

private Task StartCheckNodeCountAsync()
{
return Task.Run(async () =>
{
while (true)
{
var nodes = await _serverNodeService.GetAllNodesAsync();
_serverNodeCount = nodes.Count;
var clientCount = 0;
foreach (var item in nodes)
{
if (item.Status == NodeStatus.Online)
{
var clientInfos = await _remoteServer.GetClientsReportAsync(item.Id.ToString());
clientCount += clientInfos.ClientCount;
}
}
_clientCount = clientCount;
await Task.Delay(1000 * _checkInterval);
}
});
}

private Task StartCheckServiceCountAsync()
{
return Task.Run(async () =>
{
while (true)
{
var services = await _serviceInfoService.GetAllServiceInfoAsync();
_serviceCount = services.Count;
await Task.Delay(1000 * _checkInterval);
}
});
}
}
}
3 changes: 3 additions & 0 deletions src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using AgileConfig.Server.Data.Abstraction;
using AgileConfig.Server.Common.EventBus;
using OpenTelemetry.Resources;
using AgileConfig.Server.Apisite.Metrics;

namespace AgileConfig.Server.Apisite
{
Expand Down Expand Up @@ -91,6 +92,8 @@ public void ConfigureServices(IServiceCollection services)

services.AddOIDC();

services.AddSingleton<MeterService>();

services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(Program.AppName))
.AddOtlpTraces()
Expand Down
5 changes: 4 additions & 1 deletion src/AgileConfig.Server.Apisite/StartupExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OpenTelemetry.Exporter;
using OpenTelemetry;
using Npgsql;
using AgileConfig.Server.Apisite.Metrics;

namespace AgileConfig.Server.Apisite
{
Expand Down Expand Up @@ -52,10 +53,12 @@ public static IOpenTelemetryBuilder AddOtlpMetrics(this IOpenTelemetryBuilder bu
builder.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddOtlpExporter(op =>
.AddMeter(MeterService.MeterName)
.AddOtlpExporter((op, reader) =>
{
op.Protocol = Appsettings.OtlpMetricsProtocol == "http" ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc;
op.Endpoint = new System.Uri(Appsettings.OtlpMetricsEndpoint);
reader.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"metrics": {
"protocol": "http", // http grpc
"endpoint": "http://192.168.0.201:14318"
"endpoint": "http://localhost:9090/api/v1/otlp/v1/metrics"
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Apisite/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"metrics": {
"protocol": "http", // http grpc
"endpoint": "" // not supported yet
"endpoint": ""
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
Expand Down
14 changes: 8 additions & 6 deletions src/AgileConfig.Server.Common/EventBus/TinyEventBus.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -10,7 +11,7 @@ namespace AgileConfig.Server.Common.EventBus
public class TinyEventBus : ITinyEventBus
{
private readonly IServiceCollection _serviceCollection;
private readonly static Dictionary<Type, List<Type>> _eventHandlerMap = new Dictionary<Type, List<Type>>();
private readonly static ConcurrentDictionary<Type, List<Type>> _eventHandlerMap = new ConcurrentDictionary<Type, List<Type>>();

public TinyEventBus(IServiceCollection serviceCollection)
{
Expand All @@ -26,7 +27,7 @@ public void Register<T>() where T : class, IEventHandler
}
else
{
_eventHandlerMap.Add(eventType, new List<Type> {
_eventHandlerMap.TryAdd(eventType, new List<Type> {
handlerType
});
}
Expand All @@ -36,8 +37,9 @@ public void Register<T>() where T : class, IEventHandler

public void Fire<TEvent>(TEvent evt) where TEvent : IEvent
{
using var scope = _serviceCollection.BuildServiceProvider().CreateScope();
var logger = scope.ServiceProvider.GetService<ILoggerFactory>().CreateLogger<TinyEventBus>();
IServiceProvider sp = _serviceCollection.BuildServiceProvider();
using var scope = sp.CreateScope();
var logger = sp.GetService<ILoggerFactory>().CreateLogger<TinyEventBus>();

logger.LogInformation($"Event fired: {typeof(TEvent).Name}");

Expand All @@ -48,9 +50,9 @@ public void Fire<TEvent>(TEvent evt) where TEvent : IEvent
{
_ = Task.Run(async () =>
{
using (var scope = _serviceCollection.BuildServiceProvider().CreateScope())
using (var scope = sp.CreateScope())
{
var handler = scope.ServiceProvider.GetService(handlerType);
var handler = sp.GetService(handlerType);
if (handler != null)
{
var handlerIntance = handler as IEventHandler;
Expand Down
1 change: 0 additions & 1 deletion src/AgileConfig.Server.Event/SysLogEvents.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.Data.Entity;
using System.Xml.Linq;

namespace AgileConfig.Server.Event
{
Expand Down

0 comments on commit b98ad79

Please sign in to comment.