-
Notifications
You must be signed in to change notification settings - Fork 774
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
Async @WithSpan Instrumentation for Guava ListenableFuture #2811
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
513c9db
Add Guava instrumentation library with AsyncSpanEndStrategy
HaloFour 5c88bf7
Enable span strategy in advice
HaloFour 3afc065
Spotless
HaloFour 87fae59
Nix attempt at typeInitializer advice, leave TODO comment to revisit
HaloFour 1a41df5
Move async span strategy registration to helper class
HaloFour 81716e0
Remove use of sameThreadExecutor
HaloFour 75c1c9a
Make helper class final and add comment about relying on static initi…
HaloFour File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...src/main/java/io/opentelemetry/javaagent/instrumentation/guava/InstrumentationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.guava; | ||
|
||
import io.opentelemetry.instrumentation.api.tracer.async.AsyncSpanEndStrategies; | ||
import io.opentelemetry.instrumentation.guava.GuavaAsyncSpanEndStrategy; | ||
|
||
public class InstrumentationHelper { | ||
static { | ||
AsyncSpanEndStrategies.getInstance().registerStrategy(GuavaAsyncSpanEndStrategy.INSTANCE); | ||
} | ||
|
||
public static void initialize() {} | ||
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. Please add a comment explaining why it is important to call this method even if it does nothin (static initializer on class load) |
||
} |
105 changes: 105 additions & 0 deletions
105
instrumentation/guava-10.0/javaagent/src/test/groovy/GuavaWithSpanInstrumentationTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import com.google.common.util.concurrent.Futures | ||
import com.google.common.util.concurrent.SettableFuture | ||
import io.opentelemetry.api.trace.SpanKind | ||
import io.opentelemetry.instrumentation.guava.TracedWithSpan | ||
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification | ||
|
||
class GuavaWithSpanInstrumentationTest extends AgentInstrumentationSpecification { | ||
|
||
def "should capture span for already done ListenableFuture"() { | ||
setup: | ||
new TracedWithSpan().listenableFuture(Futures.immediateFuture("Value")) | ||
|
||
expect: | ||
assertTraces(1) { | ||
trace(0, 1) { | ||
span(0) { | ||
name "TracedWithSpan.listenableFuture" | ||
kind SpanKind.INTERNAL | ||
hasNoParent() | ||
errored false | ||
attributes { | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def "should capture span for already failed ListenableFuture"() { | ||
setup: | ||
def error = new IllegalArgumentException("Boom") | ||
new TracedWithSpan().listenableFuture(Futures.immediateFailedFuture(error)) | ||
|
||
expect: | ||
assertTraces(1) { | ||
trace(0, 1) { | ||
span(0) { | ||
name "TracedWithSpan.listenableFuture" | ||
kind SpanKind.INTERNAL | ||
hasNoParent() | ||
errored true | ||
errorEvent(IllegalArgumentException, "Boom") | ||
attributes { | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def "should capture span for eventually done ListenableFuture"() { | ||
setup: | ||
def future = SettableFuture.<String>create() | ||
new TracedWithSpan().listenableFuture(future) | ||
|
||
expect: | ||
Thread.sleep(500) // sleep a bit just to make sure no span is captured | ||
assertTraces(0) {} | ||
|
||
future.set("Value") | ||
|
||
assertTraces(1) { | ||
trace(0, 1) { | ||
span(0) { | ||
name "TracedWithSpan.listenableFuture" | ||
kind SpanKind.INTERNAL | ||
hasNoParent() | ||
errored false | ||
attributes { | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def "should capture span for eventually failed ListenableFuture"() { | ||
setup: | ||
def error = new IllegalArgumentException("Boom") | ||
def future = SettableFuture.<String>create() | ||
new TracedWithSpan().listenableFuture(future) | ||
|
||
expect: | ||
Thread.sleep(500) // sleep a bit just to make sure no span is captured | ||
assertTraces(0) {} | ||
|
||
future.setException(error) | ||
|
||
assertTraces(1) { | ||
trace(0, 1) { | ||
span(0) { | ||
name "TracedWithSpan.listenableFuture" | ||
kind SpanKind.INTERNAL | ||
hasNoParent() | ||
errored true | ||
errorEvent(IllegalArgumentException, "Boom") | ||
attributes { | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...a-10.0/javaagent/src/test/java/io/opentelemetry/instrumentation/guava/TracedWithSpan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.guava; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import io.opentelemetry.extension.annotations.WithSpan; | ||
|
||
public class TracedWithSpan { | ||
@WithSpan | ||
public ListenableFuture<String> listenableFuture(ListenableFuture<String> future) { | ||
return future; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apply from: "$rootDir/gradle/instrumentation-library.gradle" | ||
|
||
dependencies { | ||
library group: 'com.google.guava', name: 'guava', version: '10.0' | ||
} |
40 changes: 40 additions & 0 deletions
40
...brary/src/main/java/io/opentelemetry/instrumentation/guava/GuavaAsyncSpanEndStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.guava; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.tracer.BaseTracer; | ||
import io.opentelemetry.instrumentation.api.tracer.async.AsyncSpanEndStrategy; | ||
|
||
public enum GuavaAsyncSpanEndStrategy implements AsyncSpanEndStrategy { | ||
INSTANCE; | ||
|
||
@Override | ||
public boolean supports(Class<?> returnType) { | ||
return ListenableFuture.class.isAssignableFrom(returnType); | ||
} | ||
|
||
@Override | ||
public Object end(BaseTracer tracer, Context context, Object returnValue) { | ||
ListenableFuture<?> future = (ListenableFuture<?>) returnValue; | ||
if (future.isDone()) { | ||
endSpan(tracer, context, future); | ||
} else { | ||
future.addListener(() -> endSpan(tracer, context, future), Runnable::run); | ||
} | ||
return future; | ||
} | ||
|
||
private void endSpan(BaseTracer tracer, Context context, ListenableFuture<?> future) { | ||
try { | ||
future.get(); | ||
tracer.end(context); | ||
} catch (Throwable exception) { | ||
tracer.endExceptionally(context, exception); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
instrumentation/guava-10.0/library/src/test/groovy/GuavaAsyncSpanEndStrategyTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import com.google.common.util.concurrent.Futures | ||
import com.google.common.util.concurrent.ListenableFuture | ||
import com.google.common.util.concurrent.SettableFuture | ||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.instrumentation.api.tracer.BaseTracer | ||
import io.opentelemetry.instrumentation.guava.GuavaAsyncSpanEndStrategy | ||
import spock.lang.Specification | ||
|
||
class GuavaAsyncSpanEndStrategyTest extends Specification { | ||
BaseTracer tracer | ||
|
||
Context context | ||
|
||
def underTest = GuavaAsyncSpanEndStrategy.INSTANCE | ||
|
||
void setup() { | ||
tracer = Mock() | ||
context = Mock() | ||
} | ||
|
||
def "ListenableFuture is supported"() { | ||
expect: | ||
underTest.supports(ListenableFuture) | ||
} | ||
|
||
def "SettableFuture is also supported"() { | ||
expect: | ||
underTest.supports(SettableFuture) | ||
} | ||
|
||
def "ends span on already done future"() { | ||
when: | ||
underTest.end(tracer, context, Futures.immediateFuture("Value")) | ||
|
||
then: | ||
1 * tracer.end(context) | ||
} | ||
|
||
def "ends span on already failed future"() { | ||
given: | ||
def exception = new IllegalStateException() | ||
|
||
when: | ||
underTest.end(tracer, context, Futures.immediateFailedFuture(exception)) | ||
|
||
then: | ||
1 * tracer.endExceptionally(context, { it.getCause() == exception }) | ||
} | ||
|
||
def "ends span on eventually done future"() { | ||
given: | ||
def future = SettableFuture.<String>create() | ||
|
||
when: | ||
underTest.end(tracer, context, future) | ||
|
||
then: | ||
0 * tracer._ | ||
|
||
when: | ||
future.set("Value") | ||
|
||
then: | ||
1 * tracer.end(context) | ||
} | ||
|
||
def "ends span on eventually failed future"() { | ||
given: | ||
def future = SettableFuture.<String>create() | ||
def exception = new IllegalStateException() | ||
|
||
when: | ||
underTest.end(tracer, context, future) | ||
|
||
then: | ||
0 * tracer._ | ||
|
||
when: | ||
future.setException(exception) | ||
|
||
then: | ||
1 * tracer.endExceptionally(context, { it.getCause() == exception }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: can you make it final and add a private constructor?