22
33import static datadog .trace .agent .tooling .bytebuddy .matcher .NameMatchers .named ;
44import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpanWithoutScope ;
5- import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activeSpan ;
65import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .startSpan ;
76import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
87import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
1211import com .mongodb .internal .connection .DefaultServerConnection ;
1312import datadog .trace .agent .tooling .Instrumenter ;
1413import datadog .trace .agent .tooling .InstrumenterModule ;
14+ import datadog .trace .bootstrap .CallDepthThreadLocalMap ;
1515import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
16- import datadog .trace .bootstrap .instrumentation .dbm .SharedDBCommenter ;
1716import net .bytebuddy .asm .Advice ;
1817import org .bson .BsonDocument ;
1918
@@ -33,9 +32,10 @@ public String instrumentedType() {
3332 @ Override
3433 public String [] helperClassNames () {
3534 return new String [] {
36- SharedDBCommenter .class .getName (),
37- MongoCommentInjector .class .getName (),
38- MongoDecorator .class .getName (),
35+ "datadog.trace.bootstrap.instrumentation.dbm.SharedDBCommenter" ,
36+ packageName + ".MongoDecorator" ,
37+ packageName + ".MongoCommentInjector" ,
38+ packageName + ".BsonScrubber" ,
3939 };
4040 }
4141
@@ -66,9 +66,7 @@ public static void onEnter(
6666 return ;
6767 }
6868
69- AgentSpan existingSpan = activeSpan ();
70- if (existingSpan != null
71- && MongoDecorator .OPERATION_NAME .equals (existingSpan .getOperationName ())) {
69+ if (CallDepthThreadLocalMap .incrementCallDepth (DefaultServerConnection .class ) > 0 ) {
7270 // we don't re-run the advice if the command goes through multiple overloads
7371 return ;
7472 }
@@ -88,5 +86,14 @@ public static void onEnter(
8886 originalBsonDocument = MongoCommentInjector .injectComment (dbmComment , originalBsonDocument );
8987 }
9088 }
89+
90+ @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
91+ public static void onExit () {
92+ if (!MongoCommentInjector .INJECT_COMMENT ) {
93+ return ;
94+ }
95+
96+ CallDepthThreadLocalMap .decrementCallDepth (DefaultServerConnection .class );
97+ }
9198 }
9299}
0 commit comments