1010use OpenTelemetry \API \Trace \SpanKind ;
1111use OpenTelemetry \API \Trace \StatusCode ;
1212use OpenTelemetry \Context \Context ;
13+ use OpenTelemetry \SDK \Common \Configuration \Configuration ;
1314use function OpenTelemetry \Instrumentation \hook ;
1415use OpenTelemetry \SemConv \TraceAttributes ;
1516use PDO ;
@@ -200,6 +201,9 @@ public static function register(): void
200201 'fetchAll ' ,
201202 pre: static function (PDOStatement $ statement , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno ) use ($ pdoTracker , $ instrumentation ) {
202203 $ attributes = $ pdoTracker ->trackedAttributesForStatement ($ statement );
204+ if (self ::isDistributeStatementToLinkedSpansEnabled ()) {
205+ $ attributes [TraceAttributes::DB_STATEMENT ] = $ statement ->queryString ;
206+ }
203207 /** @psalm-suppress ArgumentTypeCoercion */
204208 $ builder = self ::makeBuilder ($ instrumentation , 'PDOStatement::fetchAll ' , $ function , $ class , $ filename , $ lineno )
205209 ->setSpanKind (SpanKind::KIND_CLIENT )
@@ -221,6 +225,11 @@ public static function register(): void
221225 'execute ' ,
222226 pre: static function (PDOStatement $ statement , array $ params , string $ class , string $ function , ?string $ filename , ?int $ lineno ) use ($ pdoTracker , $ instrumentation ) {
223227 $ attributes = $ pdoTracker ->trackedAttributesForStatement ($ statement );
228+
229+ if (self ::isDistributeStatementToLinkedSpansEnabled ()) {
230+ $ attributes [TraceAttributes::DB_STATEMENT ] = $ statement ->queryString ;
231+ }
232+
224233 /** @psalm-suppress ArgumentTypeCoercion */
225234 $ builder = self ::makeBuilder ($ instrumentation , 'PDOStatement::execute ' , $ function , $ class , $ filename , $ lineno )
226235 ->setSpanKind (SpanKind::KIND_CLIENT )
@@ -268,4 +277,13 @@ private static function end(?Throwable $exception): void
268277
269278 $ span ->end ();
270279 }
280+
281+ private static function isDistributeStatementToLinkedSpansEnabled (): bool
282+ {
283+ if (class_exists ('OpenTelemetry\SDK\Common\Configuration\Configuration ' )) {
284+ return Configuration::getBoolean ('OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS ' , false );
285+ }
286+
287+ return get_cfg_var ('otel.instrumentation.pdo.distribute_statement_to_linked_spans ' );
288+ }
271289}
0 commit comments