Skip to content

Commit

Permalink
Modify output depending on accept headers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart committed Nov 30, 2023
1 parent da28e9d commit 937b7da
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public async Task InvokeAsync(HttpContext httpContext)

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

var collectionResponse = await this.exporter.CollectionManager.EnterCollect(openMetricsRequested).ConfigureAwait(false);

try
{
if (collectionResponse.View.Count > 0)
Expand All @@ -79,7 +83,8 @@ public async Task InvokeAsync(HttpContext httpContext)
#else
response.Headers.Add("Last-Modified", collectionResponse.GeneratedAtUtc.ToString("R"));
#endif
if (this.exporter.OpenMetricsEnabled && this.AcceptsOpenMetrics(httpContext.Request))

if (openMetricsRequested)
{
response.ContentType = "application/openmetrics-text; version=1.0.0; charset=utf-8";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public PrometheusCollectionManager(PrometheusExporter exporter)
}

#if NET6_0_OR_GREATER
public ValueTask<CollectionResponse> EnterCollect()
public ValueTask<CollectionResponse> EnterCollect(bool openMetricsRequested)
#else
public Task<CollectionResponse> EnterCollect()
public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)
#endif
{
this.EnterGlobalLock();
Expand Down Expand Up @@ -95,7 +95,7 @@ public Task<CollectionResponse> EnterCollect()
this.ExitGlobalLock();

CollectionResponse response;
var result = this.ExecuteCollect();
var result = this.ExecuteCollect(openMetricsRequested);
if (result)
{
this.previousDataViewGeneratedAtUtc = DateTime.UtcNow;
Expand Down Expand Up @@ -170,11 +170,13 @@ private void WaitForReadersToComplete()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool ExecuteCollect()
private bool ExecuteCollect(bool openMetricsRequested)
{
this.exporter.OnExport = this.onCollectRef;
this.exporter.OpenMetricsRequested = openMetricsRequested;
var result = this.exporter.Collect(Timeout.Infinite);
this.exporter.OnExport = null;
this.exporter.OpenMetricsRequested = null;
return result;
}

Expand Down Expand Up @@ -229,7 +231,14 @@ private ExportResult OnCollect(Batch<Metric> metrics)
{
try
{
cursor = PrometheusSerializer.WriteMetric(this.buffer, cursor, metric, this.GetPrometheusMetric(metric), this.exporter.OpenMetricsEnabled);
cursor = PrometheusSerializer.WriteMetric(
this.buffer,
cursor,
metric,
this.GetPrometheusMetric(metric),
this.exporter.OpenMetricsEnabled,
this.exporter.OpenMetricsRequested ?? false);

break;
}
catch (IndexOutOfRangeException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public PrometheusExporter(PrometheusExporterOptions options)
set => this.funcExport = value;
}

internal bool? OpenMetricsRequested { get; set; }

internal Action OnDispose { get; set; }

internal PrometheusCollectionManager CollectionManager { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ public static int WriteScopeInfo(byte[] buffer, int cursor, string scopeName)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteTimestamp(byte[] buffer, int cursor, long value, bool openMetricsEnabled)
public static int WriteTimestamp(byte[] buffer, int cursor, long value, bool useOpenMetrics)
{
if (openMetricsEnabled)
if (useOpenMetrics)
{
cursor = WriteLong(buffer, cursor, value / 1000);
buffer[cursor++] = unchecked((byte)'.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static bool CanWriteMetric(Metric metric)
return true;
}

public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, bool openMetricsEnabled = true)
public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, bool openMetricsEnabled = true, bool openMetricsRequested = false)
{
cursor = WriteTypeMetadata(buffer, cursor, prometheusMetric);
cursor = WriteUnitMetadata(buffer, cursor, prometheusMetric);
Expand Down Expand Up @@ -81,7 +81,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe

buffer[cursor++] = unchecked((byte)' ');

cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsEnabled);
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);

buffer[cursor++] = ASCII_LINEFEED;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
cursor = WriteLong(buffer, cursor, totalCount);
buffer[cursor++] = unchecked((byte)' ');

cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsEnabled);
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);

buffer[cursor++] = ASCII_LINEFEED;
}
Expand All @@ -133,7 +133,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
cursor = WriteDouble(buffer, cursor, metricPoint.GetHistogramSum());
buffer[cursor++] = unchecked((byte)' ');

cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsEnabled);
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);

buffer[cursor++] = ASCII_LINEFEED;

Expand All @@ -147,7 +147,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
cursor = WriteLong(buffer, cursor, metricPoint.GetHistogramCount());
buffer[cursor++] = unchecked((byte)' ');

cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsEnabled);
cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested);

buffer[cursor++] = ASCII_LINEFEED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ private async Task ProcessRequestAsync(HttpListenerContext context)
{
try
{
var collectionResponse = await this.exporter.CollectionManager.EnterCollect().ConfigureAwait(false);
var openMetricsRequested = this.exporter.OpenMetricsEnabled && this.AcceptsOpenMetrics(context.Request.Headers);

var collectionResponse = await this.exporter.CollectionManager.EnterCollect(openMetricsRequested).ConfigureAwait(false);

try
{
context.Response.Headers.Add("Server", string.Empty);
Expand All @@ -160,7 +163,7 @@ private async Task ProcessRequestAsync(HttpListenerContext context)
context.Response.StatusCode = 200;
context.Response.Headers.Add("Last-Modified", collectionResponse.GeneratedAtUtc.ToString("R"));

if (this.AcceptsOpenMetrics(context.Request.Headers))
if (openMetricsRequested)
{
context.Response.ContentType = "application/openmetrics-text; version=1.0.0; charset=utf-8";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon
{
collectTasks[i] = Task.Run(async () =>
{
var response = await exporter.CollectionManager.EnterCollect();
var response = await exporter.CollectionManager.EnterCollect(false);
try
{
return new Response
Expand Down Expand Up @@ -98,7 +98,7 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon
counter.Add(100);

// This should use the cache and ignore the second counter update.
var task = exporter.CollectionManager.EnterCollect();
var task = exporter.CollectionManager.EnterCollect(false);
Assert.True(task.IsCompleted);
var response = await task;
try
Expand Down Expand Up @@ -129,7 +129,7 @@ public async Task EnterExitCollectTest(int scrapeResponseCacheDurationMillisecon
{
collectTasks[i] = Task.Run(async () =>
{
var response = await exporter.CollectionManager.EnterCollect();
var response = await exporter.CollectionManager.EnterCollect(false);
try
{
return new Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,6 @@ public void HistogramOneDimensionWithScopeInfo()

private static int WriteMetric(byte[] buffer, int cursor, Metric metric, bool useOpenMetrics = false)
{
return PrometheusSerializer.WriteMetric(buffer, cursor, metric, PrometheusMetric.Create(metric), useOpenMetrics);
return PrometheusSerializer.WriteMetric(buffer, cursor, metric, PrometheusMetric.Create(metric), useOpenMetrics, useOpenMetrics);
}
}

0 comments on commit 937b7da

Please sign in to comment.