Skip to content

Commit

Permalink
Merge pull request #72 from atidev/group-metrics
Browse files Browse the repository at this point in the history
Group prometheus metrics
  • Loading branch information
muphblu authored Mar 1, 2024
2 parents 26229a3 + 2321b24 commit 0e1ed49
Show file tree
Hide file tree
Showing 14 changed files with 986 additions and 950 deletions.
5 changes: 2 additions & 3 deletions ATI.Services.Common/ATI.Services.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>
<Authors>Team Services</Authors>
<Company>ATI</Company>
<LangVersion>10</LangVersion>
<PublishReadyToRun>true</PublishReadyToRun>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>atisu.services.common</PackageId>
Expand All @@ -18,15 +17,15 @@
<PackageReference Include="Dapper" Version="2.0.30" />
<PackageReference Include="HtmlSanitizer" Version="8.0.692" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.1" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
<PackageReference Include="NLog" Version="4.7.6" />
<PackageReference Include="NLog.DiagnosticSource" Version="1.3.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
<PackageReference Include="Npgsql" Version="7.0.4" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="prometheus-net" Version="8.0.0" />
<PackageReference Include="prometheus-net" Version="8.2.1" />
<PackageReference Include="StackExchange.Redis" Version="2.2.50" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
Expand Down
5 changes: 1 addition & 4 deletions ATI.Services.Common/Initializers/MetricsInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public Task InitializeAsync()
return Task.CompletedTask;
}

if (_metricsOptions.MetricsServiceName != null )
{
MetricsFactory.Init(_metricsOptions.MetricsServiceName, _metricsOptions.DefaultLongRequestTime);
}
MetricsFactory.Init(_metricsOptions.DefaultLongRequestTime);

_initialized = true;
return Task.CompletedTask;
Expand Down
3 changes: 2 additions & 1 deletion ATI.Services.Common/Logging/LogSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum LogSource
Sql = 4,
Controller = 5,
Repository = 6,
ExternalHttpClient = 8
RabbitMq = 9,
Custom = 10
}
}
78 changes: 34 additions & 44 deletions ATI.Services.Common/Metrics/ExceptionsMetricsCollector.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,49 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics.Tracing;
using Microsoft.Extensions.Configuration;
using Prometheus;
using ConfigurationManager = ATI.Services.Common.Behaviors.ConfigurationManager;

namespace ATI.Services.Common.Metrics
{
public class ExceptionsMetricsCollector : EventListener
{
// Да, даже под linux - Microsoft-Windows
private const string ExceptionSourceName = "Microsoft-Windows-DotNETRuntime";
private const int ExceptionBit = 0x8000;
private const int ExceptionNameIndex = 0;
private readonly ConcurrentDictionary<string, long> _exceptionCounters = new();
private MetricFactory _metricFactory;
private const string ExceptionsMetricName = "Exceptions";
private Gauge _gauge;
namespace ATI.Services.Common.Metrics;

private string ServiceName { get; }
public ExceptionsMetricsCollector()
{
ServiceName = ConfigurationManager.GetSection(nameof(MetricsOptions)).Get<MetricsOptions>().MetricsServiceName;
}
public class ExceptionsMetricsCollector : EventListener
{
// Да, даже под linux - Microsoft-Windows
private const string ExceptionSourceName = "Microsoft-Windows-DotNETRuntime";
private const int ExceptionBit = 0x8000;
private const int ExceptionNameIndex = 0;
private readonly ConcurrentDictionary<string, long> _exceptionCounters = new();
private MetricFactory _metricFactory;
private Gauge _gauge;

protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name != ExceptionSourceName)
return;
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name != ExceptionSourceName)
return;

EnableEvents(
eventSource,
EventLevel.Error,
(EventKeywords) ExceptionBit);
}
EnableEvents(
eventSource,
EventLevel.Error,
(EventKeywords) ExceptionBit);
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
var exceptionType = eventData.Payload[ExceptionNameIndex].ToString();
_exceptionCounters.AddOrUpdate(exceptionType, _ => 1, (_, oldValue) => oldValue + 1);
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
var exceptionType = eventData.Payload[ExceptionNameIndex].ToString();
_exceptionCounters.AddOrUpdate(exceptionType, _ => 1, (_, oldValue) => oldValue + 1);
}

public void RegisterMetrics(CollectorRegistry registry)
{
_metricFactory = Prometheus.Metrics.WithCustomRegistry(registry);
_gauge = _metricFactory.CreateGauge(ServiceName + ExceptionsMetricName, "", "machine_name", "exception_type");
}
public void RegisterMetrics(CollectorRegistry registry)
{
_metricFactory = Prometheus.Metrics.WithCustomRegistry(registry);
_gauge = _metricFactory.CreateGauge($"{MetricsFactory.Prefix}_Exceptions", "", "machine_name", "exception_type");
}

public void UpdateMetrics()
public void UpdateMetrics()
{
foreach (var exceptionType in _exceptionCounters.Keys)
{
foreach (var exceptionType in _exceptionCounters.Keys)
{
_exceptionCounters.TryRemove(exceptionType, out var count);
_gauge.WithLabels(Environment.MachineName, exceptionType).Set(count);
}
_exceptionCounters.TryRemove(exceptionType, out var count);
_gauge.WithLabels(Environment.MachineName, exceptionType).Set(count);
}
}
}
Loading

0 comments on commit 0e1ed49

Please sign in to comment.