Skip to content

Commit 82935a2

Browse files
committed
extend default collectors
1 parent d5e34e5 commit 82935a2

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

src/Prometheus.Client/Collectors/DotNetStatsCollector.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Prometheus.Client.Collectors
99
/// </summary>
1010
public class DotNetStatsCollector : IOnDemandCollector
1111
{
12+
private readonly MetricFactory _metricFactory;
1213
private readonly List<Counter.ThisChild> _collectionCounts = new List<Counter.ThisChild>();
1314
private readonly Process _process;
1415
private Counter _cpuTotal;
@@ -22,26 +23,36 @@ public class DotNetStatsCollector : IOnDemandCollector
2223
/// Constructors
2324
/// </summary>
2425
public DotNetStatsCollector()
26+
: this(Metrics.DefaultFactory)
2527
{
28+
29+
}
30+
31+
/// <summary>
32+
/// Constructors
33+
/// </summary>
34+
public DotNetStatsCollector(MetricFactory metricFactory)
35+
{
36+
_metricFactory = metricFactory;
2637
_process = Process.GetCurrentProcess();
2738
}
2839

2940
/// <inheritdoc />
3041
public void RegisterMetrics()
3142
{
32-
_perfErrors = Metrics.CreateCounter("dotnet_collection_errors_total", "Total number of errors that occured during collections");
43+
_perfErrors = _metricFactory.CreateCounter("dotnet_collection_errors_total", "Total number of errors that occured during collections");
3344

34-
var collectionCountsParent = Metrics.CreateCounter("dotnet_collection_count_total", "GC collection count", "generation");
45+
var collectionCountsParent = _metricFactory.CreateCounter("dotnet_collection_count_total", "GC collection count", "generation");
3546

3647
for (var gen = 0; gen <= GC.MaxGeneration; gen++)
3748
_collectionCounts.Add(collectionCountsParent.Labels(gen.ToString()));
3849

3950
// Metrics that make sense to compare between all operating systems
40-
_startTime = Metrics.CreateGauge("process_start_time_seconds", "Start time of the process since unix epoch in seconds");
41-
_cpuTotal = Metrics.CreateCounter("process_cpu_seconds_total", "Total user and system CPU time spent in seconds");
51+
_startTime = _metricFactory.CreateGauge("process_start_time_seconds", "Start time of the process since unix epoch in seconds");
52+
_cpuTotal = _metricFactory.CreateCounter("process_cpu_seconds_total", "Total user and system CPU time spent in seconds");
4253

4354
// .net specific metrics
44-
_totalMemory = Metrics.CreateGauge("dotnet_totalmemory", "Total known allocated memory");
55+
_totalMemory = _metricFactory.CreateGauge("dotnet_totalmemory", "Total known allocated memory");
4556

4657
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
4758
_startTime.Set((_process.StartTime.ToUniversalTime() - epoch).TotalSeconds);

src/Prometheus.Client/Collectors/PerfCounterCollector.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Prometheus.Client.Collectors
1111
/// </summary>
1212
public class PerfCounterCollector : IOnDemandCollector
1313
{
14+
private readonly MetricFactory _metricFactory;
1415
private const string MemCat = ".NET CLR Memory";
1516
private const string ProcCat = "Process";
1617

@@ -31,11 +32,22 @@ public class PerfCounterCollector : IOnDemandCollector
3132
private readonly string _instanceName;
3233
private Counter _perfErrors;
3334

35+
3436
/// <summary>
3537
/// Constructor
3638
/// </summary>
3739
public PerfCounterCollector()
40+
: this(Metrics.DefaultFactory)
41+
{
42+
43+
}
44+
45+
/// <summary>
46+
/// Constructor
47+
/// </summary>
48+
public PerfCounterCollector(MetricFactory metricFactory)
3849
{
50+
_metricFactory = metricFactory;
3951
var currentProcess = Process.GetCurrentProcess();
4052
_instanceName = currentProcess.ProcessName;
4153
if (IsLinux())
@@ -55,7 +67,7 @@ public void RegisterMetrics()
5567
RegisterPerfCounter(category, name);
5668
}
5769

58-
_perfErrors = Metrics.CreateCounter("performance_counter_errors_total", "Total number of errors that occured during performance counter collections");
70+
_perfErrors = _metricFactory.CreateCounter("performance_counter_errors_total", "Total number of errors that occured during performance counter collections");
5971
}
6072

6173
/// <summary>
@@ -81,7 +93,7 @@ private static bool IsLinux()
8193

8294
private void RegisterPerfCounter(string category, string name)
8395
{
84-
var gauge = Metrics.CreateGauge(GetName(category, name), GetHelp(name));
96+
var gauge = _metricFactory.CreateGauge(GetName(category, name), GetHelp(name));
8597
_collectors.Add(Tuple.Create(gauge, new PerformanceCounter(category, name, _instanceName)));
8698
}
8799

src/Prometheus.Client/Collectors/WindowsDotNetStatsCollector.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Prometheus.Client.Collectors
88
/// </summary>
99
public class WindowsDotNetStatsCollector : IOnDemandCollector
1010
{
11+
private readonly MetricFactory _metricFactory;
1112
private readonly Process _process;
1213
private Gauge _numThreads;
1314

@@ -17,25 +18,36 @@ public class WindowsDotNetStatsCollector : IOnDemandCollector
1718
private Gauge _virtualMemorySize;
1819
private Gauge _workingSet;
1920

21+
2022
/// <summary>
2123
/// Constructors
2224
/// </summary>
2325
public WindowsDotNetStatsCollector()
26+
: this(Metrics.DefaultFactory)
27+
{
28+
29+
}
30+
31+
/// <summary>
32+
/// Constructors
33+
/// </summary>
34+
public WindowsDotNetStatsCollector(MetricFactory metricFactory)
2435
{
36+
_metricFactory = metricFactory;
2537
_process = Process.GetCurrentProcess();
2638
}
2739

2840
/// <inheritdoc />
2941
public void RegisterMetrics()
3042
{
31-
_perfErrors = Metrics.CreateCounter("dotnet_collection_errors_total", "Total number of errors that occured during collections");
43+
_perfErrors = _metricFactory.CreateCounter("dotnet_collection_errors_total", "Total number of errors that occured during collections");
3244

3345
// Windows specific metrics
34-
_virtualMemorySize = Metrics.CreateGauge("process_windows_virtual_bytes", "Process virtual memory size");
35-
_workingSet = Metrics.CreateGauge("process_windows_working_set", "Process working set");
36-
_privateMemorySize = Metrics.CreateGauge("process_windows_private_bytes", "Process private memory size");
37-
_numThreads = Metrics.CreateGauge("process_windows_num_threads", "Total number of threads");
38-
_pid = Metrics.CreateGauge("process_windows_processid", "Process ID");
46+
_virtualMemorySize = _metricFactory.CreateGauge("process_windows_virtual_bytes", "Process virtual memory size");
47+
_workingSet = _metricFactory.CreateGauge("process_windows_working_set", "Process working set");
48+
_privateMemorySize = _metricFactory.CreateGauge("process_windows_private_bytes", "Process private memory size");
49+
_numThreads = _metricFactory.CreateGauge("process_windows_num_threads", "Total number of threads");
50+
_pid = _metricFactory.CreateGauge("process_windows_processid", "Process ID");
3951
_pid.Set(_process.Id);
4052
}
4153

src/Prometheus.Client/Metrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Prometheus.Client
1010
/// </summary>
1111
public static class Metrics
1212
{
13-
private static readonly MetricFactory DefaultFactory = new MetricFactory(CollectorRegistry.Instance);
13+
public static readonly MetricFactory DefaultFactory = new MetricFactory(CollectorRegistry.Instance);
1414

1515
/// <summary>
1616
/// Create Counter in default MetricFactory

src/Prometheus.Client/Prometheus.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Description>.NET client for prometheus.io.</Description>
55
<Copyright>2017 © Sergey Kuznetsov</Copyright>
66
<AssemblyTitle>Prometheus.Client</AssemblyTitle>
7-
<VersionPrefix>1.1.3</VersionPrefix>
7+
<VersionPrefix>1.2.1</VersionPrefix>
88
<Authors>Sergey Kuznetsov</Authors>
99
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
1010
<AssemblyName>Prometheus.Client</AssemblyName>

0 commit comments

Comments
 (0)