1- using Promitor . Parsers . Prometheus . Core ;
1+ using System ;
2+ using Promitor . Parsers . Prometheus . Core ;
23using Promitor . Parsers . Prometheus . Core . Models ;
34using Promitor . Parsers . Prometheus . Core . Models . Interfaces ;
45using System . Collections . Generic ;
56using System . ComponentModel ;
67using System . IO ;
8+ using System . Linq ;
79using System . Threading . Tasks ;
810using Xunit ;
911
@@ -12,11 +14,132 @@ namespace Promitor.Parsers.Prometheus.Tests
1214 [ Category ( "Unit" ) ]
1315 public class PrometheusMetricsParserTests
1416 {
17+ [ Theory ]
18+ [ InlineData ( 52977208 ) ]
19+ [ InlineData ( 153.1351 ) ]
20+ [ InlineData ( - 52977208 ) ]
21+ [ InlineData ( - 153.1351 ) ]
22+ [ InlineData ( - 1 ) ]
23+ [ InlineData ( double . NaN ) ]
24+ public async Task Parse_RawMetricWithTimestampAndLabels_ReturnCorrectInfo ( double metricValue )
25+ {
26+ // Arrange
27+ var metricName = "azure_container_registry_total_pull_count_discovered" ;
28+ var metricDescription = "Amount of images that were pulled from the container registry" ;
29+ var resourceGroupName = "promitor" ;
30+ var subscriptionId = "0f9d7fea-99e8-4768-8672-06a28514f77e" ;
31+ var resourceUri = "subscriptions/0f9d7fea-99e8-4768-8672-06a28514f77e/resourceGroups/promitor/providers/Microsoft.ContainerRegistry/registries/promitor" ;
32+ var instanceName = "promitor" ;
33+ var timestamp = DateTimeOffset . UtcNow ;
34+ var rawMetric = $@ "# HELP { metricName } { metricDescription }
35+ # TYPE { metricName } gauge
36+ { metricName } {{resource_group=""{ resourceGroupName } "",subscription_id=""{ subscriptionId } "",resource_uri=""{ resourceUri } "",instance_name=""{ instanceName } ""}} { metricValue } { timestamp . ToUnixTimeMilliseconds ( ) } ";
37+ var rawMetricsStream = GenerateStream ( rawMetric ) ;
38+
39+ // Act
40+ var metrics = await PrometheusMetricsParser . ParseAsync ( rawMetricsStream ) ;
41+
42+ // Assert
43+ Assert . True ( rawMetricsStream . CanRead ) ;
44+ Assert . True ( rawMetricsStream . CanSeek ) ;
45+ Assert . NotNull ( metrics ) ;
46+ Assert . Single ( metrics ) ;
47+ var testMetric = metrics . FirstOrDefault ( ) ;
48+ Assert . NotNull ( testMetric ) ;
49+ Assert . Equal ( metricName , testMetric . Name ) ;
50+ Assert . Equal ( metricDescription , testMetric . Description ) ;
51+ Assert . Equal ( MetricTypes . Gauge , testMetric . Type ) ;
52+ var testGauge = testMetric as Gauge ;
53+ Assert . Single ( testGauge . Measurements ) ;
54+ var testMeasurement = testGauge . Measurements . First ( ) ;
55+ Assert . Equal ( metricValue , testMeasurement . Value ) ;
56+ Assert . Equal ( timestamp . ToString ( "yyyy-MM-ddTHH:mm:ss.zzz" ) , testMeasurement . Timestamp ? . ToString ( "yyyy-MM-ddTHH:mm:ss.zzz" ) ) ;
57+ Assert . NotNull ( testMeasurement . Labels ) ;
58+ Assert . Equal ( 4 , testMeasurement . Labels . Count ) ;
59+ Assert . True ( testMeasurement . Labels . ContainsKey ( "resource_group" ) ) ;
60+ Assert . True ( testMeasurement . Labels . ContainsKey ( "subscription_id" ) ) ;
61+ Assert . True ( testMeasurement . Labels . ContainsKey ( "resource_uri" ) ) ;
62+ Assert . True ( testMeasurement . Labels . ContainsKey ( "instance_name" ) ) ;
63+ Assert . Equal ( resourceGroupName , testMeasurement . Labels [ "resource_group" ] ) ;
64+ Assert . Equal ( subscriptionId , testMeasurement . Labels [ "subscription_id" ] ) ;
65+ Assert . Equal ( resourceUri , testMeasurement . Labels [ "resource_uri" ] ) ;
66+ Assert . Equal ( instanceName , testMeasurement . Labels [ "instance_name" ] ) ;
67+ }
68+
69+ [ Fact ]
70+ public async Task Parse_RawMetricWithTimestampButWithoutLabels_ReturnCorrectInfo ( )
71+ {
72+ // Arrange
73+ var metricName = "promitor_runtime_dotnet_totalmemory" ;
74+ var metricDescription = "Total known allocated memory" ;
75+ double metricValue = 52977208 ;
76+ var timestamp = DateTimeOffset . UtcNow ;
77+ var rawMetric = $@ "# HELP { metricName } { metricDescription }
78+ # TYPE { metricName } gauge
79+ { metricName } { metricValue } { timestamp . ToUnixTimeMilliseconds ( ) } ";
80+ var rawMetricsStream = GenerateStream ( rawMetric ) ;
81+
82+ // Act
83+ var metrics = await PrometheusMetricsParser . ParseAsync ( rawMetricsStream ) ;
84+
85+ // Assert
86+ Assert . True ( rawMetricsStream . CanRead ) ;
87+ Assert . True ( rawMetricsStream . CanSeek ) ;
88+ Assert . NotNull ( metrics ) ;
89+ Assert . Single ( metrics ) ;
90+ var testMetric = metrics . FirstOrDefault ( ) ;
91+ Assert . NotNull ( testMetric ) ;
92+ Assert . Equal ( metricName , testMetric . Name ) ;
93+ Assert . Equal ( metricDescription , testMetric . Description ) ;
94+ Assert . Equal ( MetricTypes . Gauge , testMetric . Type ) ;
95+ var testGauge = testMetric as Gauge ;
96+ Assert . Single ( testGauge . Measurements ) ;
97+ var testMeasurement = testGauge . Measurements . First ( ) ;
98+ Assert . Equal ( metricValue , testMeasurement . Value ) ;
99+ Assert . Equal ( timestamp . ToString ( "yyyy-MM-ddTHH:mm:ss.zzz" ) , testMeasurement . Timestamp ? . ToString ( "yyyy-MM-ddTHH:mm:ss.zzz" ) ) ;
100+ Assert . NotNull ( testMeasurement . Labels ) ;
101+ Assert . Empty ( testMeasurement . Labels ) ;
102+ }
103+
104+ [ Fact ]
105+ public async Task Parse_RawMetricWithoutLabelsAndTimestamp_ReturnCorrectInfo ( )
106+ {
107+ // Arrange
108+ var metricName = "promitor_runtime_dotnet_totalmemory" ;
109+ var metricDescription = "Total known allocated memory" ;
110+ double metricValue = 52977208 ;
111+ var rawMetric = $@ "# HELP { metricName } { metricDescription }
112+ # TYPE { metricName } gauge
113+ { metricName } { metricValue } ";
114+ var rawMetricsStream = GenerateStream ( rawMetric ) ;
115+
116+ // Act
117+ var metrics = await PrometheusMetricsParser . ParseAsync ( rawMetricsStream ) ;
118+
119+ // Assert
120+ Assert . True ( rawMetricsStream . CanRead ) ;
121+ Assert . True ( rawMetricsStream . CanSeek ) ;
122+ Assert . NotNull ( metrics ) ;
123+ Assert . Single ( metrics ) ;
124+ var testMetric = metrics . FirstOrDefault ( ) ;
125+ Assert . NotNull ( testMetric ) ;
126+ Assert . Equal ( metricName , testMetric . Name ) ;
127+ Assert . Equal ( metricDescription , testMetric . Description ) ;
128+ Assert . Equal ( MetricTypes . Gauge , testMetric . Type ) ;
129+ var testGauge = testMetric as Gauge ;
130+ Assert . Single ( testGauge . Measurements ) ;
131+ var testMeasurement = testGauge . Measurements . First ( ) ;
132+ Assert . Null ( testMeasurement . Timestamp ) ;
133+ Assert . Equal ( metricValue , testMeasurement . Value ) ;
134+ Assert . NotNull ( testMeasurement . Labels ) ;
135+ Assert . Empty ( testMeasurement . Labels ) ;
136+ }
137+
15138 [ Fact ]
16- public async Task Parse_ValidInput_ReturnsMetrics ( )
139+ public async Task Parse_ValidInputWithLabels_ReturnsMetrics ( )
17140 {
18141 // Arrange
19- var filePath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Samples" , "raw-metrics.txt" ) ;
142+ var filePath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Samples" , "raw-metrics-with-labels .txt" ) ;
20143 var rawMetricsStream = File . OpenRead ( filePath ) ;
21144
22145 // Act
@@ -29,6 +152,22 @@ public async Task Parse_ValidInput_ReturnsMetrics()
29152 AssertAzureContainerRegistryPullCount ( metrics ) ;
30153 }
31154
155+ [ Fact ]
156+ public async Task Parse_ValidInputWithoutLabels_ReturnsMetrics ( )
157+ {
158+ // Arrange
159+ var filePath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Samples" , "raw-metrics-without-labels.txt" ) ;
160+ var rawMetricsStream = File . OpenRead ( filePath ) ;
161+
162+ // Act
163+ var metrics = await PrometheusMetricsParser . ParseAsync ( rawMetricsStream ) ;
164+
165+ // Assert
166+ Assert . True ( rawMetricsStream . CanRead ) ;
167+ Assert . True ( rawMetricsStream . CanSeek ) ;
168+ Assert . NotNull ( metrics ) ;
169+ }
170+
32171 private void AssertAzureContainerRegistryPullCount ( List < IMetric > metrics )
33172 {
34173 var acrMetric = metrics . Find ( f => f . Name == "azure_container_registry_total_pull_count_discovered" ) ;
@@ -37,6 +176,22 @@ private void AssertAzureContainerRegistryPullCount(List<IMetric> metrics)
37176 Assert . Equal ( "Amount of images that were pulled from the container registry" , acrMetric . Description ) ;
38177 var acrGauge = acrMetric as Gauge ;
39178 Assert . Equal ( 2 , acrGauge . Measurements . Count ) ;
179+ var firstMeasurement = acrGauge . Measurements . First ( ) ;
180+ Assert . NotNull ( firstMeasurement . Labels ) ;
181+ Assert . Equal ( 4 , firstMeasurement . Labels . Count ) ;
182+ Assert . True ( firstMeasurement . Labels . ContainsKey ( "resource_group" ) ) ;
183+ Assert . True ( firstMeasurement . Labels . ContainsKey ( "subscription_id" ) ) ;
184+ Assert . True ( firstMeasurement . Labels . ContainsKey ( "resource_uri" ) ) ;
185+ Assert . True ( firstMeasurement . Labels . ContainsKey ( "instance_name" ) ) ;
186+ }
187+ public static Stream GenerateStream ( string s )
188+ {
189+ var stream = new MemoryStream ( ) ;
190+ var writer = new StreamWriter ( stream ) ;
191+ writer . Write ( s ) ;
192+ writer . Flush ( ) ;
193+ stream . Position = 0 ;
194+ return stream ;
40195 }
41196 }
42197}
0 commit comments