Skip to content

Commit

Permalink
Merge pull request #74 from PrometheusClientNet/gauge-to-accept-NaN
Browse files Browse the repository at this point in the history
Add NaN support in Gauge #65
  • Loading branch information
phnx47 authored Aug 14, 2019
2 parents f1e763e + 1e34eed commit ee01891
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/Prometheus.Client/Gauge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ public void Set(double val)

public void Set(double val, long? timestamp)
{
if (double.IsNaN(val))
return;

_value.Value = val;
TimestampIfRequired(timestamp);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Prometheus.Client/ThreadSafeDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void Add(double increment)
long initialValue = Interlocked.Read(ref _value);
double computedValue = BitConverter.Int64BitsToDouble(initialValue) + increment;

if (double.IsNaN(computedValue))
throw new InvalidOperationException("Cannot increment the NaN value.");

if (initialValue == Interlocked.CompareExchange(ref _value, BitConverter.DoubleToInt64Bits(computedValue), initialValue))
return;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Prometheus.Client.Tests/GaugeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public async Task Collection()
gauge2.Inc(1);
gauge2.WithLabels("any", "2").Dec(5);

var nanGauge = factory.CreateGauge("nangauge", "example of NaN");
nanGauge.Set(double.NaN);

string formattedText = null;

using (var stream = new MemoryStream())
Expand All @@ -89,6 +92,7 @@ public async Task Collection()
{
((ICollector)gauge).Collect(writer);
((ICollector)gauge2).Collect(writer);
((ICollector)nanGauge).Collect(writer);
await writer.CloseWriterAsync();
}

Expand Down Expand Up @@ -264,5 +268,44 @@ public void WithoutLabels()

Assert.Equal(2, gauge.Value);
}

[Fact]
public void ShouldAllowNaN()
{
var registry = new CollectorRegistry();
var factory = new MetricFactory(registry);

var gauge = factory.CreateGauge("test_gauge", string.Empty);

gauge.Set(double.NaN);

Assert.Equal(double.NaN, gauge.Value);
}

[Fact]
public void ShouldThrowOnIncIfNaN()
{
var registry = new CollectorRegistry();
var factory = new MetricFactory(registry);

var gauge = factory.CreateGauge("test_gauge", string.Empty);

gauge.Set(double.NaN);

Assert.Throws<InvalidOperationException>(() => gauge.Inc());
}

[Fact]
public void ShouldThrowOnDecIfNaN()
{
var registry = new CollectorRegistry();
var factory = new MetricFactory(registry);

var gauge = factory.CreateGauge("test_gauge", string.Empty);

gauge.Set(double.NaN);

Assert.Throws<InvalidOperationException>(() => gauge.Dec());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ test{category="some"} 5
# TYPE nextgauge gauge
nextgauge 1
nextgauge{group="any",type="2"} -5
# HELP nangauge example of NaN
# TYPE nangauge gauge
nangauge NaN

0 comments on commit ee01891

Please sign in to comment.