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; }