Skip to content

Commit ef84209

Browse files
zeitlingertrask
andauthored
Remove scope info, always add scope labels (#7398)
Co-authored-by: Trask Stalnaker <[email protected]>
1 parent 8ea6895 commit ef84209

File tree

9 files changed

+41
-66
lines changed

9 files changed

+41
-66
lines changed

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@
5656
import java.util.Collection;
5757
import java.util.Collections;
5858
import java.util.HashMap;
59-
import java.util.LinkedHashSet;
6059
import java.util.List;
6160
import java.util.Locale;
6261
import java.util.Map;
63-
import java.util.Set;
6462
import java.util.StringJoiner;
6563
import java.util.concurrent.ConcurrentHashMap;
6664
import java.util.concurrent.TimeUnit;
@@ -82,7 +80,6 @@ final class Otel2PrometheusConverter {
8280
private static final long NANOS_PER_MILLISECOND = TimeUnit.MILLISECONDS.toNanos(1);
8381
static final int MAX_CACHE_SIZE = 10;
8482

85-
private final boolean otelScopeEnabled;
8683
@Nullable private final Predicate<String> allowedResourceAttributesFilter;
8784

8885
/**
@@ -94,14 +91,10 @@ final class Otel2PrometheusConverter {
9491
/**
9592
* Constructor with feature flag parameter.
9693
*
97-
* @param otelScopeEnabled enable generation of the OpenTelemetry instrumentation scope info
98-
* metric and labels.
9994
* @param allowedResourceAttributesFilter if not {@code null}, resource attributes with keys
10095
* matching this predicate will be added as labels on each exported metric
10196
*/
102-
Otel2PrometheusConverter(
103-
boolean otelScopeEnabled, @Nullable Predicate<String> allowedResourceAttributesFilter) {
104-
this.otelScopeEnabled = otelScopeEnabled;
97+
Otel2PrometheusConverter(@Nullable Predicate<String> allowedResourceAttributesFilter) {
10598
this.allowedResourceAttributesFilter = allowedResourceAttributesFilter;
10699
this.resourceAttributesToAllowedKeysCache =
107100
allowedResourceAttributesFilter != null
@@ -115,7 +108,6 @@ MetricSnapshots convert(@Nullable Collection<MetricData> metricDataCollection) {
115108
}
116109
Map<String, MetricSnapshot> snapshotsByName = new HashMap<>(metricDataCollection.size());
117110
Resource resource = null;
118-
Set<InstrumentationScopeInfo> scopes = new LinkedHashSet<>();
119111
for (MetricData metricData : metricDataCollection) {
120112
MetricSnapshot snapshot = convert(metricData);
121113
if (snapshot == null) {
@@ -125,16 +117,10 @@ MetricSnapshots convert(@Nullable Collection<MetricData> metricDataCollection) {
125117
if (resource == null) {
126118
resource = metricData.getResource();
127119
}
128-
if (otelScopeEnabled && !metricData.getInstrumentationScopeInfo().getAttributes().isEmpty()) {
129-
scopes.add(metricData.getInstrumentationScopeInfo());
130-
}
131120
}
132121
if (resource != null) {
133122
putOrMerge(snapshotsByName, makeTargetInfo(resource));
134123
}
135-
if (otelScopeEnabled && !scopes.isEmpty()) {
136-
putOrMerge(snapshotsByName, makeScopeInfo(scopes));
137-
}
138124
return new MetricSnapshots(snapshotsByName.values());
139125
}
140126

@@ -440,25 +426,11 @@ private InfoSnapshot makeTargetInfo(Resource resource) {
440426
resource.getAttributes()))));
441427
}
442428

443-
private InfoSnapshot makeScopeInfo(Set<InstrumentationScopeInfo> scopes) {
444-
List<InfoDataPointSnapshot> prometheusScopeInfos = new ArrayList<>(scopes.size());
445-
for (InstrumentationScopeInfo scope : scopes) {
446-
prometheusScopeInfos.add(
447-
new InfoDataPointSnapshot(
448-
convertAttributes(
449-
null, // resource attributes are only copied for point's attributes
450-
scope,
451-
scope.getAttributes())));
452-
}
453-
return new InfoSnapshot(new MetricMetadata("otel_scope"), prometheusScopeInfos);
454-
}
455-
456429
/**
457430
* Convert OpenTelemetry attributes to Prometheus labels.
458431
*
459432
* @param resource optional resource (attributes) to be converted.
460-
* @param scope will be converted to {@code otel_scope_*} labels if {@code otelScopeEnabled} is
461-
* {@code true}.
433+
* @param scope will be converted to {@code otel_scope_*} labels.
462434
* @param attributes the attributes to be converted.
463435
* @param additionalAttributes optional list of key/value pairs, may be empty.
464436
*/
@@ -485,7 +457,7 @@ private Labels convertAttributes(
485457
requireNonNull(additionalAttributes[i]), additionalAttributes[i + 1]);
486458
}
487459

488-
if (otelScopeEnabled && scope != null) {
460+
if (scope != null) {
489461
labelNameToValue.putIfAbsent(OTEL_SCOPE_NAME, scope.getName());
490462
if (scope.getVersion() != null) {
491463
labelNameToValue.putIfAbsent(OTEL_SCOPE_VERSION, scope.getVersion());

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServer.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public final class PrometheusHttpServer implements MetricReader {
4242

4343
private final String host;
4444
private final int port;
45-
private final boolean otelScopeEnabled;
4645
@Nullable private final Predicate<String> allowedResourceAttributesFilter;
4746
private final MemoryMode memoryMode;
4847
private final DefaultAggregationSelector defaultAggregationSelector;
@@ -72,21 +71,18 @@ public static PrometheusHttpServerBuilder builder() {
7271
int port,
7372
@Nullable ExecutorService executor,
7473
PrometheusRegistry prometheusRegistry,
75-
boolean otelScopeEnabled,
7674
@Nullable Predicate<String> allowedResourceAttributesFilter,
7775
MemoryMode memoryMode,
7876
@Nullable HttpHandler defaultHandler,
7977
DefaultAggregationSelector defaultAggregationSelector,
8078
@Nullable Authenticator authenticator) {
8179
this.host = host;
8280
this.port = port;
83-
this.otelScopeEnabled = otelScopeEnabled;
8481
this.allowedResourceAttributesFilter = allowedResourceAttributesFilter;
8582
this.memoryMode = memoryMode;
8683
this.defaultAggregationSelector = defaultAggregationSelector;
8784
this.builder = builder;
88-
this.prometheusMetricReader =
89-
new PrometheusMetricReader(otelScopeEnabled, allowedResourceAttributesFilter);
85+
this.prometheusMetricReader = new PrometheusMetricReader(allowedResourceAttributesFilter);
9086
this.prometheusRegistry = prometheusRegistry;
9187
prometheusRegistry.register(prometheusMetricReader);
9288
// When memory mode is REUSABLE_DATA, concurrent reads lead to data corruption. To prevent this,
@@ -171,7 +167,6 @@ public String toString() {
171167
StringJoiner joiner = new StringJoiner(",", "PrometheusHttpServer{", "}");
172168
joiner.add("host=" + host);
173169
joiner.add("port=" + port);
174-
joiner.add("otelScopeEnabled=" + otelScopeEnabled);
175170
joiner.add("allowedResourceAttributesFilter=" + allowedResourceAttributesFilter);
176171
joiner.add("memoryMode=" + memoryMode);
177172
joiner.add(

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerBuilder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public final class PrometheusHttpServerBuilder {
3030
private String host = DEFAULT_HOST;
3131
private int port = DEFAULT_PORT;
3232
private PrometheusRegistry prometheusRegistry = new PrometheusRegistry();
33-
private boolean otelScopeEnabled = true;
3433
@Nullable private Predicate<String> allowedResourceAttributesFilter;
3534
@Nullable private ExecutorService executor;
3635
private MemoryMode memoryMode = DEFAULT_MEMORY_MODE;
@@ -45,7 +44,6 @@ public final class PrometheusHttpServerBuilder {
4544
this.host = builder.host;
4645
this.port = builder.port;
4746
this.prometheusRegistry = builder.prometheusRegistry;
48-
this.otelScopeEnabled = builder.otelScopeEnabled;
4947
this.allowedResourceAttributesFilter = builder.allowedResourceAttributesFilter;
5048
this.executor = builder.executor;
5149
this.memoryMode = builder.memoryMode;
@@ -83,10 +81,14 @@ public PrometheusHttpServerBuilder setPrometheusRegistry(PrometheusRegistry prom
8381
return this;
8482
}
8583

86-
/** Set if the {@code otel_scope_*} attributes are generated. Default is {@code true}. */
84+
/**
85+
* Set if the {@code otel_scope_*} attributes are generated. Default is {@code true}.
86+
*
87+
* @deprecated {@code otel_scope_*} attributes are always generated.
88+
*/
8789
@SuppressWarnings("UnusedReturnValue")
90+
@Deprecated
8891
public PrometheusHttpServerBuilder setOtelScopeEnabled(boolean otelScopeEnabled) {
89-
this.otelScopeEnabled = otelScopeEnabled;
9092
return this;
9193
}
9294

@@ -176,7 +178,6 @@ public PrometheusHttpServer build() {
176178
port,
177179
executor,
178180
prometheusRegistry,
179-
otelScopeEnabled,
180181
allowedResourceAttributesFilter,
181182
memoryMode,
182183
defaultHandler,

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusMetricReader.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ public class PrometheusMetricReader implements MetricReader, MultiCollector {
2828
private volatile CollectionRegistration collectionRegistration = CollectionRegistration.noop();
2929
private final Otel2PrometheusConverter converter;
3030

31-
// TODO: refactor to public static create or builder pattern to align with project style
32-
/** See {@link Otel2PrometheusConverter#Otel2PrometheusConverter(boolean, Predicate)}. */
31+
/**
32+
* Deprecated. Use {@link #PrometheusMetricReader(Predicate)}.
33+
*
34+
* @deprecated use {@link #PrometheusMetricReader(Predicate)}.
35+
*/
36+
@Deprecated
37+
@SuppressWarnings({"unused", "InconsistentOverloads"})
3338
public PrometheusMetricReader(
3439
boolean otelScopeEnabled, @Nullable Predicate<String> allowedResourceAttributesFilter) {
35-
this.converter =
36-
new Otel2PrometheusConverter(otelScopeEnabled, allowedResourceAttributesFilter);
40+
this.converter = new Otel2PrometheusConverter(allowedResourceAttributesFilter);
41+
}
42+
43+
// TODO: refactor to public static create or builder pattern to align with project style
44+
public PrometheusMetricReader(@Nullable Predicate<String> allowedResourceAttributesFilter) {
45+
this.converter = new Otel2PrometheusConverter(allowedResourceAttributesFilter);
3746
}
3847

3948
@Override

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/internal/PrometheusComponentProvider.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public MetricReader create(DeclarativeConfigProperties config) {
4545
prometheusBuilder.setHost(host);
4646
}
4747

48-
Boolean withoutScopeInfo = config.getBoolean("without_scope_info");
49-
if (withoutScopeInfo != null) {
50-
prometheusBuilder.setOtelScopeEnabled(!withoutScopeInfo);
51-
}
52-
5348
DeclarativeConfigProperties withResourceConstantLabels =
5449
config.getStructured("with_resource_constant_labels");
5550
if (withResourceConstantLabels != null) {

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Otel2PrometheusConverterTest {
7171
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
7272

7373
private final Otel2PrometheusConverter converter =
74-
new Otel2PrometheusConverter(true, /* allowedResourceAttributesFilter= */ null);
74+
new Otel2PrometheusConverter(/* allowedResourceAttributesFilter= */ null);
7575

7676
@ParameterizedTest
7777
@MethodSource("metricMetadataArgs")
@@ -201,7 +201,7 @@ void resourceAttributesAddition(
201201
throws IOException {
202202

203203
Otel2PrometheusConverter converter =
204-
new Otel2PrometheusConverter(true, allowedResourceAttributesFilter);
204+
new Otel2PrometheusConverter(allowedResourceAttributesFilter);
205205

206206
ByteArrayOutputStream out = new ByteArrayOutputStream();
207207
MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData));
@@ -501,7 +501,7 @@ void validateCacheIsBounded() {
501501
};
502502

503503
Otel2PrometheusConverter otel2PrometheusConverter =
504-
new Otel2PrometheusConverter(true, /* allowedResourceAttributesFilter= */ countPredicate);
504+
new Otel2PrometheusConverter(/* allowedResourceAttributesFilter= */ countPredicate);
505505

506506
// Create 20 different metric data objects with 2 different resource attributes;
507507
Resource resource1 = Resource.builder().put("cluster", "cluster1").build();

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ void stringRepresentation() {
432432
"PrometheusHttpServer{"
433433
+ "host=localhost,"
434434
+ "port=0,"
435-
+ "otelScopeEnabled=true,"
436435
+ "allowedResourceAttributesFilter=null,"
437436
+ "memoryMode=REUSABLE_DATA,"
438437
+ "defaultAggregationSelector=DefaultAggregationSelector{COUNTER=default, UP_DOWN_COUNTER=default, HISTOGRAM=default, OBSERVABLE_COUNTER=default, OBSERVABLE_UP_DOWN_COUNTER=default, OBSERVABLE_GAUGE=default, GAUGE=default}"
@@ -547,7 +546,6 @@ void toBuilder() {
547546
PrometheusHttpServerBuilder builder = PrometheusHttpServer.builder();
548547
builder.setHost("localhost");
549548
builder.setPort(1234);
550-
builder.setOtelScopeEnabled(false);
551549

552550
Predicate<String> resourceAttributesFilter = s -> false;
553551
builder.setAllowedResourceAttributesFilter(resourceAttributesFilter);
@@ -574,7 +572,6 @@ public Result authenticate(HttpExchange exchange) {
574572
.isInstanceOf(PrometheusHttpServerBuilder.class)
575573
.hasFieldOrPropertyWithValue("host", "localhost")
576574
.hasFieldOrPropertyWithValue("port", 1234)
577-
.hasFieldOrPropertyWithValue("otelScopeEnabled", false)
578575
.hasFieldOrPropertyWithValue("allowedResourceAttributesFilter", resourceAttributesFilter)
579576
.hasFieldOrPropertyWithValue("executor", executor)
580577
.hasFieldOrPropertyWithValue("prometheusRegistry", prometheusRegistry)

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusMetricReaderTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class PrometheusMetricReaderTest {
6161
void setUp() {
6262
this.testClock.setTime(Instant.ofEpochMilli((System.currentTimeMillis() / 100) * 100));
6363
this.createdTimestamp = convertTimestamp(testClock.now());
64-
this.reader = new PrometheusMetricReader(true, /* allowedResourceAttributesFilter= */ null);
64+
this.reader = new PrometheusMetricReader(/* allowedResourceAttributesFilter= */ null);
6565
this.meter =
6666
SdkMeterProvider.builder()
6767
.setClock(testClock)
@@ -776,7 +776,7 @@ void exponentialHistogramBucketConversion() {
776776
int otelScale = random.nextInt(24) - 4;
777777
int prometheusScale = Math.min(otelScale, 8);
778778
PrometheusMetricReader reader =
779-
new PrometheusMetricReader(true, /* allowedResourceAttributesFilter= */ null);
779+
new PrometheusMetricReader(/* allowedResourceAttributesFilter= */ null);
780780
Meter meter =
781781
SdkMeterProvider.builder()
782782
.registerMetricReader(reader)
@@ -1027,9 +1027,9 @@ void otelScopeComplete() throws IOException {
10271027
}
10281028

10291029
@Test
1030-
void otelScopeDisabled() throws IOException {
1030+
void otelScopeLabelsOnly() throws IOException {
10311031
PrometheusMetricReader reader =
1032-
new PrometheusMetricReader(false, /* allowedResourceAttributesFilter= */ null);
1032+
new PrometheusMetricReader(/* allowedResourceAttributesFilter= */ null);
10331033
Meter meter =
10341034
SdkMeterProvider.builder()
10351035
.setClock(testClock)
@@ -1047,8 +1047,8 @@ void otelScopeDisabled() throws IOException {
10471047
+ "# TYPE target info\n"
10481048
+ "target_info{service_name=\"unknown_service:java\",telemetry_sdk_language=\"java\",telemetry_sdk_name=\"opentelemetry\",telemetry_sdk_version=\"1.x.x\"} 1\n"
10491049
+ "# TYPE test_count counter\n"
1050-
+ "test_count_total 1.0\n"
1051-
+ "test_count_created "
1050+
+ "test_count_total{otel_scope_name=\"test-scope\",otel_scope_version=\"a.b.c\"} 1.0\n"
1051+
+ "test_count_created{otel_scope_name=\"test-scope\",otel_scope_version=\"a.b.c\"} "
10521052
+ createdTimestamp
10531053
+ "\n"
10541054
+ "# EOF\n";
@@ -1059,8 +1059,7 @@ void otelScopeDisabled() throws IOException {
10591059
@Test
10601060
void addResourceAttributesWorks() throws IOException {
10611061
PrometheusMetricReader reader =
1062-
new PrometheusMetricReader(
1063-
true, /* allowedResourceAttributesFilter= */ Predicates.is("cluster"));
1062+
new PrometheusMetricReader(/* allowedResourceAttributesFilter= */ Predicates.is("cluster"));
10641063
Meter meter =
10651064
SdkMeterProvider.builder()
10661065
.setClock(testClock)
@@ -1091,6 +1090,14 @@ void addResourceAttributesWorks() throws IOException {
10911090
assertThat(toOpenMetrics(reader.collect())).isEqualTo(expected);
10921091
}
10931092

1093+
@SuppressWarnings("deprecation") // test deprecated constructor
1094+
@Test
1095+
void deprecatedConstructor() {
1096+
assertThat(new PrometheusMetricReader(false, null))
1097+
.usingRecursiveComparison()
1098+
.isEqualTo(new PrometheusMetricReader(null));
1099+
}
1100+
10941101
/**
10951102
* Unfortunately there is no easy way to use {@link TestClock} for Exemplar timestamps. Test if
10961103
* {@code expected} equals {@code actual} but {@code <timestamp>} matches arbitrary timestamps.

sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/MetricReaderFactoryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ void create_PullPrometheusConfigured() throws IOException {
165165
PrometheusHttpServer.builder()
166166
.setHost("localhost")
167167
.setPort(port)
168-
.setOtelScopeEnabled(false)
169168
.setAllowedResourceAttributesFilter(
170169
IncludeExcludePredicate.createPatternMatching(
171170
singletonList("foo"), singletonList("bar")))

0 commit comments

Comments
 (0)