Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jul 30, 2017
1 parent 96f5412 commit d4316ba
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 68 deletions.
4 changes: 2 additions & 2 deletions examples/WebApiApplication/WebApiApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
<HintPath>..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="protobuf-net, Version=2.2.1.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<HintPath>..\..\packages\protobuf-net.2.2.1\lib\net40\protobuf-net.dll</HintPath>
<Reference Include="protobuf-net, Version=2.3.1.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<HintPath>..\..\packages\protobuf-net.2.3.1\lib\net40\protobuf-net.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.Web.DynamicData" />
Expand Down
2 changes: 1 addition & 1 deletion examples/WebApiApplication/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.3" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.3.2" targetFramework="net452" developmentDependency="true" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="protobuf-net" version="2.2.1" targetFramework="net452" />
<package id="protobuf-net" version="2.3.1" targetFramework="net452" />
</packages>
88 changes: 42 additions & 46 deletions src/Prometheus.Client/Collectors/PerfCounterCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
namespace Prometheus.Client.Collectors
{
/// <summary>
/// Collects metrics on standard Performance Counters
/// Collects metrics on standard Performance Counters
/// </summary>
public class PerfCounterCollector : IOnDemandCollector
{
private const string MemCat = ".NET CLR Memory";
private const string ProcCat = "Process";

private static readonly string[] StandardPerfCounters =
{
MemCat, "Gen 0 heap size",
Expand All @@ -24,75 +24,46 @@ public class PerfCounterCollector : IOnDemandCollector
ProcCat, "% Processor Time",
ProcCat, "Private Bytes",
ProcCat, "Working Set",
ProcCat, "Virtual Bytes",
ProcCat, "Virtual Bytes"
};

readonly List<Tuple<Gauge, PerformanceCounter>> _collectors = new List<Tuple<Gauge, PerformanceCounter>>();
private readonly List<Tuple<Gauge, PerformanceCounter>> _collectors = new List<Tuple<Gauge, PerformanceCounter>>();
private readonly string _instanceName;
private Counter _perfErrors;

private static bool IsLinux()
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
return true;

default:
return false;
}
}

/// <summary>
/// Constructor
/// </summary>
public PerfCounterCollector()
{
Process currentProcess = Process.GetCurrentProcess();
var currentProcess = Process.GetCurrentProcess();
_instanceName = currentProcess.ProcessName;
if (IsLinux())
{
//on mono/linux instance name should be pid
_instanceName = currentProcess.Id.ToString();
}
}

private void RegisterPerfCounter(string category, string name)
{
Gauge gauge = Metrics.CreateGauge(GetName(category, name), GetHelp(name));
_collectors.Add(Tuple.Create(gauge, new PerformanceCounter(category, name, _instanceName)));
}

private string GetHelp(string name)
{
return name + " Perf Counter";
}

private string GetName(string category, string name)
{
return ToPromName(category) + "_" + ToPromName(name);
}

private string ToPromName(string name)
{
return name.Replace("%", "pct").Replace(" ", "_").Replace(".", "dot").ToLowerInvariant();
}

/// <summary>
/// Register metrics
/// </summary>
public void RegisterMetrics()
{
for (int i = 0; i < StandardPerfCounters.Length; i += 2)
for (var i = 0; i < StandardPerfCounters.Length; i += 2)
{
var category = StandardPerfCounters[i];
var name = StandardPerfCounters[i + 1];

RegisterPerfCounter(category, name);
}

_perfErrors = Metrics.CreateCounter("performance_counter_errors_total",
"Total number of errors that occured during performance counter collections");
_perfErrors = Metrics.CreateCounter("performance_counter_errors_total", "Total number of errors that occured during performance counter collections");
}

/// <summary>
/// Update metrics
/// </summary>
public void UpdateMetrics()
{
foreach (var collector in _collectors)
{
try
{
collector.Item1.Set(collector.Item2.NextValue());
Expand All @@ -101,7 +72,32 @@ public void UpdateMetrics()
{
_perfErrors.Inc();
}
}
}

private static bool IsLinux()
{
return Environment.OSVersion.Platform == PlatformID.Unix;
}

private void RegisterPerfCounter(string category, string name)
{
var gauge = Metrics.CreateGauge(GetName(category, name), GetHelp(name));
_collectors.Add(Tuple.Create(gauge, new PerformanceCounter(category, name, _instanceName)));
}

private static string GetHelp(string name)
{
return name + " Perf Counter";
}

private static string GetName(string category, string name)
{
return ToPromName(category) + "_" + ToPromName(name);
}

private static string ToPromName(string name)
{
return name.Replace("%", "pct").Replace(" ", "_").Replace(".", "dot").ToLowerInvariant();
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/Prometheus.Client/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@

namespace Prometheus.Client
{
/// <summary>
/// Counter metric type
/// <remarks>
/// https://prometheus.io/docs/concepts/metric_types/#counter
/// </remarks>
/// </summary>
public interface ICounter
{
void Inc(double increment = 1.0D);
double Value { get; }
}

/// <summary>
/// Counter metric type
/// <remarks>
/// https://prometheus.io/docs/concepts/metric_types/#counter
/// </remarks>
/// </summary>
public class Counter : Collector<Counter.ThisChild>, ICounter
{
internal Counter(string name, string help, string[] labelNames)
Expand All @@ -29,7 +41,7 @@ public class ThisChild : Child, ICounter

protected override void Populate(Metric metric)
{
metric.counter = new Contracts.Counter {value = Value};
metric.counter = new Contracts.Counter { value = Value };
}

public void Inc(double increment = 1.0D)
Expand Down
12 changes: 12 additions & 0 deletions src/Prometheus.Client/Gauge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

namespace Prometheus.Client
{
/// <summary>
/// Gauge metric type
/// <remarks>
/// https://prometheus.io/docs/concepts/metric_types/#gauge
/// </remarks>
/// </summary>
public interface IGauge
{
double Value { get; }
Expand All @@ -12,6 +18,12 @@ public interface IGauge
void Dec(double decrement = 1.0D);
}

/// <summary>
/// Gauge metric type
/// <remarks>
/// https://prometheus.io/docs/concepts/metric_types/#gauge
/// </remarks>
/// </summary>
public class Gauge : Collector<Gauge.ThisChild>, IGauge
{
internal Gauge(string name, string help, string[] labelNames)
Expand Down
15 changes: 15 additions & 0 deletions src/Prometheus.Client/Histogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@

namespace Prometheus.Client
{
/// <summary>
/// Histogram metric type
/// <remarks>
/// https://prometheus.io/docs/concepts/metric_types/#histogram
/// </remarks>
/// </summary>
public interface IHistogram
{
void Observe(double val);
}

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

/// <summary>
/// Metric Type
/// </summary>
protected override MetricType Type => MetricType.HISTOGRAM;

public void Observe(double val)
Expand Down
21 changes: 21 additions & 0 deletions src/Prometheus.Client/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,56 @@

namespace Prometheus.Client
{
/// <summary>
/// Metrics creator
/// </summary>
public static class Metrics
{
private static readonly MetricFactory DefaultFactory = new MetricFactory(CollectorRegistry.Instance);

/// <summary>
/// Create Counter in default MetricFactory
/// </summary>
public static Counter CreateCounter(string name, string help, params string[] labelNames)
{
return DefaultFactory.CreateCounter(name, help, labelNames);
}

/// <summary>
/// Create Gauge in default MetricFactory
/// </summary>
public static Gauge CreateGauge(string name, string help, params string[] labelNames)
{
return DefaultFactory.CreateGauge(name, help, labelNames);
}

/// <summary>
/// Create Summary in default MetricFactory
/// </summary>
public static Summary CreateSummary(string name, string help, params string[] labelNames)
{
return DefaultFactory.CreateSummary(name, help, labelNames);
}

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

/// <summary>
/// Create Histogram in default MetricFactory
/// </summary>
public static Histogram CreateHistogram(string name, string help, double[] buckets = null, params string[] labelNames)
{
return DefaultFactory.CreateHistogram(name, help, buckets, labelNames);
}

/// <summary>
/// Create MetricFactory with custom Registry
/// </summary>
public static MetricFactory WithCustomRegistry(ICollectorRegistry registry)
{
return new MetricFactory(registry);
Expand Down
6 changes: 5 additions & 1 deletion src/Prometheus.Client/Prometheus.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="protobuf-net" Version="2.2.1" />
<PackageReference Include="protobuf-net" Version="2.3.1" />
</ItemGroup>

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

<PropertyGroup>
<NoWarn>1591</NoWarn>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">

</ItemGroup>
Expand Down
Loading

0 comments on commit d4316ba

Please sign in to comment.