Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 9b2ac2b

Browse files
authored
Exporter/Stackdriver: Allow to override the opencensus_task label. (#1845)
1 parent 4436862 commit 9b2ac2b

11 files changed

Lines changed: 294 additions & 65 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22
- Add HTTP text format serializer to Tag propagation component.
33
- Support constant labels in Gauge APIs.
4+
- Add an option to allow users to override the default "opencensus_task" metric label in Stackdriver Stats Exporter.
45

56
## 0.20.0 - 2019-03-28
67
- Add OpenCensus Java OC-Agent Trace Exporter.

exporters/stats/stackdriver/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ Stackdriver exporter adds a new `Metric` label for each custom `Metric` to ensur
156156
of the `Timeseries`. The format of the label is: `{LANGUAGE}-{PID}@{HOSTNAME}`, if `{PID}` is not
157157
available a random number will be used.
158158

159+
You have the option to override the "opencensus_task" metric label with custom constant labels using
160+
`StackdriverStatsConfiguration.Builder.setConstantLabels()`. If you do so, make sure that the
161+
monitored resource together with these labels is unique to the current process. This is to ensure
162+
that there is only a single writer to each time series in Stackdriver.
163+
164+
You can also set `StackdriverStatsConfiguration.Builder.setConstantLabels()` to an empty map to
165+
avoid getting the default "opencensus_task" label. You should only do this if you know that the
166+
monitored resource uniquely identifies this process.
167+
159168
### Why did I get an error "java.lang.NoSuchMethodError: com.google.common...", like "java.lang.NoSuchMethodError:com.google.common.base.Throwables.throwIfInstanceOf"?
160169
This is probably because there is a version conflict on Guava in the dependency tree.
161170

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.google.monitoring.v3.CreateMetricDescriptorRequest;
2424
import com.google.monitoring.v3.ProjectName;
2525
import io.opencensus.exporter.metrics.util.MetricExporter;
26+
import io.opencensus.metrics.LabelKey;
27+
import io.opencensus.metrics.LabelValue;
2628
import io.opencensus.metrics.export.Metric;
2729
import io.opencensus.metrics.export.MetricDescriptor.Type;
2830
import io.opencensus.trace.Span;
@@ -51,18 +53,21 @@ final class CreateMetricDescriptorExporter extends MetricExporter {
5153
private final String displayNamePrefix;
5254
private final Map<String, io.opencensus.metrics.export.MetricDescriptor>
5355
registeredMetricDescriptors = new LinkedHashMap<>();
56+
private final Map<LabelKey, LabelValue> constantLabels;
5457
private final MetricExporter nextExporter;
5558

5659
CreateMetricDescriptorExporter(
5760
String projectId,
5861
MetricServiceClient metricServiceClient,
5962
@javax.annotation.Nullable String metricNamePrefix,
63+
Map<LabelKey, LabelValue> constantLabels,
6064
MetricExporter nextExporter) {
6165
this.projectId = projectId;
6266
projectName = ProjectName.newBuilder().setProject(projectId).build();
6367
this.metricServiceClient = metricServiceClient;
6468
this.domain = StackdriverExportUtils.getDomain(metricNamePrefix);
6569
this.displayNamePrefix = StackdriverExportUtils.getDisplayNamePrefix(metricNamePrefix);
70+
this.constantLabels = constantLabels;
6671
this.nextExporter = nextExporter;
6772
}
6873

@@ -95,7 +100,7 @@ private boolean registerMetricDescriptor(
95100
span.addAnnotation("Create Stackdriver Metric.");
96101
MetricDescriptor stackDriverMetricDescriptor =
97102
StackdriverExportUtils.createMetricDescriptor(
98-
metricDescriptor, projectId, domain, displayNamePrefix);
103+
metricDescriptor, projectId, domain, displayNamePrefix, constantLabels);
99104

100105
CreateMetricDescriptorRequest request =
101106
CreateMetricDescriptorRequest.newBuilder()

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateTimeSeriesExporter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.google.monitoring.v3.ProjectName;
2727
import com.google.monitoring.v3.TimeSeries;
2828
import io.opencensus.exporter.metrics.util.MetricExporter;
29+
import io.opencensus.metrics.LabelKey;
30+
import io.opencensus.metrics.LabelValue;
2931
import io.opencensus.metrics.export.Metric;
3032
import io.opencensus.trace.Span;
3133
import io.opencensus.trace.Status;
@@ -34,6 +36,7 @@
3436
import java.util.ArrayList;
3537
import java.util.Collection;
3638
import java.util.List;
39+
import java.util.Map;
3740
import java.util.logging.Level;
3841
import java.util.logging.Logger;
3942

@@ -45,16 +48,19 @@ final class CreateTimeSeriesExporter extends MetricExporter {
4548
private final MetricServiceClient metricServiceClient;
4649
private final MonitoredResource monitoredResource;
4750
private final String domain;
51+
private final Map<LabelKey, LabelValue> constantLabels;
4852

4953
CreateTimeSeriesExporter(
5054
String projectId,
5155
MetricServiceClient metricServiceClient,
5256
MonitoredResource monitoredResource,
53-
@javax.annotation.Nullable String metricNamePrefix) {
57+
@javax.annotation.Nullable String metricNamePrefix,
58+
Map<LabelKey, LabelValue> constantLabels) {
5459
projectName = ProjectName.newBuilder().setProject(projectId).build();
5560
this.metricServiceClient = metricServiceClient;
5661
this.monitoredResource = monitoredResource;
5762
this.domain = StackdriverExportUtils.getDomain(metricNamePrefix);
63+
this.constantLabels = constantLabels;
5864
}
5965

6066
@Override
@@ -63,7 +69,7 @@ public void export(Collection<Metric> metrics) {
6369
for (Metric metric : metrics) {
6470
timeSeriesList.addAll(
6571
StackdriverExportUtils.createTimeSeriesList(
66-
metric, monitoredResource, domain, projectName.getProject()));
72+
metric, monitoredResource, domain, projectName.getProject(), constantLabels));
6773
}
6874

6975
Span span = tracer.getCurrentSpan();

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,17 @@
7979
@SuppressWarnings("deprecation")
8080
final class StackdriverExportUtils {
8181

82-
// TODO(songya): do we want these constants to be customizable?
83-
@VisibleForTesting static final String OPENCENSUS_TASK = "opencensus_task";
84-
@VisibleForTesting static final String OPENCENSUS_TASK_DESCRIPTION = "Opencensus task identifier";
82+
@VisibleForTesting
83+
static final LabelKey OPENCENSUS_TASK_KEY =
84+
LabelKey.create("opencensus_task", "Opencensus task identifier");
85+
86+
@VisibleForTesting
87+
static final LabelValue OPENCENSUS_TASK_VALUE_DEFAULT =
88+
LabelValue.create(generateDefaultTaskValue());
89+
90+
static final Map<LabelKey, LabelValue> DEFAULT_CONSTANT_LABELS =
91+
Collections.singletonMap(OPENCENSUS_TASK_KEY, OPENCENSUS_TASK_VALUE_DEFAULT);
92+
8593
@VisibleForTesting static final String STACKDRIVER_PROJECT_ID_KEY = "project_id";
8694
@VisibleForTesting static final String DEFAULT_DISPLAY_NAME_PREFIX = "OpenCensus/";
8795
@VisibleForTesting static final String CUSTOM_METRIC_DOMAIN = "custom.googleapis.com/";
@@ -97,7 +105,6 @@ final class StackdriverExportUtils {
97105
@VisibleForTesting static final String AWS_REGION_VALUE_PREFIX = "aws:";
98106

99107
private static final Logger logger = Logger.getLogger(StackdriverExportUtils.class.getName());
100-
private static final String OPENCENSUS_TASK_VALUE_DEFAULT = generateDefaultTaskValue();
101108

102109
// Mappings for the well-known OC resources to applicable Stackdriver resources.
103110
private static final Map<String, String> GCP_RESOURCE_MAPPING = getGcpResourceLabelsMappings();
@@ -206,7 +213,8 @@ static MetricDescriptor createMetricDescriptor(
206213
io.opencensus.metrics.export.MetricDescriptor metricDescriptor,
207214
String projectId,
208215
String domain,
209-
String displayNamePrefix) {
216+
String displayNamePrefix,
217+
Map<LabelKey, LabelValue> constantLabels) {
210218

211219
MetricDescriptor.Builder builder = MetricDescriptor.newBuilder();
212220
String type = generateType(metricDescriptor.getName(), domain);
@@ -219,12 +227,9 @@ static MetricDescriptor createMetricDescriptor(
219227
for (LabelKey labelKey : metricDescriptor.getLabelKeys()) {
220228
builder.addLabels(createLabelDescriptor(labelKey));
221229
}
222-
builder.addLabels(
223-
LabelDescriptor.newBuilder()
224-
.setKey(OPENCENSUS_TASK)
225-
.setDescription(OPENCENSUS_TASK_DESCRIPTION)
226-
.setValueType(ValueType.STRING)
227-
.build());
230+
for (LabelKey labelKey : constantLabels.keySet()) {
231+
builder.addLabels(createLabelDescriptor(labelKey));
232+
}
228233

229234
builder.setUnit(metricDescriptor.getUnit());
230235
builder.setMetricKind(createMetricKind(metricDescriptor.getType()));
@@ -283,7 +288,8 @@ static List<TimeSeries> createTimeSeriesList(
283288
io.opencensus.metrics.export.Metric metric,
284289
MonitoredResource monitoredResource,
285290
String domain,
286-
String projectId) {
291+
String projectId,
292+
Map<LabelKey, LabelValue> constantLabels) {
287293
List<TimeSeries> timeSeriesList = Lists.newArrayList();
288294
io.opencensus.metrics.export.MetricDescriptor metricDescriptor = metric.getMetricDescriptor();
289295

@@ -301,7 +307,8 @@ static List<TimeSeries> createTimeSeriesList(
301307
for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) {
302308
// TODO(mayurkale): Consider using setPoints instead of builder clone and addPoints.
303309
TimeSeries.Builder builder = shared.clone();
304-
builder.setMetric(createMetric(metricDescriptor, timeSeries.getLabelValues(), domain));
310+
builder.setMetric(
311+
createMetric(metricDescriptor, timeSeries.getLabelValues(), domain, constantLabels));
305312

306313
io.opencensus.common.Timestamp startTimeStamp = timeSeries.getStartTimestamp();
307314
for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) {
@@ -317,7 +324,8 @@ static List<TimeSeries> createTimeSeriesList(
317324
static Metric createMetric(
318325
io.opencensus.metrics.export.MetricDescriptor metricDescriptor,
319326
List<LabelValue> labelValues,
320-
String domain) {
327+
String domain,
328+
Map<LabelKey, LabelValue> constantLabels) {
321329
Metric.Builder builder = Metric.newBuilder();
322330
builder.setType(generateType(metricDescriptor.getName(), domain));
323331
Map<String, String> stringTagMap = Maps.newHashMap();
@@ -329,7 +337,12 @@ static Metric createMetric(
329337
}
330338
stringTagMap.put(labelKeys.get(i).getKey(), value);
331339
}
332-
stringTagMap.put(OPENCENSUS_TASK, OPENCENSUS_TASK_VALUE_DEFAULT);
340+
for (Map.Entry<LabelKey, LabelValue> constantLabel : constantLabels.entrySet()) {
341+
String constantLabelKey = constantLabel.getKey().getKey();
342+
String constantLabelValue = constantLabel.getValue().getValue();
343+
constantLabelValue = constantLabelValue == null ? "" : constantLabelValue;
344+
stringTagMap.put(constantLabelKey, constantLabelValue);
345+
}
333346
builder.putAllLabels(stringTagMap);
334347
return builder.build();
335348
}

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfiguration.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
package io.opencensus.exporter.stats.stackdriver;
1818

19+
import static io.opencensus.exporter.stats.stackdriver.StackdriverExportUtils.DEFAULT_CONSTANT_LABELS;
20+
1921
import com.google.api.MonitoredResource;
2022
import com.google.auth.Credentials;
2123
import com.google.auto.value.AutoValue;
2224
import io.opencensus.common.Duration;
25+
import io.opencensus.metrics.LabelKey;
26+
import io.opencensus.metrics.LabelValue;
27+
import java.util.Map;
2328
import javax.annotation.Nullable;
2429
import javax.annotation.concurrent.Immutable;
2530

@@ -79,14 +84,23 @@ public abstract class StackdriverStatsConfiguration {
7984
@Nullable
8085
public abstract String getMetricNamePrefix();
8186

87+
/**
88+
* Returns the constant labels that will be applied to every Stackdriver metric.
89+
*
90+
* @return the constant labels.
91+
* @since 0.21
92+
*/
93+
public abstract Map<LabelKey, LabelValue> getConstantLabels();
94+
8295
/**
8396
* Returns a new {@link Builder}.
8497
*
8598
* @return a {@code Builder}.
8699
* @since 0.11
87100
*/
88101
public static Builder builder() {
89-
return new AutoValue_StackdriverStatsConfiguration.Builder();
102+
return new AutoValue_StackdriverStatsConfiguration.Builder()
103+
.setConstantLabels(DEFAULT_CONSTANT_LABELS);
90104
}
91105

92106
/**
@@ -148,6 +162,26 @@ public abstract static class Builder {
148162
*/
149163
public abstract Builder setMetricNamePrefix(String prefix);
150164

165+
/**
166+
* Sets the constant labels that will be applied to every Stackdriver metric. This default
167+
* ensures that the set of labels together with the default resource (global) are unique to this
168+
* process, as required by stackdriver.
169+
*
170+
* <p>If not set, the exporter will use the "opencensus_task" label.
171+
*
172+
* <p>If you set constant labels, make sure that the monitored resource together with these
173+
* labels is unique to the current process. This is to ensure that there is only a single writer
174+
* to each time series in Stackdriver.
175+
*
176+
* <p>Set constant labels to empty to avoid getting the default "opencensus_task" label. You
177+
* should only do this if you know that the monitored resource uniquely identifies this process.
178+
*
179+
* @param constantLabels constant labels that will be applied to every Stackdriver metric.
180+
* @return this
181+
* @since 0.21
182+
*/
183+
public abstract Builder setConstantLabels(Map<LabelKey, LabelValue> constantLabels);
184+
151185
/**
152186
* Builds a new {@link StackdriverStatsConfiguration} with current settings.
153187
*

0 commit comments

Comments
 (0)