Skip to content

Commit 67de4dd

Browse files
committed
fix npe
1 parent d0ca619 commit 67de4dd

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

BlazorApp/Service/k8s/impl/KubeService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using BlazorApp.Utils.Prometheus;
1010
using BlazorApp.Utils.Prometheus.Models.Interfaces;
1111
using Entity;
12+
using Extension;
1213
using k8s;
1314

1415
namespace BlazorApp.Service.k8s.impl;
@@ -137,11 +138,17 @@ public async Task<string> GetLivez()
137138
public async Task<List<IMetric>> GetMetrics()
138139
{
139140
var metricString = await GetStringAsync("/metrics");
141+
140142
return await ConvertStringToMetrics(metricString);
141143
}
142144

143145
public async Task<List<IMetric>> ConvertStringToMetrics(string metricString)
144146
{
147+
if (metricString.IsNullOrWhiteSpace())
148+
{
149+
return [];
150+
}
151+
145152
var byteArray = Encoding.UTF8.GetBytes(metricString);
146153
await using var ms = new MemoryStream(byteArray);
147154
var metric = await PrometheusMetricsParser.ParseAsync(ms);

BlazorApp/Utils/Prometheus/PrometheusMetricsParser.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ namespace BlazorApp.Utils.Prometheus;
1111

1212
public class PrometheusMetricsParser
1313
{
14-
private const string MetricInfoRegex = @"# (\w+) (\w*) (.*)";
14+
private const string MetricInfoRegex = @"# (\w+) (\w*) (.*)";
1515
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+)?)";
1719

1820
public static async Task<List<IMetric>> ParseAsync(Stream rawMetricsStream)
1921
{
@@ -39,11 +41,11 @@ public static async Task<List<IMetric>> ParseAsync(Stream rawMetricsStream)
3941

4042
private static async Task<List<IMetric>> InterpretRawMetricsStreamAsync(Stream rawMetricsStream)
4143
{
42-
var metrics = new List<IMetric>();
44+
var metrics = new List<IMetric>();
4345
var streamReader = new StreamReader(rawMetricsStream);
4446

4547
Metric lastMetric = null;
46-
var done = false;
48+
var done = false;
4749
//逐行进行处理
4850
var line = await streamReader.ReadLineAsync();
4951
if (string.IsNullOrWhiteSpace(line))
@@ -80,9 +82,10 @@ private static async Task<List<IMetric>> InterpretRawMetricsStreamAsync(Stream r
8082
lastMetric.Description = metricInfoMatch.Groups[3].Value;
8183
break;
8284
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;
8689

8790
break;
8891
}
@@ -124,7 +127,7 @@ private static async Task<List<IMetric>> InterpretRawMetricsStreamAsync(Stream r
124127
/// <exception cref="Exception"></exception>
125128
private static Measurement ParseMeasurement(string line)
126129
{
127-
var measurement = new Measurement();
130+
var measurement = new Measurement();
128131
var regexOutcome = Regex.Match(line, MeasurementRegex);
129132
if (regexOutcome.Success == false)
130133
{
@@ -158,19 +161,21 @@ private static double ParseMetricValue(Match regexOutcome)
158161
if (rawMetricValue.Contains("e+"))
159162
{
160163
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];
163166
// Console.WriteLine($"{rawMetricValue} = {before} * 10 的{after}次方");
164167
return double.Parse(before) * Math.Pow(10, int.Parse(after));
165168
}
169+
166170
if (rawMetricValue.Contains("e-"))
167171
{
168172
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];
171175
// Console.WriteLine($"{rawMetricValue} = {before} * 10 的 -{after}次方");
172176
return double.Parse(before) * Math.Pow(10, -int.Parse(after));
173177
}
178+
174179
return double.Parse(rawMetricValue);
175180
}
176181

@@ -203,11 +208,13 @@ private static void ParseMetricLabels(Match regexOutcome, Measurement measuremen
203208
// Get every individual raw label
204209
foreach (var rawLabel in rawLabels.Split(','))
205210
{
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+
}
211218
}
212219
}
213220
}

0 commit comments

Comments
 (0)