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

Commit bd852ef

Browse files
authored
feat: Allow users to specify a metric display name prefix, separately from the metric name prefix (#2050)
1 parent 5be7044 commit bd852ef

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Unreleased
2-
2+
- feat: Allow users to specify a metric display name prefix, separately from the metric name prefix
33

44
## 0.26.0 - 2020-03-19
55
- feat: Allow users to register the same Meter multiple times without exception (#2017)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ final class CreateMetricDescriptorExporter extends MetricExporter {
6161
String projectId,
6262
MetricServiceClient metricServiceClient,
6363
@javax.annotation.Nullable String metricNamePrefix,
64+
@javax.annotation.Nullable String displayNamePrefix,
6465
Map<LabelKey, LabelValue> constantLabels,
6566
MetricExporter nextExporter) {
6667
this.projectId = projectId;
6768
projectName = ProjectName.newBuilder().setProject(projectId).build();
6869
this.metricServiceClient = metricServiceClient;
6970
this.domain = StackdriverExportUtils.getDomain(metricNamePrefix);
70-
this.displayNamePrefix = StackdriverExportUtils.getDisplayNamePrefix(metricNamePrefix);
71+
this.displayNamePrefix =
72+
StackdriverExportUtils.getDisplayNamePrefix(
73+
displayNamePrefix == null ? metricNamePrefix : displayNamePrefix);
7174
this.constantLabels = constantLabels;
7275
this.nextExporter = nextExporter;
7376
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public abstract class StackdriverStatsConfiguration {
9494
@Nullable
9595
public abstract String getMetricNamePrefix();
9696

97+
/**
98+
* Returns the display name prefix for Stackdriver metrics.
99+
*
100+
* @return the metric display name prefix.
101+
* @since 0.27
102+
*/
103+
@Nullable
104+
public abstract String getDisplayNamePrefix();
105+
97106
/**
98107
* Returns the constant labels that will be applied to every Stackdriver metric.
99108
*
@@ -198,6 +207,15 @@ public abstract static class Builder {
198207
*/
199208
public abstract Builder setMetricNamePrefix(String prefix);
200209

210+
/**
211+
* Sets the the display name prefix for Stackdriver metrics.
212+
*
213+
* @param prefix the metric display name prefix.
214+
* @return this.
215+
* @since 0.27
216+
*/
217+
public abstract Builder setDisplayNamePrefix(String prefix);
218+
201219
/**
202220
* Sets the constant labels that will be applied to every Stackdriver metric. This default
203221
* ensures that the set of labels together with the default resource (global) are unique to this

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private StackdriverStatsExporter(
9494
Duration exportInterval,
9595
MonitoredResource monitoredResource,
9696
@Nullable String metricNamePrefix,
97+
@Nullable String displayNamePrefix,
9798
Map<LabelKey, LabelValue> constantLabels) {
9899
IntervalMetricReader.Options.Builder intervalMetricReaderOptionsBuilder =
99100
IntervalMetricReader.Options.builder();
@@ -104,6 +105,7 @@ private StackdriverStatsExporter(
104105
projectId,
105106
metricServiceClient,
106107
metricNamePrefix,
108+
displayNamePrefix,
107109
constantLabels,
108110
new CreateTimeSeriesExporter(
109111
projectId,
@@ -145,6 +147,7 @@ public static void createAndRegisterWithCredentialsAndProjectId(
145147
exportInterval,
146148
DEFAULT_RESOURCE,
147149
null,
150+
null,
148151
DEFAULT_CONSTANT_LABELS,
149152
DEFAULT_DEADLINE,
150153
null);
@@ -183,6 +186,7 @@ public static void createAndRegisterWithProjectId(String projectId, Duration exp
183186
exportInterval,
184187
DEFAULT_RESOURCE,
185188
null,
189+
null,
186190
DEFAULT_CONSTANT_LABELS,
187191
DEFAULT_DEADLINE,
188192
null);
@@ -224,6 +228,7 @@ public static void createAndRegister(StackdriverStatsConfiguration configuration
224228
configuration.getExportInterval(),
225229
configuration.getMonitoredResource(),
226230
configuration.getMetricNamePrefix(),
231+
configuration.getDisplayNamePrefix(),
227232
configuration.getConstantLabels(),
228233
configuration.getDeadline(),
229234
configuration.getMetricServiceStub());
@@ -291,6 +296,7 @@ public static void createAndRegister(Duration exportInterval) throws IOException
291296
exportInterval,
292297
DEFAULT_RESOURCE,
293298
null,
299+
null,
294300
DEFAULT_CONSTANT_LABELS,
295301
DEFAULT_DEADLINE,
296302
null);
@@ -328,6 +334,7 @@ public static void createAndRegisterWithProjectIdAndMonitoredResource(
328334
exportInterval,
329335
monitoredResource,
330336
null,
337+
null,
331338
DEFAULT_CONSTANT_LABELS,
332339
DEFAULT_DEADLINE,
333340
null);
@@ -365,6 +372,7 @@ public static void createAndRegisterWithMonitoredResource(
365372
exportInterval,
366373
monitoredResource,
367374
null,
375+
null,
368376
DEFAULT_CONSTANT_LABELS,
369377
DEFAULT_DEADLINE,
370378
null);
@@ -377,6 +385,7 @@ private static void createInternal(
377385
Duration exportInterval,
378386
MonitoredResource monitoredResource,
379387
@Nullable String metricNamePrefix,
388+
@Nullable String displayNamePrefix,
380389
Map<LabelKey, LabelValue> constantLabels,
381390
Duration deadline,
382391
@Nullable MetricServiceStub stub)
@@ -394,6 +403,7 @@ private static void createInternal(
394403
exportInterval,
395404
monitoredResource,
396405
metricNamePrefix,
406+
displayNamePrefix,
397407
constantLabels);
398408
}
399409
}

exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public void export() {
139139
PROJECT_ID,
140140
new FakeMetricServiceClient(mockStub),
141141
null,
142+
null,
142143
DEFAULT_CONSTANT_LABELS,
143144
fakeMetricExporter);
144145
exporter.export(Arrays.asList(METRIC, METRIC_2));
@@ -185,6 +186,7 @@ public void export_MetricNameWithCustomDomain() {
185186
PROJECT_ID,
186187
new FakeMetricServiceClient(mockStub),
187188
null,
189+
null,
188190
DEFAULT_CONSTANT_LABELS,
189191
fakeMetricExporter);
190192
exporter.export(Arrays.asList(METRIC_5));
@@ -215,6 +217,7 @@ public void doNotExportForEmptyMetrics() {
215217
PROJECT_ID,
216218
new FakeMetricServiceClient(mockStub),
217219
null,
220+
null,
218221
DEFAULT_CONSTANT_LABELS,
219222
fakeMetricExporter);
220223
exporter.export(Collections.<Metric>emptyList());
@@ -231,6 +234,7 @@ public void doNotExportIfFailedToRegisterMetric() {
231234
PROJECT_ID,
232235
new FakeMetricServiceClient(mockStub),
233236
null,
237+
null,
234238
DEFAULT_CONSTANT_LABELS,
235239
fakeMetricExporter);
236240

@@ -247,6 +251,7 @@ public void skipDifferentMetricsWithSameName() {
247251
PROJECT_ID,
248252
new FakeMetricServiceClient(mockStub),
249253
null,
254+
null,
250255
DEFAULT_CONSTANT_LABELS,
251256
fakeMetricExporter);
252257
exporter.export(Collections.singletonList(METRIC));
@@ -266,6 +271,7 @@ public void doNotCreateMetricDescriptorForRegisteredMetric() {
266271
PROJECT_ID,
267272
new FakeMetricServiceClient(mockStub),
268273
null,
274+
null,
269275
DEFAULT_CONSTANT_LABELS,
270276
fakeMetricExporter);
271277
exporter.export(Collections.singletonList(METRIC));
@@ -285,6 +291,7 @@ public void doNotCreateMetricDescriptorForBuiltInMetric() {
285291
PROJECT_ID,
286292
new FakeMetricServiceClient(mockStub),
287293
null,
294+
null,
288295
DEFAULT_CONSTANT_LABELS,
289296
fakeMetricExporter);
290297
exporter.export(Collections.singletonList(METRIC_4));

exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfigurationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class StackdriverStatsConfigurationTest {
5656
.putLabels("instance-id", "instance")
5757
.build();
5858
private static final String CUSTOM_PREFIX = "myorg";
59+
private static final String CUSTOM_DISPLAY_PREFIX = "display-prefix";
5960

6061
@Mock private final MetricServiceStub mockStub = Mockito.mock(MetricServiceStub.class);
6162

@@ -75,6 +76,7 @@ public void testBuild() {
7576
.setExportInterval(DURATION)
7677
.setMonitoredResource(RESOURCE)
7778
.setMetricNamePrefix(CUSTOM_PREFIX)
79+
.setDisplayNamePrefix(CUSTOM_DISPLAY_PREFIX)
7880
.setConstantLabels(Collections.<LabelKey, LabelValue>emptyMap())
7981
.setDeadline(DURATION)
8082
.setMetricServiceStub(mockStub)
@@ -84,6 +86,7 @@ public void testBuild() {
8486
assertThat(configuration.getExportInterval()).isEqualTo(DURATION);
8587
assertThat(configuration.getMonitoredResource()).isEqualTo(RESOURCE);
8688
assertThat(configuration.getMetricNamePrefix()).isEqualTo(CUSTOM_PREFIX);
89+
assertThat(configuration.getDisplayNamePrefix()).isEqualTo(CUSTOM_DISPLAY_PREFIX);
8790
assertThat(configuration.getConstantLabels()).isEmpty();
8891
assertThat(configuration.getDeadline()).isEqualTo(DURATION);
8992
assertThat(configuration.getMetricServiceStub()).isEqualTo(mockStub);
@@ -197,6 +200,16 @@ public void allowNullMetricPrefix() {
197200
assertThat(configuration.getMetricNamePrefix()).isNull();
198201
}
199202

203+
@Test
204+
public void allowNullDisplayPrefix() {
205+
StackdriverStatsConfiguration configuration =
206+
StackdriverStatsConfiguration.builder()
207+
.setProjectId(PROJECT_ID)
208+
.setDisplayNamePrefix(null)
209+
.build();
210+
assertThat(configuration.getMetricNamePrefix()).isNull();
211+
}
212+
200213
@Test
201214
public void disallowZeroDuration() {
202215
StackdriverStatsConfiguration.Builder builder =

0 commit comments

Comments
 (0)