Skip to content

Commit d12d2ed

Browse files
authored
Enable UpDownCounter For Dotnet-Monitor (#4310)
1 parent 992da7c commit d12d2ed

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Microsoft.Diagnostics.Monitoring.WebApi/Metrics/MetricsStore.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ private static string GetMetricType(EventType eventType)
202202
{
203203
// In Prometheus, rates are treated as gauges due to their non-monotonic nature
204204
case EventType.Rate:
205+
case EventType.UpDownCounter:
205206
case EventType.Gauge:
206207
return "gauge";
207208
case EventType.Histogram:

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/MetricsFormattingTests.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public async Task HistogramFormat_Test()
4949
using MemoryStream stream = await GetMetrics(payload);
5050
List<string> lines = ReadStream(stream);
5151

52-
// Question - this is manually recreating what PrometheusDataModel.GetPrometheusNormalizedName does to get the metric name;
53-
// should we call this method, or should this also be implicitly testing its behavior by having this hard-coded?
5452
string metricName = $"{MeterName.ToLowerInvariant()}_{payload[0].Name}";
5553

5654
const string quantile_50 = "{quantile=\"0.5\"}";
@@ -74,8 +72,6 @@ public async Task GaugeFormat_Test()
7472

7573
List<string> lines = ReadStream(stream);
7674

77-
// Question - this is manually recreating what PrometheusDataModel.GetPrometheusNormalizedName does to get the metric name;
78-
// should we call this method, or should this also be implicitly testing its behavior by having this hard-coded?
7975
string metricName = $"{MeterName.ToLowerInvariant()}_{payload.Name}";
8076

8177
Assert.Equal(3, lines.Count);
@@ -93,8 +89,6 @@ public async Task CounterFormat_Test()
9389

9490
List<string> lines = ReadStream(stream);
9591

96-
// Question - this is manually recreating what PrometheusDataModel.GetPrometheusNormalizedName does to get the metric name;
97-
// should we call this method, or should this also be implicitly testing its behavior by having this hard-coded?
9892
string metricName = $"{MeterName.ToLowerInvariant()}_{payload.Name}";
9993

10094
Assert.Equal(3, lines.Count);
@@ -103,6 +97,23 @@ public async Task CounterFormat_Test()
10397
Assert.Equal($"{metricName} {payload.Value} {new DateTimeOffset(payload.Timestamp).ToUnixTimeMilliseconds()}", lines[2]);
10498
}
10599

100+
[Fact]
101+
public async Task UpDownCounterFormat_Test()
102+
{
103+
ICounterPayload payload = new UpDownCounterPayload(MeterName, InstrumentName, "DisplayName", "", null, Value1, Timestamp);
104+
105+
MemoryStream stream = await GetMetrics(new() { payload });
106+
107+
List<string> lines = ReadStream(stream);
108+
109+
string metricName = $"{MeterName.ToLowerInvariant()}_{payload.Name}";
110+
111+
Assert.Equal(3, lines.Count);
112+
Assert.Equal(FormattableString.Invariant($"# HELP {metricName}{payload.Unit} {payload.DisplayName}"), lines[0]);
113+
Assert.Equal(FormattableString.Invariant($"# TYPE {metricName} gauge"), lines[1]);
114+
Assert.Equal(FormattableString.Invariant($"{metricName} {payload.Value} {new DateTimeOffset(payload.Timestamp).ToUnixTimeMilliseconds()}"), lines[2]);
115+
}
116+
106117
private async Task<MemoryStream> GetMetrics(List<ICounterPayload> payloads)
107118
{
108119
IMetricsStore metricsStore = new MetricsStore(_logger, MetricCount);

0 commit comments

Comments
 (0)