All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.javaagent.instrumentation.redisson.v3_0.RedissonPromiseWrapper Maven / Gradle / Ivy

There is a newer version: 2.8.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.javaagent.instrumentation.redisson.v3_0;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.instrumentation.redisson.EndOperationListener;
import io.opentelemetry.javaagent.instrumentation.redisson.PromiseWrapper;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;

public class RedissonPromiseWrapper extends RedissonPromise implements PromiseWrapper {
  private volatile EndOperationListener endOperationListener;

  private RedissonPromiseWrapper(RPromise delegate) {
    Context context = Context.current();
    this.whenComplete(
        (result, error) -> {
          EndOperationListener endOperationListener = this.endOperationListener;
          if (endOperationListener != null) {
            endOperationListener.accept(result, error);
          }
          try (Scope ignored = context.makeCurrent()) {
            if (error != null) {
              delegate.tryFailure(error);
            } else {
              delegate.trySuccess(result);
            }
          }
        });
  }

  /**
   * Wrap {@link RPromise} so that {@link EndOperationListener}, that is used to end the span, could
   * be attached to it.
   */
  public static  RPromise wrap(RPromise delegate) {
    if (delegate instanceof RedissonPromiseWrapper) {
      return delegate;
    }

    return new RedissonPromiseWrapper<>(delegate);
  }

  @Override
  public void setEndOperationListener(EndOperationListener endOperationListener) {
    this.endOperationListener = endOperationListener;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy