Skip to content

Commit cfbc409

Browse files
psx95dashpole
andauthored
Add config option to disable attaching InstrumentationLibrary labels (#358)
* Add config option to disable InstrumentationLibraryLabels * Showcase the config option via example * Update exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/MetricConfiguration.java Fix typo Co-authored-by: David Ashpole <[email protected]> --------- Co-authored-by: David Ashpole <[email protected]>
1 parent 7ca8f25 commit cfbc409

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

examples/metrics/src/main/java/com/google/cloud/opentelemetry/example/metrics/MetricsExporterExample.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private static MetricConfiguration generateMetricExporterConfig(boolean useDefau
7070
// Any properties not set would be retrieved from the default configuration of the exporter.
7171
return MetricConfiguration.builder()
7272
.setMetricServiceSettings(metricServiceSettingsBuilder.build())
73+
.setInstrumentationLibraryLabelsEnabled(false)
7374
.build();
7475
}
7576

exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/AggregateByLabelMetricTimeSeriesBuilder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ public final class AggregateByLabelMetricTimeSeriesBuilder implements MetricTime
6060
private final String prefix;
6161
private final Predicate<AttributeKey<?>> resourceAttributeFilter;
6262
private final MonitoredResourceDescription monitoredResourceDescription;
63+
private final boolean instrumentationLibraryLabelsEnabled;
6364

6465
@Deprecated
6566
public AggregateByLabelMetricTimeSeriesBuilder(String projectId, String prefix) {
6667
this.projectId = projectId;
6768
this.prefix = prefix;
6869
this.resourceAttributeFilter = MetricConfiguration.NO_RESOURCE_ATTRIBUTES;
6970
this.monitoredResourceDescription = MetricConfiguration.EMPTY_MONITORED_RESOURCE_DESCRIPTION;
71+
this.instrumentationLibraryLabelsEnabled = true;
7072
}
7173

7274
@Deprecated
@@ -76,8 +78,10 @@ public AggregateByLabelMetricTimeSeriesBuilder(
7678
this.prefix = prefix;
7779
this.resourceAttributeFilter = resourceAttributeFilter;
7880
this.monitoredResourceDescription = MetricConfiguration.EMPTY_MONITORED_RESOURCE_DESCRIPTION;
81+
this.instrumentationLibraryLabelsEnabled = true;
7982
}
8083

84+
@Deprecated
8185
public AggregateByLabelMetricTimeSeriesBuilder(
8286
String projectId,
8387
String prefix,
@@ -87,6 +91,20 @@ public AggregateByLabelMetricTimeSeriesBuilder(
8791
this.prefix = prefix;
8892
this.resourceAttributeFilter = resourceAttributeFilter;
8993
this.monitoredResourceDescription = monitoredResourceDescription;
94+
this.instrumentationLibraryLabelsEnabled = true;
95+
}
96+
97+
public AggregateByLabelMetricTimeSeriesBuilder(
98+
String projectId,
99+
String prefix,
100+
Predicate<AttributeKey<?>> resourceAttributeFilter,
101+
MonitoredResourceDescription monitoredResourceDescription,
102+
boolean instrumentationLibraryLabelsEnabled) {
103+
this.projectId = projectId;
104+
this.prefix = prefix;
105+
this.resourceAttributeFilter = resourceAttributeFilter;
106+
this.monitoredResourceDescription = monitoredResourceDescription;
107+
this.instrumentationLibraryLabelsEnabled = instrumentationLibraryLabelsEnabled;
90108
}
91109

92110
@Override
@@ -161,6 +179,9 @@ private Attributes extraLabelsFromResource(Resource resource) {
161179

162180
private Attributes instrumentationLibraryLabels(
163181
Attributes attributes, InstrumentationScopeInfo instrumentationScopeInfo) {
182+
if (!instrumentationLibraryLabelsEnabled) {
183+
return attributes;
184+
}
164185
return attributes.toBuilder()
165186
.put(
166187
AttributeKey.stringKey(LABEL_INSTRUMENTATION_SOURCE),

exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/InternalMetricExporter.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class InternalMetricExporter implements MetricExporter {
6969
private final Predicate<AttributeKey<?>> resourceAttributesFilter;
7070
private final boolean useCreateServiceTimeSeries;
7171
private final MonitoredResourceDescription monitoredResourceDescription;
72+
private final boolean instrumentationLibraryLabelsEnabled;
7273

7374
InternalMetricExporter(
7475
String projectId,
@@ -77,14 +78,16 @@ class InternalMetricExporter implements MetricExporter {
7778
MetricDescriptorStrategy descriptorStrategy,
7879
Predicate<AttributeKey<?>> resourceAttributesFilter,
7980
boolean useCreateServiceTimeSeries,
80-
MonitoredResourceDescription monitoredResourceDescription) {
81+
MonitoredResourceDescription monitoredResourceDescription,
82+
boolean instrumentationLibraryLabelsEnabled) {
8183
this.projectId = projectId;
8284
this.prefix = prefix;
8385
this.metricServiceClient = client;
8486
this.metricDescriptorStrategy = descriptorStrategy;
8587
this.resourceAttributesFilter = resourceAttributesFilter;
8688
this.useCreateServiceTimeSeries = useCreateServiceTimeSeries;
8789
this.monitoredResourceDescription = monitoredResourceDescription;
90+
this.instrumentationLibraryLabelsEnabled = instrumentationLibraryLabelsEnabled;
8891
}
8992

9093
static InternalMetricExporter createWithConfiguration(MetricConfiguration configuration)
@@ -103,7 +106,8 @@ static InternalMetricExporter createWithConfiguration(MetricConfiguration config
103106
configuration.getDescriptorStrategy(),
104107
configuration.getResourceAttributesFilter(),
105108
configuration.getUseServiceTimeSeries(),
106-
configuration.getMonitoredResourceDescription());
109+
configuration.getMonitoredResourceDescription(),
110+
configuration.getInstrumentationLibraryLabelsEnabled());
107111
}
108112

109113
@VisibleForTesting
@@ -114,15 +118,17 @@ static InternalMetricExporter createWithClient(
114118
MetricDescriptorStrategy descriptorStrategy,
115119
Predicate<AttributeKey<?>> resourceAttributesFilter,
116120
boolean useCreateServiceTimeSeries,
117-
MonitoredResourceDescription monitoredResourceDescription) {
121+
MonitoredResourceDescription monitoredResourceDescription,
122+
boolean instrumentationLibraryLabelsEnabled) {
118123
return new InternalMetricExporter(
119124
projectId,
120125
prefix,
121126
metricServiceClient,
122127
descriptorStrategy,
123128
resourceAttributesFilter,
124129
useCreateServiceTimeSeries,
125-
monitoredResourceDescription);
130+
monitoredResourceDescription,
131+
instrumentationLibraryLabelsEnabled);
126132
}
127133

128134
private static MetricServiceSettings generateMetricServiceSettings(
@@ -177,7 +183,11 @@ public CompletableResultCode export(Collection<MetricData> metrics) {
177183
// 3. Fire the set of time series off.
178184
MetricTimeSeriesBuilder builder =
179185
new AggregateByLabelMetricTimeSeriesBuilder(
180-
projectId, prefix, resourceAttributesFilter, monitoredResourceDescription);
186+
projectId,
187+
prefix,
188+
resourceAttributesFilter,
189+
monitoredResourceDescription,
190+
instrumentationLibraryLabelsEnabled);
181191
for (final MetricData metricData : metrics) {
182192
// Extract all the underlying points.
183193
switch (metricData.getType()) {

exporters/metrics/src/main/java/com/google/cloud/opentelemetry/metric/MetricConfiguration.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public abstract class MetricConfiguration {
7777
* MetricConfiguration.Builder#setProjectId(String)}, this method returns a {@link Supplier} that
7878
* supplies the default Project ID.
7979
*
80-
* @see ServiceOptions#getDefaultProjectId()
8180
* @return a {@link Supplier} for the GCP project ID.
81+
* @see ServiceOptions#getDefaultProjectId()
8282
*/
8383
abstract Supplier<String> getProjectIdSupplier();
8484

@@ -104,10 +104,10 @@ public final String getProjectId() {
104104
/**
105105
* Returns the prefix prepended to metric names.
106106
*
107+
* @return the prefix to attach to metrics.
107108
* @see <a href="https://cloud.google.com/monitoring/custom-metrics#identifier">Custom Metrics
108109
* Identifiers</a>
109110
* <p>Defaults to workload.googleapis.com.
110-
* @return the prefix to attach to metrics.
111111
*/
112112
public abstract String getPrefix();
113113

@@ -182,6 +182,15 @@ public final String getProjectId() {
182182
@Nullable
183183
public abstract MetricServiceSettings getMetricServiceSettings();
184184

185+
/**
186+
* Returns a boolean indicating if the {@link MetricConfiguration} is configured to add
187+
* instrumentation library labels to the metric attributes during export.
188+
*
189+
* @return true if the {@link MetricConfiguration} is configured to add instrumentation library
190+
* labels to metrics, false otherwise.
191+
*/
192+
public abstract boolean getInstrumentationLibraryLabelsEnabled();
193+
185194
@VisibleForTesting
186195
abstract boolean getInsecureEndpoint();
187196

@@ -206,6 +215,7 @@ public static Builder builder() {
206215
.setDescriptorStrategy(MetricDescriptorStrategy.SEND_ONCE)
207216
.setInsecureEndpoint(false)
208217
.setUseServiceTimeSeries(false)
218+
.setInstrumentationLibraryLabelsEnabled(true)
209219
.setResourceAttributesFilter(DEFAULT_RESOURCE_ATTRIBUTES_FILTER)
210220
.setMonitoredResourceDescription(EMPTY_MONITORED_RESOURCE_DESCRIPTION)
211221
.setMetricServiceEndpoint(DEFAULT_METRIC_SERVICE_ENDPOINT);
@@ -310,15 +320,37 @@ public abstract Builder setMonitoredResourceDescription(
310320
* <li>{@link MetricConfiguration.Builder#setMetricServiceEndpoint(String)}
311321
* </ul>
312322
*
313-
* The intended effect of setting these values in the configuration should instead be achieved
314-
* by configuring the {@link MetricServiceSettings} object.
323+
* <p>The intended effect of setting these values in the configuration should instead be
324+
* achieved by configuring the {@link MetricServiceSettings} object.
315325
*
316326
* @param metricServiceSettings the {@link MetricServiceSettings} containing the configured
317327
* options.
318328
* @return this.
319329
*/
320330
public abstract Builder setMetricServiceSettings(MetricServiceSettings metricServiceSettings);
321331

332+
/**
333+
* Sets the {@link MetricConfiguration} to configure the exporter to add instrumentation library
334+
* labels as metric attributes during export.
335+
*
336+
* <p>Enabling instrumentation library labels adds the following metric attributes to exported
337+
* metric data points:
338+
*
339+
* <ul>
340+
* <li>instrumentation_source
341+
* <li>instrumentation_version
342+
* </ul>
343+
*
344+
* The value for these metric attributes is retrieved from {@link
345+
* io.opentelemetry.sdk.common.InstrumentationScopeInfo}.
346+
*
347+
* @param instrumentationLibraryLabelsEnabled boolean indicating whether to add instrumentation
348+
* library labels.
349+
* @return this.
350+
*/
351+
public abstract Builder setInstrumentationLibraryLabelsEnabled(
352+
boolean instrumentationLibraryLabelsEnabled);
353+
322354
@VisibleForTesting
323355
abstract Builder setInsecureEndpoint(boolean value);
324356

exporters/metrics/src/test/java/com/google/cloud/opentelemetry/metric/GoogleCloudMetricExporterTest.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ public void testExportSendsAllDescriptorsOnce() {
221221
MetricDescriptorStrategy.SEND_ONCE,
222222
DEFAULT_RESOURCE_ATTRIBUTES_FILTER,
223223
false,
224-
EMPTY_MONITORED_RESOURCE_DESCRIPTION);
224+
EMPTY_MONITORED_RESOURCE_DESCRIPTION,
225+
true);
225226
CompletableResultCode result = exporter.export(ImmutableList.of(aMetricData, aHistogram));
226227
assertTrue(result.isSuccess());
227228
CompletableResultCode result2 = exporter.export(ImmutableList.of(aMetricData, aHistogram));
@@ -332,7 +333,8 @@ public void testExportSucceeds() {
332333
MetricDescriptorStrategy.ALWAYS_SEND,
333334
DEFAULT_RESOURCE_ATTRIBUTES_FILTER,
334335
false,
335-
EMPTY_MONITORED_RESOURCE_DESCRIPTION);
336+
EMPTY_MONITORED_RESOURCE_DESCRIPTION,
337+
true);
336338

337339
CompletableResultCode result = exporter.export(ImmutableList.of(aMetricData));
338340
verify(mockClient, times(1)).createMetricDescriptor(metricDescriptorCaptor.capture());
@@ -455,7 +457,8 @@ public void testExportWithHistogram_Succeeds() {
455457
MetricDescriptorStrategy.ALWAYS_SEND,
456458
DEFAULT_RESOURCE_ATTRIBUTES_FILTER,
457459
false,
458-
EMPTY_MONITORED_RESOURCE_DESCRIPTION);
460+
EMPTY_MONITORED_RESOURCE_DESCRIPTION,
461+
true);
459462
CompletableResultCode result = exporter.export(ImmutableList.of(aHistogram));
460463
verify(mockClient, times(1)).createMetricDescriptor(metricDescriptorCaptor.capture());
461464
verify(mockClient, times(1))
@@ -477,7 +480,8 @@ public void testExportWithNonSupportedMetricTypeReturnsFailure() {
477480
MetricDescriptorStrategy.ALWAYS_SEND,
478481
NO_RESOURCE_ATTRIBUTES,
479482
false,
480-
EMPTY_MONITORED_RESOURCE_DESCRIPTION);
483+
EMPTY_MONITORED_RESOURCE_DESCRIPTION,
484+
true);
481485

482486
MetricData metricData =
483487
ImmutableMetricData.createDoubleSummary(
@@ -585,7 +589,8 @@ public void testExportWithMonitoredResourceMappingSucceeds() {
585589
MetricDescriptorStrategy.ALWAYS_SEND,
586590
customAttributesFilter,
587591
false,
588-
monitoredResourceDescription);
592+
monitoredResourceDescription,
593+
true);
589594

590595
CompletableResultCode result = exporter.export(ImmutableList.of(aMetricDataWithCustomResource));
591596
verify(mockClient, times(1)).createMetricDescriptor(metricDescriptorCaptor.capture());
@@ -681,7 +686,8 @@ public void testExportWithMonitoredResourceMappingSucceeds_NoMRLabels() {
681686
MetricDescriptorStrategy.ALWAYS_SEND,
682687
customAttributesFilter,
683688
false,
684-
monitoredResourceDescription);
689+
monitoredResourceDescription,
690+
true);
685691

686692
CompletableResultCode result = exporter.export(ImmutableList.of(aMetricDataWithCustomResource));
687693
verify(mockClient, times(1)).createMetricDescriptor(metricDescriptorCaptor.capture());
@@ -696,7 +702,8 @@ public void testExportWithMonitoredResourceMappingSucceeds_NoMRLabels() {
696702
}
697703

698704
@Test
699-
public void testExportWithMonitoredResourceMappingSucceeds_NoResourceLabels() {
705+
public void
706+
testExportWithMonitoredResourceMappingSucceeds_NoResourceLabels_NoInstrumentationLabels() {
700707
MonitoredResourceDescription monitoredResourceDescription =
701708
new MonitoredResourceDescription(
702709
"custom_mr_instance", Set.of("service_instance_id", "host_id", "location"));
@@ -743,15 +750,14 @@ public void testExportWithMonitoredResourceMappingSucceeds_NoResourceLabels() {
743750
.setValue(TypedValue.newBuilder().setInt64Value(aLongPoint.getValue()))
744751
.setInterval(expectedTimeInterval)
745752
.build();
753+
// expected timeseries metric does not have the instrumentation library labels
746754
TimeSeries expectedTimeSeries =
747755
TimeSeries.newBuilder()
748756
.setMetric(
749757
Metric.newBuilder()
750758
.setType(expectedDescriptor.getType())
751759
.putLabels("label1", "value1")
752760
.putLabels("label2", "false")
753-
.putLabels(LABEL_INSTRUMENTATION_SOURCE, "instrumentName")
754-
.putLabels(LABEL_INSTRUMENTATION_VERSION, "0")
755761
.build())
756762
.addPoints(expectedPoint)
757763
.setMetricKind(expectedDescriptor.getMetricKind())
@@ -772,7 +778,8 @@ public void testExportWithMonitoredResourceMappingSucceeds_NoResourceLabels() {
772778
MetricDescriptorStrategy.ALWAYS_SEND,
773779
customAttributesFilter,
774780
false,
775-
monitoredResourceDescription);
781+
monitoredResourceDescription,
782+
false); // disable instrumentationLibrary labels generation
776783

777784
CompletableResultCode result =
778785
exporter.export(ImmutableList.of(aMetricDataWithEmptyResourceAttributes));
@@ -849,7 +856,8 @@ public void verifyExporterExportGoogleServiceMetrics() {
849856
MetricDescriptorStrategy.ALWAYS_SEND,
850857
NO_RESOURCE_ATTRIBUTES,
851858
true,
852-
EMPTY_MONITORED_RESOURCE_DESCRIPTION);
859+
EMPTY_MONITORED_RESOURCE_DESCRIPTION,
860+
true);
853861

854862
CompletableResultCode result =
855863
exporter.export(ImmutableList.of(googleComputeServiceMetricData));

0 commit comments

Comments
 (0)