Skip to content

Commit d4316ba

Browse files
committed
small refactor
1 parent 96f5412 commit d4316ba

File tree

10 files changed

+136
-68
lines changed

10 files changed

+136
-68
lines changed

examples/WebApiApplication/WebApiApplication.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
<HintPath>..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
4848
</Reference>
4949
<Reference Include="Microsoft.CSharp" />
50-
<Reference Include="protobuf-net, Version=2.2.1.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
51-
<HintPath>..\..\packages\protobuf-net.2.2.1\lib\net40\protobuf-net.dll</HintPath>
50+
<Reference Include="protobuf-net, Version=2.3.1.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
51+
<HintPath>..\..\packages\protobuf-net.2.3.1\lib\net40\protobuf-net.dll</HintPath>
5252
</Reference>
5353
<Reference Include="System.Net.Http" />
5454
<Reference Include="System.Web.DynamicData" />

examples/WebApiApplication/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.3" targetFramework="net452" />
88
<package id="Microsoft.Net.Compilers" version="1.3.2" targetFramework="net452" developmentDependency="true" />
99
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
10-
<package id="protobuf-net" version="2.2.1" targetFramework="net452" />
10+
<package id="protobuf-net" version="2.3.1" targetFramework="net452" />
1111
</packages>

src/Prometheus.Client/Collectors/PerfCounterCollector.cs

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
namespace Prometheus.Client.Collectors
88
{
99
/// <summary>
10-
/// Collects metrics on standard Performance Counters
10+
/// Collects metrics on standard Performance Counters
1111
/// </summary>
1212
public class PerfCounterCollector : IOnDemandCollector
1313
{
1414
private const string MemCat = ".NET CLR Memory";
1515
private const string ProcCat = "Process";
16-
16+
1717
private static readonly string[] StandardPerfCounters =
1818
{
1919
MemCat, "Gen 0 heap size",
@@ -24,75 +24,46 @@ public class PerfCounterCollector : IOnDemandCollector
2424
ProcCat, "% Processor Time",
2525
ProcCat, "Private Bytes",
2626
ProcCat, "Working Set",
27-
ProcCat, "Virtual Bytes",
27+
ProcCat, "Virtual Bytes"
2828
};
2929

30-
readonly List<Tuple<Gauge, PerformanceCounter>> _collectors = new List<Tuple<Gauge, PerformanceCounter>>();
30+
private readonly List<Tuple<Gauge, PerformanceCounter>> _collectors = new List<Tuple<Gauge, PerformanceCounter>>();
3131
private readonly string _instanceName;
3232
private Counter _perfErrors;
3333

34-
private static bool IsLinux()
35-
{
36-
switch (Environment.OSVersion.Platform)
37-
{
38-
case PlatformID.Unix:
39-
return true;
40-
41-
default:
42-
return false;
43-
}
44-
}
45-
34+
/// <summary>
35+
/// Constructor
36+
/// </summary>
4637
public PerfCounterCollector()
4738
{
48-
Process currentProcess = Process.GetCurrentProcess();
39+
var currentProcess = Process.GetCurrentProcess();
4940
_instanceName = currentProcess.ProcessName;
5041
if (IsLinux())
51-
{
52-
//on mono/linux instance name should be pid
5342
_instanceName = currentProcess.Id.ToString();
54-
}
55-
}
56-
57-
private void RegisterPerfCounter(string category, string name)
58-
{
59-
Gauge gauge = Metrics.CreateGauge(GetName(category, name), GetHelp(name));
60-
_collectors.Add(Tuple.Create(gauge, new PerformanceCounter(category, name, _instanceName)));
61-
}
62-
63-
private string GetHelp(string name)
64-
{
65-
return name + " Perf Counter";
66-
}
67-
68-
private string GetName(string category, string name)
69-
{
70-
return ToPromName(category) + "_" + ToPromName(name);
71-
}
72-
73-
private string ToPromName(string name)
74-
{
75-
return name.Replace("%", "pct").Replace(" ", "_").Replace(".", "dot").ToLowerInvariant();
7643
}
7744

45+
/// <summary>
46+
/// Register metrics
47+
/// </summary>
7848
public void RegisterMetrics()
7949
{
80-
for (int i = 0; i < StandardPerfCounters.Length; i += 2)
50+
for (var i = 0; i < StandardPerfCounters.Length; i += 2)
8151
{
8252
var category = StandardPerfCounters[i];
8353
var name = StandardPerfCounters[i + 1];
8454

8555
RegisterPerfCounter(category, name);
8656
}
8757

88-
_perfErrors = Metrics.CreateCounter("performance_counter_errors_total",
89-
"Total number of errors that occured during performance counter collections");
58+
_perfErrors = Metrics.CreateCounter("performance_counter_errors_total", "Total number of errors that occured during performance counter collections");
9059
}
9160

61+
/// <summary>
62+
/// Update metrics
63+
/// </summary>
9264
public void UpdateMetrics()
9365
{
9466
foreach (var collector in _collectors)
95-
{
9667
try
9768
{
9869
collector.Item1.Set(collector.Item2.NextValue());
@@ -101,7 +72,32 @@ public void UpdateMetrics()
10172
{
10273
_perfErrors.Inc();
10374
}
104-
}
75+
}
76+
77+
private static bool IsLinux()
78+
{
79+
return Environment.OSVersion.Platform == PlatformID.Unix;
80+
}
81+
82+
private void RegisterPerfCounter(string category, string name)
83+
{
84+
var gauge = Metrics.CreateGauge(GetName(category, name), GetHelp(name));
85+
_collectors.Add(Tuple.Create(gauge, new PerformanceCounter(category, name, _instanceName)));
86+
}
87+
88+
private static string GetHelp(string name)
89+
{
90+
return name + " Perf Counter";
91+
}
92+
93+
private static string GetName(string category, string name)
94+
{
95+
return ToPromName(category) + "_" + ToPromName(name);
96+
}
97+
98+
private static string ToPromName(string name)
99+
{
100+
return name.Replace("%", "pct").Replace(" ", "_").Replace(".", "dot").ToLowerInvariant();
105101
}
106102
}
107103
}

src/Prometheus.Client/Counter.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@
55

66
namespace Prometheus.Client
77
{
8+
/// <summary>
9+
/// Counter metric type
10+
/// <remarks>
11+
/// https://prometheus.io/docs/concepts/metric_types/#counter
12+
/// </remarks>
13+
/// </summary>
814
public interface ICounter
915
{
1016
void Inc(double increment = 1.0D);
1117
double Value { get; }
1218
}
1319

20+
/// <summary>
21+
/// Counter metric type
22+
/// <remarks>
23+
/// https://prometheus.io/docs/concepts/metric_types/#counter
24+
/// </remarks>
25+
/// </summary>
1426
public class Counter : Collector<Counter.ThisChild>, ICounter
1527
{
1628
internal Counter(string name, string help, string[] labelNames)
@@ -29,7 +41,7 @@ public class ThisChild : Child, ICounter
2941

3042
protected override void Populate(Metric metric)
3143
{
32-
metric.counter = new Contracts.Counter {value = Value};
44+
metric.counter = new Contracts.Counter { value = Value };
3345
}
3446

3547
public void Inc(double increment = 1.0D)

src/Prometheus.Client/Gauge.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
namespace Prometheus.Client
66
{
7+
/// <summary>
8+
/// Gauge metric type
9+
/// <remarks>
10+
/// https://prometheus.io/docs/concepts/metric_types/#gauge
11+
/// </remarks>
12+
/// </summary>
713
public interface IGauge
814
{
915
double Value { get; }
@@ -12,6 +18,12 @@ public interface IGauge
1218
void Dec(double decrement = 1.0D);
1319
}
1420

21+
/// <summary>
22+
/// Gauge metric type
23+
/// <remarks>
24+
/// https://prometheus.io/docs/concepts/metric_types/#gauge
25+
/// </remarks>
26+
/// </summary>
1527
public class Gauge : Collector<Gauge.ThisChild>, IGauge
1628
{
1729
internal Gauge(string name, string help, string[] labelNames)

src/Prometheus.Client/Histogram.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@
66

77
namespace Prometheus.Client
88
{
9+
/// <summary>
10+
/// Histogram metric type
11+
/// <remarks>
12+
/// https://prometheus.io/docs/concepts/metric_types/#histogram
13+
/// </remarks>
14+
/// </summary>
915
public interface IHistogram
1016
{
1117
void Observe(double val);
1218
}
1319

20+
/// <summary>
21+
/// Histogram metric type
22+
/// <remarks>
23+
/// https://prometheus.io/docs/concepts/metric_types/#histogram
24+
/// </remarks>
25+
/// </summary>
1426
public class Histogram : Collector<Histogram.ThisChild>, IHistogram
1527
{
1628
private static readonly double[] DefaultBuckets = { .005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10 };
@@ -94,6 +106,9 @@ public void Observe(double val)
94106
}
95107
}
96108

109+
/// <summary>
110+
/// Metric Type
111+
/// </summary>
97112
protected override MetricType Type => MetricType.HISTOGRAM;
98113

99114
public void Observe(double val)

src/Prometheus.Client/Metrics.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,56 @@
55

66
namespace Prometheus.Client
77
{
8+
/// <summary>
9+
/// Metrics creator
10+
/// </summary>
811
public static class Metrics
912
{
1013
private static readonly MetricFactory DefaultFactory = new MetricFactory(CollectorRegistry.Instance);
1114

15+
/// <summary>
16+
/// Create Counter in default MetricFactory
17+
/// </summary>
1218
public static Counter CreateCounter(string name, string help, params string[] labelNames)
1319
{
1420
return DefaultFactory.CreateCounter(name, help, labelNames);
1521
}
1622

23+
/// <summary>
24+
/// Create Gauge in default MetricFactory
25+
/// </summary>
1726
public static Gauge CreateGauge(string name, string help, params string[] labelNames)
1827
{
1928
return DefaultFactory.CreateGauge(name, help, labelNames);
2029
}
2130

31+
/// <summary>
32+
/// Create Summary in default MetricFactory
33+
/// </summary>
2234
public static Summary CreateSummary(string name, string help, params string[] labelNames)
2335
{
2436
return DefaultFactory.CreateSummary(name, help, labelNames);
2537
}
2638

39+
/// <summary>
40+
/// Create Summary in default MetricFactory
41+
/// </summary>
2742
public static Summary CreateSummary(string name, string help, string[] labelNames, IList<QuantileEpsilonPair> objectives, TimeSpan maxAge, int? ageBuckets, int? bufCap)
2843
{
2944
return DefaultFactory.CreateSummary(name, help, labelNames, objectives, maxAge, ageBuckets, bufCap);
3045
}
3146

47+
/// <summary>
48+
/// Create Histogram in default MetricFactory
49+
/// </summary>
3250
public static Histogram CreateHistogram(string name, string help, double[] buckets = null, params string[] labelNames)
3351
{
3452
return DefaultFactory.CreateHistogram(name, help, buckets, labelNames);
3553
}
3654

55+
/// <summary>
56+
/// Create MetricFactory with custom Registry
57+
/// </summary>
3758
public static MetricFactory WithCustomRegistry(ICollectorRegistry registry)
3859
{
3960
return new MetricFactory(registry);

src/Prometheus.Client/Prometheus.Client.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="protobuf-net" Version="2.2.1" />
22+
<PackageReference Include="protobuf-net" Version="2.3.1" />
2323
</ItemGroup>
2424

2525
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
@@ -31,6 +31,10 @@
3131
<DefineConstants>$(DefineConstants);COREFX</DefineConstants>
3232
</PropertyGroup>
3333

34+
<PropertyGroup>
35+
<NoWarn>1591</NoWarn>
36+
</PropertyGroup>
37+
3438
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
3539

3640
</ItemGroup>

0 commit comments

Comments
 (0)