Search in sources :

Example 26 with ApiException

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;
    }
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest) Span(io.opencensus.trace.Span) ApiException(com.google.api.gax.rpc.ApiException)

Example 27 with ApiException

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"));
}
Also used : TopicName(com.google.pubsub.v1.TopicName) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 28 with ApiException

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"));
}
Also used : TopicName(com.google.pubsub.v1.TopicName) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 29 with ApiException

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);
}
Also used : PubSubHealthIndicator(org.springframework.cloud.gcp.autoconfigure.pubsub.health.PubSubHealthIndicator) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 30 with ApiException

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);
}
Also used : PubSubHealthIndicator(org.springframework.cloud.gcp.autoconfigure.pubsub.health.PubSubHealthIndicator) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Aggregations

ApiException (com.google.api.gax.rpc.ApiException)30 Test (org.junit.Test)21 IOException (java.io.IOException)5 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 StatusCode (com.google.api.gax.rpc.StatusCode)3 Publisher (com.google.cloud.pubsub.v1.Publisher)3 CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)3 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)3 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)3 RpcReadAttempt (org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcReadAttempt)3 Code (com.google.api.gax.rpc.StatusCode.Code)2 Offset (com.google.cloud.pubsublite.Offset)2 Write (com.google.firestore.v1.Write)2 ByteString (com.google.protobuf.ByteString)2 TopicName (com.google.pubsub.v1.TopicName)2 Span (io.opencensus.trace.Span)2 FirestoreProtoHelpers.newWrite (org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite)2 WriteElement (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement)2 SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)2 Instant (org.joda.time.Instant)2