Skip to content

Commit

Permalink
Check Null values in Labels
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jun 7, 2017
1 parent 082edb9 commit e4004c1
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 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.1.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<HintPath>..\..\packages\protobuf-net.2.1.0\lib\net451\protobuf-net.dll</HintPath>
<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>
<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.1.0" targetFramework="net452" />
<package id="protobuf-net" version="2.2.1" targetFramework="net452" />
</packages>
2 changes: 2 additions & 0 deletions src/Prometheus.Client/Collectors/Collector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ namespace Prometheus.Client.Collectors
private readonly string _help;
private readonly Lazy<T> _unlabelledLazy;

// ReSharper disable StaticFieldInGenericType
private static readonly Regex MetricName = new Regex(MetricNameRe);
private static readonly Regex LabelNameRegex = new Regex("^[a-zA-Z_:][a-zA-Z0-9_:]*$");
private static readonly Regex ReservedLabelRegex = new Regex("^__.*$");
private static readonly LabelValues EmptyLabelValues = new LabelValues(new string[0], new string[0]);
// ReSharper restore StaticFieldInGenericType

protected abstract MetricType Type { get; }

Expand Down
4 changes: 1 addition & 3 deletions src/Prometheus.Client/Collectors/DotNetStatsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public class DotNetStatsCollector : IOnDemandCollector


/// <summary>
/// Constructors
/// Constructors
/// </summary>
public DotNetStatsCollector()
{
_process = Process.GetCurrentProcess();
}

/// <inheritdoc />

public void RegisterMetrics()
{
_perfErrors = Metrics.CreateCounter("dotnet_collection_errors_total", "Total number of errors that occured during collections");
Expand All @@ -43,7 +42,6 @@ public void RegisterMetrics()

// .net specific metrics
_totalMemory = Metrics.CreateGauge("dotnet_totalmemory", "Total known allocated memory");


var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
_startTime.Set((_process.StartTime.ToUniversalTime() - epoch).TotalSeconds);
Expand Down
36 changes: 25 additions & 11 deletions src/Prometheus.Client/Internal/LabelValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,39 @@ public class LabelValues
private readonly string[] _values;
internal readonly List<LabelPair> WireLabels = new List<LabelPair>();


public LabelValues(IReadOnlyCollection<string> names, string[] values)
public LabelValues(IReadOnlyCollection<string> names, IReadOnlyList<string> values)
{
if (names.Count != values.Length)
if (values == null)
throw new InvalidOperationException("Label values is null");

if (names.Count != values.Count)
throw new InvalidOperationException("Label values must be of same length as label names");
_values = values;
WireLabels.AddRange(names.Zip(values, (s, s1) => new LabelPair {name = s, value = s1}));

_values = new string[values.Count];
for (var i = 0; i < values.Count; i++)
_values[i] = values[i] ?? "";

WireLabels.AddRange(names.Zip(values, (s, s1) => new LabelPair { name = s, value = s1 }));
}


public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
var other = (LabelValues) obj;
if (ReferenceEquals(null, obj))
return false;

if (ReferenceEquals(this, obj))
return true;

if (obj.GetType() != GetType())
return false;

var labelValues = (LabelValues)obj;

if (labelValues._values.Length != _values.Length)
return false;

if (other._values.Length != _values.Length) return false;
return !_values.Where((t, i) => t != other._values[i]).Any();
return !_values.Where((t, i) => t != labelValues._values[i]).Any();
}

public override int GetHashCode()
Expand Down
6 changes: 3 additions & 3 deletions src/Prometheus.Client/Prometheus.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>.NET client for prometheus.io. Fork of prometheus-net</Description>
<Description>.NET client for prometheus.io.</Description>
<Copyright>2017 © Sergey Kuznetsov</Copyright>
<AssemblyTitle>Prometheus.Client</AssemblyTitle>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.1.1</VersionPrefix>
<Authors>Sergey Kuznetsov</Authors>
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
<AssemblyName>Prometheus.Client</AssemblyName>
Expand All @@ -18,7 +18,7 @@
</PropertyGroup>

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

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
Expand Down
18 changes: 18 additions & 0 deletions test/Prometheus.Client.Tests/MetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ public void Api_Usage()
Assert.Equal(4.4, counter.Labels("a").Value);
}

[Fact]
public void Null_Labels()
{
var counter = Metrics.CreateCounter("name2", "help2", "label1", "label2");

try
{
counter.Labels(null).Inc();
Assert.True(false);
}
catch (InvalidOperationException)
{

}

counter.Labels("param1", null).Inc(); // not down
}

[Fact]
public void Cannot_Create_Metrics_With_The_Same_Name_But_Different_Labels()
{
Expand Down
4 changes: 4 additions & 0 deletions test/Prometheus.Client.Tests/Prometheus.Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<ProjectReference Include="..\..\src\Prometheus.Client\Prometheus.Client.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>

0 comments on commit e4004c1

Please sign in to comment.