-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from atidev/group-metrics
Group prometheus metrics
- Loading branch information
Showing
14 changed files
with
986 additions
and
950 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 34 additions & 44 deletions
78
ATI.Services.Common/Metrics/ExceptionsMetricsCollector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.