11package datadog .trace .instrumentation .jdbc ;
22
33import datadog .trace .api .Config ;
4+ import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
5+ import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
46import java .io .UnsupportedEncodingException ;
57import java .net .URLEncoder ;
68import java .nio .charset .StandardCharsets ;
@@ -15,6 +17,7 @@ public class SQLCommenter {
1517 private static final String DATABASE_SERVICE = encode ("dddbs" );
1618 private static final String DD_HOSTNAME = encode ("ddh" );
1719 private static final String DD_DB_NAME = encode ("dddb" );
20+ private static final String DD_PEER_SERVICE = encode ("ddprs" );
1821 private static final String DD_ENV = encode ("dde" );
1922 private static final String DD_VERSION = encode ("ddpv" );
2023 private static final String TRACEPARENT = encode ("traceparent" );
@@ -25,6 +28,7 @@ public class SQLCommenter {
2528 private static final String OPEN_COMMENT = "/*" ;
2629 private static final String CLOSE_COMMENT = "*/" ;
2730 private static final int INITIAL_CAPACITY = computeInitialCapacity ();
31+ private static final String DD_DB_INSTANCE = "ddprs" ;
2832
2933 public static String append (
3034 final String sql ,
@@ -140,6 +144,109 @@ public static String inject(
140144 return sb .toString ();
141145 }
142146
147+ public static String inject (
148+ final String sql ,
149+ final String dbService ,
150+ final String dbType ,
151+ final String hostname ,
152+ final String dbName ,
153+ final String dbInstance ,
154+ final String traceParent ,
155+ final boolean injectTrace ,
156+ boolean appendComment ) {
157+ System .out .println ("HELLO IN INJECT PEER SERVICE" );
158+
159+ // Map<String, Object> tagMap = span.getTags();
160+ // Object peerService = tagMap.get(Tags.PEER_SERVICE);
161+
162+ // String peerService = span.getTag(Tags.PEER_SERVICE);
163+ AgentSpan currSpan = AgentTracer .activeSpan ();
164+ // System.out.println(currSpan.getTags());
165+ // String myPeerService = currSpan.getTag(Tags.PEER_SERVICE).toString();
166+ //
167+ // if (myPeerService != null) {
168+ // System.out.print(myPeerService);
169+ // }
170+
171+ if (sql == null || sql .isEmpty ()) {
172+ return sql ;
173+ }
174+ if (hasDDComment (sql , appendComment )) {
175+ return sql ;
176+ }
177+
178+ if (dbType != null ) {
179+ final String firstWord = getFirstWord (sql );
180+
181+ // The Postgres JDBC parser doesn't allow SQL comments anywhere in a JDBC callable statements
182+ // https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/core/Parser.java#L1038
183+ // TODO: Could we inject the comment after the JDBC has been converted to standard SQL?
184+ if (firstWord .startsWith ("{" ) && dbType .startsWith ("postgres" )) {
185+ return sql ;
186+ }
187+
188+ // Append the comment for mysql JDBC callable statements
189+ if (firstWord .startsWith ("{" ) && "mysql" .equals (dbType )) {
190+ appendComment = true ;
191+ }
192+
193+ // Both Postgres and MySQL are unhappy with anything before CALL in a stored procedure
194+ // invocation but they seem ok with it after so we force append mode
195+ if (firstWord .equalsIgnoreCase ("call" )) {
196+ appendComment = true ;
197+ }
198+ }
199+ final Config config = Config .get ();
200+ final String parentService = config .getServiceName ();
201+ final String env = config .getEnv ();
202+ final String version = config .getVersion ();
203+ // String configPeerService = config.getPeerServiceMapping().get(Tags.PEER_SERVICE);
204+ // System.out.println(configPeerService);
205+
206+ final int commentSize = capacity (traceParent , parentService , dbService , env , version );
207+ StringBuilder sb = new StringBuilder (sql .length () + commentSize );
208+ boolean commentAdded = false ;
209+ if (appendComment ) {
210+ sb .append (sql );
211+ sb .append (SPACE );
212+ sb .append (OPEN_COMMENT );
213+ commentAdded =
214+ toComment (
215+ sb ,
216+ injectTrace ,
217+ parentService ,
218+ dbService ,
219+ hostname ,
220+ dbName ,
221+ dbInstance ,
222+ env ,
223+ version ,
224+ traceParent );
225+ sb .append (CLOSE_COMMENT );
226+ } else {
227+ sb .append (OPEN_COMMENT );
228+ commentAdded =
229+ toComment (
230+ sb ,
231+ injectTrace ,
232+ parentService ,
233+ dbService ,
234+ hostname ,
235+ dbName ,
236+ dbInstance ,
237+ env ,
238+ version ,
239+ traceParent );
240+ sb .append (CLOSE_COMMENT );
241+ sb .append (SPACE );
242+ sb .append (sql );
243+ }
244+ if (!commentAdded ) {
245+ return sql ;
246+ }
247+ return sb .toString ();
248+ }
249+
143250 private static boolean hasDDComment (String sql , final boolean appendComment ) {
144251 // first check to see if sql ends with a comment
145252 if ((!(sql .endsWith (CLOSE_COMMENT )) && appendComment )
@@ -215,6 +322,33 @@ protected static boolean toComment(
215322 return sb .length () > emptySize ;
216323 }
217324
325+ protected static boolean toComment (
326+ StringBuilder sb ,
327+ final boolean injectTrace ,
328+ final String parentService ,
329+ final String dbService ,
330+ final String hostname ,
331+ final String dbName ,
332+ final String dbInstance ,
333+ final String env ,
334+ final String version ,
335+ final String traceparent ) {
336+ System .out .println ("in the new to Comment" );
337+ int emptySize = sb .length ();
338+ System .out .println ("DB INSTANCE PEER SERVICE:" + dbInstance );
339+ append (sb , PARENT_SERVICE , parentService , false );
340+ append (sb , DATABASE_SERVICE , dbService , sb .length () > emptySize );
341+ append (sb , DD_HOSTNAME , hostname , sb .length () > emptySize );
342+ append (sb , DD_DB_NAME , dbName , sb .length () > emptySize );
343+ append (sb , DD_DB_INSTANCE , dbInstance , sb .length () > emptySize );
344+ append (sb , DD_ENV , env , sb .length () > emptySize );
345+ append (sb , DD_VERSION , version , sb .length () > emptySize );
346+ if (injectTrace ) {
347+ append (sb , TRACEPARENT , traceparent , sb .length () > emptySize );
348+ }
349+ return sb .length () > emptySize ;
350+ }
351+
218352 private static void append (StringBuilder sb , String key , String value , boolean prependComma ) {
219353 if (null != value && !value .isEmpty ()) {
220354 try {
0 commit comments