Skip to content

Commit

Permalink
MetricType prefix config for StackdriverMeterRegistry (#3175)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rafal-dudek and shakuzen authored May 16, 2022
1 parent 15e4391 commit 14b05fe
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ default boolean useSemanticMetricTypes() {
return getBoolean(this, "useSemanticMetricTypes").orElse(false);
}

/**
* Available prefixes defined in
* <a href= "https://cloud.google.com/monitoring/custom-metrics#identifier">Google
* Cloud documentation</a>
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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> 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> 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");
}

}

0 comments on commit 14b05fe

Please sign in to comment.