Skip to content

Commit

Permalink
[Exporter.PrometherusHttpListener] Restore ScrapeResponseCacheDuratio…
Browse files Browse the repository at this point in the history
…nMilliseconds
  • Loading branch information
Kielek committed Sep 23, 2022
1 parent ed0450e commit 2fcd36f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
OpenTelemetry.Exporter.PrometheusHttpListenerOptions
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeResponseCacheDurationMilliseconds.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.get -> System.Collections.Generic.IReadOnlyCollection<string>
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ OpenTelemetry.Exporter.PrometheusHttpListenerOptions
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.PrometheusHttpListenerOptions() -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.get -> string
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeResponseCacheDurationMilliseconds.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.get -> System.Collections.Generic.IReadOnlyCollection<string>
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.set -> void
OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Bug fix for Prometheus Exporter reporting StatusCode 204
instead of 200, when no metrics are collected
([#3643](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3643))
* Bug fix, restore endpoint response caching feature &
`ScrapeResponseCacheDurationMilliseconds` option
([#3694](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3694))
* Added overloads which accept a name to the `MeterProviderBuilder`
`AddPrometheusHttpListener` extension to allow for more fine-grained options
management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -35,16 +36,18 @@ internal sealed class PrometheusHttpListener : IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="PrometheusHttpListener"/> class.
/// </summary>
/// <param name="exporter"><see cref="PrometheusExporter"/>The exporter instance.</param>
/// <param name="options"><see cref="PrometheusHttpListenerOptions"/>The configured HttpListener options.</param>
public PrometheusHttpListener(PrometheusExporter exporter, PrometheusHttpListenerOptions options)
/// <param name="exporter"><see cref="PrometheusExporter"/>The path to use for the scraping endpoint.</param>
/// <param name="scrapeEndpointPath">The configured HttpListener options.</param>
/// <param name="uriPrefixes">The URI (Uniform Resource Identifier) prefixes to use for the http listener.</param>
public PrometheusHttpListener(PrometheusExporter exporter, string scrapeEndpointPath, IEnumerable<string> uriPrefixes)
{
Guard.ThrowIfNull(exporter);
Guard.ThrowIfNull(options);
Guard.ThrowIfNullOrWhitespace(scrapeEndpointPath);
Guard.ThrowIfNull(uriPrefixes);

this.exporter = exporter;

string path = options.ScrapeEndpointPath;
string path = scrapeEndpointPath;

if (!path.StartsWith("/"))
{
Expand All @@ -56,7 +59,7 @@ public PrometheusHttpListener(PrometheusExporter exporter, PrometheusHttpListene
path = $"{path}/";
}

foreach (string uriPrefix in options.UriPrefixes)
foreach (string uriPrefix in uriPrefixes)
{
this.httpListener.Prefixes.Add($"{uriPrefix.TrimEnd('/')}{path}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static MeterProviderBuilder AddPrometheusHttpListener(
MeterProviderBuilder builder,
PrometheusHttpListenerOptions options)
{
var exporter = new PrometheusExporter();
var exporter = new PrometheusExporter(options.ScrapeResponseCacheDurationMilliseconds);

var reader = new BaseExportingMetricReader(exporter)
{
Expand All @@ -89,7 +89,7 @@ private static MeterProviderBuilder AddPrometheusHttpListener(

try
{
var listener = new PrometheusHttpListener(exporter, options);
var listener = new PrometheusHttpListener(exporter, options.ScrapeEndpointPath, options.UriPrefixes);
exporter.OnDispose = () => listener.Dispose();
listener.Start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,31 @@ namespace OpenTelemetry.Exporter
/// </summary>
public class PrometheusHttpListenerOptions
{
private IReadOnlyCollection<string> uriPrefixes = new string[] { "http://localhost:9464/" };
private int scrapeResponseCacheDurationMilliseconds = 300;
private IReadOnlyCollection<string> uriPrefixes = new[] { "http://localhost:9464/" };

/// <summary>
/// Gets or sets the path to use for the scraping endpoint. Default value: "/metrics".
/// </summary>
public string ScrapeEndpointPath { get; set; } = "/metrics";

/// <summary>
/// Gets or sets the cache duration in milliseconds for scrape responses. Default value: 300.
/// </summary>
/// <remarks>
/// Note: Specify 0 to disable response caching.
/// </remarks>
public int ScrapeResponseCacheDurationMilliseconds
{
get => this.scrapeResponseCacheDurationMilliseconds;
set
{
Guard.ThrowIfOutOfRange(value, min: 0);

this.scrapeResponseCacheDurationMilliseconds = value;
}
}

/// <summary>
/// Gets or sets the URI (Uniform Resource Identifier) prefixes to use for the http listener.
/// Default value: <c>["http://localhost:9464/"]</c>.
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ for more details.

Defines the Prometheus scrape endpoint path. 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 `300`. Set to `0` to disable response caching.

## Troubleshooting

This component uses an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ namespace OpenTelemetry.Exporter.Prometheus.Tests
{
public sealed class PrometheusCollectionManagerTests
{
#if PROMETHEUS_HTTP_LISTENER
[Fact(Skip = "Might be flaky. Might be a bug. See: https://github.com/open-telemetry/opentelemetry-dotnet/issues/3679")]
#else
[Fact]
#endif
public async Task EnterExitCollectTest()
{
using var meter = new Meter(Utils.GetCurrentMethodName());
Expand Down

0 comments on commit 2fcd36f

Please sign in to comment.