@@ -24,6 +24,8 @@ class PDOInstrumentation
2424 public const NAME = 'pdo ' ;
2525 private const UNDEFINED = 'undefined ' ;
2626
27+ private const ALL = 'all ' ;
28+
2729 public static function register (): void
2830 {
2931 $ instrumentation = new CachedInstrumentation (
@@ -128,16 +130,18 @@ public static function register(): void
128130
129131 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
130132 if (self ::isSqlCommenterEnabled () && $ sqlStatement !== self ::UNDEFINED ) {
131- $ sqlStatement = self ::addSqlComments ($ sqlStatement , true );
132- if (self ::isSqlCommenterAttributeEnabled ()) {
133- $ span ->setAttributes ([
134- TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
135- ]);
133+ if (array_key_exists (TraceAttributes::DB_SYSTEM_NAME , $ attributes ) && self ::isSQLCommenterSupportedDatabase ((string ) ($ attributes [TraceAttributes::DB_SYSTEM_NAME ]))) {
134+ $ sqlStatement = self ::addSqlComments ($ sqlStatement , true );
135+ if (self ::isSqlCommenterAttributeEnabled ()) {
136+ $ span ->setAttributes ([
137+ TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
138+ ]);
139+ }
140+
141+ return [
142+ 0 => $ sqlStatement ,
143+ ];
136144 }
137-
138- return [
139- 0 => $ sqlStatement ,
140- ];
141145 }
142146
143147 return [];
@@ -169,16 +173,18 @@ public static function register(): void
169173
170174 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
171175 if (self ::isSqlCommenterEnabled () && $ sqlStatement !== self ::UNDEFINED ) {
172- $ sqlStatement = self ::addSqlComments ($ sqlStatement , true );
173- if (self ::isSqlCommenterAttributeEnabled ()) {
174- $ span ->setAttributes ([
175- TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
176- ]);
176+ if (array_key_exists (TraceAttributes::DB_SYSTEM_NAME , $ attributes ) && self ::isSQLCommenterSupportedDatabase ((string ) ($ attributes [TraceAttributes::DB_SYSTEM_NAME ]))) {
177+ $ sqlStatement = self ::addSqlComments ($ sqlStatement , true );
178+ if (self ::isSqlCommenterAttributeEnabled ()) {
179+ $ span ->setAttributes ([
180+ TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
181+ ]);
182+ }
183+
184+ return [
185+ 0 => $ sqlStatement ,
186+ ];
177187 }
178-
179- return [
180- 0 => $ sqlStatement ,
181- ];
182188 }
183189
184190 return [];
@@ -210,16 +216,18 @@ public static function register(): void
210216
211217 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
212218 if (self ::isSqlCommenterEnabled () && $ sqlStatement !== self ::UNDEFINED ) {
213- $ sqlStatement = self ::addSqlComments ($ sqlStatement , false );
214- if (self ::isSqlCommenterAttributeEnabled ()) {
215- $ span ->setAttributes ([
216- TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
217- ]);
219+ if (array_key_exists (TraceAttributes::DB_SYSTEM_NAME , $ attributes ) && self ::isSQLCommenterSupportedDatabase ((string ) ($ attributes [TraceAttributes::DB_SYSTEM_NAME ]))) {
220+ $ sqlStatement = self ::addSqlComments ($ sqlStatement , false );
221+ if (self ::isSqlCommenterAttributeEnabled ()) {
222+ $ span ->setAttributes ([
223+ TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
224+ ]);
225+ }
226+
227+ return [
228+ 0 => $ sqlStatement ,
229+ ];
218230 }
219-
220- return [
221- 0 => $ sqlStatement ,
222- ];
223231 }
224232
225233 return [];
@@ -413,6 +421,15 @@ private static function isSqlCommenterAttributeEnabled(): bool
413421 return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter.attribute ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
414422 }
415423
424+ private static function getSqlCommenterDatabase (): array
425+ {
426+ if (class_exists ('OpenTelemetry\SDK\Common\Configuration\Configuration ' )) {
427+ return Configuration::getList ('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_DATABASE ' , [self ::ALL ]);
428+ }
429+
430+ return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter.database ' ), FILTER_REQUIRE_ARRAY , FILTER_NULL_ON_FAILURE ) ?? [self ::ALL ];
431+ }
432+
416433 private static function addSqlComments (string $ query , bool $ withTraceContext ): string
417434 {
418435 $ comments = [];
@@ -436,4 +453,11 @@ private static function addSqlComments(string $query, bool $withTraceContext): s
436453 return $ query . Utils::formatComments (array_filter ($ comments )) . ($ hasSemicolon ? '; ' : '' );
437454
438455 }
456+
457+ private static function isSQLCommenterSupportedDatabase (string $ db ) : bool
458+ {
459+ $ supported = self ::getSqlCommenterDatabase ();
460+
461+ return in_array (strtolower ($ db ), $ supported ) || in_array (self ::ALL , $ supported );
462+ }
439463}
0 commit comments