From fcd5133884230c1ee877958496ef53fb7262853b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:03:17 +0100 Subject: [PATCH 01/40] [Exporter.Prometheus] enable code analysis --- .../OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj | 1 + .../OpenTelemetry.Exporter.Prometheus.HttpListener.csproj | 1 + 2 files changed, 2 insertions(+) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj index f6a49fe6a64..c0b202cc640 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj @@ -7,6 +7,7 @@ $(PackageTags);prometheus;metrics coreunstable- $(DefineConstants);PROMETHEUS_ASPNETCORE + latest-all diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj index 4e087919be2..a886b62ed65 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj @@ -5,6 +5,7 @@ Stand-alone HttpListener for hosting OpenTelemetry .NET Prometheus Exporter $(PackageTags);prometheus;metrics coreunstable- + latest-all From dc1faa51afdb6c1f2402d2aea002588d60e66439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:19:58 +0100 Subject: [PATCH 02/40] CA1822 --- .../Internal/PrometheusCollectionManager.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs index 1c74e36bcf8..0a7616beea3 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs @@ -148,6 +148,22 @@ public void ExitCollect() Interlocked.Decrement(ref this.readerCount); } + private static bool IncreaseBufferSize(ref byte[] buffer) + { + var newBufferSize = buffer.Length * 2; + + if (newBufferSize > 100 * 1024 * 1024) + { + return false; + } + + var newBuffer = new byte[newBufferSize]; + buffer.CopyTo(newBuffer, 0); + buffer = newBuffer; + + return true; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void EnterGlobalLock() { @@ -230,7 +246,7 @@ private ExportResult OnCollect(in Batch metrics) } catch (IndexOutOfRangeException) { - if (!this.IncreaseBufferSize(ref buffer)) + if (!IncreaseBufferSize(ref buffer)) { // there are two cases we might run into the following condition: // 1. we have many metrics to be exported - in this case we probably want @@ -268,7 +284,7 @@ private ExportResult OnCollect(in Batch metrics) } catch (IndexOutOfRangeException) { - if (!this.IncreaseBufferSize(ref buffer)) + if (!IncreaseBufferSize(ref buffer)) { throw; } @@ -285,7 +301,7 @@ private ExportResult OnCollect(in Batch metrics) } catch (IndexOutOfRangeException) { - if (!this.IncreaseBufferSize(ref buffer)) + if (!IncreaseBufferSize(ref buffer)) { throw; } @@ -332,7 +348,7 @@ private int WriteTargetInfo(ref byte[] buffer) } catch (IndexOutOfRangeException) { - if (!this.IncreaseBufferSize(ref buffer)) + if (!IncreaseBufferSize(ref buffer)) { throw; } @@ -343,22 +359,6 @@ private int WriteTargetInfo(ref byte[] buffer) return this.targetInfoBufferLength; } - private bool IncreaseBufferSize(ref byte[] buffer) - { - var newBufferSize = buffer.Length * 2; - - if (newBufferSize > 100 * 1024 * 1024) - { - return false; - } - - var newBuffer = new byte[newBufferSize]; - buffer.CopyTo(newBuffer, 0); - buffer = newBuffer; - - return true; - } - private PrometheusMetric GetPrometheusMetric(Metric metric) { // Optimize writing metrics with bounded cache that has pre-calculated Prometheus names. From 5a4c569334db08acfcb9b3d9200702fd5d271287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:46:29 +0100 Subject: [PATCH 03/40] CA1310 --- ...PrometheusExporterApplicationBuilderExtensions.cs | 2 +- ...ometheusExporterEndpointRouteBuilderExtensions.cs | 2 +- .../Internal/PrometheusMetric.cs | 8 ++++---- .../PrometheusHttpListener.cs | 12 ++++++++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs index 38559e6b201..baf8738f321 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs @@ -114,7 +114,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint( path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath; } - if (!path.StartsWith("/")) + if (!path.StartsWith('/')) { path = $"/{path}"; } diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs index 45639b378fc..6bbc70d4b94 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs @@ -84,7 +84,7 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( path = options.ScrapeEndpointPath ?? PrometheusAspNetCoreOptions.DefaultScrapeEndpointPath; } - if (!path.StartsWith("/")) + if (!path.StartsWith('/')) { path = $"/{path}"; } diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs index cc9176ea0c5..8c1d8713509 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs @@ -40,7 +40,7 @@ public PrometheusMetric(string name, string unit, PrometheusType type, bool disa // [OpenMetrics UNIT metadata](https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#metricfamily) // and as a suffix to the metric name. The unit suffix comes before any type-specific suffixes. // https://github.com/open-telemetry/opentelemetry-specification/blob/3dfb383fe583e3b74a2365c5a1d90256b273ee76/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata-1 - if (!sanitizedName.EndsWith(sanitizedUnit)) + if (!sanitizedName.EndsWith(sanitizedUnit, StringComparison.Ordinal)) { sanitizedName += $"_{sanitizedUnit}"; openMetricsName += $"_{sanitizedUnit}"; @@ -51,14 +51,14 @@ public PrometheusMetric(string name, string unit, PrometheusType type, bool disa // Exporters SHOULD provide a configuration option to disable the addition of `_total` suffixes. // https://github.com/open-telemetry/opentelemetry-specification/blob/b2f923fb1650dde1f061507908b834035506a796/specification/compatibility/prometheus_and_openmetrics.md#L286 // Note that we no longer append '_ratio' for units that are '1', see: https://github.com/open-telemetry/opentelemetry-specification/issues/4058 - if (type == PrometheusType.Counter && !sanitizedName.EndsWith("_total") && !disableTotalNameSuffixForCounters) + if (type == PrometheusType.Counter && !sanitizedName.EndsWith("_total", StringComparison.Ordinal) && !disableTotalNameSuffixForCounters) { sanitizedName += "_total"; } // For counters requested using OpenMetrics format, the MetricFamily name MUST be suffixed with '_total', regardless of the setting to disable the 'total' suffix. // https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#counter-1 - if (type == PrometheusType.Counter && !openMetricsName.EndsWith("_total")) + if (type == PrometheusType.Counter && !openMetricsName.EndsWith("_total", StringComparison.Ordinal)) { openMetricsName += "_total"; } @@ -179,7 +179,7 @@ internal static string RemoveAnnotations(string unit) private static string SanitizeOpenMetricsName(string metricName) { - if (metricName.EndsWith("_total")) + if (metricName.EndsWith("_total", StringComparison.Ordinal)) { return metricName.Substring(0, metricName.Length - 6); } diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs index cecda73f7c9..06ad54274f0 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs @@ -30,12 +30,20 @@ public PrometheusHttpListener(PrometheusExporter exporter, PrometheusHttpListene string path = options.ScrapeEndpointPath ?? PrometheusHttpListenerOptions.DefaultScrapeEndpointPath; - if (!path.StartsWith("/")) +#if NET + if (!path.StartsWith('/')) +#else + if (!path.StartsWith("/", StringComparison.Ordinal)) +#endif { path = $"/{path}"; } - if (!path.EndsWith("/")) +#if NET + if (!path.EndsWith('/')) +#else + if (!path.EndsWith("/", StringComparison.Ordinal)) +#endif { path = $"{path}/"; } From 6cbb691fdb7a308e5bb621823a88b35c4bb4b423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:48:52 +0100 Subject: [PATCH 04/40] Disable CA2000 - ownership is transferred to other objects --- .../PrometheusExporterMeterProviderBuilderExtensions.cs | 2 ++ .../PrometheusHttpListenerMeterProviderBuilderExtensions.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs index f73455d3b08..299061d1f85 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs @@ -64,7 +64,9 @@ public static MeterProviderBuilder AddPrometheusExporter( private static MetricReader BuildPrometheusExporterMetricReader(PrometheusAspNetCoreOptions options) { +#pragma warning disable CA2000 var exporter = new PrometheusExporter(options.ExporterOptions); +#pragma warning restore CA2000 return new BaseExportingMetricReader(exporter) { diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs index 7289432cdcf..b87c312b87b 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs @@ -79,7 +79,7 @@ private static MetricReader BuildPrometheusHttpListenerMetricReader( try { var listener = new PrometheusHttpListener(exporter, options); - exporter.OnDispose = () => listener.Dispose(); + exporter.OnDispose = listener.Dispose; listener.Start(); } catch (Exception ex) From 9ceef3a96e3ea438fbb85775b0c905404bb4b8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:49:39 +0100 Subject: [PATCH 05/40] CA1859 --- .../PrometheusExporterMeterProviderBuilderExtensions.cs | 2 +- .../PrometheusHttpListenerMeterProviderBuilderExtensions.cs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs index 299061d1f85..203581d2ca1 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs @@ -62,7 +62,7 @@ public static MeterProviderBuilder AddPrometheusExporter( }); } - private static MetricReader BuildPrometheusExporterMetricReader(PrometheusAspNetCoreOptions options) + private static BaseExportingMetricReader BuildPrometheusExporterMetricReader(PrometheusAspNetCoreOptions options) { #pragma warning disable CA2000 var exporter = new PrometheusExporter(options.ExporterOptions); diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs index b87c312b87b..df8b9c62653 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs @@ -62,7 +62,7 @@ public static MeterProviderBuilder AddPrometheusHttpListener( }); } - private static MetricReader BuildPrometheusHttpListenerMetricReader( + private static BaseExportingMetricReader BuildPrometheusHttpListenerMetricReader( PrometheusHttpListenerOptions options) { var exporter = new PrometheusExporter(new PrometheusExporterOptions @@ -78,7 +78,9 @@ private static MetricReader BuildPrometheusHttpListenerMetricReader( try { +#pragma warning disable CA2000 var listener = new PrometheusHttpListener(exporter, options); +#pragma warning restore CA2000 exporter.OnDispose = listener.Dispose; listener.Start(); } From c26527ae0092d978b026caea035744467ea9f185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:50:01 +0100 Subject: [PATCH 06/40] CA1823 --- .../Internal/PrometheusSerializer.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs index 54cbfac4170..c199a6695c5 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs @@ -17,8 +17,6 @@ internal static partial class PrometheusSerializer { #pragma warning disable SA1310 // Field name should not contain an underscore private const byte ASCII_QUOTATION_MARK = 0x22; // '"' - private const byte ASCII_FULL_STOP = 0x2E; // '.' - private const byte ASCII_HYPHEN_MINUS = 0x2D; // '-' private const byte ASCII_REVERSE_SOLIDUS = 0x5C; // '\\' private const byte ASCII_LINEFEED = 0x0A; // `\n` #pragma warning restore SA1310 // Field name should not contain an underscore From 34e363b69174c175270214dbc04c557e36ccbd69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:54:42 +0100 Subject: [PATCH 07/40] CA1835 --- .../PrometheusExporterMiddleware.cs | 2 +- .../PrometheusHttpListener.cs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs index f91a93f66bb..863695b975f 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs @@ -74,7 +74,7 @@ public async Task InvokeAsync(HttpContext httpContext) ? "application/openmetrics-text; version=1.0.0; charset=utf-8" : "text/plain; charset=utf-8; version=0.0.4"; - await response.Body.WriteAsync(dataView.Array!, 0, dataView.Count).ConfigureAwait(false); + await response.Body.WriteAsync(dataView.Array.AsMemory(0, dataView.Count)).ConfigureAwait(false); } else { diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs index 06ad54274f0..b3bdf286f34 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs @@ -172,7 +172,11 @@ private async Task ProcessRequestAsync(HttpListenerContext context) ? "application/openmetrics-text; version=1.0.0; charset=utf-8" : "text/plain; charset=utf-8; version=0.0.4"; - await context.Response.OutputStream.WriteAsync(dataView.Array!, 0, dataView.Count).ConfigureAwait(false); +#if NET + await context.Response.OutputStream.WriteAsync(dataView.Array.AsMemory(0, dataView.Count)).ConfigureAwait(false); +#else + await context.Response.OutputStream.WriteAsync(dataView.Array, 0, dataView.Count).ConfigureAwait(false); +#endif } else { From c36a8a4f13f84652db301aac13faef482d6891ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:55:10 +0100 Subject: [PATCH 08/40] IDE0300 --- .../Internal/PrometheusMetric.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs index 8c1d8713509..5b14ad5b402 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs @@ -16,10 +16,10 @@ Histogram becomes histogram UpDownCounter becomes gauge * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#otlp-metric-points-to-prometheus */ - private static readonly PrometheusType[] MetricTypes = new PrometheusType[] - { + private static readonly PrometheusType[] MetricTypes = + [ PrometheusType.Untyped, PrometheusType.Counter, PrometheusType.Gauge, PrometheusType.Summary, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Gauge, - }; + ]; public PrometheusMetric(string name, string unit, PrometheusType type, bool disableTotalNameSuffixForCounters) { From 5c50b486352ae796995d07e52c0a63881a3a34fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:55:44 +0100 Subject: [PATCH 09/40] IDE0301 --- .../Internal/PrometheusCollectionManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs index 0a7616beea3..ae429048bb5 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs @@ -323,11 +323,11 @@ private ExportResult OnCollect(in Batch metrics) { if (this.exporter.OpenMetricsRequested) { - this.previousOpenMetricsDataView = new ArraySegment(Array.Empty(), 0, 0); + this.previousOpenMetricsDataView = new ArraySegment([], 0, 0); } else { - this.previousPlainTextDataView = new ArraySegment(Array.Empty(), 0, 0); + this.previousPlainTextDataView = new ArraySegment([], 0, 0); } return ExportResult.Failure; From 325db5ba2a11a4b1435082ca8eb91dc421d46310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:56:00 +0100 Subject: [PATCH 10/40] IDE0090 --- .../Internal/PrometheusMetric.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs index 5b14ad5b402..9f80d6dc5d3 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs @@ -127,7 +127,7 @@ internal static string SanitizeMetricName(string metricName) return sb?.ToString() ?? metricName; - static StringBuilder CreateStringBuilder(string name) => new StringBuilder(name.Length); + static StringBuilder CreateStringBuilder(string name) => new(name.Length); } internal static string RemoveAnnotations(string unit) From 72d7bd8faef17b132751f66e8516503d4cb95d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:56:27 +0100 Subject: [PATCH 11/40] IDE0074 --- .../Internal/PrometheusCollectionManager.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs index ae429048bb5..4b25c8560ab 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs @@ -68,10 +68,7 @@ public Task EnterCollect(bool openMetricsRequested) // If a collection is already running, return a task to wait on the result. if (this.collectionRunning) { - if (this.collectionTcs == null) - { - this.collectionTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - } + this.collectionTcs ??= new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); Interlocked.Increment(ref this.readerCount); this.ExitGlobalLock(); From bf044843b24023759be8bc87e2c74f8be3561ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:57:41 +0100 Subject: [PATCH 12/40] IDE0028 --- .../Internal/PrometheusCollectionManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs index 4b25c8560ab..ce4c5384485 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs @@ -34,8 +34,8 @@ public PrometheusCollectionManager(PrometheusExporter exporter) this.exporter = exporter; this.scrapeResponseCacheDurationMilliseconds = this.exporter.ScrapeResponseCacheDurationMilliseconds; this.onCollectRef = this.OnCollect; - this.metricsCache = new Dictionary(); - this.scopes = new HashSet(); + this.metricsCache = []; + this.scopes = []; } #if NET From b3fb89d5f1057a016c0a8225a73090ca33e451cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 13:58:19 +0100 Subject: [PATCH 13/40] IDE0032 --- .../Metrics/Reader/BaseExportingMetricReader.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry/Metrics/Reader/BaseExportingMetricReader.cs b/src/OpenTelemetry/Metrics/Reader/BaseExportingMetricReader.cs index 80f96cc143a..eeaa3fc68c5 100644 --- a/src/OpenTelemetry/Metrics/Reader/BaseExportingMetricReader.cs +++ b/src/OpenTelemetry/Metrics/Reader/BaseExportingMetricReader.cs @@ -17,7 +17,6 @@ public class BaseExportingMetricReader : MetricReader /// protected readonly BaseExporter exporter; - private readonly ExportModes supportedExportModes = ExportModes.Push | ExportModes.Pull; private readonly string exportCalledMessage; private readonly string exportSucceededMessage; private readonly string exportFailedMessage; @@ -38,12 +37,12 @@ public BaseExportingMetricReader(BaseExporter exporter) if (attributes.Length > 0) { var attr = (ExportModesAttribute)attributes[attributes.Length - 1]; - this.supportedExportModes = attr.Supported; + this.SupportedExportModes = attr.Supported; } if (exporter is IPullMetricExporter pullExporter) { - if (this.supportedExportModes.HasFlag(ExportModes.Push)) + if (this.SupportedExportModes.HasFlag(ExportModes.Push)) { pullExporter.Collect = this.Collect; } @@ -69,7 +68,7 @@ public BaseExportingMetricReader(BaseExporter exporter) /// /// Gets the supported . /// - protected ExportModes SupportedExportModes => this.supportedExportModes; + protected ExportModes SupportedExportModes { get; } = ExportModes.Push | ExportModes.Pull; internal override void SetParentProvider(BaseProvider parentProvider) { @@ -106,12 +105,12 @@ internal override bool ProcessMetrics(in Batch metrics, int timeoutMilli /// protected override bool OnCollect(int timeoutMilliseconds) { - if (this.supportedExportModes.HasFlag(ExportModes.Push)) + if (this.SupportedExportModes.HasFlag(ExportModes.Push)) { return base.OnCollect(timeoutMilliseconds); } - if (this.supportedExportModes.HasFlag(ExportModes.Pull) && PullMetricScope.IsPullAllowed) + if (this.SupportedExportModes.HasFlag(ExportModes.Pull) && PullMetricScope.IsPullAllowed) { return base.OnCollect(timeoutMilliseconds); } From c412172c9ba9e6380c35665ba346e1db4bac8a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 4 Mar 2025 14:01:35 +0100 Subject: [PATCH 14/40] CA1062 --- .../PrometheusExporterApplicationBuilderExtensions.cs | 2 ++ .../PrometheusExporterEndpointRouteBuilderExtensions.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs index baf8738f321..92bef6f1b59 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs @@ -99,6 +99,8 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint( Action? configureBranchedPipeline, string? optionsName) { + Guard.ThrowIfNull(app); + // Note: Order is important here. MeterProvider is accessed before // GetOptions so that any changes made to // PrometheusAspNetCoreOptions in deferred AddPrometheusExporter diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs index 6bbc70d4b94..6604935ebf0 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs @@ -69,6 +69,8 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( Action? configureBranchedPipeline, string? optionsName) { + Guard.ThrowIfNull(endpoints); + var builder = endpoints.CreateApplicationBuilder(); // Note: Order is important here. MeterProvider is accessed before From 260277c0c968276d83575c5f3a0db88fc49ba60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:45:35 +0100 Subject: [PATCH 15/40] Enable for tests --- .../OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj | 1 + ...OpenTelemetry.Exporter.Prometheus.HttpListener.Tests.csproj | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj index 6c4ea8cb626..c8b8dd686ee 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj @@ -4,6 +4,7 @@ Unit test project for Prometheus Exporter AspNetCore for OpenTelemetry $(TargetFrameworksForAspNetCoreTests) $(DefineConstants);PROMETHEUS_ASPNETCORE + latest-all diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests.csproj b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests.csproj index 6d6c38ff489..cc044afe37f 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests.csproj +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests.csproj @@ -4,9 +4,10 @@ Unit test project for Prometheus Exporter HttpListener for OpenTelemetry $(TargetFrameworksForTests) $(DefineConstants);PROMETHEUS_HTTP_LISTENER + latest-all - + From ede82242d570562a41d72ea941bd55d0e7408a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:47:50 +0100 Subject: [PATCH 16/40] CA1305 - Tests --- .../PrometheusHttpListenerTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 0ac04d2f299..913a3cf7f4d 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 using System.Diagnostics.Metrics; +using System.Globalization; using System.Net; #if NETFRAMEWORK using System.Net.Http; @@ -179,7 +180,7 @@ public async Task PrometheusExporterHttpServerIntegration_TestBufferSizeIncrease var oneKb = new string('A', 1024); for (var x = 0; x < 8500; x++) { - attributes.Add(new KeyValuePair(x.ToString(), oneKb)); + attributes.Add(new KeyValuePair(x.ToString(CultureInfo.InvariantCulture), oneKb)); } var provider = BuildMeterProvider(meter, attributes, out var address); From 582479e84eb9051f0aca2a2bd1aa3bd01f050c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:49:49 +0100 Subject: [PATCH 17/40] CA1307 - Tests --- .../PrometheusExporterMiddlewareTests.cs | 3 ++- .../PrometheusHttpListenerTests.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index c34dba29582..ffebe0c1c05 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -3,6 +3,7 @@ #if !NETFRAMEWORK using System.Diagnostics.Metrics; +using System.Globalization; using System.Net; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Builder; @@ -464,7 +465,7 @@ private static async Task VerifyAsync(long beginTimestamp, long endTimestamp, Ht Assert.True(matches.Count == 1, content); - var timestamp = long.Parse(matches[0].Groups[1].Value.Replace(".", string.Empty)); + var timestamp = long.Parse(matches[0].Groups[1].Value.Replace(".", string.Empty, StringComparison.Ordinal), CultureInfo.InvariantCulture); Assert.True(beginTimestamp <= timestamp && timestamp <= endTimestamp, $"{beginTimestamp} {timestamp} {endTimestamp}"); } diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 913a3cf7f4d..9cdefa7c450 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -202,7 +202,7 @@ public async Task PrometheusExporterHttpServerIntegration_TestBufferSizeIncrease Assert.Equal(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); - Assert.Contains("counter_double_999", content); + Assert.Contains("counter_double_999", content, StringComparison.Ordinal); Assert.DoesNotContain('\0', content); provider.Dispose(); From 45cf3ae75b755e360ec47aca54977abfb483d1db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:50:29 +0100 Subject: [PATCH 18/40] CA1310 - Tests --- .../PrometheusExporterMiddlewareTests.cs | 2 +- .../PrometheusHttpListenerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index ffebe0c1c05..db91ea94137 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -373,7 +373,7 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest( string acceptHeader = "application/openmetrics-text", KeyValuePair[]? meterTags = null) { - var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text"); + var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text", StringComparison.Ordinal); using var host = await StartTestHostAsync(configure, configureServices, registerMeterProvider, configureOptions); diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 9cdefa7c450..18d4ea78a40 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -263,7 +263,7 @@ private static MeterProvider BuildMeterProvider(Meter meter, IEnumerable[]? meterTags = null) { - var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text"); + var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text", StringComparison.Ordinal); using var meter = new Meter(MeterName, MeterVersion, meterTags); From 6f8db2c936d93be028ac35c53bdfb0bda60bd462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:51:21 +0100 Subject: [PATCH 19/40] CA1825 - Tests --- .../PrometheusHttpListenerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 18d4ea78a40..87f02ca5550 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -48,7 +48,7 @@ public void UriPrefixesEmptyList() { Assert.Throws(() => { - TestPrometheusHttpListenerUriPrefixOptions(new string[] { }); + TestPrometheusHttpListenerUriPrefixOptions([]); }); } From 0b270a76efb04dcb36f7167c808f63afdc8b868b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:52:00 +0100 Subject: [PATCH 20/40] CA1822 - Tests --- .../PrometheusHttpListenerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 87f02ca5550..6c2687a50d6 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -261,7 +261,7 @@ private static MeterProvider BuildMeterProvider(Meter meter, IEnumerable[]? meterTags = null) + private static Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetrics = false, string acceptHeader = "application/openmetrics-text", KeyValuePair[]? meterTags = null) { var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text", StringComparison.Ordinal); From 984c4ed0664910ec3de0b36676241ea569c8e967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:52:44 +0100 Subject: [PATCH 21/40] IDE0301 - Tests --- .../PrometheusExporterMiddlewareTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index db91ea94137..dca977086fe 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -113,7 +113,7 @@ public Task PrometheusExporterMiddlewareIntegration_MixedPredicateAndPath() { if (!rsp.Headers.TryGetValues("X-MiddlewareExecuted", out IEnumerable? headers)) { - headers = Array.Empty(); + headers = []; } Assert.Equal("true", headers.FirstOrDefault()); @@ -140,7 +140,7 @@ public Task PrometheusExporterMiddlewareIntegration_MixedPath() { if (!rsp.Headers.TryGetValues("X-MiddlewareExecuted", out IEnumerable? headers)) { - headers = Array.Empty(); + headers = []; } Assert.Equal("true", headers.FirstOrDefault()); From 0f48075c7fecd52fd270189fff2a6bf2582c9bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:53:37 +0100 Subject: [PATCH 22/40] IDE0305 - Tests --- .../PrometheusCollectionManagerTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs index 36f5e124e67..34b18652e5f 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs @@ -60,7 +60,7 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon return new Response { CollectionResponse = response, - ViewPayload = openMetricsRequested ? response.OpenMetricsView.ToArray() : response.PlainTextView.ToArray(), + ViewPayload = openMetricsRequested ? [.. response.OpenMetricsView] : [.. response.PlainTextView], }; } finally @@ -124,7 +124,7 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon return new Response { CollectionResponse = response, - ViewPayload = openMetricsRequested ? response.OpenMetricsView.ToArray() : response.PlainTextView.ToArray(), + ViewPayload = openMetricsRequested ? [.. response.OpenMetricsView] : [.. response.PlainTextView], }; } finally From bf0ff333c482188505d84f44ce43ec1575b648c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:54:21 +0100 Subject: [PATCH 23/40] IDE0046 - Tests --- .../PrometheusHttpListenerTests.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 6c2687a50d6..6bfb0ab015f 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -253,12 +253,7 @@ private static MeterProvider BuildMeterProvider(Meter meter, IEnumerable[]? meterTags = null) From bf4fbf528280f0afb8d5d0fb59678c5687351f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 16:55:51 +0100 Subject: [PATCH 24/40] CS4032 --- .../PrometheusHttpListenerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 6bfb0ab015f..08d371af81e 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -256,7 +256,7 @@ private static MeterProvider BuildMeterProvider(Meter meter, IEnumerable[]? meterTags = null) + private static async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetrics = false, string acceptHeader = "application/openmetrics-text", KeyValuePair[]? meterTags = null) { var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text", StringComparison.Ordinal); From f381f7e044e669b02a604b68e5cd0543ecb89044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 17:42:02 +0100 Subject: [PATCH 25/40] CA1852 - Tests --- test/OpenTelemetry.Tests/Shared/TestEventListener.cs | 2 +- test/OpenTelemetry.Tests/Shared/Utils.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Tests/Shared/TestEventListener.cs b/test/OpenTelemetry.Tests/Shared/TestEventListener.cs index fb497190f5e..d442f10ef97 100644 --- a/test/OpenTelemetry.Tests/Shared/TestEventListener.cs +++ b/test/OpenTelemetry.Tests/Shared/TestEventListener.cs @@ -8,7 +8,7 @@ namespace OpenTelemetry.Tests; /// /// Event listener for testing event sources. /// -internal class TestEventListener : EventListener +internal sealed class TestEventListener : EventListener { /// Unique Id used to identify events from the test thread. private readonly Guid activityId; diff --git a/test/OpenTelemetry.Tests/Shared/Utils.cs b/test/OpenTelemetry.Tests/Shared/Utils.cs index d85c8c74e47..95f24821d0e 100644 --- a/test/OpenTelemetry.Tests/Shared/Utils.cs +++ b/test/OpenTelemetry.Tests/Shared/Utils.cs @@ -6,7 +6,7 @@ namespace OpenTelemetry.Tests; -internal class Utils +internal static class Utils { [MethodImpl(MethodImplOptions.NoInlining)] public static string GetCurrentMethodName() From d763a8e05a089d813d71dc6a0171c2dfc5d5dfb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 5 Mar 2025 17:44:33 +0100 Subject: [PATCH 26/40] CA1860 - Tests --- .../PrometheusExporterMiddlewareTests.cs | 2 +- .../PrometheusHttpListenerTests.cs | 2 +- test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index dca977086fe..78990b4d9cf 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -433,7 +433,7 @@ private static async Task VerifyAsync(long beginTimestamp, long endTimestamp, Ht Assert.Equal("text/plain; charset=utf-8; version=0.0.4", response.Content.Headers.ContentType!.ToString()); } - var additionalTags = meterTags != null && meterTags.Any() + var additionalTags = meterTags is { Length: > 0 } ? $"{string.Join(",", meterTags.Select(x => $"{x.Key}=\"{x.Value}\""))}," : string.Empty; diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 08d371af81e..0599341f946 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -300,7 +300,7 @@ private static async Task RunPrometheusExporterHttpServerIntegrationTest(bool sk Assert.Equal("text/plain; charset=utf-8; version=0.0.4", response.Content.Headers.ContentType!.ToString()); } - var additionalTags = meterTags != null && meterTags.Any() + var additionalTags = meterTags is { Length: > 0 } ? $"{string.Join(",", meterTags.Select(x => $"{x.Key}='{x.Value}'"))}," : string.Empty; diff --git a/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs b/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs index d7066c3195c..11cfabe2464 100644 --- a/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs +++ b/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs @@ -128,6 +128,6 @@ private static EventAttribute GetEventAttribute(MethodInfo eventMethod) private static IEnumerable GetEventMethods(EventSource eventSource) { MethodInfo[] methods = eventSource.GetType().GetMethods(); - return methods.Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Any()); + return methods.Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Length > 0); } } From 5eacfb3ad75dcb30d60194a5485cc3a7f8e61a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 09:14:27 +0100 Subject: [PATCH 27/40] CS0176 - Tests --- .../PrometheusHttpListenerTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 0599341f946..cf041700a61 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -64,25 +64,25 @@ public void UriPrefixesInvalid() [Fact] public async Task PrometheusExporterHttpServerIntegration() { - await this.RunPrometheusExporterHttpServerIntegrationTest(); + await RunPrometheusExporterHttpServerIntegrationTest(); } [Fact] public async Task PrometheusExporterHttpServerIntegration_NoMetrics() { - await this.RunPrometheusExporterHttpServerIntegrationTest(skipMetrics: true); + await RunPrometheusExporterHttpServerIntegrationTest(skipMetrics: true); } [Fact] public async Task PrometheusExporterHttpServerIntegration_NoOpenMetrics() { - await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: string.Empty); + await RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: string.Empty); } [Fact] public async Task PrometheusExporterHttpServerIntegration_UseOpenMetricsVersionHeader() { - await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: "application/openmetrics-text; version=1.0.0"); + await RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: "application/openmetrics-text; version=1.0.0"); } [Fact] @@ -94,7 +94,7 @@ public async Task PrometheusExporterHttpServerIntegration_NoOpenMetrics_WithMete new("meter2", "value2"), }; - await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: string.Empty, meterTags: tags); + await RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: string.Empty, meterTags: tags); } [Fact] @@ -106,7 +106,7 @@ public async Task PrometheusExporterHttpServerIntegration_OpenMetrics_WithMeterT new("meter2", "value2"), }; - await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: "application/openmetrics-text; version=1.0.0", meterTags: tags); + await RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: "application/openmetrics-text; version=1.0.0", meterTags: tags); } [Fact] From 65d74605490cbebdda42c057f7a36a2a6ecb8f6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 09:32:45 +0100 Subject: [PATCH 28/40] CA2213 - Tests --- test/OpenTelemetry.Tests/Shared/TestEventListener.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/OpenTelemetry.Tests/Shared/TestEventListener.cs b/test/OpenTelemetry.Tests/Shared/TestEventListener.cs index d442f10ef97..572ce1a6c4b 100644 --- a/test/OpenTelemetry.Tests/Shared/TestEventListener.cs +++ b/test/OpenTelemetry.Tests/Shared/TestEventListener.cs @@ -66,6 +66,12 @@ public void ClearMessages() this.events.Clear(); } + public override void Dispose() + { + this.eventWritten.Dispose(); + base.Dispose(); + } + /// Handler for event source writes. /// The event data that was written. protected override void OnEventWritten(EventWrittenEventArgs eventData) From 781c1cf4a802f1667f069ac835b149d989b9e4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 09:34:40 +0100 Subject: [PATCH 29/40] CA2234 - Tests --- .../PrometheusExporterMiddlewareTests.cs | 4 ++-- .../PrometheusHttpListenerTests.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index 78990b4d9cf..809c87d8561 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -315,7 +315,7 @@ public async Task PrometheusExporterMiddlewareIntegration_TestBufferSizeIncrease using var client = host.GetTestClient(); - using var response = await client.GetAsync("/metrics"); + using var response = await client.GetAsync(new Uri("/metrics")); var text = await response.Content.ReadAsStringAsync(); Assert.NotEmpty(text); @@ -401,7 +401,7 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest( client.DefaultRequestHeaders.Add("Accept", acceptHeader); } - using var response = await client.GetAsync(path); + using var response = await client.GetAsync(new Uri(path)); var endTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index cf041700a61..1ff2521854e 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -198,7 +198,7 @@ public async Task PrometheusExporterHttpServerIntegration_TestBufferSizeIncrease client.DefaultRequestHeaders.Add("Accept", acceptHeader); } - using var response = await client.GetAsync($"{address}metrics"); + using var response = await client.GetAsync(new Uri($"{address}metrics")); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); @@ -284,7 +284,7 @@ private static async Task RunPrometheusExporterHttpServerIntegrationTest(bool sk client.DefaultRequestHeaders.Add("Accept", acceptHeader); } - using var response = await client.GetAsync($"{address}metrics"); + using var response = await client.GetAsync(new Uri($"{address}metrics")); if (!skipMetrics) { From 515e7df495c0b715835e15b2e038d1a75b74c1ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 09:51:07 +0100 Subject: [PATCH 30/40] CA2201 - Tests --- test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs b/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs index 11cfabe2464..8b30f7694b1 100644 --- a/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs +++ b/test/OpenTelemetry.Tests/Shared/EventSourceTestHelper.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Tracing; using System.Globalization; using System.Reflection; +using Xunit.Sdk; namespace OpenTelemetry.Tests; @@ -34,11 +35,11 @@ private static void VerifyMethodImplementation(EventSource eventSource, MethodIn actualEvent = listener.Messages.FirstOrDefault(x => x.EventId == 0); if (actualEvent != null) { - throw new Exception(actualEvent.Message); + throw new InvalidOperationException(actualEvent.Message); } // give up - throw new Exception("Listener failed to collect event."); + throw new InvalidOperationException("Listener failed to collect event."); } VerifyEventId(eventMethod, actualEvent); @@ -49,7 +50,7 @@ private static void VerifyMethodImplementation(EventSource eventSource, MethodIn { var name = eventMethod.DeclaringType?.Name + "." + eventMethod.Name; - throw new Exception("Method '" + name + "' is implemented incorrectly.", e); + throw new InvalidOperationException("Method '" + name + "' is implemented incorrectly.", e); } finally { @@ -116,7 +117,7 @@ private static void AssertEqual(string methodName, T expected, T actual) methodName, expected, actual); - throw new Exception(errorMessage); + throw EqualException.ForMismatchedValuesWithError(expected, actual, banner: errorMessage); } } From 6d36eeb96d70d9e97d17993f5632be44533dbad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 09:52:23 +0100 Subject: [PATCH 31/40] Ignore CA2000 - Tests - it should be disposed by other object --- .../PrometheusCollectionManagerTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs index 34b18652e5f..1641ef91a62 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs @@ -31,7 +31,9 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon #endif .Build()) { +#pragma warning disable CA2000 if (!provider.TryFindExporter(out PrometheusExporter? exporter)) +#pragma warning restore CA2000 { throw new InvalidOperationException("PrometheusExporter could not be found on MeterProvider."); } From 26acef7809dbdf432d32b7cab2bfc98b308b179f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 10:04:57 +0100 Subject: [PATCH 32/40] Disable CA2007 for tests --- .editorconfig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.editorconfig b/.editorconfig index e9277261794..5f172a29b3a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -160,6 +160,9 @@ dotnet_diagnostic.RS0041.severity = suggestion # CA1515: Disable making types internal for Tests classes. It is required by xunit dotnet_diagnostic.CA1515.severity = none +# CA2007: Disable Consider calling ConfigureAwait on the awaited task. It is not working with xunit +dotnet_diagnostic.CA2007.severity = none + [**/obj/**.cs] generated_code = true From eee5b5f5f3c148a361a5f7161f9848443287bbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 10:33:27 +0100 Subject: [PATCH 33/40] Disable CA5394 - nothing to secure here --- .../PrometheusHttpListenerTests.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 1ff2521854e..8dafcff7b9a 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -114,7 +114,6 @@ public void PrometheusHttpListenerThrowsOnStart() { Random random = new Random(); int retryAttempts = 5; - int port = 0; string? address = null; PrometheusExporter? exporter = null; @@ -123,7 +122,9 @@ public void PrometheusHttpListenerThrowsOnStart() // Step 1: Start a listener on a random port. while (retryAttempts-- != 0) { - port = random.Next(2000, 5000); +#pragma warning disable CA5394 + int port = random.Next(2000, 5000); +#pragma warning restore CA5394 address = $"http://localhost:{port}/"; try @@ -223,13 +224,14 @@ private static MeterProvider BuildMeterProvider(Meter meter, IEnumerable Date: Fri, 7 Mar 2025 10:33:58 +0100 Subject: [PATCH 34/40] IDE0028 - Tests --- test/OpenTelemetry.Tests/Logs/LogRecordSharedPoolTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Tests/Logs/LogRecordSharedPoolTests.cs b/test/OpenTelemetry.Tests/Logs/LogRecordSharedPoolTests.cs index d38c82d1a0c..3da599ad2b9 100644 --- a/test/OpenTelemetry.Tests/Logs/LogRecordSharedPoolTests.cs +++ b/test/OpenTelemetry.Tests/Logs/LogRecordSharedPoolTests.cs @@ -170,7 +170,7 @@ public async Task ExportTest(bool warmup) using BatchLogRecordExportProcessor processor = new(new NoopExporter()); - List tasks = new(); + List tasks = []; for (int i = 0; i < Environment.ProcessorCount; i++) { @@ -230,7 +230,7 @@ public async Task DeadlockTest() var pool = LogRecordSharedPool.Current; - List tasks = new(); + List tasks = []; for (int i = 0; i < Environment.ProcessorCount; i++) { From 665de90a0644ebacc9d7a7102fabca10582b0bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 10:34:49 +0100 Subject: [PATCH 35/40] CA1852 - Tests --- .../PrometheusCollectionManagerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs index 1641ef91a62..7089b03f284 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs @@ -154,7 +154,7 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon } } - private class Response + private sealed class Response { public PrometheusCollectionManager.CollectionResponse CollectionResponse; From 60109a13bd2f987ed5be585ddb516e828c10462d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 11:29:41 +0100 Subject: [PATCH 36/40] CA1515 - Tests --- .../OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj | 2 +- .../{EventSourceTest.cs => EventSourceTests.cs} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/{EventSourceTest.cs => EventSourceTests.cs} (92%) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj index c8b8dd686ee..8c37feb0e25 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj @@ -29,7 +29,7 @@ - + diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTest.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTests.cs similarity index 92% rename from test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTest.cs rename to test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTests.cs index baf8dc43377..b1aed407d63 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTest.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTests.cs @@ -6,7 +6,7 @@ namespace OpenTelemetry.Exporter.Prometheus.Tests; -public class EventSourceTest +public class EventSourceTests { [Fact] public void EventSourceTest_PrometheusExporterEventSource() From 6023b6cdfef0d4ac276ce5d438604bb8ae1d651d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 7 Mar 2025 12:49:31 +0100 Subject: [PATCH 37/40] Fix parsing Uri - tests --- .../PrometheusExporterMiddlewareTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index 809c87d8561..98d503e8e92 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -315,7 +315,7 @@ public async Task PrometheusExporterMiddlewareIntegration_TestBufferSizeIncrease using var client = host.GetTestClient(); - using var response = await client.GetAsync(new Uri("/metrics")); + using var response = await client.GetAsync(new Uri("/metrics", UriKind.Relative)); var text = await response.Content.ReadAsStringAsync(); Assert.NotEmpty(text); @@ -401,7 +401,7 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest( client.DefaultRequestHeaders.Add("Accept", acceptHeader); } - using var response = await client.GetAsync(new Uri(path)); + using var response = await client.GetAsync(new Uri(path, UriKind.Relative)); var endTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); From 19a2212ebf3f3fbb8c6d7707eef6302f4b6c4eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 11 Mar 2025 14:39:13 +0100 Subject: [PATCH 38/40] #pragma warning description --- .../PrometheusExporterMeterProviderBuilderExtensions.cs | 4 ++-- ...rometheusHttpListenerMeterProviderBuilderExtensions.cs | 4 ++-- .../PrometheusCollectionManagerTests.cs | 4 ++-- .../PrometheusHttpListenerTests.cs | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs index 203581d2ca1..e3567d4ad63 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs @@ -64,9 +64,9 @@ public static MeterProviderBuilder AddPrometheusExporter( private static BaseExportingMetricReader BuildPrometheusExporterMetricReader(PrometheusAspNetCoreOptions options) { -#pragma warning disable CA2000 +#pragma warning disable CA2000 // Dispose objects before losing scope var exporter = new PrometheusExporter(options.ExporterOptions); -#pragma warning restore CA2000 +#pragma warning restore CA2000 // Dispose objects before losing scope return new BaseExportingMetricReader(exporter) { diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs index df8b9c62653..94eea0f5742 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs @@ -78,9 +78,9 @@ private static BaseExportingMetricReader BuildPrometheusHttpListenerMetricReader try { -#pragma warning disable CA2000 +#pragma warning disable CA2000 // Dispose objects before losing scope var listener = new PrometheusHttpListener(exporter, options); -#pragma warning restore CA2000 +#pragma warning restore CA2000 // Dispose objects before losing scope exporter.OnDispose = listener.Dispose; listener.Start(); } diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs index 7089b03f284..b7c05d84e75 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs @@ -31,9 +31,9 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon #endif .Build()) { -#pragma warning disable CA2000 +#pragma warning disable CA2000 // Dispose objects before losing scope if (!provider.TryFindExporter(out PrometheusExporter? exporter)) -#pragma warning restore CA2000 +#pragma warning restore CA2000 // Dispose objects before losing scope { throw new InvalidOperationException("PrometheusExporter could not be found on MeterProvider."); } diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index 8dafcff7b9a..7ff58940692 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -122,9 +122,9 @@ public void PrometheusHttpListenerThrowsOnStart() // Step 1: Start a listener on a random port. while (retryAttempts-- != 0) { -#pragma warning disable CA5394 +#pragma warning disable CA5394 // Do not use insecure randomness int port = random.Next(2000, 5000); -#pragma warning restore CA5394 +#pragma warning restore CA5394 // Do not use insecure randomness address = $"http://localhost:{port}/"; try @@ -229,9 +229,9 @@ private static MeterProvider BuildMeterProvider(Meter meter, IEnumerable Date: Tue, 11 Mar 2025 14:54:37 +0100 Subject: [PATCH 39/40] Avoid same names in different contexts --- .../PrometheusCollectionManagerTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs index b7c05d84e75..718364cc7e3 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs @@ -120,13 +120,13 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon { collectTasks[i] = Task.Run(async () => { - var response = await exporter.CollectionManager.EnterCollect(openMetricsRequested); + var collectionResponse = await exporter.CollectionManager.EnterCollect(openMetricsRequested); try { return new Response { - CollectionResponse = response, - ViewPayload = openMetricsRequested ? [.. response.OpenMetricsView] : [.. response.PlainTextView], + CollectionResponse = collectionResponse, + ViewPayload = openMetricsRequested ? [.. collectionResponse.OpenMetricsView] : [.. collectionResponse.PlainTextView], }; } finally From 3038ff092f35a0820ef5c54f939b6713640c5233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 12 Mar 2025 10:26:23 +0100 Subject: [PATCH 40/40] Disable CA1849 --- .../PrometheusCollectionManagerTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs index 718364cc7e3..75e3261b71a 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs @@ -112,7 +112,10 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon exporter.CollectionManager.ExitCollect(); } +#pragma warning disable CA1849 // 'Thread.Sleep(int)' synchronously blocks. Use await instead. + // Changing to await Task.Delay leads to test instability. Thread.Sleep(exporter.ScrapeResponseCacheDurationMilliseconds); +#pragma warning restore CA1849 // 'Thread.Sleep(int)' synchronously blocks. Use await instead. counter.Add(100);