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

Commit

Permalink
feat: Allow users to specify a metric display name prefix, separately…
Browse files Browse the repository at this point in the history
… from the metric name prefix (#2050)
  • Loading branch information
OmerLitov committed Aug 11, 2020
1 parent 5be7044 commit bd852ef
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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)
Expand Down
Expand Up @@ -61,13 +61,16 @@ final class CreateMetricDescriptorExporter extends MetricExporter {
String projectId,
MetricServiceClient metricServiceClient,
@javax.annotation.Nullable String metricNamePrefix,
@javax.annotation.Nullable String displayNamePrefix,
Map<LabelKey, LabelValue> 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;
}
Expand Down
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -94,6 +94,7 @@ private StackdriverStatsExporter(
Duration exportInterval,
MonitoredResource monitoredResource,
@Nullable String metricNamePrefix,
@Nullable String displayNamePrefix,
Map<LabelKey, LabelValue> constantLabels) {
IntervalMetricReader.Options.Builder intervalMetricReaderOptionsBuilder =
IntervalMetricReader.Options.builder();
Expand All @@ -104,6 +105,7 @@ private StackdriverStatsExporter(
projectId,
metricServiceClient,
metricNamePrefix,
displayNamePrefix,
constantLabels,
new CreateTimeSeriesExporter(
projectId,
Expand Down Expand Up @@ -145,6 +147,7 @@ public static void createAndRegisterWithCredentialsAndProjectId(
exportInterval,
DEFAULT_RESOURCE,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
Expand Down Expand Up @@ -183,6 +186,7 @@ public static void createAndRegisterWithProjectId(String projectId, Duration exp
exportInterval,
DEFAULT_RESOURCE,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
Expand Down Expand Up @@ -224,6 +228,7 @@ public static void createAndRegister(StackdriverStatsConfiguration configuration
configuration.getExportInterval(),
configuration.getMonitoredResource(),
configuration.getMetricNamePrefix(),
configuration.getDisplayNamePrefix(),
configuration.getConstantLabels(),
configuration.getDeadline(),
configuration.getMetricServiceStub());
Expand Down Expand Up @@ -291,6 +296,7 @@ public static void createAndRegister(Duration exportInterval) throws IOException
exportInterval,
DEFAULT_RESOURCE,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
Expand Down Expand Up @@ -328,6 +334,7 @@ public static void createAndRegisterWithProjectIdAndMonitoredResource(
exportInterval,
monitoredResource,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
Expand Down Expand Up @@ -365,6 +372,7 @@ public static void createAndRegisterWithMonitoredResource(
exportInterval,
monitoredResource,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
Expand All @@ -377,6 +385,7 @@ private static void createInternal(
Duration exportInterval,
MonitoredResource monitoredResource,
@Nullable String metricNamePrefix,
@Nullable String displayNamePrefix,
Map<LabelKey, LabelValue> constantLabels,
Duration deadline,
@Nullable MetricServiceStub stub)
Expand All @@ -394,6 +403,7 @@ private static void createInternal(
exportInterval,
monitoredResource,
metricNamePrefix,
displayNamePrefix,
constantLabels);
}
}
Expand Down
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -215,6 +217,7 @@ public void doNotExportForEmptyMetrics() {
PROJECT_ID,
new FakeMetricServiceClient(mockStub),
null,
null,
DEFAULT_CONSTANT_LABELS,
fakeMetricExporter);
exporter.export(Collections.<Metric>emptyList());
Expand All @@ -231,6 +234,7 @@ public void doNotExportIfFailedToRegisterMetric() {
PROJECT_ID,
new FakeMetricServiceClient(mockStub),
null,
null,
DEFAULT_CONSTANT_LABELS,
fakeMetricExporter);

Expand All @@ -247,6 +251,7 @@ public void skipDifferentMetricsWithSameName() {
PROJECT_ID,
new FakeMetricServiceClient(mockStub),
null,
null,
DEFAULT_CONSTANT_LABELS,
fakeMetricExporter);
exporter.export(Collections.singletonList(METRIC));
Expand All @@ -266,6 +271,7 @@ public void doNotCreateMetricDescriptorForRegisteredMetric() {
PROJECT_ID,
new FakeMetricServiceClient(mockStub),
null,
null,
DEFAULT_CONSTANT_LABELS,
fakeMetricExporter);
exporter.export(Collections.singletonList(METRIC));
Expand All @@ -285,6 +291,7 @@ public void doNotCreateMetricDescriptorForBuiltInMetric() {
PROJECT_ID,
new FakeMetricServiceClient(mockStub),
null,
null,
DEFAULT_CONSTANT_LABELS,
fakeMetricExporter);
exporter.export(Collections.singletonList(METRIC_4));
Expand Down
Expand Up @@ -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);

Expand All @@ -75,6 +76,7 @@ public void testBuild() {
.setExportInterval(DURATION)
.setMonitoredResource(RESOURCE)
.setMetricNamePrefix(CUSTOM_PREFIX)
.setDisplayNamePrefix(CUSTOM_DISPLAY_PREFIX)
.setConstantLabels(Collections.<LabelKey, LabelValue>emptyMap())
.setDeadline(DURATION)
.setMetricServiceStub(mockStub)
Expand All @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit bd852ef

Please sign in to comment.