Search in sources :

Example 1 with RequestSpan

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

the class RequestContext method logicallyComplete.

/**
 * Signals that this request is completed fully, including streaming sections or logical sub-requests also being
 * completed (i.e. observe polling).
 */
@Stability.Internal
public RequestContext logicallyComplete() {
    this.logicallyCompletedAt = System.nanoTime();
    RequestSpan span = request.requestSpan();
    if (span != null) {
        span.attribute(TracingIdentifiers.ATTR_RETRIES, retryAttempts());
        span.end();
    }
    if (!(environment().meter() instanceof NoopMeter)) {
        long latency = logicalRequestLatency();
        if (latency > 0) {
            core().responseMetric(request).recordValue(latency);
        }
    }
    return this;
}
Also used : NoopMeter(com.couchbase.client.core.cnc.metrics.NoopMeter) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 2 with RequestSpan

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

the class AsyncQueryIndexManager method watchIndexes.

/**
 * Watches/Polls indexes until they are online with custom options.
 * <p>
 * By default, this method will watch the indexes on the bucket. If the indexes should be watched on a collection,
 * both {@link WatchQueryIndexesOptions#scopeName(String)} and
 * {@link WatchQueryIndexesOptions#collectionName(String)} must be set.
 *
 * @param bucketName the name of the bucket where the indexes should be watched.
 * @param indexNames the names of the indexes to watch.
 * @param timeout the maximum amount of time the indexes should be watched.
 * @param options the custom options to apply.
 * @return a {@link CompletableFuture} completing when the operation is applied or failed with an error.
 * @throws CouchbaseException (async) if any other generic unhandled/unexpected errors.
 */
public CompletableFuture<Void> watchIndexes(final String bucketName, final Collection<String> indexNames, final Duration timeout, final WatchQueryIndexesOptions options) {
    notNullOrEmpty(bucketName, "BucketName");
    notNull(indexNames, "IndexNames");
    notNull(timeout, "Timeout");
    notNull(options, "Options");
    Set<String> indexNameSet = new HashSet<>(indexNames);
    WatchQueryIndexesOptions.Built builtOpts = options.build();
    RequestSpan parent = cluster.environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_MQ_WATCH_INDEXES, null);
    parent.attribute(TracingIdentifiers.ATTR_SYSTEM, TracingIdentifiers.ATTR_SYSTEM_COUCHBASE);
    return Mono.fromFuture(() -> failIfIndexesOffline(bucketName, indexNameSet, builtOpts.watchPrimary(), parent, builtOpts.scopeName(), builtOpts.collectionName())).retryWhen(Retry.onlyIf(ctx -> hasCause(ctx.exception(), IndexesNotReadyException.class)).exponentialBackoff(Duration.ofMillis(50), Duration.ofSeconds(1)).timeout(timeout).toReactorRetry()).onErrorMap(t -> t instanceof RetryExhaustedException ? toWatchTimeoutException(t, timeout) : t).toFuture().whenComplete((r, t) -> parent.end());
}
Also used : Arrays(java.util.Arrays) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CbThrowables.findCause(com.couchbase.client.core.util.CbThrowables.findCause) TimeoutException(java.util.concurrent.TimeoutException) QueryResult(com.couchbase.client.java.query.QueryResult) Collectors.toMap(java.util.stream.Collectors.toMap) Duration(java.time.Duration) Map(java.util.Map) CbThrowables.throwIfUnchecked(com.couchbase.client.core.util.CbThrowables.throwIfUnchecked) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) READ_ONLY(com.couchbase.client.java.manager.query.AsyncQueryIndexManager.QueryType.READ_ONLY) IndexExistsException(com.couchbase.client.core.error.IndexExistsException) Collectors.toSet(java.util.stream.Collectors.toSet) DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions) CbThrowables.hasCause(com.couchbase.client.core.util.CbThrowables.hasCause) Predicate(java.util.function.Predicate) Mapper(com.couchbase.client.core.json.Mapper) Collection(java.util.Collection) Set(java.util.Set) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) QueryOptions(com.couchbase.client.java.query.QueryOptions) List(java.util.List) GetAllQueryIndexesOptions.getAllQueryIndexesOptions(com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions) RedactableArgument.redactMeta(com.couchbase.client.core.logging.RedactableArgument.redactMeta) Optional(java.util.Optional) WRITE(com.couchbase.client.java.manager.query.AsyncQueryIndexManager.QueryType.WRITE) CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions) CreateQueryIndexOptions.createQueryIndexOptions(com.couchbase.client.java.manager.query.CreateQueryIndexOptions.createQueryIndexOptions) Retry(com.couchbase.client.core.retry.reactor.Retry) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) CompletableFuture(java.util.concurrent.CompletableFuture) WatchQueryIndexesOptions.watchQueryIndexesOptions(com.couchbase.client.java.manager.query.WatchQueryIndexesOptions.watchQueryIndexesOptions) QueryException(com.couchbase.client.core.error.QueryException) Function(java.util.function.Function) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CommonOptions(com.couchbase.client.java.CommonOptions) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) IndexFailureException(com.couchbase.client.core.error.IndexFailureException) IndexesNotReadyException(com.couchbase.client.core.error.IndexesNotReadyException) DropQueryIndexOptions.dropQueryIndexOptions(com.couchbase.client.java.manager.query.DropQueryIndexOptions.dropQueryIndexOptions) Reactor(com.couchbase.client.core.Reactor) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) Mono(reactor.core.publisher.Mono) JsonArray(com.couchbase.client.java.json.JsonArray) Collectors.toList(java.util.stream.Collectors.toList) BuildQueryIndexOptions.buildDeferredQueryIndexesOptions(com.couchbase.client.java.manager.query.BuildQueryIndexOptions.buildDeferredQueryIndexesOptions) AsyncCluster(com.couchbase.client.java.AsyncCluster) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) HashSet(java.util.HashSet) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 3 with RequestSpan

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

the class OpenTracingRequestTracer method requestSpan.

@Override
public RequestSpan requestSpan(final String operationName, final RequestSpan parent) {
    try {
        Tracer.SpanBuilder builder = tracer.buildSpan(operationName);
        if (parent != null) {
            builder.asChildOf(castSpan(parent));
        }
        Span span = builder.start();
        tracer.activateSpan(span).close();
        return OpenTracingRequestSpan.wrap(tracer, span);
    } catch (Exception ex) {
        throw new TracerException("Failed to create OpenTracingRequestSpan", ex);
    }
}
Also used : Tracer(io.opentracing.Tracer) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) TracerException(com.couchbase.client.core.error.TracerException) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) Span(io.opentracing.Span) TracerException(com.couchbase.client.core.error.TracerException)

Example 4 with RequestSpan

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

the class AsyncCollection method touchRequest.

/**
 * Helper method to create the touch request.
 *
 * @param id the id of the document to update.
 * @param expiry the new expiry for the document.
 * @param options the custom options.
 * @return the touch request.
 */
TouchRequest touchRequest(final String id, final Expiry expiry, final TouchOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(expiry, "Expiry", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "TouchOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    TouchOptions.Built opts = options.build();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_TOUCH, opts.parentSpan().orElse(null));
    long encodedExpiry = expiry.encode();
    TouchRequest request = new TouchRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, encodedExpiry, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : Duration(java.time.Duration) GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) TouchOptions(com.couchbase.client.java.kv.TouchOptions) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) TouchRequest(com.couchbase.client.core.msg.kv.TouchRequest) GetAndTouchRequest(com.couchbase.client.core.msg.kv.GetAndTouchRequest) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 5 with RequestSpan

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

the class AsyncCollection method upsertRequest.

/**
 * Helper method to generate the upsert request.
 *
 * @param id the document id to upsert.
 * @param content the document content to upsert.
 * @param opts custom options to customize the upsert behavior.
 * @return the upsert request.
 */
UpsertRequest upsertRequest(final String id, final Object content, final UpsertOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(content, "Content", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    Duration timeout = decideKvTimeout(opts, environment.timeoutConfig());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_UPSERT, opts.parentSpan().orElse(null));
    final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
    long start = System.nanoTime();
    Transcoder.EncodedValue encoded;
    try {
        encoded = transcoder.encode(content);
    } finally {
        encodeSpan.end();
    }
    long end = System.nanoTime();
    long expiry = opts.expiry().encode();
    final UpsertRequest request = new UpsertRequest(id, encoded.encoded(), expiry, opts.preserveExpiry(), encoded.flags(), timeout, coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
    return request;
}
Also used : UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) Duration(java.time.Duration) Transcoder(com.couchbase.client.java.codec.Transcoder) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Aggregations

RequestSpan (com.couchbase.client.core.cnc.RequestSpan)42 Duration (java.time.Duration)30 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)28 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 Stability (com.couchbase.client.core.annotation.Stability)10 Validators.notNullOrEmpty (com.couchbase.client.core.util.Validators.notNullOrEmpty)10 List (java.util.List)10 Core (com.couchbase.client.core.Core)9 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 BucketConfig (com.couchbase.client.core.config.BucketConfig)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)8 Objects.requireNonNull (java.util.Objects.requireNonNull)8 Mono (reactor.core.publisher.Mono)8 Reactor (com.couchbase.client.core.Reactor)7 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)7 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)7 Function (java.util.function.Function)7 CoreContext (com.couchbase.client.core.CoreContext)6