Skip to content

Commit cc99ae6

Browse files
CopilottraskSylvainJugeotelbot[bot]
authored andcommitted
Add shouldStart checks to lettuce-4.0 and lettuce-5.0 javaagent instrumentation to prevent span creation when disabled (open-telemetry#14666)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: trask <[email protected]> Co-authored-by: Trask Stalnaker <[email protected]> Co-authored-by: SylvainJuge <[email protected]> Co-authored-by: otelbot <[email protected]>
1 parent 207d7eb commit cc99ae6

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

instrumentation/lettuce/lettuce-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/lettuce/v4_0/LettuceAsyncCommandsInstrumentation.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public void end(
6363
public static AdviceScope onEnter(@Advice.Argument(0) RedisCommand<?, ?, ?> command) {
6464

6565
Context parentContext = currentContext();
66+
if (!instrumenter().shouldStart(parentContext, command)) {
67+
return null;
68+
}
69+
6670
Context context = instrumenter().start(parentContext, command);
6771
// remember the context that called dispatch, it is used in LettuceAsyncCommandInstrumentation
6872
context = context.with(LettuceSingletons.COMMAND_CONTEXT_KEY, parentContext);
@@ -72,10 +76,12 @@ public static AdviceScope onEnter(@Advice.Argument(0) RedisCommand<?, ?, ?> comm
7276
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7377
public static void onExit(
7478
@Advice.Argument(0) RedisCommand<?, ?, ?> command,
75-
@Advice.Thrown Throwable throwable,
76-
@Advice.Return AsyncCommand<?, ?, ?> asyncCommand,
77-
@Advice.Enter AdviceScope adviceScope) {
78-
adviceScope.end(throwable, command, asyncCommand);
79+
@Advice.Thrown @Nullable Throwable throwable,
80+
@Advice.Return @Nullable AsyncCommand<?, ?, ?> asyncCommand,
81+
@Advice.Enter @Nullable AdviceScope adviceScope) {
82+
if (adviceScope != null) {
83+
adviceScope.end(throwable, command, asyncCommand);
84+
}
7985
}
8086
}
8187
}

instrumentation/lettuce/lettuce-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/lettuce/v4_0/LettuceConnectInstrumentation.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,23 @@ public void end(Throwable throwable, RedisURI redisUri) {
5454

5555
@Advice.OnMethodEnter(suppress = Throwable.class)
5656
public static AdviceScope onEnter(@Advice.Argument(1) RedisURI redisUri) {
57-
Context context = connectInstrumenter().start(currentContext(), redisUri);
57+
Context parentContext = currentContext();
58+
if (!connectInstrumenter().shouldStart(parentContext, redisUri)) {
59+
return null;
60+
}
61+
62+
Context context = connectInstrumenter().start(parentContext, redisUri);
5863
return new AdviceScope(context, context.makeCurrent());
5964
}
6065

6166
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6267
public static void onExit(
6368
@Advice.Argument(1) RedisURI redisUri,
6469
@Advice.Thrown @Nullable Throwable throwable,
65-
@Advice.Enter AdviceScope adviceScope) {
66-
adviceScope.end(throwable, redisUri);
70+
@Advice.Enter @Nullable AdviceScope adviceScope) {
71+
if (adviceScope != null) {
72+
adviceScope.end(throwable, redisUri);
73+
}
6774
}
6875
}
6976
}

instrumentation/lettuce/lettuce-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/lettuce/v5_0/LettuceAsyncCommandsInstrumentation.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public void end(
7575
public static AdviceScope onEnter(@Advice.Argument(0) RedisCommand<?, ?, ?> command) {
7676

7777
Context parentContext = currentContext();
78+
if (!instrumenter().shouldStart(parentContext, command)) {
79+
return null;
80+
}
81+
7882
Context context = instrumenter().start(parentContext, command);
7983
// remember the context that called dispatch, it is used in LettuceAsyncCommandInstrumentation
8084
context = context.with(LettuceSingletons.COMMAND_CONTEXT_KEY, parentContext);
@@ -85,10 +89,12 @@ public static AdviceScope onEnter(@Advice.Argument(0) RedisCommand<?, ?, ?> comm
8589
public static void stopSpan(
8690
@Advice.Argument(0) RedisCommand<?, ?, ?> command,
8791
@Advice.Thrown @Nullable Throwable throwable,
88-
@Advice.Return AsyncCommand<?, ?, ?> asyncCommand,
89-
@Advice.Enter AdviceScope adviceScope) {
92+
@Advice.Return @Nullable AsyncCommand<?, ?, ?> asyncCommand,
93+
@Advice.Enter @Nullable AdviceScope adviceScope) {
9094

91-
adviceScope.end(throwable, command, asyncCommand);
95+
if (adviceScope != null) {
96+
adviceScope.end(throwable, command, asyncCommand);
97+
}
9298
}
9399
}
94100
}

instrumentation/lettuce/lettuce-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/lettuce/v5_0/LettuceClientInstrumentation.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,24 @@ public void end(
7272

7373
@Advice.OnMethodEnter(suppress = Throwable.class)
7474
public static AdviceScope onEnter(@Advice.Argument(1) RedisURI redisUri) {
75-
Context context = connectInstrumenter().start(currentContext(), redisUri);
75+
Context parentContext = currentContext();
76+
if (!connectInstrumenter().shouldStart(parentContext, redisUri)) {
77+
return null;
78+
}
79+
80+
Context context = connectInstrumenter().start(parentContext, redisUri);
7681
return new AdviceScope(context, context.makeCurrent());
7782
}
7883

7984
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
8085
public static void stopSpan(
8186
@Advice.Argument(1) RedisURI redisUri,
8287
@Advice.Thrown @Nullable Throwable throwable,
83-
@Advice.Return ConnectionFuture<?> connectionFuture,
84-
@Advice.Enter AdviceScope adviceScope) {
85-
adviceScope.end(throwable, redisUri, connectionFuture);
88+
@Advice.Return @Nullable ConnectionFuture<?> connectionFuture,
89+
@Advice.Enter @Nullable AdviceScope adviceScope) {
90+
if (adviceScope != null) {
91+
adviceScope.end(throwable, redisUri, connectionFuture);
92+
}
8693
}
8794
}
8895
}

0 commit comments

Comments
 (0)