Skip to content

Commit

Permalink
Revert resource attribute labels
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart committed Mar 6, 2024
1 parent 0a0ef68 commit 5ddf850
Show file tree
Hide file tree
Showing 17 changed files with 16 additions and 149 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensions
Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.AllowedResourceAttributesFilter.get -> System.Predicate<string>
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.AllowedResourceAttributesFilter.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTotalNameSuffixForCounters.get -> bool
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.DisableTotalNameSuffixForCounters.set -> void
OpenTelemetry.Exporter.PrometheusAspNetCoreOptions.PrometheusAspNetCoreOptions() -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

* Added `target_info` and resource attributes to Prometheus exporters ([#3087](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3087))
* Added `target_info` to Prometheus exporters ([#3087](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5407))

* Added option to disable _total suffix addition to counter metrics
([#5305](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5305))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporter.cs" Link="Includes/PrometheusExporter.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporterEventSource.cs" Link="Includes/PrometheusExporterEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusExporterOptions.cs" Link="Includes/PrometheusExporterOptions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusResourceTagCollection.cs" Link="Includes/PrometheusResourceTagCollection.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializer.cs" Link="Includes/PrometheusSerializer.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializerExt.cs" Link="Includes/PrometheusSerializerExt.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusType.cs" Link="Includes/PrometheusType.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,5 @@ public int ScrapeResponseCacheDurationMilliseconds
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
}

/// <summary>
/// Gets or sets the allowed resource attributes filter. Default value: null (no attributes allowed).
/// </summary>
public Predicate<string> AllowedResourceAttributesFilter
{
get => this.ExporterOptions.AllowedResourceAttributesFilter;
set => this.ExporterOptions.AllowedResourceAttributesFilter = 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.AllowedResourceAttributesFilter.get -> System.Predicate<string>
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.AllowedResourceAttributesFilter.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCounters.get -> bool
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.DisableTotalNameSuffixForCounters.set -> void
OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.get -> System.Collections.Generic.IReadOnlyCollection<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

* Added `target_info` and resource attributes to Prometheus exporters ([#3087](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3087))
* Added `target_info` to Prometheus exporters ([#3087](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5407))

* Added option to disable _total suffix addition to counter metrics
([#5305](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5305))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ private ExportResult OnCollect(Batch<Metric> metrics)

try
{
var resourceTags = new PrometheusResourceTagCollection(this.exporter.Resource, this.exporter.AllowedResourceAttributesFilter);

if (this.exporter.OpenMetricsRequested)
{
cursor = PrometheusSerializer.WriteTargetInfo(this.buffer, cursor, this.exporter.Resource);
Expand Down Expand Up @@ -226,7 +224,6 @@ private ExportResult OnCollect(Batch<Metric> metrics)
cursor,
metric,
this.GetPrometheusMetric(metric),
resourceTags,
this.exporter.OpenMetricsRequested);

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public PrometheusExporter(PrometheusExporterOptions options)

this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;
this.DisableTotalNameSuffixForCounters = options.DisableTotalNameSuffixForCounters;
this.AllowedResourceAttributesFilter = options.AllowedResourceAttributesFilter;

this.CollectionManager = new PrometheusCollectionManager(this);
}
Expand Down Expand Up @@ -58,8 +57,6 @@ internal Func<Batch<Metric>, ExportResult> OnExport

internal bool OpenMetricsRequested { get; set; }

internal Predicate<string> AllowedResourceAttributesFilter { get; set; }

internal Resource Resource => this.resource ??= this.ParentProvider.GetResource();

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public int ScrapeResponseCacheDurationMilliseconds
}
}

/// <summary>
/// Gets or sets the allowed resource attributes filter. Default value: null (no attributes allowed).
/// </summary>
public Predicate<string> AllowedResourceAttributesFilter { get; set; } = null;

/// <summary>
/// Gets or sets a value indicating whether addition of _total suffix for counter metric names is disabled. Default value: <see langword="false"/>.
/// </summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,13 @@ 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, PrometheusResourceTagCollection resourceTags = default, bool writeEnclosingBraces = true)
public static int WriteTags(byte[] buffer, int cursor, Metric metric, ReadOnlyTagCollection tags, bool writeEnclosingBraces = true)
{
if (writeEnclosingBraces)
{
buffer[cursor++] = unchecked((byte)'{');
}

foreach (var resourceAttribute in resourceTags.Attributes)
{
cursor = WriteLabel(buffer, cursor, resourceAttribute.Key, resourceAttribute.Value);
buffer[cursor++] = unchecked((byte)',');
}

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 @@ -22,7 +22,7 @@ public static bool CanWriteMetric(Metric metric)
return true;
}

public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, PrometheusResourceTagCollection resourceTags = default, bool openMetricsRequested = false)
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 @@ -36,7 +36,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, resourceTags);
cursor = WriteTags(buffer, cursor, metric, metricPoint.Tags);

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

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

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

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

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

Expand All @@ -127,7 +127,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, resourceTags);
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 @@ -69,7 +69,6 @@ private static MetricReader BuildPrometheusHttpListenerMetricReader(
{
ScrapeResponseCacheDurationMilliseconds = 0,
DisableTotalNameSuffixForCounters = options.DisableTotalNameSuffixForCounters,
AllowedResourceAttributesFilter = options.AllowedResourceAttributesFilter,
});

var reader = new BaseExportingMetricReader(exporter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,4 @@ public IReadOnlyCollection<string> UriPrefixes
this.uriPrefixes = value;
}
}

/// <summary>
/// Gets or sets the allowed resource attributes filter. Default value: null (no attributes allowed).
/// </summary>
public Predicate<string> AllowedResourceAttributesFilter { get; set; } = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,6 @@ public Task PrometheusExporterMiddlewareIntegration_UseOpenMetricsVersionHeader(
acceptHeader: "application/openmetrics-text; version=1.0.0");
}

[Fact]
public Task PrometheusExporterMiddlewareIntegration_AddResourceAttributesAsTags()
{
return RunPrometheusExporterMiddlewareIntegrationTest(
"/metrics",
app => app.UseOpenTelemetryPrometheusScrapingEndpoint(),
configureOptions: o => o.AllowedResourceAttributesFilter = s => s == "service.name",
addServiceNameResourceTag: true);
}

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

Expand Down Expand Up @@ -336,10 +325,6 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest(

string content = await response.Content.ReadAsStringAsync();

var resourceTagAttributes = addServiceNameResourceTag
? "service_name='my_service',"
: string.Empty;

string expected = requestOpenMetrics
? "# TYPE target info\n"
+ "# HELP target Target metadata\n"
Expand All @@ -348,10 +333,10 @@ private static async Task RunPrometheusExporterMiddlewareIntegrationTest(
+ "# 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{{{resourceTagAttributes}otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\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{{{resourceTagAttributes}otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ public async Task PrometheusExporterHttpServerIntegration_UseOpenMetricsVersionH
await this.RunPrometheusExporterHttpServerIntegrationTest(acceptHeader: "application/openmetrics-text; version=1.0.0");
}

[Fact]
public async Task PrometheusExporterHttpServerIntegration_AddResourceAttributeAsTag()
{
await this.RunPrometheusExporterHttpServerIntegrationTest(addServiceNameResourceTag: true);
}

[Fact]
public void PrometheusHttpListenerThrowsOnStart()
{
Expand Down Expand Up @@ -163,8 +157,7 @@ private static void TestPrometheusHttpListenerUriPrefixOptions(string[] uriPrefi

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

Expand All @@ -189,11 +182,6 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(
.AddPrometheusHttpListener(options =>
{
options.UriPrefixes = new string[] { address };

if (addServiceNameResourceTag)
{
options.AllowedResourceAttributesFilter = s => s == "service.name";
}
})
.Build();

Expand Down Expand Up @@ -248,10 +236,6 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(

var content = await response.Content.ReadAsStringAsync();

var resourceTagAttributes = addServiceNameResourceTag
? "service_name='my_service',"
: string.Empty;

var expected = requestOpenMetrics
? "# TYPE target info\n"
+ "# HELP target Target metadata\n"
Expand All @@ -260,10 +244,10 @@ private async Task RunPrometheusExporterHttpServerIntegrationTest(
+ "# 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{{{resourceTagAttributes}otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+\\.\\d{{3}})\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{{{resourceTagAttributes}otel_scope_name='{MeterName}',otel_scope_version='{MeterVersion}',key1='value1',key2='value2'}} 101.17 (\\d+)\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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics.Metrics;
using System.Text;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Tests;
using Xunit;

Expand Down Expand Up @@ -527,36 +526,6 @@ public void SumWithOpenMetricsFormat()
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void SumWithResourceAttributes()
{
var buffer = new byte[85000];
var metrics = new List<Metric>();

var resource = ResourceBuilder.CreateEmpty().AddService("my_service");

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics)
.SetResourceBuilder(resource)
.Build();

var counter = meter.CreateUpDownCounter<double>("test_updown_counter");
counter.Add(10);
counter.Add(-11);

provider.ForceFlush();

var cursor = WriteMetric(buffer, 0, metrics[0], true, new PrometheusResourceTagCollection(resource.Build(), s => s == "service.name"));
Assert.Matches(
("^"
+ "# TYPE test_updown_counter gauge\n"
+ $"test_updown_counter{{service_name='my_service',otel_scope_name='{Utils.GetCurrentMethodName()}'}} -1 \\d+\\.\\d{{3}}\n"
+ "$").Replace('\'', '"'),
Encoding.UTF8.GetString(buffer, 0, cursor));
}

[Fact]
public void HistogramOneDimensionWithOpenMetricsFormat()
{
Expand Down Expand Up @@ -684,8 +653,8 @@ public void HistogramOneDimensionWithScopeVersion()
Encoding.UTF8.GetString(buffer, 0, cursor));
}

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

0 comments on commit 5ddf850

Please sign in to comment.