Skip to content

Commit c9cbd8a

Browse files
authored
Fix flaky finagle test (#11441)
1 parent 84f3459 commit c9cbd8a

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.finaglehttp.v23_11;
7+
8+
import io.opentelemetry.context.Context;
9+
import io.opentelemetry.context.Scope;
10+
import scala.Function1;
11+
12+
public final class Function1Wrapper {
13+
14+
public static <T1, R> Function1<T1, R> wrap(Function1<T1, R> function1) {
15+
Context context = Context.current();
16+
return value -> {
17+
try (Scope ignored = context.makeCurrent()) {
18+
return function1.apply(value);
19+
}
20+
};
21+
}
22+
23+
private Function1Wrapper() {}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.finaglehttp.v23_11;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
9+
import static net.bytebuddy.matcher.ElementMatchers.named;
10+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
11+
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 scala.Function1;
18+
19+
public class PromiseMonitoredInstrumentation implements TypeInstrumentation {
20+
21+
@Override
22+
public ElementMatcher<TypeDescription> typeMatcher() {
23+
return named("com.twitter.util.Promise$Monitored");
24+
}
25+
26+
@Override
27+
public void transform(TypeTransformer transformer) {
28+
transformer.applyAdviceToMethod(
29+
isConstructor().and(takesArgument(1, named("scala.Function1"))),
30+
this.getClass().getName() + "$WrapFunctionAdvice");
31+
}
32+
33+
@SuppressWarnings("unused")
34+
public static class WrapFunctionAdvice {
35+
36+
@Advice.OnMethodEnter(suppress = Throwable.class)
37+
public static void wrap(
38+
@Advice.Argument(value = 1, readOnly = false) Function1<?, ?> function1) {
39+
if (function1 == null) {
40+
return;
41+
}
42+
43+
function1 = Function1Wrapper.wrap(function1);
44+
}
45+
}
46+
}

instrumentation/finagle-http-23.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/finaglehttp/v23_11/TwitterUtilCoreInstrumentationModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.finaglehttp.v23_11;
77

8-
import static java.util.Collections.singletonList;
8+
import static java.util.Arrays.asList;
99

1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
@@ -21,6 +21,7 @@ public TwitterUtilCoreInstrumentationModule() {
2121

2222
@Override
2323
public List<TypeInstrumentation> typeInstrumentations() {
24-
return singletonList(new LocalSchedulerActivationInstrumentation());
24+
return asList(
25+
new LocalSchedulerActivationInstrumentation(), new PromiseMonitoredInstrumentation());
2526
}
2627
}

0 commit comments

Comments
 (0)