diff --git a/docs/metrics/getting-started-prometheus-grafana/getting-started-prometheus-grafana.csproj b/docs/metrics/getting-started-prometheus-grafana/getting-started-prometheus-grafana.csproj index 455ed30ceb4..8d59ff99ce3 100644 --- a/docs/metrics/getting-started-prometheus-grafana/getting-started-prometheus-grafana.csproj +++ b/docs/metrics/getting-started-prometheus-grafana/getting-started-prometheus-grafana.csproj @@ -1,7 +1,4 @@ - - netstandard2.0 - diff --git a/examples/Console/TestPrometheusExporter.cs b/examples/Console/TestPrometheusExporter.cs index 29fc3879f39..0b1284c4ae0 100644 --- a/examples/Console/TestPrometheusExporter.cs +++ b/examples/Console/TestPrometheusExporter.cs @@ -52,8 +52,7 @@ internal static object Run(int port) .AddMeter(MyMeter.Name) .AddMeter(MyMeter2.Name) .AddPrometheusHttpListener( - exporterOptions => exporterOptions.ScrapeResponseCacheDurationMilliseconds = 0, - listenerOptions => listenerOptions.Prefixes = new string[] { $"http://localhost:{port}/" }) + options => options.Prefixes = new string[] { $"http://localhost:{port}/" }) .Build(); var process = Process.GetCurrentProcess(); diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt index f9271183c1c..3c9d701eb94 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt @@ -1,11 +1,11 @@ Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions -OpenTelemetry.Exporter.Prometheus.AspNetCore.PrometheusExporterOptions -OpenTelemetry.Exporter.Prometheus.AspNetCore.PrometheusExporterOptions.PrometheusExporterOptions() -> void -OpenTelemetry.Exporter.Prometheus.AspNetCore.PrometheusExporterOptions.ScrapeEndpointPath.get -> string -OpenTelemetry.Exporter.Prometheus.AspNetCore.PrometheusExporterOptions.ScrapeEndpointPath.set -> void -OpenTelemetry.Exporter.Prometheus.AspNetCore.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.get -> int -OpenTelemetry.Exporter.Prometheus.AspNetCore.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.set -> void +OpenTelemetry.Exporter.Prometheus.PrometheusExporterOptions +OpenTelemetry.Exporter.Prometheus.PrometheusExporterOptions.PrometheusExporterOptions() -> void +OpenTelemetry.Exporter.Prometheus.PrometheusExporterOptions.ScrapeEndpointPath.get -> string +OpenTelemetry.Exporter.Prometheus.PrometheusExporterOptions.ScrapeEndpointPath.set -> void +OpenTelemetry.Exporter.Prometheus.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.get -> int +OpenTelemetry.Exporter.Prometheus.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.set -> void OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions.UseOpenTelemetryPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, OpenTelemetry.Metrics.MeterProvider meterProvider, System.Func predicate, string path, System.Action configureBranchedPipeline) -> Microsoft.AspNetCore.Builder.IApplicationBuilder @@ -14,4 +14,4 @@ static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensio static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path = null, OpenTelemetry.Metrics.MeterProvider meterProvider = null, System.Action configureBranchedPipeline = null) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder -static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder 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 33a00e05f1a..f8f55cb18e3 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj @@ -3,7 +3,7 @@ netcoreapp3.1 - AspNetCore middleware for hosting OpenTelemetry .NET Prometheus exporter + ASP.NET Core middleware for hosting OpenTelemetry .NET Prometheus Exporter $(PackageTags);prometheus;metrics core- @@ -19,14 +19,13 @@ - - - - - - + + + + + diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs index 00fe170e6fb..491e70eac02 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs @@ -19,7 +19,7 @@ using System; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using OpenTelemetry.Exporter.Prometheus.AspNetCore; +using OpenTelemetry.Exporter.Prometheus; using OpenTelemetry.Internal; using OpenTelemetry.Metrics; diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs index 372a96e8a81..5d260878ffa 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs @@ -20,7 +20,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; -using OpenTelemetry.Exporter.Prometheus.AspNetCore; +using OpenTelemetry.Exporter.Prometheus; using OpenTelemetry.Internal; using OpenTelemetry.Metrics; diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs index d230d03d042..7039db2a369 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs @@ -15,8 +15,7 @@ // using System; -using OpenTelemetry.Exporter.Prometheus.AspNetCore; -using OpenTelemetry.Exporter.Prometheus.HttpListener.Shared; +using OpenTelemetry.Exporter.Prometheus; using OpenTelemetry.Internal; namespace OpenTelemetry.Metrics @@ -51,7 +50,7 @@ private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder b { configure?.Invoke(options); - var exporter = new PrometheusExporter(options); + var exporter = new PrometheusExporter(scrapeEndpointPath: options.ScrapeEndpointPath, scrapeResponseCacheDurationMilliseconds: options.ScrapeResponseCacheDurationMilliseconds); var reader = new BaseExportingMetricReader(exporter) { TemporalityPreference = MetricReaderTemporalityPreference.Cumulative, diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs index d76a85d44d3..826ca135763 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs @@ -19,11 +19,10 @@ using System.Diagnostics; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using OpenTelemetry.Exporter.Prometheus.HttpListener.Shared; using OpenTelemetry.Internal; using OpenTelemetry.Metrics; -namespace OpenTelemetry.Exporter.Prometheus.AspNetCore +namespace OpenTelemetry.Exporter.Prometheus { /// /// ASP.NET Core middleware for exposing a Prometheus metrics scraping endpoint. diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporterOptions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterOptions.cs similarity index 85% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporterOptions.cs rename to src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterOptions.cs index 4662ee3e263..bf7576117b1 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterOptions.cs @@ -14,14 +14,9 @@ // limitations under the License. // -using System; using OpenTelemetry.Internal; -#if PROMETHEUS_ASPNETCORE -namespace OpenTelemetry.Exporter.Prometheus.AspNetCore -#else -namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Shared -#endif +namespace OpenTelemetry.Exporter.Prometheus { /// /// Prometheus exporter options. @@ -29,12 +24,11 @@ namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Shared public class PrometheusExporterOptions { internal const string DefaultScrapeEndpointPath = "/metrics"; - internal Func GetUtcNowDateTimeOffset = () => DateTimeOffset.UtcNow; private int scrapeResponseCacheDurationMilliseconds = 10 * 1000; /// - /// Gets or sets the path to use for the scraping endpoint. Default value: /metrics. + /// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics". /// public string ScrapeEndpointPath { get; set; } = DefaultScrapeEndpointPath; diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt index 5f48f2c7562..7b61e63513e 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1,12 +1,8 @@ -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions.Prefixes.get -> System.Collections.Generic.IReadOnlyCollection -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions.Prefixes.set -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.PrometheusExporterOptions() -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeEndpointPath.get -> string -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeEndpointPath.set -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.get -> int -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.set -> void -OpenTelemetry.Metrics.PrometheusExporterHttpListenerMeterProviderBuilderExtensions -static OpenTelemetry.Metrics.PrometheusExporterHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configureExporterOptions = null, System.Action configureListenerOptions = null) -> OpenTelemetry.Metrics.MeterProviderBuilder +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.Prefixes.get -> System.Collections.Generic.IReadOnlyCollection +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.Prefixes.set -> void +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.ScrapeEndpointPath.get -> string +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.ScrapeEndpointPath.set -> void +OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 5f48f2c7562..7b61e63513e 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,12 +1,8 @@ -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions.Prefixes.get -> System.Collections.Generic.IReadOnlyCollection -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions.Prefixes.set -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.PrometheusExporterOptions() -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeEndpointPath.get -> string -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeEndpointPath.set -> void -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.get -> int -OpenTelemetry.Exporter.Prometheus.HttpListener.Shared.PrometheusExporterOptions.ScrapeResponseCacheDurationMilliseconds.set -> void -OpenTelemetry.Metrics.PrometheusExporterHttpListenerMeterProviderBuilderExtensions -static OpenTelemetry.Metrics.PrometheusExporterHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configureExporterOptions = null, System.Action configureListenerOptions = null) -> OpenTelemetry.Metrics.MeterProviderBuilder +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.Prefixes.get -> System.Collections.Generic.IReadOnlyCollection +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.Prefixes.set -> void +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.ScrapeEndpointPath.get -> string +OpenTelemetry.Exporter.Prometheus.PrometheusHttpListenerOptions.ScrapeEndpointPath.set -> void +OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusCollectionManager.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs similarity index 95% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusCollectionManager.cs rename to src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs index 43da102246f..704422b637e 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusCollectionManager.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs @@ -20,12 +20,12 @@ using System.Threading.Tasks; using OpenTelemetry.Metrics; -namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Shared +namespace OpenTelemetry.Exporter.Prometheus { internal sealed class PrometheusCollectionManager { private readonly PrometheusExporter exporter; - private readonly int scrapeResponseCacheDurationInMilliseconds; + private readonly int scrapeResponseCacheDurationMilliseconds; private readonly Func, ExportResult> onCollectRef; private byte[] buffer = new byte[85000]; // encourage the object to live in LOH (large object heap) private int globalLockState; @@ -38,7 +38,7 @@ internal sealed class PrometheusCollectionManager public PrometheusCollectionManager(PrometheusExporter exporter) { this.exporter = exporter; - this.scrapeResponseCacheDurationInMilliseconds = this.exporter.Options.ScrapeResponseCacheDurationMilliseconds; + this.scrapeResponseCacheDurationMilliseconds = this.exporter.ScrapeResponseCacheDurationMilliseconds; this.onCollectRef = this.OnCollect; } @@ -53,8 +53,8 @@ public Task EnterCollect() // If we are within {ScrapeResponseCacheDurationMilliseconds} of the // last successful collect, return the previous view. if (this.previousDataViewGeneratedAtUtc.HasValue - && this.scrapeResponseCacheDurationInMilliseconds > 0 - && this.previousDataViewGeneratedAtUtc.Value.AddMilliseconds(this.scrapeResponseCacheDurationInMilliseconds) >= DateTime.UtcNow) + && this.scrapeResponseCacheDurationMilliseconds > 0 + && this.previousDataViewGeneratedAtUtc.Value.AddMilliseconds(this.scrapeResponseCacheDurationMilliseconds) >= DateTime.UtcNow) { Interlocked.Increment(ref this.readerCount); this.ExitGlobalLock(); diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporter.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs similarity index 74% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporter.cs rename to src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs index ec2dbf0467b..93514ef4c97 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporter.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs @@ -15,12 +15,10 @@ // using System; -#if PROMETHEUS_ASPNETCORE -using OpenTelemetry.Exporter.Prometheus.AspNetCore; -#endif +using OpenTelemetry.Internal; using OpenTelemetry.Metrics; -namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Shared +namespace OpenTelemetry.Exporter.Prometheus { /// /// Exporter of OpenTelemetry metrics to Prometheus. @@ -28,8 +26,6 @@ namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Shared [ExportModes(ExportModes.Pull)] internal sealed class PrometheusExporter : BaseExporter, IPullMetricExporter { - internal const string HttpListenerStartFailureExceptionMessage = "PrometheusExporter http listener could not be started."; - internal readonly PrometheusExporterOptions Options; private Func funcCollect; private Func, ExportResult> funcExport; private bool disposed = false; @@ -37,10 +33,16 @@ internal sealed class PrometheusExporter : BaseExporter, IPullMetricExpo /// /// Initializes a new instance of the class. /// - /// Options for the exporter. - public PrometheusExporter(PrometheusExporterOptions options) + /// Scraping endpoint. + /// + /// The cache duration in milliseconds for scrape responses. Default value: 0. + /// + public PrometheusExporter(string scrapeEndpointPath = null, int scrapeResponseCacheDurationMilliseconds = 0) { - this.Options = options; + Guard.ThrowIfOutOfRange(scrapeResponseCacheDurationMilliseconds, min: 0); + + this.ScrapeEndpointPath = scrapeEndpointPath ?? "/metrics"; + this.ScrapeResponseCacheDurationMilliseconds = scrapeResponseCacheDurationMilliseconds; this.CollectionManager = new PrometheusCollectionManager(this); } @@ -63,6 +65,10 @@ internal Func, ExportResult> OnExport internal PrometheusCollectionManager CollectionManager { get; } + internal int ScrapeResponseCacheDurationMilliseconds { get; } + + internal string ScrapeEndpointPath { get; } + /// public override ExportResult Export(in Batch metrics) { diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporterEventSource.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporterEventSource.cs similarity index 97% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporterEventSource.cs rename to src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporterEventSource.cs index 6cdbf9a9024..b678cb436e2 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusExporterEventSource.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporterEventSource.cs @@ -18,7 +18,7 @@ using System.Diagnostics.Tracing; using OpenTelemetry.Internal; -namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Shared +namespace OpenTelemetry.Exporter.Prometheus { /// /// EventSource events emitted from the project. diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusSerializer.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs similarity index 100% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusSerializer.cs rename to src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusSerializerExt.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializerExt.cs similarity index 100% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/Shared/PrometheusSerializerExt.cs rename to src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializerExt.cs 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 ff00ef3db79..e9c46371703 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj @@ -3,7 +3,7 @@ netstandard2.0;net462 - Stand-alone HttpListener for hosting OpenTelemetry .NET exporter + Stand-alone HttpListener for hosting OpenTelemetry .NET Prometheus Exporter $(PackageTags);prometheus;metrics core- diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs index 6badb4d5337..efd022d79b1 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs @@ -18,10 +18,9 @@ using System.Net; using System.Threading; using System.Threading.Tasks; -using OpenTelemetry.Exporter.Prometheus.HttpListener.Shared; using OpenTelemetry.Internal; -namespace OpenTelemetry.Exporter.Prometheus.HttpListener +namespace OpenTelemetry.Exporter.Prometheus { internal sealed class PrometheusHttpListener : IDisposable { @@ -47,7 +46,7 @@ public PrometheusHttpListener(PrometheusExporter exporter, PrometheusHttpListene } this.exporter = exporter; - string path = this.exporter.Options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath; + string path = this.exporter.ScrapeEndpointPath; if (!path.StartsWith("/")) { path = $"/{path}"; diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusExporterHttpListenerMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs similarity index 52% rename from src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusExporterHttpListenerMeterProviderBuilderExtensions.cs rename to src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs index 8c53f09837d..6c0b7b75a41 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusExporterHttpListenerMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,8 +15,7 @@ // using System; -using OpenTelemetry.Exporter.Prometheus.HttpListener; -using OpenTelemetry.Exporter.Prometheus.HttpListener.Shared; +using OpenTelemetry.Exporter.Prometheus; using OpenTelemetry.Internal; namespace OpenTelemetry.Metrics @@ -24,19 +23,17 @@ namespace OpenTelemetry.Metrics /// /// Extension methods to simplify registering a PrometheusHttpListener. /// - public static class PrometheusExporterHttpListenerMeterProviderBuilderExtensions + public static class PrometheusHttpListenerMeterProviderBuilderExtensions { /// - /// Adds Prometheus exporter to MeterProviderBuilder. + /// Adds PrometheusHttpListener to MeterProviderBuilder. /// /// builder to use. - /// Exporter configuration options. - /// HttpListener options. + /// PrometheusHttpListenerOptions options. /// The instance of to chain calls. public static MeterProviderBuilder AddPrometheusHttpListener( this MeterProviderBuilder builder, - Action configureExporterOptions = null, - Action configureListenerOptions = null) + Action configure = null) { Guard.ThrowIfNull(builder); @@ -44,44 +41,30 @@ public static MeterProviderBuilder AddPrometheusHttpListener( { return deferredMeterProviderBuilder.Configure((sp, builder) => { - AddPrometheusHttpListener( - builder, - sp.GetOptions(), - sp.GetOptions(), - configureExporterOptions, - configureListenerOptions); + AddPrometheusHttpListener(builder, sp.GetOptions(), configure); }); } - return AddPrometheusHttpListener( - builder, - new PrometheusExporterOptions(), - new PrometheusHttpListenerOptions(), - configureExporterOptions, - configureListenerOptions); + return AddPrometheusHttpListener(builder, new PrometheusHttpListenerOptions(), configure); } private static MeterProviderBuilder AddPrometheusHttpListener( MeterProviderBuilder builder, - PrometheusExporterOptions exporterOptions, - PrometheusHttpListenerOptions listenerOptions, - Action configureExporterOptions = null, - Action configureListenerOptions = null) + PrometheusHttpListenerOptions options, + Action configure = null) { - configureExporterOptions?.Invoke(exporterOptions); - configureListenerOptions?.Invoke(listenerOptions); + configure?.Invoke(options); - var exporter = new PrometheusExporter(exporterOptions); + var exporter = new PrometheusExporter(scrapeEndpointPath: options.ScrapeEndpointPath); var reader = new BaseExportingMetricReader(exporter) { TemporalityPreference = MetricReaderTemporalityPreference.Cumulative, }; - const string HttpListenerStartFailureExceptionMessage = "PrometheusExporter HttpListener could not be started."; try { - var listener = new PrometheusHttpListener(exporter, listenerOptions); + var listener = new PrometheusHttpListener(exporter, options); exporter.OnDispose = () => listener.Dispose(); listener.Start(); } @@ -95,7 +78,7 @@ private static MeterProviderBuilder AddPrometheusHttpListener( { } - throw new InvalidOperationException(HttpListenerStartFailureExceptionMessage, ex); + throw new InvalidOperationException("PrometheusExporter HttpListener could not be started.", ex); } return builder.AddReader(reader); diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerOptions.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerOptions.cs index 264a5f229fd..dc1e8e5d9ea 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerOptions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerOptions.cs @@ -18,7 +18,7 @@ using System.Collections.Generic; using OpenTelemetry.Internal; -namespace OpenTelemetry.Exporter.Prometheus.HttpListener +namespace OpenTelemetry.Exporter.Prometheus { /// /// options. @@ -27,9 +27,14 @@ public class PrometheusHttpListenerOptions { private IReadOnlyCollection prefixes = new string[] { "http://localhost:9464/" }; + /// + /// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics". + /// + public string ScrapeEndpointPath { get; set; } = "/metrics"; + /// /// Gets or sets the prefixes to use for the http listener. - /// Default value: http://localhost:9464/. + /// Default value: ["http://localhost:9464/"]. /// public IReadOnlyCollection Prefixes { diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md index 8e772a4754e..b997b5adecc 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md @@ -23,8 +23,7 @@ dotnet add package OpenTelemetry.Exporter.Prometheus.HttpListener ### Step 2: Add PrometheusHttpListener -Add and configure `PrometheusHttpListener` with `PrometheusExporterOptions` as -the first argument and `PrometheusHttpListenerOptions` as the second argument. +Add and configure `PrometheusHttpListener` with `PrometheusHttpListenerOptions`. For example: @@ -32,8 +31,7 @@ For example: using var meterProvider = Sdk.CreateMeterProviderBuilder() .AddMeter(MyMeter.Name) .AddPrometheusHttpListener( - exporterOptions => exporterOptions.ScrapeResponseCacheDurationMilliseconds = 0, - listenerOptions => listenerOptions.Prefixes = new string[] { "http://localhost:9464/" }) + options => options.Prefixes = new string[] { "http://localhost:9464/" }) .Build(); ``` @@ -50,13 +48,6 @@ For details see: Defines the path for the Prometheus scrape endpoint for by `UseOpenTelemetryPrometheusScrapingEndpoint`. Default value: `"/metrics"`. -### ScrapeResponseCacheDurationMilliseconds - -Configures scrape endpoint response caching. Multiple scrape requests within the -cache duration time period will receive the same previously generated response. -The default value is `10000` (10 seconds). Set to `0` to disable response -caching. - ## Troubleshooting This component uses an diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusCollectionManagerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusCollectionManagerTests.cs index aa680d6495c..6f739cbc175 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusCollectionManagerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusCollectionManagerTests.cs @@ -21,12 +21,11 @@ #endif using System.Threading; using System.Threading.Tasks; -using OpenTelemetry.Exporter.Prometheus.HttpListener.Shared; using OpenTelemetry.Metrics; using OpenTelemetry.Tests; using Xunit; -namespace OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests +namespace OpenTelemetry.Exporter.Prometheus.Tests { public sealed class PrometheusCollectionManagerTests { @@ -110,7 +109,7 @@ public async Task EnterExitCollectTest() exporter.CollectionManager.ExitCollect(); } - Thread.Sleep(exporter.Options.ScrapeResponseCacheDurationMilliseconds); + Thread.Sleep(exporter.ScrapeResponseCacheDurationMilliseconds); counter.Add(100); diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusExporterHttpListenerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs similarity index 88% rename from test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusExporterHttpListenerTests.cs rename to test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs index d197ffea85a..bfaa2f06f56 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusExporterHttpListenerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,9 +24,9 @@ using OpenTelemetry.Tests; using Xunit; -namespace OpenTelemetry.Exporter.Prometheus.HttpListener.Tests +namespace OpenTelemetry.Exporter.Prometheus.Tests { - public class PrometheusExporterHttpListenerTests + public class PrometheusHttpListenerTests { private readonly string meterName = Utils.GetCurrentMethodName(); @@ -38,7 +38,7 @@ public class PrometheusExporterHttpListenerTests public void ServerEndpointSanityCheckPositiveTest(params string[] uris) { using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder() - .AddPrometheusHttpListener(null, listenerOptions => listenerOptions.Prefixes = uris) + .AddPrometheusHttpListener(options => options.Prefixes = uris) .Build(); } @@ -52,7 +52,7 @@ public void ServerEndpointSanityCheckNegativeTest(params string[] uris) try { using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder() - .AddPrometheusHttpListener(null, listenerOptions => listenerOptions.Prefixes = uris) + .AddPrometheusHttpListener(options => options.Prefixes = uris) .Build(); } catch (Exception ex) @@ -97,9 +97,9 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri address = $"http://localhost:{port}/"; provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddPrometheusHttpListener(null, listenerOptions => listenerOptions.Prefixes = new string[] { address }) - .Build(); + .AddMeter(meter.Name) + .AddPrometheusHttpListener(options => options.Prefixes = new string[] { address }) + .Build(); } var tags = new KeyValuePair[] diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusSerializerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusSerializerTests.cs index e6c0865cc3a..cb2ea1d5641 100644 --- a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusSerializerTests.cs +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusSerializerTests.cs @@ -21,7 +21,7 @@ using OpenTelemetry.Tests; using Xunit; -namespace OpenTelemetry.Exporter.Prometheus +namespace OpenTelemetry.Exporter.Prometheus.Tests { public sealed class PrometheusSerializerTests { diff --git a/test/OpenTelemetry.Exporter.Prometheus.Shared.Tests/OpenTelemetry.Exporter.Prometheus.Shared.Tests.csproj b/test/OpenTelemetry.Exporter.Prometheus.Shared.Tests/OpenTelemetry.Exporter.Prometheus.Shared.Tests.csproj deleted file mode 100644 index 640b3541f2e..00000000000 --- a/test/OpenTelemetry.Exporter.Prometheus.Shared.Tests/OpenTelemetry.Exporter.Prometheus.Shared.Tests.csproj +++ /dev/null @@ -1,32 +0,0 @@ - - - Unit test project of Prometheus exporter shared code for both Prometheus exporter HttpListener and Prometheus Exporter AspNetCore - - netcoreapp3.1 - $(TargetFrameworks);net462 - - false - - - - - - - - all - runtime; build; native; contentfiles; analyzers - - - - - - - - - - - - - - - diff --git a/test/OpenTelemetry.Exporter.Prometheus.Shared.Tests/PrometheusSerializerTests.cs b/test/OpenTelemetry.Exporter.Prometheus.Shared.Tests/PrometheusSerializerTests.cs deleted file mode 100644 index 1def2012b8e..00000000000 --- a/test/OpenTelemetry.Exporter.Prometheus.Shared.Tests/PrometheusSerializerTests.cs +++ /dev/null @@ -1,387 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System.Collections.Generic; -using System.Diagnostics.Metrics; -using System.Text; -using OpenTelemetry.Metrics; -using OpenTelemetry.Tests; -using Xunit; - -namespace OpenTelemetry.Exporter.Prometheus.Shared.Tests -{ - public sealed class PrometheusSerializerTests - { - [Fact] - public void GaugeZeroDimension() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - meter.CreateObservableGauge("test_gauge", () => 123); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_gauge gauge\n" - + "test_gauge 123 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void GaugeZeroDimensionWithDescription() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - meter.CreateObservableGauge("test_gauge", () => 123, description: "Hello, world!"); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# HELP test_gauge Hello, world!\n" - + "# TYPE test_gauge gauge\n" - + "test_gauge 123 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void GaugeZeroDimensionWithUnit() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - meter.CreateObservableGauge("test_gauge", () => 123, unit: "seconds"); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_gauge_seconds gauge\n" - + "test_gauge_seconds 123 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void GaugeOneDimension() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - meter.CreateObservableGauge( - "test_gauge", - () => new Measurement(123, new KeyValuePair("tagKey", "tagValue"))); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_gauge gauge\n" - + "test_gauge{tagKey='tagValue'} 123 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void GaugeDoubleSubnormal() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - meter.CreateObservableGauge("test_gauge", () => new List> - { - new(double.NegativeInfinity, new("x", "1"), new("y", "2")), - new(double.PositiveInfinity, new("x", "3"), new("y", "4")), - new(double.NaN, new("x", "5"), new("y", "6")), - }); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_gauge gauge\n" - + "test_gauge{x='1',y='2'} -Inf \\d+\n" - + "test_gauge{x='3',y='4'} \\+Inf \\d+\n" - + "test_gauge{x='5',y='6'} Nan \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void SumDoubleInfinites() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - var counter = meter.CreateCounter("test_counter"); - counter.Add(1.0E308); - counter.Add(1.0E308); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_counter counter\n" - + "test_counter \\+Inf \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void HistogramZeroDimension() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - var histogram = meter.CreateHistogram("test_histogram"); - histogram.Record(18); - histogram.Record(100); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_histogram histogram\n" - + "test_histogram_bucket{le='0'} 0 \\d+\n" - + "test_histogram_bucket{le='5'} 0 \\d+\n" - + "test_histogram_bucket{le='10'} 0 \\d+\n" - + "test_histogram_bucket{le='25'} 1 \\d+\n" - + "test_histogram_bucket{le='50'} 1 \\d+\n" - + "test_histogram_bucket{le='75'} 1 \\d+\n" - + "test_histogram_bucket{le='100'} 2 \\d+\n" - + "test_histogram_bucket{le='250'} 2 \\d+\n" - + "test_histogram_bucket{le='500'} 2 \\d+\n" - + "test_histogram_bucket{le='1000'} 2 \\d+\n" - + "test_histogram_bucket{le='\\+Inf'} 2 \\d+\n" - + "test_histogram_sum 118 \\d+\n" - + "test_histogram_count 2 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void HistogramOneDimension() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - var histogram = meter.CreateHistogram("test_histogram"); - histogram.Record(18, new KeyValuePair("x", "1")); - histogram.Record(100, new KeyValuePair("x", "1")); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_histogram histogram\n" - + "test_histogram_bucket{x='1',le='0'} 0 \\d+\n" - + "test_histogram_bucket{x='1',le='5'} 0 \\d+\n" - + "test_histogram_bucket{x='1',le='10'} 0 \\d+\n" - + "test_histogram_bucket{x='1',le='25'} 1 \\d+\n" - + "test_histogram_bucket{x='1',le='50'} 1 \\d+\n" - + "test_histogram_bucket{x='1',le='75'} 1 \\d+\n" - + "test_histogram_bucket{x='1',le='100'} 2 \\d+\n" - + "test_histogram_bucket{x='1',le='250'} 2 \\d+\n" - + "test_histogram_bucket{x='1',le='500'} 2 \\d+\n" - + "test_histogram_bucket{x='1',le='1000'} 2 \\d+\n" - + "test_histogram_bucket{x='1',le='\\+Inf'} 2 \\d+\n" - + "test_histogram_sum{x='1'} 118 \\d+\n" - + "test_histogram_count{x='1'} 2 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void HistogramTwoDimensions() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - var histogram = meter.CreateHistogram("test_histogram"); - histogram.Record(18, new("x", "1"), new("y", "2")); - histogram.Record(100, new("x", "1"), new("y", "2")); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_histogram histogram\n" - + "test_histogram_bucket{x='1',y='2',le='0'} 0 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='5'} 0 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='10'} 0 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='25'} 1 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='50'} 1 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='75'} 1 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='100'} 2 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='250'} 2 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='500'} 2 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='1000'} 2 \\d+\n" - + "test_histogram_bucket{x='1',y='2',le='\\+Inf'} 2 \\d+\n" - + "test_histogram_sum{x='1',y='2'} 118 \\d+\n" - + "test_histogram_count{x='1',y='2'} 2 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void HistogramInfinites() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - var histogram = meter.CreateHistogram("test_histogram"); - histogram.Record(18); - histogram.Record(double.PositiveInfinity); - histogram.Record(double.PositiveInfinity); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_histogram histogram\n" - + "test_histogram_bucket{le='0'} 0 \\d+\n" - + "test_histogram_bucket{le='5'} 0 \\d+\n" - + "test_histogram_bucket{le='10'} 0 \\d+\n" - + "test_histogram_bucket{le='25'} 1 \\d+\n" - + "test_histogram_bucket{le='50'} 1 \\d+\n" - + "test_histogram_bucket{le='75'} 1 \\d+\n" - + "test_histogram_bucket{le='100'} 1 \\d+\n" - + "test_histogram_bucket{le='250'} 1 \\d+\n" - + "test_histogram_bucket{le='500'} 1 \\d+\n" - + "test_histogram_bucket{le='1000'} 1 \\d+\n" - + "test_histogram_bucket{le='\\+Inf'} 3 \\d+\n" - + "test_histogram_sum \\+Inf \\d+\n" - + "test_histogram_count 3 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - - [Fact] - public void HistogramNaN() - { - var buffer = new byte[85000]; - var metrics = new List(); - - using var meter = new Meter(Utils.GetCurrentMethodName()); - using var provider = Sdk.CreateMeterProviderBuilder() - .AddMeter(meter.Name) - .AddInMemoryExporter(metrics) - .Build(); - - var histogram = meter.CreateHistogram("test_histogram"); - histogram.Record(18); - histogram.Record(double.PositiveInfinity); - histogram.Record(double.NaN); - - provider.ForceFlush(); - - var cursor = PrometheusSerializer.WriteMetric(buffer, 0, metrics[0]); - Assert.Matches( - ("^" - + "# TYPE test_histogram histogram\n" - + "test_histogram_bucket{le='0'} 0 \\d+\n" - + "test_histogram_bucket{le='5'} 0 \\d+\n" - + "test_histogram_bucket{le='10'} 0 \\d+\n" - + "test_histogram_bucket{le='25'} 1 \\d+\n" - + "test_histogram_bucket{le='50'} 1 \\d+\n" - + "test_histogram_bucket{le='75'} 1 \\d+\n" - + "test_histogram_bucket{le='100'} 1 \\d+\n" - + "test_histogram_bucket{le='250'} 1 \\d+\n" - + "test_histogram_bucket{le='500'} 1 \\d+\n" - + "test_histogram_bucket{le='1000'} 1 \\d+\n" - + "test_histogram_bucket{le='\\+Inf'} 3 \\d+\n" - + "test_histogram_sum Nan \\d+\n" - + "test_histogram_count 3 \\d+\n" - + "$").Replace('\'', '"'), - Encoding.UTF8.GetString(buffer, 0, cursor)); - } - } -} diff --git a/test/OpenTelemetry.Tests.Stress.Metrics/Program.cs b/test/OpenTelemetry.Tests.Stress.Metrics/Program.cs index 6a56c5a4bcb..91d5e64a26a 100644 --- a/test/OpenTelemetry.Tests.Stress.Metrics/Program.cs +++ b/test/OpenTelemetry.Tests.Stress.Metrics/Program.cs @@ -47,8 +47,7 @@ public static void Main() using var meterProvider = Sdk.CreateMeterProviderBuilder() .AddMeter(TestMeter.Name) .AddPrometheusHttpListener( - exporterOptions => exporterOptions.ScrapeResponseCacheDurationMilliseconds = 0, - listenerOptions => listenerOptions.Prefixes = new string[] { $"http://localhost:9185/" }) + options => options.Prefixes = new string[] { $"http://localhost:9185/" }) .Build(); Stress(prometheusPort: 9184); diff --git a/test/OpenTelemetry.Tests.Stress/Skeleton.cs b/test/OpenTelemetry.Tests.Stress/Skeleton.cs index e372b108710..967bcd53a49 100644 --- a/test/OpenTelemetry.Tests.Stress/Skeleton.cs +++ b/test/OpenTelemetry.Tests.Stress/Skeleton.cs @@ -76,8 +76,7 @@ public static void Stress(int concurrency = 0, int prometheusPort = 0) .AddMeter(meter.Name) .AddRuntimeInstrumentation() .AddPrometheusHttpListener( - exporterOptions => exporterOptions.ScrapeResponseCacheDurationMilliseconds = 0, - listenerOptions => listenerOptions.Prefixes = new string[] { $"http://localhost:{prometheusPort}/" }) + options => options.Prefixes = new string[] { $"http://localhost:{prometheusPort}/" }) .Build() : null; var statistics = new long[concurrency];