@@ -48,9 +48,13 @@ public static function register(): void
4848 $ builder = self ::makeBuilder ($ instrumentation , 'PDO::connect ' , $ function , $ class , $ filename , $ lineno )
4949 ->setSpanKind (SpanKind::KIND_CLIENT );
5050 if ($ class === PDO ::class) {
51+ // Parse the DSN to extract host and port
52+ $ dsn = $ params [0 ] ?? '' ;
53+ [$ host , $ port ] = self ::extractFromDSN ($ dsn );
54+
5155 $ builder
52- ->setAttribute (TraceAttributes::SERVER_ADDRESS , $ params [ 0 ] ?? ' unknown ' )
53- ->setAttribute (TraceAttributes::SERVER_PORT , $ params [ 0 ] ?? null );
56+ ->setAttribute (TraceAttributes::SERVER_ADDRESS , $ host )
57+ ->setAttribute (TraceAttributes::SERVER_PORT , $ port );
5458 }
5559 $ parent = Context::getCurrent ();
5660 $ span = $ builder ->startSpan ();
@@ -89,10 +93,13 @@ public static function register(): void
8993 $ builder = self ::makeBuilder ($ instrumentation , 'PDO::__construct ' , $ function , $ class , $ filename , $ lineno )
9094 ->setSpanKind (SpanKind::KIND_CLIENT );
9195 if ($ class === PDO ::class) {
92- //@todo split params[0] into host + port, replace deprecated trace attribute
96+ // Parse the DSN to extract host and port
97+ $ dsn = $ params [0 ] ?? '' ;
98+ [$ host , $ port ] = self ::extractFromDSN ($ dsn );
99+
93100 $ builder
94- ->setAttribute (TraceAttributes::SERVER_ADDRESS , $ params [ 0 ] ?? ' unknown ' )
95- ->setAttribute (TraceAttributes::SERVER_PORT , $ params [ 0 ] ?? null );
101+ ->setAttribute (TraceAttributes::SERVER_ADDRESS , $ host )
102+ ->setAttribute (TraceAttributes::SERVER_PORT , $ port );
96103 }
97104 $ parent = Context::getCurrent ();
98105 $ span = $ builder ->startSpan ();
@@ -340,4 +347,24 @@ private static function isDistributeStatementToLinkedSpansEnabled(): bool
340347
341348 return filter_var (get_cfg_var ('otel.instrumentation.pdo.distribute_statement_to_linked_spans ' ), FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) ?? false ;
342349 }
350+
351+ private static function extractFromDSN (string $ dsn ): array
352+ {
353+ $ host = 'unknown ' ;
354+ $ port = null ;
355+
356+ if (preg_match ('/host=([^;]+)/ ' , $ dsn , $ hostMatches )) {
357+ $ host = $ hostMatches [1 ];
358+ } elseif (preg_match ('/mysql:([^;:]+)/ ' , $ dsn , $ hostMatches )) {
359+ $ host = $ hostMatches [1 ];
360+ }
361+
362+ if (preg_match ('/port=([0-9]+)/ ' , $ dsn , $ portMatches )) {
363+ $ port = (int ) $ portMatches [1 ];
364+ } elseif (preg_match ('/:[0-9]+/ ' , $ dsn , $ portMatches )) {
365+ $ port = (int ) substr ($ portMatches [0 ], 1 );
366+ }
367+
368+ return [$ host , $ port ];
369+ }
343370}
0 commit comments