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

Commit b94ddf7

Browse files
authored
Exporter/Stackdriver: Do create metric descriptor for custom metrics. (#1866)
* Exporter/Stackdriver: Do create metric descriptor for custom metrics. * Fix Appveyor build failure. * Use endWith instead of contains.
1 parent 6026ee1 commit b94ddf7

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporter.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ final class CreateMetricDescriptorExporter extends MetricExporter {
4444
private static final Logger logger =
4545
Logger.getLogger(CreateMetricDescriptorExporter.class.getName());
4646
private static final ImmutableSet<String> SUPPORTED_EXTERNAL_DOMAINS =
47-
ImmutableSet.<String>of("custom.googleapis.com/", "external.googleapis.com/");
47+
ImmutableSet.<String>of("custom.googleapis.com", "external.googleapis.com");
48+
private static final String GOOGLE_APIS_DOMAIN_SUFFIX = "googleapis.com";
4849

4950
private final String projectId;
5051
private final ProjectName projectName;
@@ -92,7 +93,7 @@ private boolean registerMetricDescriptor(
9293
}
9394
}
9495
registeredMetricDescriptors.put(metricName, metricDescriptor);
95-
if (!isSupportedExternalMetric(metricName)) {
96+
if (isBuiltInMetric(metricName)) {
9697
return true; // skip creating metric descriptor for stackdriver built-in metrics.
9798
}
9899

@@ -153,12 +154,17 @@ public void export(Collection<Metric> metrics) {
153154
nextExporter.export(registeredMetrics);
154155
}
155156

156-
private static boolean isSupportedExternalMetric(String metricName) {
157-
for (String domain : SUPPORTED_EXTERNAL_DOMAINS) {
158-
if (metricName.startsWith(domain)) {
159-
return true;
160-
}
157+
private static boolean isBuiltInMetric(String metricName) {
158+
int domainIndex = metricName.indexOf('/');
159+
if (domainIndex < 0) {
160+
return false;
161+
}
162+
String metricDomain = metricName.substring(0, domainIndex);
163+
if (!metricDomain.endsWith(GOOGLE_APIS_DOMAIN_SUFFIX)) {
164+
return false; // domains like "my.org" are not Stackdriver built-in metrics.
161165
}
162-
return false;
166+
// All googleapis.com domains except "custom.googleapis.com" or "external.googleapis.com"
167+
// are built-in metrics.
168+
return !SUPPORTED_EXTERNAL_DOMAINS.contains(metricDomain);
163169
}
164170
}

exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/CreateMetricDescriptorExporterTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class CreateMetricDescriptorExporterTest {
6060
private static final String METRIC_NAME = CUSTOM_OPENCENSUS_DOMAIN + "my_metric";
6161
private static final String METRIC_NAME_2 = CUSTOM_OPENCENSUS_DOMAIN + "my_metric_2";
6262
private static final String METRIC_NAME_3 = "bigquery.googleapis.com/query/count";
63+
private static final String METRIC_NAME_CUSTOM_DOMAIN = "my.org/my_metric_3";
6364
private static final String METRIC_DESCRIPTION = "metric_description";
6465
private static final String METRIC_DESCRIPTION_2 = "metric_description2";
6566
private static final String METRIC_UNIT = "us";
@@ -86,6 +87,15 @@ public class CreateMetricDescriptorExporterTest {
8687
io.opencensus.metrics.export.MetricDescriptor.create(
8788
METRIC_NAME_3, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_INT64, LABEL_KEY);
8889

90+
// Metric with no domain.
91+
private static final io.opencensus.metrics.export.MetricDescriptor METRIC_DESCRIPTOR_5 =
92+
io.opencensus.metrics.export.MetricDescriptor.create(
93+
METRIC_NAME_CUSTOM_DOMAIN,
94+
METRIC_DESCRIPTION,
95+
METRIC_UNIT,
96+
Type.CUMULATIVE_INT64,
97+
LABEL_KEY);
98+
8999
private static final Value VALUE_LONG = Value.longValue(12345678);
90100
private static final Timestamp TIMESTAMP = Timestamp.fromMillis(3000);
91101
private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(1000);
@@ -102,6 +112,8 @@ public class CreateMetricDescriptorExporterTest {
102112
Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_3, CUMULATIVE_TIME_SERIES);
103113
private static final Metric METRIC_4 =
104114
Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_4, CUMULATIVE_TIME_SERIES);
115+
private static final Metric METRIC_5 =
116+
Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_5, CUMULATIVE_TIME_SERIES);
105117

106118
@Mock private MetricServiceStub mockStub;
107119

@@ -165,6 +177,36 @@ public void export() {
165177
assertThat(fakeMetricExporter.getLastExported()).containsExactly(METRIC, METRIC_2);
166178
}
167179

180+
@Test
181+
public void export_MetricNameWithCustomDomain() {
182+
FakeMetricExporter fakeMetricExporter = new FakeMetricExporter();
183+
CreateMetricDescriptorExporter exporter =
184+
new CreateMetricDescriptorExporter(
185+
PROJECT_ID,
186+
new FakeMetricServiceClient(mockStub),
187+
null,
188+
DEFAULT_CONSTANT_LABELS,
189+
fakeMetricExporter);
190+
exporter.export(Arrays.asList(METRIC_5));
191+
192+
verify(mockStub, times(1)).createMetricDescriptorCallable();
193+
194+
MetricDescriptor descriptor =
195+
StackdriverExportUtils.createMetricDescriptor(
196+
METRIC_DESCRIPTOR_5,
197+
PROJECT_ID,
198+
CUSTOM_OPENCENSUS_DOMAIN,
199+
DEFAULT_DISPLAY_NAME_PREFIX,
200+
DEFAULT_CONSTANT_LABELS);
201+
verify(mockCreateMetricDescriptorCallable, times(1))
202+
.call(
203+
eq(
204+
CreateMetricDescriptorRequest.newBuilder()
205+
.setName("projects/" + PROJECT_ID)
206+
.setMetricDescriptor(descriptor)
207+
.build()));
208+
}
209+
168210
@Test
169211
public void doNotExportForEmptyMetrics() {
170212
FakeMetricExporter fakeMetricExporter = new FakeMetricExporter();

exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceConfigurationTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,16 @@ public void allowToUseDefaultProjectId() {
9898

9999
@Test
100100
public void disallowNullFixedAttributes() {
101-
StackdriverTraceConfiguration.Builder builder = StackdriverTraceConfiguration.builder();
101+
StackdriverTraceConfiguration.Builder builder =
102+
StackdriverTraceConfiguration.builder().setProjectId("test");
102103
thrown.expect(NullPointerException.class);
103104
builder.setFixedAttributes(null);
104105
}
105106

106107
@Test
107108
public void disallowNullFixedAttributeKey() {
108-
StackdriverTraceConfiguration.Builder builder = StackdriverTraceConfiguration.builder();
109+
StackdriverTraceConfiguration.Builder builder =
110+
StackdriverTraceConfiguration.builder().setProjectId("test");
109111
Map<String, AttributeValue> attributes =
110112
Collections.singletonMap(null, AttributeValue.stringAttributeValue("val"));
111113
builder.setFixedAttributes(attributes);
@@ -115,7 +117,8 @@ public void disallowNullFixedAttributeKey() {
115117

116118
@Test
117119
public void disallowNullFixedAttributeValue() {
118-
StackdriverTraceConfiguration.Builder builder = StackdriverTraceConfiguration.builder();
120+
StackdriverTraceConfiguration.Builder builder =
121+
StackdriverTraceConfiguration.builder().setProjectId("test");
119122
Map<String, AttributeValue> attributes = Collections.singletonMap("key", null);
120123
builder.setFixedAttributes(attributes);
121124
thrown.expect(NullPointerException.class);

0 commit comments

Comments
 (0)