Skip to content

Commit

Permalink
Scope info is not optional
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart committed Dec 10, 2023
1 parent 63f0b36 commit 033a903
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 169 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.ScopeInfoEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.ScopeInfoEnabled.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 scope info. Default value: true.
/// </summary>
public bool ScopeInfoEnabled
{
get => this.ExporterOptions.ScopeInfoEnabled;
set => this.ExporterOptions.ScopeInfoEnabled = value;
}

internal PrometheusExporterOptions ExporterOptions { get; } = new();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
OpenTelemetry.Exporter.PrometheusHttpListenerOptions
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScopeInfoEnabled.get -> bool
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScopeInfoEnabled.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 @@ -185,7 +185,7 @@ private ExportResult OnCollect(Batch<Metric> metrics)

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

Expand Down Expand Up @@ -237,8 +237,7 @@ private ExportResult OnCollect(Batch<Metric> metrics)
cursor,
metric,
this.GetPrometheusMetric(metric),
this.exporter.OpenMetricsRequested,
this.exporter.ScopeInfoEnabled);
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.ScopeInfoEnabled = options.ScopeInfoEnabled;

this.CollectionManager = new PrometheusCollectionManager(this);
}
Expand Down Expand Up @@ -66,8 +65,6 @@ public PrometheusExporter(PrometheusExporterOptions options)

internal bool OpenMetricsRequested { get; set; }

internal bool ScopeInfoEnabled { get; set; }

/// <inheritdoc/>
public override ExportResult Export(in Batch<Metric> metrics)
{
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 scope info. Default value: true.
/// </summary>
public bool ScopeInfoEnabled { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,37 +379,32 @@ 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 scopeInfoEnabled, bool writeEnclosingBraces = true)
public static int WriteTags(byte[] buffer, int cursor, Metric metric, ReadOnlyTagCollection tags, bool writeEnclosingBraces = true)
{
if (tags.Count > 0 || scopeInfoEnabled)
if (writeEnclosingBraces)
{
if (writeEnclosingBraces)
{
buffer[cursor++] = unchecked((byte)'{');
}
buffer[cursor++] = unchecked((byte)'{');
}

if (scopeInfoEnabled)
{
cursor = WriteLabel(buffer, cursor, "otel_scope_name", metric.MeterName);
buffer[cursor++] = unchecked((byte)',');

if (!string.IsNullOrEmpty(metric.MeterVersion))
{
cursor = WriteLabel(buffer, cursor, "otel_scope_version", metric.MeterVersion);
buffer[cursor++] = unchecked((byte)',');
}
}
cursor = WriteLabel(buffer, cursor, "otel_scope_name", metric.MeterName);
buffer[cursor++] = unchecked((byte)',');

foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
buffer[cursor++] = unchecked((byte)',');
}
if (!string.IsNullOrEmpty(metric.MeterVersion))
{
cursor = WriteLabel(buffer, cursor, "otel_scope_version", metric.MeterVersion);
buffer[cursor++] = unchecked((byte)',');
}

if (writeEnclosingBraces)
{
buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra.
}
foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
buffer[cursor++] = unchecked((byte)',');
}

if (writeEnclosingBraces)
{
buffer[cursor - 1] =
unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra.
}

return cursor;
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 openMetricsRequested = false, bool scopeInfoEnabled = true)
public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, bool openMetricsRequested = false)
{
cursor = WriteTypeMetadata(buffer, cursor, prometheusMetric);
cursor = WriteUnitMetadata(buffer, cursor, prometheusMetric);
Expand All @@ -49,7 +49,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, scopeInfoEnabled);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags);

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

Expand Down Expand Up @@ -100,7 +100,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, scopeInfoEnabled, writeEnclosingBraces: false);
cursor = WriteTags(buffer, cursor, metric, tags, writeEnclosingBraces: false);

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

Expand All @@ -126,7 +126,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, scopeInfoEnabled);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags);

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

Expand All @@ -140,7 +140,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, scopeInfoEnabled);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags);

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

Expand Down
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,
ScopeInfoEnabled = options.ScopeInfoEnabled,
});
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 @@ -30,11 +30,6 @@ public class PrometheusHttpListenerOptions
/// </summary>
public string ScrapeEndpointPath { get; set; } = "/metrics";

/// <summary>
/// Gets or sets a value indicating whether to export scope info. Default value: true.
/// </summary>
public bool ScopeInfoEnabled { get; set; } = true;

/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,6 @@ public Task PrometheusExporterMiddlewareIntegration_TextPlainResponse()
acceptHeader: "text/plain");
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_TextPlainResponse_NoScopeInfo()
{
return RunPrometheusExporterMiddlewareIntegrationTest(
"/metrics",
app => app.UseOpenTelemetryPrometheusScrapingEndpoint(),
acceptHeader: "text/plain",
scopeInfoEnabled: false);
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_UseOpenMetricsVersionHeader()
{
Expand All @@ -268,15 +258,6 @@ public Task PrometheusExporterMiddlewareIntegration_UseOpenMetricsVersionHeader(
acceptHeader: "application/openmetrics-text; version=1.0.0");
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_NoScopeInfo()
{
return RunPrometheusExporterMiddlewareIntegrationTest(
"/metrics",
app => app.UseOpenTelemetryPrometheusScrapingEndpoint(),
scopeInfoEnabled: false);
}

private static async Task RunPrometheusExporterMiddlewareIntegrationTest(
string path,
Action<IApplicationBuilder> configure,
Expand All @@ -285,8 +266,7 @@ public Task PrometheusExporterMiddlewareIntegration_NoScopeInfo()
bool registerMeterProvider = true,
Action<PrometheusAspNetCoreOptions> configureOptions = null,
bool skipMetrics = false,
string acceptHeader = "application/openmetrics-text",
bool scopeInfoEnabled = true)
string acceptHeader = "application/openmetrics-text")
{
var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text");

Expand All @@ -301,7 +281,6 @@ public Task PrometheusExporterMiddlewareIntegration_NoScopeInfo()
.AddMeter(MeterName)
.AddPrometheusExporter(o =>
{
o.ScopeInfoEnabled = scopeInfoEnabled;
configureOptions?.Invoke(o);
}));
}
Expand Down Expand Up @@ -354,7 +333,17 @@ public Task PrometheusExporterMiddlewareIntegration_NoScopeInfo()
}

string content = await response.Content.ReadAsStringAsync();
string expected = GetExpectedContent(requestOpenMetrics, scopeInfoEnabled);

string expected = requestOpenMetrics
? "# TYPE otel_scope_info info\n"
+ "# HELP otel_scope_info Scope metadata\n"
+ $"otel_scope_info{{otel_scope_name='{MeterName}'}} 1\n"
+ "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\n"
+ "# EOF\n"
: "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\n"
+ "# EOF\n";

var matches = Regex.Matches(content, ("^" + expected + "$").Replace('\'', '"'));

Expand All @@ -373,36 +362,5 @@ public Task PrometheusExporterMiddlewareIntegration_NoScopeInfo()

await host.StopAsync();
}

private static string GetExpectedContent(bool requestedOpenMetrics, bool scopeInfoEnabled)
{
if (requestedOpenMetrics && scopeInfoEnabled)
{
return "# TYPE otel_scope_info info\n"
+ "# HELP otel_scope_info Scope metadata\n"
+ $"otel_scope_info{{otel_scope_name='{MeterName}'}} 1\n"
+ "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\n"
+ "# EOF\n";
}

if (!requestedOpenMetrics && scopeInfoEnabled)
{
return "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\n"
+ "# EOF\n";
}

if (requestedOpenMetrics && !scopeInfoEnabled)
{
return "# TYPE counter_double_total counter\n"
+ "counter_double_total{key1='value1',key2='value2'} 101.17 (\\d+\\.\\d{3})\n"
+ "# EOF\n";
}

return "# TYPE counter_double_total counter\n"
+ "counter_double_total{key1='value1',key2='value2'} 101.17 (\\d+)\n"
+ "# EOF\n";
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -98,56 +98,13 @@ public async Task PrometheusExporterHttpServerIntegration_NoOpenMetrics()
await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: string.Empty);
}

[Fact]
public async Task PrometheusExporterHttpServerIntegration_NoOpenMetrics_NoScopeInfo()
{
await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: string.Empty, scopeInfoEnabled: false);
}

[Fact]
public async Task PrometheusExporterHttpServerIntegration_UseOpenMetricsVersionHeader()
{
await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: "application/openmetrics-text; version=1.0.0");
}

[Fact]
public async Task PrometheusExporterHttpServerIntegration_NoScopeInfo()
{
await this.RunPrometheusExporterHttpServerIntegrationTest(scopeInfoEnabled: false);
}

private static string GetExpectedContent(bool requestedOpenMetrics, bool scopeInfoEnabled)
{
if (requestedOpenMetrics && scopeInfoEnabled)
{
return "# TYPE otel_scope_info info\n"
+ "# HELP otel_scope_info Scope metadata\n"
+ $"otel_scope_info{{otel_scope_name='{MeterName}'}} 1\n"
+ "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\n"
+ "# EOF\n";
}

if (!requestedOpenMetrics && scopeInfoEnabled)
{
return "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\n"
+ "# EOF\n";
}

if (requestedOpenMetrics && !scopeInfoEnabled)
{
return "# TYPE counter_double_total counter\n"
+ "counter_double_total{key1='value1',key2='value2'} 101.17 (\\d+\\.\\d{3})\n"
+ "# EOF\n";
}

return "# TYPE counter_double_total counter\n"
+ "counter_double_total{key1='value1',key2='value2'} 101.17 (\\d+)\n"
+ "# EOF\n";
}

private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetrics = false, string acceptHeader = "application/openmetrics-text", bool scopeInfoEnabled = true)
private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetrics = false, string acceptHeader = "application/openmetrics-text")
{
var requestOpenMetrics = acceptHeader.StartsWith("application/openmetrics-text");

Expand All @@ -170,7 +127,6 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri
.AddMeter(meter.Name)
.AddPrometheusHttpListener(options =>
{
options.ScopeInfoEnabled = scopeInfoEnabled;
options.UriPrefixes = new string[] { address };
})
.Build();
Expand Down Expand Up @@ -225,7 +181,17 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(bool skipMetri
}

var content = await response.Content.ReadAsStringAsync();
var expected = GetExpectedContent(requestOpenMetrics, scopeInfoEnabled);

var expected = requestOpenMetrics
? "# TYPE otel_scope_info info\n"
+ "# HELP otel_scope_info Scope metadata\n"
+ $"otel_scope_info{{otel_scope_name='{MeterName}'}} 1\n"
+ "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\n"
+ "# EOF\n"
: "# TYPE counter_double_total counter\n"
+ $"counter_double_total{{otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\n"
+ "# EOF\n";

Assert.Matches(("^" + expected + "$").Replace('\'', '"'), content);
}
Expand Down
Loading

0 comments on commit 033a903

Please sign in to comment.