Skip to content

Commit cfc2d0c

Browse files
[Prometheus.HttpListener] Avoid array lookup (#6851)
1 parent 058e8ff commit cfc2d0c

File tree

2 files changed

+296
-81
lines changed

2 files changed

+296
-81
lines changed

src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs

Lines changed: 71 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ namespace OpenTelemetry.Exporter.Prometheus;
1010

1111
internal sealed class PrometheusMetric
1212
{
13-
/* Counter becomes counter
14-
Gauge becomes gauge
15-
Histogram becomes histogram
16-
UpDownCounter becomes gauge
17-
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#otlp-metric-points-to-prometheus
18-
*/
19-
private static readonly PrometheusType[] MetricTypes =
20-
[
21-
PrometheusType.Untyped, PrometheusType.Counter, PrometheusType.Gauge, PrometheusType.Summary, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Gauge,
22-
];
23-
2413
public PrometheusMetric(string name, string unit, PrometheusType type, bool disableTotalNameSuffixForCounters)
2514
{
2615
// The metric name is
@@ -87,9 +76,7 @@ public PrometheusMetric(string name, string unit, PrometheusType type, bool disa
8776
public PrometheusType Type { get; }
8877

8978
public static PrometheusMetric Create(Metric metric, bool disableTotalNameSuffixForCounters)
90-
{
91-
return new PrometheusMetric(metric.Name, metric.Unit, GetPrometheusType(metric.MetricType), disableTotalNameSuffixForCounters);
92-
}
79+
=> new(metric.Name, metric.Unit, GetPrometheusType(metric.MetricType), disableTotalNameSuffixForCounters);
9380

9481
internal static string SanitizeMetricName(string metricName)
9582
{
@@ -180,19 +167,28 @@ internal static string RemoveAnnotations(string unit)
180167
internal static PrometheusType GetPrometheusType(MetricType openTelemetryMetricType)
181168
{
182169
int metricType = (int)openTelemetryMetricType >> 4;
183-
return MetricTypes[metricType];
184-
}
185170

186-
private static string SanitizeOpenMetricsName(string metricName)
187-
{
188-
if (metricName.EndsWith("_total", StringComparison.Ordinal))
171+
/* Counter becomes counter
172+
Gauge becomes gauge
173+
Histogram becomes histogram
174+
UpDownCounter becomes gauge
175+
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#otlp-metric-points-to-prometheus
176+
*/
177+
return metricType switch
189178
{
190-
return metricName.Substring(0, metricName.Length - 6);
191-
}
192-
193-
return metricName;
179+
0 => PrometheusType.Untyped,
180+
1 => PrometheusType.Counter,
181+
2 => PrometheusType.Gauge,
182+
3 => PrometheusType.Summary,
183+
4 or 5 or 6 or 7 => PrometheusType.Histogram,
184+
8 => PrometheusType.Gauge,
185+
_ => throw new InvalidOperationException($"Invalid {nameof(MetricType)} value."),
186+
};
194187
}
195188

189+
private static string SanitizeOpenMetricsName(string metricName)
190+
=> metricName.EndsWith("_total", StringComparison.Ordinal) ? metricName.Substring(0, metricName.Length - 6) : metricName;
191+
196192
private static string GetUnit(string unit)
197193
{
198194
// Dropping the portions of the Unit within brackets (e.g. {packet}). Brackets MUST NOT be included in the resulting unit. A "count of foo" is considered unitless in Prometheus.
@@ -242,67 +238,61 @@ private static bool TryProcessRateUnits(string updatedUnit, [NotNullWhen(true)]
242238
// (See also https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/metrics.md#instrument-units)
243239
// Prometheus best practices for units: https://prometheus.io/docs/practices/naming/#base-units
244240
// OpenMetrics specification for units: https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#units-and-base-units
245-
private static string MapUnit(ReadOnlySpan<char> unit)
241+
private static string MapUnit(ReadOnlySpan<char> unit) => unit switch
246242
{
247-
return unit switch
248-
{
249-
// Time
250-
"d" => "days",
251-
"h" => "hours",
252-
"min" => "minutes",
253-
"s" => "seconds",
254-
"ms" => "milliseconds",
255-
"us" => "microseconds",
256-
"ns" => "nanoseconds",
257-
258-
// Bytes
259-
"By" => "bytes",
260-
"KiBy" => "kibibytes",
261-
"MiBy" => "mebibytes",
262-
"GiBy" => "gibibytes",
263-
"TiBy" => "tibibytes",
264-
"KBy" => "kilobytes",
265-
"MBy" => "megabytes",
266-
"GBy" => "gigabytes",
267-
"TBy" => "terabytes",
268-
"B" => "bytes",
269-
"KB" => "kilobytes",
270-
"MB" => "megabytes",
271-
"GB" => "gigabytes",
272-
"TB" => "terabytes",
273-
274-
// SI
275-
"m" => "meters",
276-
"V" => "volts",
277-
"A" => "amperes",
278-
"J" => "joules",
279-
"W" => "watts",
280-
"g" => "grams",
281-
282-
// Misc
283-
"Cel" => "celsius",
284-
"Hz" => "hertz",
285-
"1" => string.Empty,
286-
"%" => "percent",
287-
"$" => "dollars",
288-
_ => unit.ToString(),
289-
};
290-
}
243+
// Time
244+
"d" => "days",
245+
"h" => "hours",
246+
"min" => "minutes",
247+
"s" => "seconds",
248+
"ms" => "milliseconds",
249+
"us" => "microseconds",
250+
"ns" => "nanoseconds",
251+
252+
// Bytes
253+
"By" => "bytes",
254+
"KiBy" => "kibibytes",
255+
"MiBy" => "mebibytes",
256+
"GiBy" => "gibibytes",
257+
"TiBy" => "tibibytes",
258+
"KBy" => "kilobytes",
259+
"MBy" => "megabytes",
260+
"GBy" => "gigabytes",
261+
"TBy" => "terabytes",
262+
"B" => "bytes",
263+
"KB" => "kilobytes",
264+
"MB" => "megabytes",
265+
"GB" => "gigabytes",
266+
"TB" => "terabytes",
267+
268+
// SI
269+
"m" => "meters",
270+
"V" => "volts",
271+
"A" => "amperes",
272+
"J" => "joules",
273+
"W" => "watts",
274+
"g" => "grams",
275+
276+
// Misc
277+
"Cel" => "celsius",
278+
"Hz" => "hertz",
279+
"1" => string.Empty,
280+
"%" => "percent",
281+
"$" => "dollars",
282+
_ => unit.ToString(),
283+
};
291284

292285
// The map that translates the "per" unit
293286
// Example: s => per second (singular)
294-
private static string MapPerUnit(ReadOnlySpan<char> perUnit)
287+
private static string MapPerUnit(ReadOnlySpan<char> perUnit) => perUnit switch
295288
{
296-
return perUnit switch
297-
{
298-
"s" => "second",
299-
"m" => "minute",
300-
"h" => "hour",
301-
"d" => "day",
302-
"w" => "week",
303-
"mo" => "month",
304-
"y" => "year",
305-
_ => perUnit.ToString(),
306-
};
307-
}
289+
"s" => "second",
290+
"m" => "minute",
291+
"h" => "hour",
292+
"d" => "day",
293+
"w" => "week",
294+
"mo" => "month",
295+
"y" => "year",
296+
_ => perUnit.ToString(),
297+
};
308298
}

0 commit comments

Comments
 (0)