Skip to content

Commit e4004c1

Browse files
committed
Check Null values in Labels
1 parent 082edb9 commit e4004c1

File tree

8 files changed

+56
-20
lines changed

8 files changed

+56
-20
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.1.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
51-
<HintPath>..\..\packages\protobuf-net.2.1.0\lib\net451\protobuf-net.dll</HintPath>
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>
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.1.0" targetFramework="net452" />
10+
<package id="protobuf-net" version="2.2.1" targetFramework="net452" />
1111
</packages>

src/Prometheus.Client/Collectors/Collector.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ namespace Prometheus.Client.Collectors
1414
private readonly string _help;
1515
private readonly Lazy<T> _unlabelledLazy;
1616

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

2224
protected abstract MetricType Type { get; }
2325

src/Prometheus.Client/Collectors/DotNetStatsCollector.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ public class DotNetStatsCollector : IOnDemandCollector
1919

2020

2121
/// <summary>
22-
/// Constructors
22+
/// Constructors
2323
/// </summary>
2424
public DotNetStatsCollector()
2525
{
2626
_process = Process.GetCurrentProcess();
2727
}
2828

2929
/// <inheritdoc />
30-
3130
public void RegisterMetrics()
3231
{
3332
_perfErrors = Metrics.CreateCounter("dotnet_collection_errors_total", "Total number of errors that occured during collections");
@@ -43,7 +42,6 @@ public void RegisterMetrics()
4342

4443
// .net specific metrics
4544
_totalMemory = Metrics.CreateGauge("dotnet_totalmemory", "Total known allocated memory");
46-
4745

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

src/Prometheus.Client/Internal/LabelValues.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,39 @@ public class LabelValues
1111
private readonly string[] _values;
1212
internal readonly List<LabelPair> WireLabels = new List<LabelPair>();
1313

14-
15-
public LabelValues(IReadOnlyCollection<string> names, string[] values)
14+
public LabelValues(IReadOnlyCollection<string> names, IReadOnlyList<string> values)
1615
{
17-
if (names.Count != values.Length)
16+
if (values == null)
17+
throw new InvalidOperationException("Label values is null");
18+
19+
if (names.Count != values.Count)
1820
throw new InvalidOperationException("Label values must be of same length as label names");
19-
_values = values;
20-
WireLabels.AddRange(names.Zip(values, (s, s1) => new LabelPair {name = s, value = s1}));
21+
22+
_values = new string[values.Count];
23+
for (var i = 0; i < values.Count; i++)
24+
_values[i] = values[i] ?? "";
25+
26+
WireLabels.AddRange(names.Zip(values, (s, s1) => new LabelPair { name = s, value = s1 }));
2127
}
2228

2329

2430
public override bool Equals(object obj)
2531
{
26-
if (ReferenceEquals(null, obj)) return false;
27-
if (ReferenceEquals(this, obj)) return true;
28-
if (obj.GetType() != GetType()) return false;
29-
var other = (LabelValues) obj;
32+
if (ReferenceEquals(null, obj))
33+
return false;
34+
35+
if (ReferenceEquals(this, obj))
36+
return true;
37+
38+
if (obj.GetType() != GetType())
39+
return false;
40+
41+
var labelValues = (LabelValues)obj;
42+
43+
if (labelValues._values.Length != _values.Length)
44+
return false;
3045

31-
if (other._values.Length != _values.Length) return false;
32-
return !_values.Where((t, i) => t != other._values[i]).Any();
46+
return !_values.Where((t, i) => t != labelValues._values[i]).Any();
3347
}
3448

3549
public override int GetHashCode()

src/Prometheus.Client/Prometheus.Client.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Description>.NET client for prometheus.io. Fork of prometheus-net</Description>
4+
<Description>.NET client for prometheus.io.</Description>
55
<Copyright>2017 © Sergey Kuznetsov</Copyright>
66
<AssemblyTitle>Prometheus.Client</AssemblyTitle>
7-
<VersionPrefix>1.1.0</VersionPrefix>
7+
<VersionPrefix>1.1.1</VersionPrefix>
88
<Authors>Sergey Kuznetsov</Authors>
99
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
1010
<AssemblyName>Prometheus.Client</AssemblyName>
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="protobuf-net" Version="2.1.0" />
21+
<PackageReference Include="protobuf-net" Version="2.2.1" />
2222
</ItemGroup>
2323

2424
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">

test/Prometheus.Client.Tests/MetricsTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ public void Api_Usage()
3939
Assert.Equal(4.4, counter.Labels("a").Value);
4040
}
4141

42+
[Fact]
43+
public void Null_Labels()
44+
{
45+
var counter = Metrics.CreateCounter("name2", "help2", "label1", "label2");
46+
47+
try
48+
{
49+
counter.Labels(null).Inc();
50+
Assert.True(false);
51+
}
52+
catch (InvalidOperationException)
53+
{
54+
55+
}
56+
57+
counter.Labels("param1", null).Inc(); // not down
58+
}
59+
4260
[Fact]
4361
public void Cannot_Create_Metrics_With_The_Same_Name_But_Different_Labels()
4462
{

test/Prometheus.Client.Tests/Prometheus.Client.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
<ProjectReference Include="..\..\src\Prometheus.Client\Prometheus.Client.csproj" />
2222
</ItemGroup>
2323

24+
<ItemGroup>
25+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
26+
</ItemGroup>
27+
2428
</Project>

0 commit comments

Comments
 (0)