use of com.google.api.gax.rpc.ApiException 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.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.
the class PubSubAdminTests method testGetTopic_serviceDown.
@Test
public void testGetTopic_serviceDown() {
when(this.mockTopicAdminClient.getTopic(any(TopicName.class))).thenThrow(new ApiException(null, GrpcStatusCode.of(io.grpc.Status.Code.UNAVAILABLE), false));
PubSubAdmin psa = new PubSubAdmin(() -> "test-project", this.mockTopicAdminClient, this.mockSubscriptionAdminClient);
assertThatExceptionOfType(ApiException.class).isThrownBy(() -> psa.getTopic("fooTopic"));
verify(this.mockTopicAdminClient).getTopic(TopicName.of("test-project", "fooTopic"));
}
use of com.google.api.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.
the class PubSubAdminTests method testGetTopic_notFound.
@Test
public void testGetTopic_notFound() {
when(this.mockTopicAdminClient.getTopic(any(TopicName.class))).thenThrow(new ApiException(null, GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), false));
assertThat(new PubSubAdmin(() -> "test-project", this.mockTopicAdminClient, this.mockSubscriptionAdminClient).getTopic("fooTopic")).isNull();
verify(this.mockTopicAdminClient).getTopic(TopicName.of("test-project", "fooTopic"));
}
use of com.google.api.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.
the class PubSubHealthIndicatorTests method healthDown.
@Test
public void healthDown() {
when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())).thenThrow(new ApiException(new IllegalStateException("Illegal State"), GrpcStatusCode.of(io.grpc.Status.Code.INVALID_ARGUMENT), false));
PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate);
assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.DOWN);
}
use of com.google.api.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.
the class PubSubHealthIndicatorTests method healthUpFor404.
@Test
public void healthUpFor404() throws Exception {
when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())).thenThrow(new ApiException(new IllegalStateException("Illegal State"), GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), false));
PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate);
assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.UP);
}
Aggregations