Skip to content

Commit 6911464

Browse files
committed
fix: code review cleanup
1 parent 4d9f952 commit 6911464

4 files changed

Lines changed: 14 additions & 13 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.opentelemetry.context.Scope;
1515
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1616
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
17+
import javax.annotation.Nullable;
1718
import net.bytebuddy.asm.Advice;
1819
import net.bytebuddy.description.type.TypeDescription;
1920
import net.bytebuddy.matcher.ElementMatcher;
@@ -44,19 +45,20 @@ public void transform(TypeTransformer transformer) {
4445
public static class DispatchAdvice {
4546

4647
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
48+
@Nullable
4749
public static Scope methodEnter(@Advice.Argument(0) Object req) {
4850
if (req instanceof Request) {
4951
// practically this will always be a Request, from HttpServerDispatcher
5052
Request request = (Request) req;
5153
// if this is null, there's a bug in the instrumentation
5254
Context context = request.ctx().apply(Helpers.OTEL_CONTEXT_KEY);
53-
return context.makeCurrent();
55+
return context != null ? context.makeCurrent() : null;
5456
}
5557
return null;
5658
}
5759

5860
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
59-
public static void methodExit(@Advice.Enter Scope scope) {
61+
public static void methodExit(@Advice.Enter @Nullable Scope scope) {
6062
if (scope != null) {
6163
scope.close();
6264
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Helpers {
4343
public static final RecordSchema.Field<Context> OTEL_CONTEXT_KEY =
4444
Request$.MODULE$.Schema().newField();
4545

46-
public static final String OTEL_NETTY_HANDLER = "otelFinagleNettyHandler";
46+
private static final String OTEL_NETTY_HANDLER = "otelFinagleNettyHandler";
4747

4848
private Helpers() {}
4949

@@ -163,8 +163,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
163163
FULL_HTTP_REQUEST_CONTEXT.set((FullHttpRequest) msg, Context.current());
164164
} else if (msg instanceof HttpRequest) {
165165
HTTP_REQUEST_CONTEXT.set((HttpRequest) msg, Context.current());
166-
} else {
167-
throw new IllegalArgumentException("unexpected request type: " + msg);
168166
}
169167

170168
super.channelRead(ctx, msg);
@@ -177,15 +175,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
177175
Part 2/3 of bridging the otel Context from netty to finagle.
178176
*/
179177
public static void chainContextToFinagle(Object msg, Request request) {
180-
Context context;
178+
Context context = null;
181179
// type switch courtesy of com.twitter.finagle.netty4.http.Netty4ServerStreamTransport.read()
182180
if (msg instanceof FullHttpRequest) {
183181
context = FULL_HTTP_REQUEST_CONTEXT.get((FullHttpRequest) msg);
184182
} else if (msg instanceof HttpRequest) {
185183
context = HTTP_REQUEST_CONTEXT.get((HttpRequest) msg);
186-
} else {
187-
// shouldn't practically reach here
188-
throw new IllegalArgumentException("unexpected request type: " + msg);
189184
}
190185

191186
// hook the Context from netty's request up to finagle's request

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static class ConstructorAdvice {
3939

4040
@Advice.OnMethodExit(suppress = Throwable.class, inline = false)
4141
@Advice.AssignReturned.ToArguments(@ToArgument(1))
42-
public static PartialFunction<Throwable, BoxedUnit> onEnter(
42+
public static PartialFunction<Throwable, BoxedUnit> onExit(
4343
@Advice.Argument(1) PartialFunction<Throwable, BoxedUnit> handler) {
4444
if (handler instanceof TwitterUtilCoreHelpers.InterruptibleWithContext) {
4545
return handler;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.context.Scope;
1616
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1717
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
18+
import javax.annotation.Nullable;
1819
import net.bytebuddy.asm.Advice;
1920
import net.bytebuddy.description.type.TypeDescription;
2021
import net.bytebuddy.matcher.ElementMatcher;
@@ -55,15 +56,18 @@ public static void onExit(@Advice.This Promise.K thiz) {
5556
@SuppressWarnings("unused")
5657
public static class ApplyAdvice {
5758
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
59+
@Nullable
5860
public static Scope onApplyEnter(@Advice.This Promise.K thiz) {
5961
// if this is null, there's a bug in the instrumentation
6062
Context savedContext = TwitterUtilCoreHelpers.PROMISE_K_CONTEXT_FIELD.get(thiz);
61-
return savedContext.makeCurrent();
63+
return savedContext != null ? savedContext.makeCurrent() : null;
6264
}
6365

6466
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)
65-
public static void onApplyExit(@Advice.Enter Scope scope) {
66-
scope.close();
67+
public static void onApplyExit(@Advice.Enter @Nullable Scope scope) {
68+
if (scope != null) {
69+
scope.close();
70+
}
6771
}
6872
}
6973
}

0 commit comments

Comments
 (0)