Skip to content

Commit 2ead50c

Browse files
committed
Fix flaky ratpack test
1 parent be153f0 commit 2ead50c

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.ratpack;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.named;
9+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
10+
11+
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
12+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
14+
import net.bytebuddy.asm.Advice;
15+
import net.bytebuddy.description.type.TypeDescription;
16+
import net.bytebuddy.matcher.ElementMatcher;
17+
import org.reactivestreams.Subscriber;
18+
19+
public class ExecutionBoundPublisherInstrumentation implements TypeInstrumentation {
20+
21+
@Override
22+
public ElementMatcher<TypeDescription> typeMatcher() {
23+
return named("ratpack.exec.internal.DefaultExecution$ExecutionBoundPublisher");
24+
}
25+
26+
@Override
27+
public void transform(TypeTransformer transformer) {
28+
transformer.applyAdviceToMethod(
29+
named("subscribe").and(takesArgument(0, named("org.reactivestreams.Subscriber"))),
30+
this.getClass().getName() + "$SubscribeAdvice");
31+
}
32+
33+
@SuppressWarnings("unused")
34+
public static class SubscribeAdvice {
35+
36+
@Advice.OnMethodEnter(suppress = Throwable.class)
37+
public static <T> void wrap(
38+
@Advice.Argument(value = 0, readOnly = false) Subscriber<T> subscriber) {
39+
subscriber = new TracingSubscriber<>(subscriber, Java8BytecodeBridge.currentContext());
40+
}
41+
}
42+
}

instrumentation/ratpack/ratpack-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/RatpackInstrumentationModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public List<TypeInstrumentation> typeInstrumentations() {
3333
new ContinuationStreamInstrumentation(),
3434
new DefaultExecutionInstrumentation(),
3535
new DefaultExecStarterInstrumentation(),
36+
new ExecutionBoundPublisherInstrumentation(),
3637
new ServerErrorHandlerInstrumentation(),
3738
new ServerRegistryInstrumentation());
3839
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.ratpack;
7+
8+
import io.opentelemetry.context.Context;
9+
import io.opentelemetry.context.Scope;
10+
import org.reactivestreams.Subscriber;
11+
import org.reactivestreams.Subscription;
12+
13+
public final class TracingSubscriber<T> implements Subscriber<T> {
14+
private final Subscriber<T> delegate;
15+
private final Context context;
16+
17+
public TracingSubscriber(Subscriber<T> delegate, Context context) {
18+
this.delegate = delegate;
19+
this.context = context;
20+
}
21+
22+
@Override
23+
public void onSubscribe(Subscription subscription) {
24+
try (Scope ignore = context.makeCurrent()) {
25+
delegate.onSubscribe(subscription);
26+
}
27+
}
28+
29+
@Override
30+
public void onNext(T t) {
31+
try (Scope ignore = context.makeCurrent()) {
32+
delegate.onNext(t);
33+
}
34+
}
35+
36+
@Override
37+
public void onError(Throwable throwable) {
38+
try (Scope ignore = context.makeCurrent()) {
39+
delegate.onError(throwable);
40+
}
41+
}
42+
43+
@Override
44+
public void onComplete() {
45+
try (Scope ignore = context.makeCurrent()) {
46+
delegate.onComplete();
47+
}
48+
}
49+
}

instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackForkedHttpServerTest.groovy

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,4 @@ class RatpackForkedHttpServerTest extends AbstractRatpackForkedHttpServerTest im
2424
boolean testHttpPipelining() {
2525
false
2626
}
27-
28-
@Override
29-
boolean testPostStream() {
30-
// controller span is parent of onNext span which is not expected
31-
Boolean.getBoolean("testLatestDeps")
32-
}
3327
}

instrumentation/ratpack/ratpack-1.4/testing/src/main/groovy/io/opentelemetry/instrumentation/ratpack/server/AbstractRatpackHttpServerTest.groovy

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ abstract class AbstractRatpackHttpServerTest extends HttpServerTest<RatpackServe
235235
response.contentUtf8() == POST_STREAM.body
236236

237237
def hasHandlerSpan = hasHandlerSpan(POST_STREAM)
238+
// when using javaagent instrumentation the parent of reactive callbacks is the controller span
239+
// where subscribe was called, for library instrumentation server span is the parent
240+
def reactiveCallbackParent = hasHandlerSpan ? 2 : 0
238241
assertTraces(1) {
239242
trace(0, 5 + (hasHandlerSpan ? 1 : 0)) {
240243
span(0) {
@@ -255,15 +258,15 @@ abstract class AbstractRatpackHttpServerTest extends HttpServerTest<RatpackServe
255258
}
256259
span(2 + offset) {
257260
name "onNext"
258-
childOf span(0)
261+
childOf span(reactiveCallbackParent)
259262
}
260263
span(3 + offset) {
261264
name "onNext"
262-
childOf span(0)
265+
childOf span(reactiveCallbackParent)
263266
}
264267
span(4 + offset) {
265268
name "onComplete"
266-
childOf span(0)
269+
childOf span(reactiveCallbackParent)
267270
}
268271
}
269272
}

0 commit comments

Comments
 (0)