Skip to content

Commit

Permalink
Make static and remove redundant set
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart committed Dec 6, 2023
1 parent 5ec4852 commit 8dd0cbf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace OpenTelemetry.Exporter;
internal sealed class PrometheusExporterMiddleware
{
private readonly PrometheusExporter exporter;
private readonly PrometheusHeadersParser headersParser = new();

/// <summary>
/// Initializes a new instance of the <see cref="PrometheusExporterMiddleware"/> class.
Expand Down Expand Up @@ -66,7 +65,7 @@ public async Task InvokeAsync(HttpContext httpContext)

try
{
var openMetricsRequested = this.AcceptsOpenMetrics(httpContext.Request);
var openMetricsRequested = AcceptsOpenMetrics(httpContext.Request);
var collectionResponse = await this.exporter.CollectionManager.EnterCollect(openMetricsRequested).ConfigureAwait(false);

try
Expand Down Expand Up @@ -109,7 +108,7 @@ public async Task InvokeAsync(HttpContext httpContext)
this.exporter.OnExport = null;
}

private bool AcceptsOpenMetrics(HttpRequest request)
private static bool AcceptsOpenMetrics(HttpRequest request)
{
var acceptHeader = request.Headers.Accept;

Expand All @@ -118,6 +117,6 @@ private bool AcceptsOpenMetrics(HttpRequest request)
return false;
}

return acceptHeader.Any(x => this.headersParser.AcceptsOpenMetrics(x));
return acceptHeader.Any(PrometheusHeadersParser.AcceptsOpenMetrics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ private bool ExecuteCollect(bool openMetricsRequested)
this.exporter.OpenMetricsRequested = openMetricsRequested;
var result = this.exporter.Collect(Timeout.Infinite);
this.exporter.OnExport = null;
this.exporter.OpenMetricsRequested = false;
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

namespace OpenTelemetry.Exporter.Prometheus;

internal class PrometheusHeadersParser
internal static class PrometheusHeadersParser
{
private const string OpenMetricsMediaType = "application/openmetrics-text";

internal bool AcceptsOpenMetrics(string contentType)
internal static bool AcceptsOpenMetrics(string contentType)
{
var value = contentType.AsSpan();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace OpenTelemetry.Exporter;
internal sealed class PrometheusHttpListener : IDisposable
{
private readonly PrometheusExporter exporter;
private readonly PrometheusHeadersParser headersParser = new();
private readonly HttpListener httpListener = new();
private readonly object syncObject = new();

Expand Down Expand Up @@ -111,6 +110,18 @@ public void Dispose()
}
}

private static bool AcceptsOpenMetrics(HttpListenerRequest request)
{
var acceptHeader = request.Headers["Accept"];

if (string.IsNullOrEmpty(acceptHeader))
{
return false;
}

return PrometheusHeadersParser.AcceptsOpenMetrics(acceptHeader);
}

private void WorkerProc()
{
this.httpListener.Start();
Expand Down Expand Up @@ -149,7 +160,7 @@ private async Task ProcessRequestAsync(HttpListenerContext context)
{
try
{
var openMetricsRequested = this.AcceptsOpenMetrics(context.Request);
var openMetricsRequested = AcceptsOpenMetrics(context.Request);
var collectionResponse = await this.exporter.CollectionManager.EnterCollect(openMetricsRequested).ConfigureAwait(false);

try
Expand Down Expand Up @@ -192,16 +203,4 @@ private async Task ProcessRequestAsync(HttpListenerContext context)
{
}
}

private bool AcceptsOpenMetrics(HttpListenerRequest request)
{
var acceptHeader = request.Headers["Accept"];

if (string.IsNullOrEmpty(acceptHeader))
{
return false;
}

return this.headersParser.AcceptsOpenMetrics(acceptHeader);
}
}

0 comments on commit 8dd0cbf

Please sign in to comment.