11package datadog .trace .instrumentation .jdbc ;
22
3- import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activeSpan ;
4-
5- import datadog .trace .api .BaseHash ;
6- import datadog .trace .api .Config ;
7- import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
8- import datadog .trace .bootstrap .instrumentation .api .Tags ;
9- import java .io .UnsupportedEncodingException ;
10- import java .net .URLEncoder ;
11- import java .nio .charset .StandardCharsets ;
12- import org .slf4j .Logger ;
13- import org .slf4j .LoggerFactory ;
3+ import datadog .trace .bootstrap .instrumentation .dbm .SharedDBCommenter ;
144
155public class SQLCommenter {
16- private static final Logger log = LoggerFactory .getLogger (SQLCommenter .class );
17- private static final String UTF8 = StandardCharsets .UTF_8 .toString ();
18-
19- private static final char EQUALS = '=' ;
20- private static final char COMMA = ',' ;
21- private static final char QUOTE = '\'' ;
6+ // SQL-specific constants, rest defined in SharedDBCommenter
227 private static final char SPACE = ' ' ;
238 private static final String OPEN_COMMENT = "/*" ;
249 private static final int OPEN_COMMENT_LEN = OPEN_COMMENT .length ();
2510 private static final String CLOSE_COMMENT = "*/" ;
2611
27- // Injected fields. When adding a new one, be sure to update this and the methods below.
28- private static final int NUMBER_OF_FIELDS = 9 ;
29- private static final String PARENT_SERVICE = encode ("ddps" );
30- private static final String DATABASE_SERVICE = encode ("dddbs" );
31- private static final String DD_HOSTNAME = encode ("ddh" );
32- private static final String DD_DB_NAME = encode ("dddb" );
33- private static final String DD_PEER_SERVICE = "ddprs" ;
34- private static final String DD_ENV = encode ("dde" );
35- private static final String DD_VERSION = encode ("ddpv" );
36- private static final String TRACEPARENT = encode ("traceparent" );
37- private static final String DD_SERVICE_HASH = encode ("ddsh" );
38-
39- private static final int KEY_AND_SEPARATORS_ESTIMATED_SIZE = 10 ;
40- private static final int VALUE_ESTIMATED_SIZE = 10 ;
41- private static final int TRACE_PARENT_EXTRA_ESTIMATED_SIZE = 50 ;
42- private static final int INJECTED_COMMENT_ESTIMATED_SIZE =
43- NUMBER_OF_FIELDS * (KEY_AND_SEPARATORS_ESTIMATED_SIZE + VALUE_ESTIMATED_SIZE )
44- + TRACE_PARENT_EXTRA_ESTIMATED_SIZE ;
12+ // Size estimation for StringBuilder pre-allocation
13+ private static final int SPACE_CHARS = 2 ; // Leading and trailing spaces
14+ private static final int COMMENT_DELIMITERS = 4 ; // "/*" + "*/"
15+ private static final int BUFFER_EXTRA = 4 ;
16+ private static final int SQL_COMMENT_OVERHEAD = SPACE_CHARS + COMMENT_DELIMITERS + BUFFER_EXTRA ;
4517
4618 protected static String getFirstWord (String sql ) {
4719 int beginIndex = 0 ;
@@ -99,9 +71,16 @@ public static String inject(
9971 return sql ;
10072 }
10173
102- Config config = Config .get ();
74+ String commentContent =
75+ SharedDBCommenter .buildComment (dbService , dbType , hostname , dbName , traceParent );
76+
77+ if (commentContent == null ) {
78+ return sql ;
79+ }
10380
104- StringBuilder sb = new StringBuilder (sql .length () + INJECTED_COMMENT_ESTIMATED_SIZE );
81+ // SQL-specific wrapping with /* */
82+ StringBuilder sb =
83+ new StringBuilder (sql .length () + commentContent .length () + SQL_COMMENT_OVERHEAD );
10584 int closingSemicolonIndex = indexOfClosingSemicolon (sql );
10685 if (appendComment ) {
10786 if (closingSemicolonIndex > -1 ) {
@@ -113,22 +92,7 @@ public static String inject(
11392 }
11493
11594 sb .append (OPEN_COMMENT );
116- int initSize = sb .length ();
117- append (sb , PARENT_SERVICE , config .getServiceName (), initSize );
118- append (sb , DATABASE_SERVICE , dbService , initSize );
119- append (sb , DD_HOSTNAME , hostname , initSize );
120- append (sb , DD_DB_NAME , dbName , initSize );
121- append (sb , DD_PEER_SERVICE , getPeerService (), initSize );
122- append (sb , DD_ENV , config .getEnv (), initSize );
123- append (sb , DD_VERSION , config .getVersion (), initSize );
124- append (sb , TRACEPARENT , traceParent , initSize );
125- if (config .isDbmInjectSqlBaseHash () && config .isExperimentalPropagateProcessTagsEnabled ()) {
126- append (sb , DD_SERVICE_HASH , BaseHash .getBaseHashStr (), initSize );
127- }
128- if (initSize == sb .length ()) {
129- // no comment was added
130- return sql ;
131- }
95+ sb .append (commentContent );
13296 sb .append (CLOSE_COMMENT );
13397 if (!appendComment ) {
13498 sb .append (SPACE );
@@ -142,72 +106,30 @@ public static String inject(
142106 return sb .toString ();
143107 }
144108
145- private static String getPeerService () {
146- AgentSpan span = activeSpan ();
147- Object peerService = null ;
148- if (span != null ) {
149- peerService = span .getTag (Tags .PEER_SERVICE );
150- }
151- return peerService != null ? peerService .toString () : null ;
152- }
153-
154109 private static boolean hasDDComment (String sql , boolean appendComment ) {
155110 if ((!sql .endsWith (CLOSE_COMMENT ) && appendComment )
156111 || ((!sql .startsWith (OPEN_COMMENT )) && !appendComment )) {
157112 return false ;
158113 }
159- int startIdx = OPEN_COMMENT_LEN ;
160- if (appendComment ) {
161- startIdx += sql .lastIndexOf (OPEN_COMMENT );
162- }
163- return hasMatchingSubstring (sql , startIdx , PARENT_SERVICE )
164- || hasMatchingSubstring (sql , startIdx , DATABASE_SERVICE )
165- || hasMatchingSubstring (sql , startIdx , DD_HOSTNAME )
166- || hasMatchingSubstring (sql , startIdx , DD_DB_NAME )
167- || hasMatchingSubstring (sql , startIdx , DD_PEER_SERVICE )
168- || hasMatchingSubstring (sql , startIdx , DD_ENV )
169- || hasMatchingSubstring (sql , startIdx , DD_VERSION )
170- || hasMatchingSubstring (sql , startIdx , TRACEPARENT )
171- || hasMatchingSubstring (sql , startIdx , DD_SERVICE_HASH );
172- }
173114
174- private static boolean hasMatchingSubstring (String sql , int startIndex , String substring ) {
175- boolean tooLong = startIndex + substring .length () >= sql .length ();
176- if (tooLong || !(sql .charAt (startIndex + substring .length ()) == EQUALS )) {
177- return false ;
178- }
179- return sql .startsWith (substring , startIndex );
115+ String commentContent = extractCommentContent (sql , appendComment );
116+ return SharedDBCommenter .containsTraceComment (commentContent );
180117 }
181118
182- private static String encode (String val ) {
183- try {
184- return URLEncoder .encode (val , UTF8 );
185- } catch (UnsupportedEncodingException exe ) {
186- if (log .isDebugEnabled ()) {
187- log .debug ("exception thrown while encoding sql comment key %s" , exe );
188- }
189- }
190- return val ;
191- }
192-
193- private static void append (StringBuilder sb , String key , String value , int initSize ) {
194- if (null == value || value .isEmpty ()) {
195- return ;
196- }
197- String encodedValue ;
198- try {
199- encodedValue = URLEncoder .encode (value , UTF8 );
200- } catch (UnsupportedEncodingException e ) {
201- if (log .isDebugEnabled ()) {
202- log .debug ("exception thrown while encoding sql comment %s" , e );
203- }
204- return ;
119+ private static String extractCommentContent (String sql , boolean appendComment ) {
120+ int startIdx ;
121+ int endIdx ;
122+ if (appendComment ) {
123+ startIdx = sql .lastIndexOf (OPEN_COMMENT );
124+ endIdx = sql .lastIndexOf (CLOSE_COMMENT );
125+ } else {
126+ startIdx = sql .indexOf (OPEN_COMMENT );
127+ endIdx = sql .indexOf (CLOSE_COMMENT );
205128 }
206-
207- if (sb .length () > initSize ) {
208- sb .append (COMMA );
129+ if (startIdx != -1 && endIdx != -1 && endIdx > startIdx ) {
130+ return sql .substring (startIdx + OPEN_COMMENT_LEN , endIdx );
209131 }
210- sb . append ( key ). append ( EQUALS ). append ( QUOTE ). append ( encodedValue ). append ( QUOTE ) ;
132+ return "" ;
211133 }
212134
213135 /**
0 commit comments