Skip to content

Commit

Permalink
Fix merge
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart committed Dec 7, 2023
1 parent 4e5e577 commit b5c80db
Show file tree
Hide file tree
Showing 12 changed files with 12 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions
Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.PrometheusAspNetCoreOptions() -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.OpenMetricsEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.OpenMetricsEnabled.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.get -> string
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeEndpointPath.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScrapeResponseCacheDurationMilliseconds.get -> int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,5 @@ public int ScrapeResponseCacheDurationMilliseconds
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
}

/// <summary>
/// Gets or sets a value indicating whether to export OpenMetrics compatible scrape responses. Default value: true.
/// </summary>
public bool OpenMetricsEnabled
{
get => this.ExporterOptions.OpenMetricsEnabled;
set => this.ExporterOptions.OpenMetricsEnabled = value;
}

internal PrometheusExporterOptions ExporterOptions { get; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ namespace OpenTelemetry.Exporter;
/// </summary>
internal sealed class PrometheusExporterMiddleware
{
private const string OpenMetricsMediaType = "application/openmetrics-text";

private readonly PrometheusExporter exporter;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
OpenTelemetry.Exporter.PrometheusHttpListenerOptions
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.OpenMetricsEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.OpenMetricsEnabled.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 @@ -176,7 +176,6 @@ private bool ExecuteCollect(bool openMetricsRequested)
this.exporter.OpenMetricsRequested = openMetricsRequested;
var result = this.exporter.Collect(Timeout.Infinite);
this.exporter.OnExport = null;
this.exporter.OpenMetricsRequested = null;
return result;
}

Expand All @@ -186,7 +185,7 @@ private ExportResult OnCollect(Batch<Metric> metrics)

try
{
if (this.exporter.OpenMetricsEnabled)
if (this.exporter.OpenMetricsRequested)
{
this.scopes.Clear();

Expand All @@ -202,12 +201,7 @@ private ExportResult OnCollect(Batch<Metric> metrics)
{
try
{
cursor = PrometheusSerializer.WriteMetric(
this.buffer,
cursor,
metric,
this.GetPrometheusMetric(metric),
this.exporter.OpenMetricsRequested);
cursor = PrometheusSerializer.WriteScopeInfo(this.buffer, cursor, scope);

break;
}
Expand Down Expand Up @@ -243,8 +237,7 @@ private ExportResult OnCollect(Batch<Metric> metrics)
cursor,
metric,
this.GetPrometheusMetric(metric),
this.exporter.OpenMetricsEnabled,
this.exporter.OpenMetricsRequested ?? false);
this.exporter.OpenMetricsRequested);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public PrometheusExporter(PrometheusExporterOptions options)
Guard.ThrowIfNull(options);

this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;
this.OpenMetricsEnabled = options.OpenMetricsEnabled;

this.CollectionManager = new PrometheusCollectionManager(this);
}
Expand All @@ -58,8 +57,6 @@ 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 @@ -41,9 +41,4 @@ public int ScrapeResponseCacheDurationMilliseconds
this.scrapeResponseCacheDurationMilliseconds = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether to export OpenMetrics compatible scrape responses. Default value: true.
/// </summary>
public bool OpenMetricsEnabled { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,16 @@ public static int WriteTimestamp(byte[] buffer, int cursor, long value, bool use
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteTags(byte[] buffer, int cursor, Metric metric, ReadOnlyTagCollection tags, bool openMetricsEnabled, bool writeEnclosingBraces = true)
public static int WriteTags(byte[] buffer, int cursor, Metric metric, ReadOnlyTagCollection tags, bool openMetricsRequested, bool writeEnclosingBraces = true)
{
if (tags.Count > 0 || openMetricsEnabled)
if (tags.Count > 0 || openMetricsRequested)
{
if (writeEnclosingBraces)
{
buffer[cursor++] = unchecked((byte)'{');
}

if (openMetricsEnabled)
if (openMetricsRequested)
{
cursor = WriteLabel(buffer, cursor, "otel_scope_name", metric.MeterName);
buffer[cursor++] = unchecked((byte)',');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe

// Counter and Gauge
cursor = WriteMetricName(buffer, cursor, prometheusMetric);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, openMetricsEnabled);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, openMetricsRequested);

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

Expand Down Expand Up @@ -101,7 +101,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe

cursor = WriteMetricName(buffer, cursor, prometheusMetric);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_bucket{");
cursor = WriteTags(buffer, cursor, metric, tags, openMetricsEnabled, writeEnclosingBraces: false);
cursor = WriteTags(buffer, cursor, metric, tags, openMetricsRequested, writeEnclosingBraces: false);

cursor = WriteAsciiStringNoEscape(buffer, cursor, "le=\"");

Expand All @@ -127,7 +127,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
// Histogram sum
cursor = WriteMetricName(buffer, cursor, prometheusMetric);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_sum");
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, openMetricsEnabled);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, openMetricsRequested);

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

Expand All @@ -141,7 +141,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
// Histogram count
cursor = WriteMetricName(buffer, cursor, prometheusMetric);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_count");
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, openMetricsEnabled);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags, openMetricsRequested);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
// </copyright>

using System.Collections.Specialized;
using System.Net;
using OpenTelemetry.Exporter.Prometheus;
using OpenTelemetry.Internal;
Expand All @@ -23,8 +22,6 @@ namespace OpenTelemetry.Exporter;

internal sealed class PrometheusHttpListener : IDisposable
{
private const string OpenMetricsMediaType = "application/openmetrics-text";

private readonly PrometheusExporter exporter;
private readonly HttpListener httpListener = new();
private readonly object syncObject = new();
Expand Down Expand Up @@ -206,28 +203,4 @@ private async Task ProcessRequestAsync(HttpListenerContext context)
{
}
}

private bool AcceptsOpenMetrics(NameValueCollection headers)
{
var requestAccept = headers["Accept"];

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

var acceptTypes = requestAccept.Split(',');

foreach (var acceptType in acceptTypes)
{
var acceptSubType = acceptType.Split(';').FirstOrDefault()?.Trim();

if (acceptSubType == OpenMetricsMediaType)
{
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ public static MeterProviderBuilder AddPrometheusHttpListener(this MeterProviderB
private static MetricReader BuildPrometheusHttpListenerMetricReader(
PrometheusHttpListenerOptions options)
{
var exporter = new PrometheusExporter(new PrometheusExporterOptions
{
ScrapeResponseCacheDurationMilliseconds = 0,
OpenMetricsEnabled = options.OpenMetricsEnabled,
});
var exporter = new PrometheusExporter(new PrometheusExporterOptions { ScrapeResponseCacheDurationMilliseconds = 0 });

var reader = new BaseExportingMetricReader(exporter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ public void ScopeInfo()
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void SumWithOpenMetricsFormat()
{
var buffer = new byte[85000];
Expand Down

0 comments on commit b5c80db

Please sign in to comment.