@@ -111,8 +111,9 @@ public static function register(): void
111111 /** @psalm-suppress ArgumentTypeCoercion */
112112 $ builder = self ::makeBuilder ($ instrumentation , 'PDO::query ' , $ function , $ class , $ filename , $ lineno )
113113 ->setSpanKind (SpanKind::KIND_CLIENT );
114+ $ sqlStatement = mb_convert_encoding ($ params [0 ] ?? 'undefined ' , 'UTF-8 ' );
114115 if ($ class === PDO ::class) {
115- $ builder ->setAttribute (TraceAttributes::DB_QUERY_TEXT , mb_convert_encoding ( $ params [ 0 ] ?? ' undefined ' , ' UTF-8 ' ) );
116+ $ builder ->setAttribute (TraceAttributes::DB_QUERY_TEXT , $ sqlStatement );
116117 }
117118 $ parent = Context::getCurrent ();
118119 $ span = $ builder ->startSpan ();
@@ -121,6 +122,18 @@ public static function register(): void
121122 $ span ->setAttributes ($ attributes );
122123
123124 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
125+ if (self ::isSqlCommenterEnabled () && $ sqlStatement !== 'undefined ' ) {
126+ $ sqlStatement = self ::appendSqlComments ($ sqlStatement );
127+ $ span ->setAttributes ([
128+ TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
129+ ]);
130+
131+ return [
132+ 0 => $ sqlStatement ,
133+ ];
134+ }
135+
136+ return [];
124137 },
125138 post: static function (PDO $ pdo , array $ params , mixed $ statement , ?Throwable $ exception ) {
126139 self ::end ($ exception );
@@ -134,8 +147,9 @@ public static function register(): void
134147 /** @psalm-suppress ArgumentTypeCoercion */
135148 $ builder = self ::makeBuilder ($ instrumentation , 'PDO::exec ' , $ function , $ class , $ filename , $ lineno )
136149 ->setSpanKind (SpanKind::KIND_CLIENT );
150+ $ sqlStatement = mb_convert_encoding ($ params [0 ] ?? 'undefined ' , 'UTF-8 ' );
137151 if ($ class === PDO ::class) {
138- $ builder ->setAttribute (TraceAttributes::DB_QUERY_TEXT , mb_convert_encoding ( $ params [ 0 ] ?? ' undefined ' , ' UTF-8 ' ) );
152+ $ builder ->setAttribute (TraceAttributes::DB_QUERY_TEXT , $ sqlStatement );
139153 }
140154 $ parent = Context::getCurrent ();
141155 $ span = $ builder ->startSpan ();
@@ -144,6 +158,18 @@ public static function register(): void
144158 $ span ->setAttributes ($ attributes );
145159
146160 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
161+ if (self ::isSqlCommenterEnabled () && $ sqlStatement !== 'undefined ' ) {
162+ $ sqlStatement = self ::appendSqlComments ($ sqlStatement );
163+ $ span ->setAttributes ([
164+ TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
165+ ]);
166+
167+ return [
168+ 0 => $ sqlStatement ,
169+ ];
170+ }
171+
172+ return [];
147173 },
148174 post: static function (PDO $ pdo , array $ params , mixed $ statement , ?Throwable $ exception ) {
149175 self ::end ($ exception );
@@ -157,8 +183,9 @@ public static function register(): void
157183 /** @psalm-suppress ArgumentTypeCoercion */
158184 $ builder = self ::makeBuilder ($ instrumentation , 'PDO::prepare ' , $ function , $ class , $ filename , $ lineno )
159185 ->setSpanKind (SpanKind::KIND_CLIENT );
186+ $ sqlStatement = mb_convert_encoding ($ params [0 ] ?? 'undefined ' , 'UTF-8 ' );
160187 if ($ class === PDO ::class) {
161- $ builder ->setAttribute (TraceAttributes::DB_QUERY_TEXT , mb_convert_encoding ( $ params [ 0 ] ?? ' undefined ' , ' UTF-8 ' ) );
188+ $ builder ->setAttribute (TraceAttributes::DB_QUERY_TEXT , $ sqlStatement );
162189 }
163190 $ parent = Context::getCurrent ();
164191 $ span = $ builder ->startSpan ();
@@ -167,6 +194,18 @@ public static function register(): void
167194 $ span ->setAttributes ($ attributes );
168195
169196 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
197+ if (self ::isSqlCommenterEnabled () && $ sqlStatement !== 'undefined ' ) {
198+ $ sqlStatement = self ::appendSqlComments ($ sqlStatement , false );
199+ $ span ->setAttributes ([
200+ TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
201+ ]);
202+
203+ return [
204+ 0 => $ sqlStatement ,
205+ ];
206+ }
207+
208+ return [];
170209 },
171210 post: static function (PDO $ pdo , array $ params , mixed $ statement , ?Throwable $ exception ) use ($ pdoTracker ) {
172211 if ($ statement instanceof PDOStatement) {
@@ -329,4 +368,29 @@ private static function isDistributeStatementToLinkedSpansEnabled(): bool
329368
330369 return filter_var (get_cfg_var ('otel.instrumentation.pdo.distribute_statement_to_linked_spans ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
331370 }
371+
372+ private static function isSqlCommenterEnabled (): bool
373+ {
374+ if (class_exists ('OpenTelemetry\SDK\Common\Configuration\Configuration ' )) {
375+ return Configuration::getBoolean ('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER ' , false );
376+ }
377+
378+ return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
379+ }
380+
381+ private static function appendSqlComments (string $ query , bool $ withTraceContext = true ): string
382+ {
383+ $ comments = [];
384+ if ($ withTraceContext ) {
385+ $ comments = Opentelemetry::getTraceContextValues ();
386+ }
387+ foreach (Opentelemetry::getServiceNameValues () as $ key => $ value ) {
388+ $ comments [$ key ] = $ value ;
389+ }
390+ $ query = trim ($ query );
391+ $ hasSemicolon = $ query [-1 ] === '; ' ;
392+ $ query = rtrim ($ query , '; ' );
393+
394+ return $ query . Utils::formatComments (array_filter ($ comments )) . ($ hasSemicolon ? '; ' : '' );
395+ }
332396}
0 commit comments