-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support propagating trace context to callback #287
base: master
Are you sure you want to change the base?
Changes from 1 commit
f082ae9
8042f32
a254644
e28e5c3
ac8eb45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Copyright 2017-2020 The OpenTracing Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package io.opentracing.contrib.spring.cloud.async.instrument; | ||
|
||
import io.opentracing.Span; | ||
import io.opentracing.Tracer; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.springframework.util.concurrent.FailureCallback; | ||
import org.springframework.util.concurrent.ListenableFuture; | ||
import org.springframework.util.concurrent.ListenableFutureCallback; | ||
import org.springframework.util.concurrent.SuccessCallback; | ||
|
||
/** | ||
* @author MiNG | ||
*/ | ||
public class TracedListenableFuture<T> implements ListenableFuture<T> { | ||
|
||
private final ListenableFuture<T> delegate; | ||
private final Tracer tracer; | ||
private final Span span; | ||
|
||
public TracedListenableFuture(ListenableFuture<T> delegate, Tracer tracer) { | ||
this(delegate, tracer, tracer.activeSpan()); | ||
} | ||
|
||
public TracedListenableFuture(ListenableFuture<T> delegate, Tracer tracer, Span span) { | ||
this.delegate = delegate; | ||
this.tracer = tracer; | ||
this.span = span; | ||
} | ||
|
||
@Override | ||
public void addCallback(ListenableFutureCallback<? super T> callback) { | ||
delegate.addCallback(new TracedListenableFutureCallback<>(callback, tracer, span)); | ||
} | ||
|
||
@Override | ||
public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) { | ||
delegate.addCallback(new TracedListenableFutureCallback<>(successCallback, failureCallback, tracer, span)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<T> completable() { | ||
return delegate.completable(); | ||
} | ||
|
||
@Override | ||
public boolean cancel(boolean mayInterruptIfRunning) { | ||
return delegate.cancel(mayInterruptIfRunning); | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() { | ||
return delegate.isCancelled(); | ||
} | ||
|
||
@Override | ||
public boolean isDone() { | ||
return delegate.isDone(); | ||
} | ||
|
||
@Override | ||
public T get() throws InterruptedException, ExecutionException { | ||
return delegate.get(); | ||
} | ||
|
||
@Override | ||
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { | ||
return delegate.get(timeout, unit); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright 2017-2020 The OpenTracing Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package io.opentracing.contrib.spring.cloud.async.instrument; | ||
|
||
import io.opentracing.Scope; | ||
import io.opentracing.Span; | ||
import io.opentracing.Tracer; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.concurrent.FailureCallback; | ||
import org.springframework.util.concurrent.ListenableFutureCallback; | ||
import org.springframework.util.concurrent.SuccessCallback; | ||
|
||
/** | ||
* @author MiNG | ||
*/ | ||
public class TracedListenableFutureCallback<T> implements ListenableFutureCallback<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are all the constructors necessary? If no keep only what is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to your comment here. |
||
|
||
private final SuccessCallback<T> successDelegate; | ||
private final FailureCallback failureDelegate; | ||
private final Tracer tracer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a tracer here? Ir might be cleaner to use the scope manager to indicate that this class only propagates the context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Em... This style follows the |
||
private final Span span; | ||
|
||
public TracedListenableFutureCallback(ListenableFutureCallback<T> delegate, Tracer tracer) { | ||
this(delegate, delegate, tracer, tracer.activeSpan()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? Should be the success and failure callback the same instance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's ok to put them together. |
||
} | ||
|
||
public TracedListenableFutureCallback(ListenableFutureCallback<T> delegate, Tracer tracer, Span span) { | ||
this(delegate, delegate, tracer, span); | ||
} | ||
|
||
public TracedListenableFutureCallback(SuccessCallback<T> successDelegate, FailureCallback failureDelegate, Tracer tracer) { | ||
this(successDelegate, failureDelegate, tracer, tracer.activeSpan()); | ||
} | ||
|
||
public TracedListenableFutureCallback(@Nullable SuccessCallback<T> successDelegate, @Nullable FailureCallback failureDelegate, Tracer tracer, Span span) { | ||
Assert.notNull(successDelegate, "'successDelegate' must not be null"); | ||
Assert.notNull(failureDelegate, "'failureDelegate' must not be null"); | ||
this.successDelegate = successDelegate; | ||
this.failureDelegate = failureDelegate; | ||
this.tracer = tracer; | ||
this.span = span; | ||
} | ||
|
||
@Override | ||
public void onSuccess(T result) { | ||
try (Scope ignored = span == null ? null : tracer.scopeManager().activate(span)) { | ||
successDelegate.onSuccess(result); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable ex) { | ||
try (Scope ignored = span == null ? null : tracer.scopeManager().activate(span)) { | ||
failureDelegate.onFailure(ex); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get around only with this constructor? We could also change tracer to scope manager. It better indicates intentions that we just propagate spans.