diff --git a/build.gradle b/build.gradle index 13bbc7096a..e2fab21a6a 100644 --- a/build.gradle +++ b/build.gradle @@ -339,6 +339,8 @@ subprojects { txtOutputFile = file("${project.buildDir}/reports/japi.txt") ignoreMissingClasses = true includeSynthetic = true + // TODO remove methodExcludes when gh-3181 is resolved + methodExcludes = ['io.micrometer.stackdriver.StackdriverConfig#metricTypePrefix()'] onlyIf { compatibleVersion != 'SKIP' } } diff --git a/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverConfig.java b/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverConfig.java index 50c138d2de..2a6a7c3d6b 100644 --- a/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverConfig.java +++ b/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverConfig.java @@ -83,6 +83,16 @@ default boolean useSemanticMetricTypes() { return getBoolean(this, "useSemanticMetricTypes").orElse(false); } + /** + * Available prefixes defined in + * Google + * Cloud documentation + * @return a prefix for MetricType + */ + default String metricTypePrefix() { + return getString(this, "metricTypePrefix").orElse("custom.googleapis.com/"); + } + /** * Return {@link CredentialsProvider} to use. * @return {@code CredentialsProvider} to use diff --git a/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverMeterRegistry.java b/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverMeterRegistry.java index ad3e9fc96a..edae0c5b60 100644 --- a/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverMeterRegistry.java +++ b/implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverMeterRegistry.java @@ -426,7 +426,7 @@ private void prePopulateVerifiedDescriptors() { } private String metricType(Meter.Id id, @Nullable String statistic) { - StringBuilder metricType = new StringBuilder("custom.googleapis.com/").append(getConventionName(id)); + StringBuilder metricType = new StringBuilder(config.metricTypePrefix()).append(getConventionName(id)); if (statistic != null) { metricType.append('/').append(statistic); } diff --git a/implementations/micrometer-registry-stackdriver/src/test/java/io/micrometer/stackdriver/MetricTypePrefixTest.java b/implementations/micrometer-registry-stackdriver/src/test/java/io/micrometer/stackdriver/MetricTypePrefixTest.java new file mode 100644 index 0000000000..de19463995 --- /dev/null +++ b/implementations/micrometer-registry-stackdriver/src/test/java/io/micrometer/stackdriver/MetricTypePrefixTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2022 VMware, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micrometer.stackdriver; + +import com.google.monitoring.v3.TimeSeries; +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.MockClock; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MetricTypePrefixTest { + + private final Map config = new HashMap<>( + Collections.singletonMap("stackdriver.projectId", "projectId")); + + private final StackdriverMeterRegistry meterRegistry = new StackdriverMeterRegistry(config::get, new MockClock()); + + @Test + void metricTypePrefixWhenNotConfigured() { + StackdriverMeterRegistry.Batch batch = meterRegistry.new Batch(); + List timeSeries = meterRegistry + .createCounter(batch, Counter.builder("counter").register(meterRegistry)).collect(Collectors.toList()); + assertThat(timeSeries).hasSize(1); + assertThat(timeSeries.get(0).getMetric().getType()).isEqualTo("custom.googleapis.com/counter"); + } + + @Test + void metricTypePrefixWhenConfigured() { + config.put("stackdriver.metricTypePrefix", "external.googleapis.com/user/"); + + StackdriverMeterRegistry.Batch batch = meterRegistry.new Batch(); + List timeSeries = meterRegistry + .createCounter(batch, Counter.builder("counter").register(meterRegistry)).collect(Collectors.toList()); + assertThat(timeSeries).hasSize(1); + assertThat(timeSeries.get(0).getMetric().getType()).isEqualTo("external.googleapis.com/user/counter"); + } + +}