From 646c94c1f6feb33c49e2ee4a8d623db133f2f5a6 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 14 Dec 2021 10:01:13 -0800 Subject: [PATCH] Code review feedback. --- .../Implementation/PrometheusSerializerExt.cs | 27 +++++-------------- .../PrometheusExporterOptions.cs | 6 ++--- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs index dc98b92221c..805a51a2488 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs @@ -52,18 +52,13 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) { buffer[cursor++] = unchecked((byte)'{'); - int i = 0; foreach (var tag in tags) { - if (i++ > 0) - { - buffer[cursor++] = unchecked((byte)','); - } - cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value); + buffer[cursor++] = unchecked((byte)','); } - buffer[cursor++] = unchecked((byte)'}'); + buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra. } buffer[cursor++] = unchecked((byte)' '); @@ -151,18 +146,13 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) { buffer[cursor++] = unchecked((byte)'{'); - int i = 0; foreach (var tag in tags) { - if (i++ > 0) - { - buffer[cursor++] = unchecked((byte)','); - } - cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value); + buffer[cursor++] = unchecked((byte)','); } - buffer[cursor++] = unchecked((byte)'}'); + buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra. } buffer[cursor++] = unchecked((byte)' '); @@ -182,18 +172,13 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) { buffer[cursor++] = unchecked((byte)'{'); - int i = 0; foreach (var tag in tags) { - if (i++ > 0) - { - buffer[cursor++] = unchecked((byte)','); - } - cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value); + buffer[cursor++] = unchecked((byte)','); } - buffer[cursor++] = unchecked((byte)'}'); + buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra. } buffer[cursor++] = unchecked((byte)' '); diff --git a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs index cea95b37c04..e42fcb89bc3 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using OpenTelemetry.Internal; namespace OpenTelemetry.Exporter { @@ -65,10 +66,7 @@ public int ScrapeResponseCacheDurationMilliseconds get => this.scrapeResponseCacheDurationMilliseconds; set { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "Value should be greater than or equal to zero."); - } + Guard.Range(value, nameof(value), min: 0); this.scrapeResponseCacheDurationMilliseconds = value; }