use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class StackdriverExportUtilsTest method createMetricDescriptor_WithCustomConstantLabels.
@Test
public void createMetricDescriptor_WithCustomConstantLabels() {
Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(LabelKey.create("my_key", "desc"), LabelValue.create("value"));
MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, "custom.googleapis.com/myorg/", "myorg/", constantLabels);
assertThat(metricDescriptor.getLabelsList()).containsExactly(LabelDescriptor.newBuilder().setKey(LABEL_KEY.get(0).getKey()).setDescription(LABEL_KEY.get(0).getDescription()).setValueType(ValueType.STRING).build(), LabelDescriptor.newBuilder().setKey("my_key").setDescription("desc").setValueType(ValueType.STRING).build());
}
use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class CreateMetricDescriptorExporterTest method export_MetricNameWithCustomDomain.
@Test
public void export_MetricNameWithCustomDomain() {
FakeMetricExporter fakeMetricExporter = new FakeMetricExporter();
CreateMetricDescriptorExporter exporter = new CreateMetricDescriptorExporter(PROJECT_ID, new FakeMetricServiceClient(mockStub), null, null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter);
exporter.export(Arrays.asList(METRIC_5));
verify(mockStub, times(1)).createMetricDescriptorCallable();
MetricDescriptor descriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR_5, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
verify(mockCreateMetricDescriptorCallable, times(1)).call(eq(CreateMetricDescriptorRequest.newBuilder().setName("projects/" + PROJECT_ID).setMetricDescriptor(descriptor).build()));
}
use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class CreateMetricDescriptorExporterTest method export.
@Test
public void export() {
FakeMetricExporter fakeMetricExporter = new FakeMetricExporter();
CreateMetricDescriptorExporter exporter = new CreateMetricDescriptorExporter(PROJECT_ID, new FakeMetricServiceClient(mockStub), null, null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter);
exporter.export(Arrays.asList(METRIC, METRIC_2));
verify(mockStub, times(2)).createMetricDescriptorCallable();
MetricDescriptor descriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
verify(mockCreateMetricDescriptorCallable, times(1)).call(eq(CreateMetricDescriptorRequest.newBuilder().setName("projects/" + PROJECT_ID).setMetricDescriptor(descriptor).build()));
MetricDescriptor descriptor2 = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR_2, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
verify(mockCreateMetricDescriptorCallable, times(1)).call(eq(CreateMetricDescriptorRequest.newBuilder().setName("projects/" + PROJECT_ID).setMetricDescriptor(descriptor2).build()));
assertThat(fakeMetricExporter.getLastExported()).containsExactly(METRIC, METRIC_2);
}
use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class CreateMetricDescriptorExporter method registerMetricDescriptor.
// Returns true if the given metricDescriptor is successfully registered to Stackdriver
// Monitoring, or the
// exact same metric has already been registered. Returns false otherwise.
private boolean registerMetricDescriptor(io.opencensus.metrics.export.MetricDescriptor metricDescriptor) {
String metricName = metricDescriptor.getName();
io.opencensus.metrics.export.MetricDescriptor existingMetricDescriptor = registeredMetricDescriptors.get(metricName);
if (existingMetricDescriptor != null) {
if (existingMetricDescriptor.equals(metricDescriptor)) {
// Ignore metricDescriptor that are already registered.
return true;
} else {
logger.log(Level.WARNING, "A different metric with the same name is already registered: " + existingMetricDescriptor);
return false;
}
}
registeredMetricDescriptors.put(metricName, metricDescriptor);
if (isBuiltInMetric(metricName)) {
// skip creating metric descriptor for stackdriver built-in metrics.
return true;
}
Span span = tracer.getCurrentSpan();
span.addAnnotation("Create Stackdriver Metric.");
MetricDescriptor stackDriverMetricDescriptor = StackdriverExportUtils.createMetricDescriptor(metricDescriptor, projectId, domain, displayNamePrefix, constantLabels);
CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(projectName.toString()).setMetricDescriptor(stackDriverMetricDescriptor).build();
try {
metricServiceClient.createMetricDescriptor(request);
span.addAnnotation("Finish creating MetricDescriptor.");
return true;
} catch (ApiException e) {
logger.log(Level.WARNING, "ApiException thrown when creating MetricDescriptor.", e);
span.setStatus(Status.CanonicalCode.valueOf(e.getStatusCode().getCode().name()).toStatus().withDescription("ApiException thrown when creating MetricDescriptor: " + StackdriverExportUtils.exceptionMessage(e)));
return false;
} catch (Throwable e) {
logger.log(Level.WARNING, "Exception thrown when creating MetricDescriptor.", e);
span.setStatus(Status.UNKNOWN.withDescription("Exception thrown when creating MetricDescriptor: " + StackdriverExportUtils.exceptionMessage(e)));
return false;
}
}
use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class StackdriverExportUtils method createMetricDescriptor.
// Convert a OpenCensus MetricDescriptor to a StackDriver MetricDescriptor
static MetricDescriptor createMetricDescriptor(io.opencensus.metrics.export.MetricDescriptor metricDescriptor, String projectId, String domain, String displayNamePrefix, Map<LabelKey, LabelValue> constantLabels) {
MetricDescriptor.Builder builder = MetricDescriptor.newBuilder();
String type = generateType(metricDescriptor.getName(), domain);
// Name format refers to
// cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create
builder.setName("projects/" + projectId + "/metricDescriptors/" + type);
builder.setType(type);
builder.setDescription(metricDescriptor.getDescription());
builder.setDisplayName(createDisplayName(metricDescriptor.getName(), displayNamePrefix));
for (LabelKey labelKey : metricDescriptor.getLabelKeys()) {
builder.addLabels(createLabelDescriptor(labelKey));
}
for (LabelKey labelKey : constantLabels.keySet()) {
builder.addLabels(createLabelDescriptor(labelKey));
}
builder.setUnit(metricDescriptor.getUnit());
builder.setMetricKind(createMetricKind(metricDescriptor.getType()));
builder.setValueType(createValueType(metricDescriptor.getType()));
return builder.build();
}
Aggregations