io.opentelemetry.javaagent.instrumentation.redisson.v3_0.RedissonPromiseWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-javaagent-redisson-3.0 Show documentation
Show all versions of opentelemetry-javaagent-redisson-3.0 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
/*
* 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;
}
}