Skip to content

Commit

Permalink
#18 Add SetTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jan 4, 2019
1 parent b3c87bb commit 5479985
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It's a fork of [prometheus-net](https://github.com/prometheus-net/prometheus-net
prometheus-net didn't develop a long time. This is alternative was created with the possibility of rapid development:

- Support <img src="https://img.shields.io/badge/.net-4.5-green.svg"></img>, <img src="https://img.shields.io/badge/.netstandard-1.3-green.svg"></img>
and <img src="https://img.shields.io/badge/.netstandard-2.0-green.svg"></img>. This library always will be support legacy versions.
and <img src="https://img.shields.io/badge/.netstandard-2.0-green.svg"></img>.
- Independent of protobuf-net.
- More Extensions. Extensions extracted to packages.
- There are differences in the internal implementation: MetricServer, MetricPusher, Middleware.
Expand Down
15 changes: 12 additions & 3 deletions src/Prometheus.Client/Child.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Prometheus.Client.Collectors;
using Prometheus.Client.Collectors.Abstractions;
using Prometheus.Client.Contracts;
Expand All @@ -6,12 +7,14 @@ namespace Prometheus.Client
{
public abstract class Child
{
protected long? Timestamp;
private long? _timestamp;
protected bool IncludeTimestamp;
private LabelValues _labelValues;

internal virtual void Init(ICollector parent, LabelValues labelValues)
internal virtual void Init(ICollector parent, LabelValues labelValues, bool includeTimestamp)
{
_labelValues = labelValues;
IncludeTimestamp = includeTimestamp;
}

protected abstract void Populate(CMetric cMetric);
Expand All @@ -21,8 +24,14 @@ internal CMetric Collect()
var metric = new CMetric();
Populate(metric);
metric.Labels = _labelValues.WireLabels;
metric.Timestamp = Timestamp;
if (IncludeTimestamp)
metric.Timestamp = _timestamp;
return metric;
}

protected void SetTimestamp()
{
_timestamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
}
}
}
6 changes: 3 additions & 3 deletions src/Prometheus.Client/Collectors/Collector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class Collector<TChild> : ICollector
private static readonly LabelValues _emptyLabelValues = new LabelValues(new string[0], new string[0]);

protected readonly string Help;
protected readonly bool IncludeTimestamp;
private readonly bool _includeTimestamp;
protected readonly ConcurrentDictionary<LabelValues, TChild> LabelledMetrics = new ConcurrentDictionary<LabelValues, TChild>();

protected abstract CMetricType Type { get; }
Expand All @@ -32,7 +32,7 @@ protected Collector(string name, string help, bool includeTimestamp, string[] la
{
Name = name;
Help = help;
IncludeTimestamp = includeTimestamp;
_includeTimestamp = includeTimestamp;
LabelNames = labelNames;

if (!_metricNameLabelRegex.IsMatch(name))
Expand Down Expand Up @@ -69,7 +69,7 @@ private TChild GetOrAddLabelled(LabelValues key)
return LabelledMetrics.GetOrAdd(key, labels1 =>
{
var child = new TChild();
child.Init(this, labels1);
child.Init(this, labels1, _includeTimestamp);
return child;
});
}
Expand Down
7 changes: 5 additions & 2 deletions src/Prometheus.Client/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal Counter(string name, string help, string[] labelNames)
: this(name, help, false, labelNames)
{
}

internal Counter(string name, string help, bool includeTimestamp, string[] labelNames)
: base(name, help, includeTimestamp, labelNames)
{
Expand All @@ -35,7 +35,7 @@ public void Reset()
{
Unlabelled.ResetValue();
foreach (var labelledMetric in LabelledMetrics)
labelledMetric.Value.ResetValue();
labelledMetric.Value.ResetValue();
}

protected override CMetricType Type => CMetricType.Counter;
Expand All @@ -60,6 +60,9 @@ public void Inc(double increment)
throw new ArgumentOutOfRangeException(nameof(increment), "Counter cannot go down");

_value.Add(increment);

if (IncludeTimestamp)
SetTimestamp();
}

public double Value => _value.Value;
Expand Down
4 changes: 4 additions & 0 deletions src/Prometheus.Client/Gauge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ public void Inc()
public void Inc(double increment)
{
_value.Add(increment);
if (IncludeTimestamp)
SetTimestamp();
}

public void Set(double val)
{
_value.Value = val;
if (IncludeTimestamp)
SetTimestamp();
}

public void Dec()
Expand Down
9 changes: 6 additions & 3 deletions src/Prometheus.Client/Histogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal Histogram(string name, string help, string[] labelNames, double[] bucke
: this(name, help, false, labelNames, buckets)
{
}

internal Histogram(string name, string help, bool includeTimestamp, string[] labelNames, double[] buckets = null)
: base(name, help, includeTimestamp, labelNames)
{
Expand Down Expand Up @@ -69,11 +69,14 @@ public void Observe(double val)
}

_sum.Add(val);

if (IncludeTimestamp)
SetTimestamp();
}

internal override void Init(ICollector parent, LabelValues labelValues)
internal override void Init(ICollector parent, LabelValues labelValues, bool includeTimestamp)
{
base.Init(parent, labelValues);
base.Init(parent, labelValues, includeTimestamp);

_upperBounds = ((Histogram) parent)._buckets;
_bucketCounts = new ThreadSafeLong[_upperBounds.Length];
Expand Down
11 changes: 7 additions & 4 deletions src/Prometheus.Client/Summary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ public void Observe(double val)
Observe(val, DateTime.UtcNow);
}

internal override void Init(ICollector parent, LabelValues labelValues)
internal override void Init(ICollector parent, LabelValues labelValues, bool includeTimestamp)
{
Init(parent, labelValues, DateTime.UtcNow);
Init(parent, labelValues, includeTimestamp, DateTime.UtcNow);
}

internal void Init(ICollector parent, LabelValues labelValues, DateTime now)
internal void Init(ICollector parent, LabelValues labelValues, bool includeTimestamp, DateTime now)
{
base.Init(parent, labelValues);
base.Init(parent, labelValues, includeTimestamp);

_objectives = ((Summary) parent)._objectives;
_maxAge = ((Summary) parent)._maxAge;
Expand Down Expand Up @@ -240,6 +240,9 @@ internal void Observe(double val, DateTime now)

if (_hotBuf.IsFull)
Flush(now);

if (IncludeTimestamp)
SetTimestamp();
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Prometheus.Client.Tests/SummaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void TestSummaryDecay()
var sum = new Summary("test_summary", "helpless", new string[0],
objectives: new List<QuantileEpsilonPair> { new QuantileEpsilonPair(0.1d, 0.001d) }, maxAge: TimeSpan.FromSeconds(100), ageBuckets: 10);
var child = new Summary.ThisChild();
child.Init(sum, LabelValues.Empty, baseTime);
child.Init(sum, LabelValues.Empty, false, baseTime);

CSummary m;
var metric = new CMetric();
Expand Down

0 comments on commit 5479985

Please sign in to comment.