From bd852ef85134258c3f22d85f35dcc2e3e87e30bf Mon Sep 17 00:00:00 2001 From: OmerLitov Date: Tue, 11 Aug 2020 16:32:10 +0300 Subject: [PATCH] feat: Allow users to specify a metric display name prefix, separately from the metric name prefix (#2050) --- CHANGELOG.md | 2 +- .../CreateMetricDescriptorExporter.java | 5 ++++- .../StackdriverStatsConfiguration.java | 18 ++++++++++++++++++ .../stackdriver/StackdriverStatsExporter.java | 10 ++++++++++ .../CreateMetricDescriptorExporterTest.java | 7 +++++++ .../StackdriverStatsConfigurationTest.java | 13 +++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08cdac2509..8d4aa6cbd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## Unreleased - +- feat: Allow users to specify a metric display name prefix, separately from the metric name prefix ## 0.26.0 - 2020-03-19 - feat: Allow users to register the same Meter multiple times without exception (#2017) diff --git a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporter.java b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporter.java index c857a47914..f247a6f479 100644 --- a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporter.java +++ b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporter.java @@ -61,13 +61,16 @@ final class CreateMetricDescriptorExporter extends MetricExporter { String projectId, MetricServiceClient metricServiceClient, @javax.annotation.Nullable String metricNamePrefix, + @javax.annotation.Nullable String displayNamePrefix, Map constantLabels, MetricExporter nextExporter) { this.projectId = projectId; projectName = ProjectName.newBuilder().setProject(projectId).build(); this.metricServiceClient = metricServiceClient; this.domain = StackdriverExportUtils.getDomain(metricNamePrefix); - this.displayNamePrefix = StackdriverExportUtils.getDisplayNamePrefix(metricNamePrefix); + this.displayNamePrefix = + StackdriverExportUtils.getDisplayNamePrefix( + displayNamePrefix == null ? metricNamePrefix : displayNamePrefix); this.constantLabels = constantLabels; this.nextExporter = nextExporter; } diff --git a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfiguration.java b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfiguration.java index 5b8cefca01..63598b971b 100644 --- a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfiguration.java +++ b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfiguration.java @@ -94,6 +94,15 @@ public abstract class StackdriverStatsConfiguration { @Nullable public abstract String getMetricNamePrefix(); + /** + * Returns the display name prefix for Stackdriver metrics. + * + * @return the metric display name prefix. + * @since 0.27 + */ + @Nullable + public abstract String getDisplayNamePrefix(); + /** * Returns the constant labels that will be applied to every Stackdriver metric. * @@ -198,6 +207,15 @@ public abstract static class Builder { */ public abstract Builder setMetricNamePrefix(String prefix); + /** + * Sets the the display name prefix for Stackdriver metrics. + * + * @param prefix the metric display name prefix. + * @return this. + * @since 0.27 + */ + public abstract Builder setDisplayNamePrefix(String prefix); + /** * Sets the constant labels that will be applied to every Stackdriver metric. This default * ensures that the set of labels together with the default resource (global) are unique to this diff --git a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsExporter.java b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsExporter.java index e7825a8a8c..d68019a399 100644 --- a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsExporter.java +++ b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsExporter.java @@ -94,6 +94,7 @@ private StackdriverStatsExporter( Duration exportInterval, MonitoredResource monitoredResource, @Nullable String metricNamePrefix, + @Nullable String displayNamePrefix, Map constantLabels) { IntervalMetricReader.Options.Builder intervalMetricReaderOptionsBuilder = IntervalMetricReader.Options.builder(); @@ -104,6 +105,7 @@ private StackdriverStatsExporter( projectId, metricServiceClient, metricNamePrefix, + displayNamePrefix, constantLabels, new CreateTimeSeriesExporter( projectId, @@ -145,6 +147,7 @@ public static void createAndRegisterWithCredentialsAndProjectId( exportInterval, DEFAULT_RESOURCE, null, + null, DEFAULT_CONSTANT_LABELS, DEFAULT_DEADLINE, null); @@ -183,6 +186,7 @@ public static void createAndRegisterWithProjectId(String projectId, Duration exp exportInterval, DEFAULT_RESOURCE, null, + null, DEFAULT_CONSTANT_LABELS, DEFAULT_DEADLINE, null); @@ -224,6 +228,7 @@ public static void createAndRegister(StackdriverStatsConfiguration configuration configuration.getExportInterval(), configuration.getMonitoredResource(), configuration.getMetricNamePrefix(), + configuration.getDisplayNamePrefix(), configuration.getConstantLabels(), configuration.getDeadline(), configuration.getMetricServiceStub()); @@ -291,6 +296,7 @@ public static void createAndRegister(Duration exportInterval) throws IOException exportInterval, DEFAULT_RESOURCE, null, + null, DEFAULT_CONSTANT_LABELS, DEFAULT_DEADLINE, null); @@ -328,6 +334,7 @@ public static void createAndRegisterWithProjectIdAndMonitoredResource( exportInterval, monitoredResource, null, + null, DEFAULT_CONSTANT_LABELS, DEFAULT_DEADLINE, null); @@ -365,6 +372,7 @@ public static void createAndRegisterWithMonitoredResource( exportInterval, monitoredResource, null, + null, DEFAULT_CONSTANT_LABELS, DEFAULT_DEADLINE, null); @@ -377,6 +385,7 @@ private static void createInternal( Duration exportInterval, MonitoredResource monitoredResource, @Nullable String metricNamePrefix, + @Nullable String displayNamePrefix, Map constantLabels, Duration deadline, @Nullable MetricServiceStub stub) @@ -394,6 +403,7 @@ private static void createInternal( exportInterval, monitoredResource, metricNamePrefix, + displayNamePrefix, constantLabels); } } diff --git a/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporterTest.java b/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporterTest.java index a783c72781..a508217ad8 100644 --- a/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporterTest.java +++ b/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporterTest.java @@ -139,6 +139,7 @@ public void export() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); exporter.export(Arrays.asList(METRIC, METRIC_2)); @@ -185,6 +186,7 @@ public void export_MetricNameWithCustomDomain() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); exporter.export(Arrays.asList(METRIC_5)); @@ -215,6 +217,7 @@ public void doNotExportForEmptyMetrics() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); exporter.export(Collections.emptyList()); @@ -231,6 +234,7 @@ public void doNotExportIfFailedToRegisterMetric() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); @@ -247,6 +251,7 @@ public void skipDifferentMetricsWithSameName() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); exporter.export(Collections.singletonList(METRIC)); @@ -266,6 +271,7 @@ public void doNotCreateMetricDescriptorForRegisteredMetric() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); exporter.export(Collections.singletonList(METRIC)); @@ -285,6 +291,7 @@ public void doNotCreateMetricDescriptorForBuiltInMetric() { PROJECT_ID, new FakeMetricServiceClient(mockStub), null, + null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter); exporter.export(Collections.singletonList(METRIC_4)); diff --git a/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfigurationTest.java b/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfigurationTest.java index bc82462ef5..cde84d795d 100644 --- a/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfigurationTest.java +++ b/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverStatsConfigurationTest.java @@ -56,6 +56,7 @@ public class StackdriverStatsConfigurationTest { .putLabels("instance-id", "instance") .build(); private static final String CUSTOM_PREFIX = "myorg"; + private static final String CUSTOM_DISPLAY_PREFIX = "display-prefix"; @Mock private final MetricServiceStub mockStub = Mockito.mock(MetricServiceStub.class); @@ -75,6 +76,7 @@ public void testBuild() { .setExportInterval(DURATION) .setMonitoredResource(RESOURCE) .setMetricNamePrefix(CUSTOM_PREFIX) + .setDisplayNamePrefix(CUSTOM_DISPLAY_PREFIX) .setConstantLabels(Collections.emptyMap()) .setDeadline(DURATION) .setMetricServiceStub(mockStub) @@ -84,6 +86,7 @@ public void testBuild() { assertThat(configuration.getExportInterval()).isEqualTo(DURATION); assertThat(configuration.getMonitoredResource()).isEqualTo(RESOURCE); assertThat(configuration.getMetricNamePrefix()).isEqualTo(CUSTOM_PREFIX); + assertThat(configuration.getDisplayNamePrefix()).isEqualTo(CUSTOM_DISPLAY_PREFIX); assertThat(configuration.getConstantLabels()).isEmpty(); assertThat(configuration.getDeadline()).isEqualTo(DURATION); assertThat(configuration.getMetricServiceStub()).isEqualTo(mockStub); @@ -197,6 +200,16 @@ public void allowNullMetricPrefix() { assertThat(configuration.getMetricNamePrefix()).isNull(); } + @Test + public void allowNullDisplayPrefix() { + StackdriverStatsConfiguration configuration = + StackdriverStatsConfiguration.builder() + .setProjectId(PROJECT_ID) + .setDisplayNamePrefix(null) + .build(); + assertThat(configuration.getMetricNamePrefix()).isNull(); + } + @Test public void disallowZeroDuration() { StackdriverStatsConfiguration.Builder builder =