@@ -127,7 +127,7 @@ public static function register(): void
127127
128128 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
129129 if (self ::isSqlCommenterEnabled () && $ sqlStatement !== self ::UNDEFINED ) {
130- $ sqlStatement = self ::appendSqlComments ($ sqlStatement , true );
130+ $ sqlStatement = self ::addSqlComments ($ sqlStatement , true );
131131 if (self ::isSqlCommenterAttributeEnabled ()) {
132132 $ span ->setAttributes ([
133133 TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
@@ -168,7 +168,7 @@ public static function register(): void
168168
169169 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
170170 if (self ::isSqlCommenterEnabled () && $ sqlStatement !== self ::UNDEFINED ) {
171- $ sqlStatement = self ::appendSqlComments ($ sqlStatement , true );
171+ $ sqlStatement = self ::addSqlComments ($ sqlStatement , true );
172172 if (self ::isSqlCommenterAttributeEnabled ()) {
173173 $ span ->setAttributes ([
174174 TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
@@ -209,7 +209,7 @@ public static function register(): void
209209
210210 Context::storage ()->attach ($ span ->storeInContext ($ parent ));
211211 if (self ::isSqlCommenterEnabled () && $ sqlStatement !== self ::UNDEFINED ) {
212- $ sqlStatement = self ::appendSqlComments ($ sqlStatement , false );
212+ $ sqlStatement = self ::addSqlComments ($ sqlStatement , false );
213213 if (self ::isSqlCommenterAttributeEnabled ()) {
214214 $ span ->setAttributes ([
215215 TraceAttributes::DB_QUERY_TEXT => $ sqlStatement ,
@@ -394,16 +394,25 @@ private static function isSqlCommenterEnabled(): bool
394394 return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
395395 }
396396
397+ private static function isSqlCommenterPrepend (): bool
398+ {
399+ if (class_exists ('OpenTelemetry\SDK\Common\Configuration\Configuration ' )) {
400+ return Configuration::getBoolean ('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND ' , false );
401+ }
402+
403+ return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter.prepend ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
404+ }
405+
397406 private static function isSqlCommenterAttributeEnabled (): bool
398407 {
399408 if (class_exists ('OpenTelemetry\SDK\Common\Configuration\Configuration ' )) {
400409 return Configuration::getBoolean ('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_ATTRIBUTE ' , false );
401410 }
402411
403- return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter_attribute ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
412+ return filter_var (get_cfg_var ('otel.instrumentation.pdo.sql_commenter.attribute ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
404413 }
405414
406- private static function appendSqlComments (string $ query , bool $ withTraceContext ): string
415+ private static function addSqlComments (string $ query , bool $ withTraceContext ): string
407416 {
408417 $ comments = [];
409418 if ($ withTraceContext ) {
@@ -413,9 +422,13 @@ private static function appendSqlComments(string $query, bool $withTraceContext)
413422 $ comments [$ key ] = $ value ;
414423 }
415424 $ query = trim ($ query );
425+ if (self ::isSqlCommenterPrepend ()) {
426+ return Utils::formatComments (array_filter ($ comments )) . $ query ;
427+ }
416428 $ hasSemicolon = $ query !== '' && $ query [strlen ($ query ) - 1 ] === '; ' ;
417429 $ query = rtrim ($ query , '; ' );
418430
419431 return $ query . Utils::formatComments (array_filter ($ comments )) . ($ hasSemicolon ? '; ' : '' );
432+
420433 }
421434}
0 commit comments