@@ -11,9 +11,11 @@ namespace BlazorApp.Utils.Prometheus;
11
11
12
12
public class PrometheusMetricsParser
13
13
{
14
- private const string MetricInfoRegex = @"# (\w+) (\w*) (.*)" ;
14
+ private const string MetricInfoRegex = @"# (\w+) (\w*) (.*)" ;
15
15
private const string MeasurementRegex1 = "([^{\\ ]+)({.+})* ((?:-?\\ d+(?:\\ .\\ d*)*)*(?:NaN)*)+ *(\\ d*)*" ;
16
- private const string MeasurementRegex = @"([^{\\ ]+)({.+})* ((?:-?\\d+(?:\\.\\d*)*)*(?:NaN)*)(\d+\.?\d*(?:e[+-]?\d+)?)" ;
16
+
17
+ private const string MeasurementRegex =
18
+ @"([^{\\ ]+)({.+})* ((?:-?\\d+(?:\\.\\d*)*)*(?:NaN)*)(\d+\.?\d*(?:e[+-]?\d+)?)" ;
17
19
18
20
public static async Task < List < IMetric > > ParseAsync ( Stream rawMetricsStream )
19
21
{
@@ -39,11 +41,11 @@ public static async Task<List<IMetric>> ParseAsync(Stream rawMetricsStream)
39
41
40
42
private static async Task < List < IMetric > > InterpretRawMetricsStreamAsync ( Stream rawMetricsStream )
41
43
{
42
- var metrics = new List < IMetric > ( ) ;
44
+ var metrics = new List < IMetric > ( ) ;
43
45
var streamReader = new StreamReader ( rawMetricsStream ) ;
44
46
45
47
Metric lastMetric = null ;
46
- var done = false ;
48
+ var done = false ;
47
49
//逐行进行处理
48
50
var line = await streamReader . ReadLineAsync ( ) ;
49
51
if ( string . IsNullOrWhiteSpace ( line ) )
@@ -80,9 +82,10 @@ private static async Task<List<IMetric>> InterpretRawMetricsStreamAsync(Stream r
80
82
lastMetric . Description = metricInfoMatch . Groups [ 3 ] . Value ;
81
83
break ;
82
84
case "TYPE" :
83
- lastMetric . Name = metricInfoMatch . Groups [ 2 ] . Value ;
84
- var currentMetricType = Enum . Parse < MetricTypes > ( metricInfoMatch . Groups [ 3 ] . Value , ignoreCase : true ) ;
85
- lastMetric . Type = currentMetricType ;
85
+ lastMetric . Name = metricInfoMatch . Groups [ 2 ] . Value ;
86
+ var currentMetricType =
87
+ Enum . Parse < MetricTypes > ( metricInfoMatch . Groups [ 3 ] . Value , ignoreCase : true ) ;
88
+ lastMetric . Type = currentMetricType ;
86
89
87
90
break ;
88
91
}
@@ -124,7 +127,7 @@ private static async Task<List<IMetric>> InterpretRawMetricsStreamAsync(Stream r
124
127
/// <exception cref="Exception"></exception>
125
128
private static Measurement ParseMeasurement ( string line )
126
129
{
127
- var measurement = new Measurement ( ) ;
130
+ var measurement = new Measurement ( ) ;
128
131
var regexOutcome = Regex . Match ( line , MeasurementRegex ) ;
129
132
if ( regexOutcome . Success == false )
130
133
{
@@ -158,19 +161,21 @@ private static double ParseMetricValue(Match regexOutcome)
158
161
if ( rawMetricValue . Contains ( "e+" ) )
159
162
{
160
163
var numbers = rawMetricValue . Split ( "e+" ) ;
161
- var before = numbers [ 0 ] ;
162
- var after = numbers [ 1 ] ;
164
+ var before = numbers [ 0 ] ;
165
+ var after = numbers [ 1 ] ;
163
166
// Console.WriteLine($"{rawMetricValue} = {before} * 10 的{after}次方");
164
167
return double . Parse ( before ) * Math . Pow ( 10 , int . Parse ( after ) ) ;
165
168
}
169
+
166
170
if ( rawMetricValue . Contains ( "e-" ) )
167
171
{
168
172
var numbers = rawMetricValue . Split ( "e-" ) ;
169
- var before = numbers [ 0 ] ;
170
- var after = numbers [ 1 ] ;
173
+ var before = numbers [ 0 ] ;
174
+ var after = numbers [ 1 ] ;
171
175
// Console.WriteLine($"{rawMetricValue} = {before} * 10 的 -{after}次方");
172
176
return double . Parse ( before ) * Math . Pow ( 10 , - int . Parse ( after ) ) ;
173
177
}
178
+
174
179
return double . Parse ( rawMetricValue ) ;
175
180
}
176
181
@@ -203,11 +208,13 @@ private static void ParseMetricLabels(Match regexOutcome, Measurement measuremen
203
208
// Get every individual raw label
204
209
foreach ( var rawLabel in rawLabels . Split ( ',' ) )
205
210
{
206
- // Split label into information
207
- var splitLabelInfo = rawLabel . Split ( '=' ) ;
208
-
209
- // Add to the outcome
210
- measurement . Labels . Add ( splitLabelInfo [ 0 ] , splitLabelInfo [ 1 ] . Replace ( "\" " , "" ) ) ;
211
+ if ( rawLabel . Contains ( '=' ) )
212
+ {
213
+ // Split label into information
214
+ var splitLabelInfo = rawLabel . Split ( '=' ) ;
215
+ // Add to the outcome
216
+ measurement . Labels . Add ( splitLabelInfo [ 0 ] , splitLabelInfo [ 1 ] . Replace ( "\" " , "" ) ) ;
217
+ }
211
218
}
212
219
}
213
220
}
0 commit comments