From 14b05fe95af9fa6561a944cf82ac6ce21ec6598d Mon Sep 17 00:00:00 2001 From: rafal-dudek <32459286+rafal-dudek@users.noreply.github.com> Date: Mon, 16 May 2022 10:43:11 +0200 Subject: [PATCH] MetricType prefix config for StackdriverMeterRegistry (#3175) Instead of hard-coding the MetricType prefix, this makes it configurable, as there are multiple options for valid prefixes, and different users may want to use different ones. The default is kept the same as before for backwards compatibility reasons. Resolves gh-3171 Co-authored-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com> --- build.gradle | 2 + .../stackdriver/StackdriverConfig.java | 10 ++++ .../stackdriver/StackdriverMeterRegistry.java | 2 +- .../stackdriver/MetricTypePrefixTest.java | 58 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 implementations/micrometer-registry-stackdriver/src/test/java/io/micrometer/stackdriver/MetricTypePrefixTest.java 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"); + } + +}