Skip to content

Commit

Permalink
#18 Add Timestamp to TextFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jan 4, 2019
1 parent bce89e9 commit 544a6e5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/Prometheus.Client/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ private static void WriteMetric(StreamWriter streamWriter, CMetricFamily family,

if (metric.CGauge != null)
{
WriteMetricValue(streamWriter, familyName, null, metric.CGauge.Value, metric.Labels);
WriteMetricValue(streamWriter, familyName, null, metric.CGauge.Value, metric.Timestamp, metric.Labels);
}
else if (metric.CCounter != null)
{
WriteMetricValue(streamWriter, familyName, null, metric.CCounter.Value, metric.Labels);
WriteMetricValue(streamWriter, familyName, null, metric.CCounter.Value, metric.Timestamp, metric.Labels);
}
else if (metric.CSummary != null)
{
WriteMetricValue(streamWriter, familyName, "_sum", metric.CSummary.SampleSum, metric.Labels);
WriteMetricValue(streamWriter, familyName, "_count", metric.CSummary.SampleCount, metric.Labels);
WriteMetricValue(streamWriter, familyName, "_sum", metric.CSummary.SampleSum, metric.Timestamp, metric.Labels);
WriteMetricValue(streamWriter, familyName, "_count", metric.CSummary.SampleCount, metric.Timestamp, metric.Labels);

foreach (var quantileValuePair in metric.CSummary.Quantiles)
{
Expand All @@ -65,13 +65,13 @@ private static void WriteMetric(StreamWriter streamWriter, CMetricFamily family,
new CLabelPair { Name = "quantile", Value = quantile }
}).ToArray();

WriteMetricValue(streamWriter, familyName, null, quantileValuePair.Value, quantileLabels);
WriteMetricValue(streamWriter, familyName, null, quantileValuePair.Value, metric.Timestamp, quantileLabels);
}
}
else if (metric.CHistogram != null)
{
WriteMetricValue(streamWriter, familyName, "_sum", metric.CHistogram.SampleSum, metric.Labels);
WriteMetricValue(streamWriter, familyName, "_count", metric.CHistogram.SampleCount, metric.Labels);
WriteMetricValue(streamWriter, familyName, "_sum", metric.CHistogram.SampleSum, metric.Timestamp, metric.Labels);
WriteMetricValue(streamWriter, familyName, "_count", metric.CHistogram.SampleCount, metric.Timestamp, metric.Labels);

foreach (var bucket in metric.CHistogram.Buckets)
{
Expand All @@ -82,12 +82,12 @@ private static void WriteMetric(StreamWriter streamWriter, CMetricFamily family,
new CLabelPair { Name = "le", Value = value }
}).ToArray();

WriteMetricValue(streamWriter, familyName, "_bucket", bucket.CumulativeCount, bucketLabels);
WriteMetricValue(streamWriter, familyName, "_bucket", bucket.CumulativeCount, metric.Timestamp, bucketLabels);
}
}
}

private static void WriteMetricValue(StreamWriter writer, string familyName, string postfix, double value, CLabelPair[] labels)
private static void WriteMetricValue(StreamWriter writer, string familyName, string postfix, double value, long? timestamp, CLabelPair[] labels)
{
writer.Write(familyName);

Expand Down Expand Up @@ -117,7 +117,15 @@ private static void WriteMetricValue(StreamWriter writer, string familyName, str
}

writer.Write(' ');
writer.WriteLine(value.ToString(CultureInfo.InvariantCulture));
writer.Write(value.ToString(CultureInfo.InvariantCulture));

if (timestamp.HasValue)
{
writer.Write(' ');
writer.Write(timestamp.Value);
}

writer.WriteLine();
}


Expand Down
72 changes: 69 additions & 3 deletions tests/Prometheus.Client.Tests/TextFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
using System.Text;
using Prometheus.Client.Contracts;
using Xunit;
using Xunit.Abstractions;

namespace Prometheus.Client.Tests
{
public sealed class TextFormatterTests
{
private readonly ITestOutputHelper _output;

public TextFormatterTests(ITestOutputHelper output)
{
_output = output;
}

[Theory]
[InlineData("simple-label-value-1")]
[InlineData("with\nlinebreaks")]
Expand Down Expand Up @@ -45,16 +53,74 @@ public void Family_Should_Be_Formatted_To_One_Line(string labelValue)
using (var sr = new StringReader(Encoding.UTF8.GetString(ms.ToArray())))
{
var linesCount = 0;
var line = "";
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
_output.WriteLine(line);
linesCount += 1;
}

Assert.Equal(3, linesCount);
}
}
}

[Theory]
[InlineData(1)]
[InlineData(-2)]
[InlineData(-6)]
[InlineData(20)]
[InlineData(0)]
public void Timestamp_Should_Be_Parse_To_Long(int diffMinutes)
{
using (var ms = new MemoryStream())
{
var metricFamily = new CMetricFamily
{
Name = "family1",
Help = "help",
Type = CMetricType.Counter
};

var metricCounter = new CCounter
{
Value = 65
};

var timestamp = (long) (DateTime.UtcNow.AddMinutes(diffMinutes) - new DateTime(1970, 1, 1)).TotalSeconds;

metricFamily.Metrics = new[]
{
new CMetric
{
CCounter = metricCounter,
Timestamp = timestamp
}
};

TextFormatter.Format(ms, new[]
{
metricFamily
});

using (var sr = new StringReader(Encoding.UTF8.GetString(ms.ToArray())))
{
var linesCount = 0;
string line;
string lineMetric = null;
while ((line = sr.ReadLine()) != null)
{
_output.WriteLine(line);
linesCount += 1;
if (linesCount == 3)
lineMetric = line;
}

Assert.NotNull(lineMetric);
var arrMetric = lineMetric.Split(' ');
Assert.Equal(timestamp, long.Parse(arrMetric[2]));
}
}
}
}
}
}

0 comments on commit 544a6e5

Please sign in to comment.