From 0203e649d5c0553f1a632d50fb1d0decf437b843 Mon Sep 17 00:00:00 2001 From: Robert Coltheart Date: Tue, 28 Nov 2023 09:10:35 +1100 Subject: [PATCH] Fix tests --- .../PrometheusExporterMiddlewareTests.cs | 26 +++++++++++++------ .../PrometheusHttpListenerTests.cs | 13 ++++++++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs index 6fe77fefb5e..c348cf0709a 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs @@ -244,7 +244,8 @@ public async Task PrometheusExporterMiddlewareIntegration_DisableExportScopeInfo await RunPrometheusExporterMiddlewareIntegrationTest( "/metrics", app => app.UseOpenTelemetryPrometheusScrapingEndpoint(), - configureOptions: o => o.ScopeInfoEnabled = false); + configureOptions: o => o.ScopeInfoEnabled = false, + skipScopeInfo: true); } private static async Task RunPrometheusExporterMiddlewareIntegrationTest( @@ -254,7 +255,8 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest( Action validateResponse = null, bool registerMeterProvider = true, Action configureOptions = null, - bool skipMetrics = false) + bool skipMetrics = false, + bool skipScopeInfo = false) { using var host = await new HostBuilder() .ConfigureWebHost(webBuilder => webBuilder @@ -305,14 +307,22 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest( string content = await response.Content.ReadAsStringAsync(); + string expected = skipScopeInfo + ? "# TYPE counter_double_total counter\n" + + "counter_double_total{key1='value1',key2='value2'} 101.17 (\\d+)\n" + + "\n" + + "# EOF\n" + : "# TYPE otel_scope_info info\n" + + "# HELP otel_scope_info Scope metadata\n" + + $"otel_scope_info{{otel_scope_name='{MeterName}'}} 1\n" + + "# TYPE counter_double_total counter\n" + + $"counter_double_total{{otel_scope_name='{MeterName}',key1='value1',key2='value2'}} 101.17 (\\d+)\n" + + "\n" + + "# EOF\n"; + var matches = Regex.Matches( content, - ("^" - + "# TYPE counter_double_total counter\n" - + "counter_double_total{key1='value1',key2='value2'} 101.17 (\\d+)\n" - + "\n" - + "# EOF\n" - + "$").Replace('\'', '"')); + ("^" + expected + "$").Replace('\'', '"')); Assert.Single(matches); diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index d964432009e..1d27f372cde 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -133,9 +133,18 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri Assert.True(response.Content.Headers.Contains("Last-Modified")); Assert.Equal("text/plain; charset=utf-8; version=0.0.4", response.Content.Headers.ContentType.ToString()); + var content = await response.Content.ReadAsStringAsync(); + Assert.Matches( - "^# TYPE counter_double_total counter\ncounter_double_total{key1='value1',key2='value2'} 101.17 \\d+\n\n# EOF\n$".Replace('\'', '"'), - await response.Content.ReadAsStringAsync()); + ("^" + + "# TYPE otel_scope_info info\n" + + "# HELP otel_scope_info Scope metadata\n" + + $"otel_scope_info{{otel_scope_name='{this.meterName}'}} 1\n" + + "# TYPE counter_double_total counter\n" + + $"counter_double_total{{otel_scope_name='{this.meterName}',key1='value1',key2='value2'}} 101.17 \\d+\n\n" + + "# EOF\n" + + "$").Replace('\'', '"'), + content); } else {