Search in sources :

Example 1 with IngestServiceStub

use of com.newrelic.trace.v1.IngestServiceGrpc.IngestServiceStub in project newrelic-java-agent by newrelic.

the class ChannelManager method getSpanObserver.

/**
 * Obtain a span observer. Creates a channel if one is not open. Creates a span observer if one
 * does not exist. If the channel has been shutdown and is backing off via
 * {@link #shutdownChannelAndBackoff(int)}, awaits the backoff period before recreating the channel.
 *
 * @return a span observer
 */
ClientCallStreamObserver<V1.Span> getSpanObserver() {
    // Obtain the lock, and await the backoff if in progress
    CountDownLatch latch;
    synchronized (lock) {
        latch = backoffLatch;
    }
    if (latch != null) {
        try {
            logger.log(Level.FINE, "Awaiting backoff.");
            latch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException("Thread interrupted while awaiting backoff.");
        }
    }
    // Obtain the lock, and possibly recreate the channel or the span observer
    synchronized (lock) {
        if (isShutdownForever) {
            throw new RuntimeException("No longer accepting connections to gRPC.");
        }
        if (managedChannel == null) {
            logger.log(Level.FINE, "Creating gRPC channel.");
            managedChannel = buildChannel();
        }
        if (recreateSpanObserver) {
            if (spanObserver != null) {
                logger.log(Level.FINE, "Cancelling and recreating gRPC span observer.");
                spanObserver.cancel("CLOSING_CONNECTION", new ChannelClosingException());
            }
            IngestServiceStub ingestServiceStub = buildStub(managedChannel);
            ResponseObserver responseObserver = buildResponseObserver();
            spanObserver = (ClientCallStreamObserver<V1.Span>) ingestServiceStub.recordSpan(responseObserver);
            aggregator.incrementCounter("Supportability/InfiniteTracing/Connect");
            recreateSpanObserver = false;
        }
        return spanObserver;
    }
}
Also used : IngestServiceStub(com.newrelic.trace.v1.IngestServiceGrpc.IngestServiceStub) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IngestServiceStub (com.newrelic.trace.v1.IngestServiceGrpc.IngestServiceStub)1 CountDownLatch (java.util.concurrent.CountDownLatch)1