Search in sources :

Example 1 with RequestTracer

use of com.couchbase.client.core.cnc.RequestTracer in project couchbase-jvm-clients by couchbase.

the class ChunkedMessageHandler method write.

@Override
@SuppressWarnings("unchecked")
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    // toes.
    if (!pipelined && currentRequest != null) {
        RetryOrchestrator.maybeRetry(endpointContext, (REQ) msg, RetryReason.NOT_PIPELINED_REQUEST_IN_FLIGHT);
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        return;
    }
    try {
        currentRequest = (REQ) msg;
        FullHttpRequest encoded = currentRequest.encode();
        encoded.headers().set(HttpHeaderNames.HOST, remoteHost);
        encoded.headers().set(HttpHeaderNames.USER_AGENT, endpointContext.environment().userAgent().formattedLong());
        chunkResponseParser.updateRequestContext(currentRequest.context());
        dispatchTimingStart = System.nanoTime();
        if (currentRequest.requestSpan() != null) {
            RequestTracer tracer = endpointContext.environment().requestTracer();
            currentDispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, currentRequest.requestSpan());
            if (!CbTracing.isInternalTracer(tracer)) {
                setCommonDispatchSpanAttributes(currentDispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), currentRequest.operationId());
            }
        }
        ctx.write(encoded, promise);
    } catch (Throwable t) {
        currentRequest.response().completeExceptionally(t);
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
    }
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) RequestTracer(com.couchbase.client.core.cnc.RequestTracer)

Example 2 with RequestTracer

use of com.couchbase.client.core.cnc.RequestTracer in project couchbase-jvm-clients by couchbase.

the class NonChunkedHttpMessageHandler method write.

/**
 * Writes a given request and encodes it.
 *
 * @param ctx the channel handler context.
 * @param msg the msg to write.
 * @param promise the promise that will be passed along.
 */
@Override
@SuppressWarnings("unchecked")
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    // toes.
    if (currentRequest != null) {
        RetryOrchestrator.maybeRetry(endpointContext, (Request<? extends Response>) msg, RetryReason.NOT_PIPELINED_REQUEST_IN_FLIGHT);
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        return;
    }
    if (msg instanceof NonChunkedHttpRequest) {
        try {
            currentRequest = (NonChunkedHttpRequest<Response>) msg;
            FullHttpRequest encoded = ((NonChunkedHttpRequest<Response>) msg).encode();
            encoded.headers().set(HttpHeaderNames.HOST, remoteHost);
            encoded.headers().set(HttpHeaderNames.USER_AGENT, endpointContext.environment().userAgent().formattedLong());
            dispatchTimingStart = System.nanoTime();
            if (currentRequest.requestSpan() != null) {
                RequestTracer tracer = endpointContext.environment().requestTracer();
                currentDispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, currentRequest.requestSpan());
                if (!CbTracing.isInternalTracer(tracer)) {
                    setCommonDispatchSpanAttributes(currentDispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), currentRequest.operationId());
                }
            }
            ctx.write(encoded, promise);
        } catch (Throwable t) {
            currentRequest.response().completeExceptionally(t);
            if (endpoint != null) {
                endpoint.decrementOutstandingRequests();
            }
        }
    } else {
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, serviceType, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : FullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse) Response(com.couchbase.client.core.msg.Response) FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) NonChunkedHttpRequest(com.couchbase.client.core.msg.NonChunkedHttpRequest) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)

Example 3 with RequestTracer

use of com.couchbase.client.core.cnc.RequestTracer in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandler method write.

@Override
@SuppressWarnings({ "unchecked" })
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    if (msg instanceof KeyValueRequest) {
        KeyValueRequest<Response> request = (KeyValueRequest<Response>) msg;
        int opaque = request.opaque();
        writtenRequests.put(opaque, request);
        try {
            ctx.write(request.encode(ctx.alloc(), opaque, channelContext), promise);
            writtenRequestDispatchTimings.put(opaque, (Long) System.nanoTime());
            if (request.requestSpan() != null) {
                RequestTracer tracer = endpointContext.environment().requestTracer();
                RequestSpan dispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, request.requestSpan());
                if (!isInternalTracer) {
                    setCommonDispatchSpanAttributes(dispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), null);
                    setNumericOperationId(dispatchSpan, request.opaque());
                    setCommonKVSpanAttributes(dispatchSpan, request);
                }
                writtenRequestDispatchSpans.put(opaque, dispatchSpan);
            }
        } catch (Throwable err) {
            writtenRequests.remove(opaque);
            if (err instanceof CollectionNotFoundException) {
                if (channelContext.collectionsEnabled()) {
                    ConfigurationProvider cp = ioContext.core().configurationProvider();
                    if (cp.collectionRefreshInProgress(request.collectionIdentifier())) {
                        RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.COLLECTION_MAP_REFRESH_IN_PROGRESS);
                    } else if (cp.config().bucketConfig(request.bucket()) instanceof MemcachedBucketConfig) {
                        request.fail(FeatureNotAvailableException.collectionsForMemcached());
                    } else {
                        handleOutdatedCollection(request, RetryReason.COLLECTION_NOT_FOUND);
                    }
                    return;
                }
            }
            request.fail(err);
        }
    } else {
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, ServiceType.KV, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : Response(com.couchbase.client.core.msg.Response) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CollectionNotFoundException(com.couchbase.client.core.error.CollectionNotFoundException) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 4 with RequestTracer

use of com.couchbase.client.core.cnc.RequestTracer in project couchbase-jvm-clients by couchbase.

the class RequestTracerIntegrationTest method beforeAll.

@BeforeAll
static void beforeAll() {
    requestTracer = new TrackingRequestTracer();
    cluster = createCluster(env -> env.requestTracer(requestTracer));
    Bucket bucket = cluster.bucket(config().bucketname());
    collection = bucket.defaultCollection();
    bucket.waitUntilReady(Duration.ofSeconds(5));
}
Also used : JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Assertions.fail(org.junit.jupiter.api.Assertions.fail) TimeoutException(com.couchbase.client.core.error.TimeoutException) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) Instant(java.time.Instant) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) Collection(com.couchbase.client.java.Collection) Bucket(com.couchbase.client.java.Bucket) List(java.util.List) BeforeAll(org.junit.jupiter.api.BeforeAll) Cluster(com.couchbase.client.java.Cluster) Duration(java.time.Duration) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RequestContext(com.couchbase.client.core.msg.RequestContext) JsonObject(com.couchbase.client.java.json.JsonObject) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetOptions(com.couchbase.client.java.kv.GetOptions) Collections(java.util.Collections) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) Bucket(com.couchbase.client.java.Bucket) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

RequestTracer (com.couchbase.client.core.cnc.RequestTracer)4 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 ChannelClosedProactivelyEvent (com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent)2 InvalidRequestDetectedEvent (com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)2 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)2 Response (com.couchbase.client.core.msg.Response)2 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)1 MemcachedBucketConfig (com.couchbase.client.core.config.MemcachedBucketConfig)1 FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)1 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)1 CollectionNotFoundException (com.couchbase.client.core.error.CollectionNotFoundException)1 TimeoutException (com.couchbase.client.core.error.TimeoutException)1 NonChunkedHttpRequest (com.couchbase.client.core.msg.NonChunkedHttpRequest)1 RequestContext (com.couchbase.client.core.msg.RequestContext)1 KeyValueRequest (com.couchbase.client.core.msg.kv.KeyValueRequest)1 Bucket (com.couchbase.client.java.Bucket)1 Cluster (com.couchbase.client.java.Cluster)1 Collection (com.couchbase.client.java.Collection)1 JsonObject (com.couchbase.client.java.json.JsonObject)1 GetOptions (com.couchbase.client.java.kv.GetOptions)1