@@ -485,9 +485,18 @@ internal async Task Open(NpgsqlTimeout timeout, bool async, CancellationToken ca
485485 LogMessages . OpeningPhysicalConnection ( ConnectionLogger , Host , Port , Database , UserFacingConnectionString ) ;
486486 var startOpenTimestamp = Stopwatch . GetTimestamp ( ) ;
487487
488+ Activity ? activity = null ;
489+
488490 try
489491 {
490- await OpenCore ( this , Settings . SslMode , timeout , async, cancellationToken ) . ConfigureAwait ( false ) ;
492+ var username = await GetUsernameAsync ( async , cancellationToken ) . ConfigureAwait( false) ;
493+
494+ activity = NpgsqlActivitySource. ConnectionOpen( this ) ;
495+
496+ await OpenCore ( this , username , Settings . SslMode , timeout , async, cancellationToken ) . ConfigureAwait ( false ) ;
497+
498+ if ( activity is not null )
499+ NpgsqlActivitySource . Enrich ( activity , this ) ;
491500
492501 await DataSource . Bootstrap ( this , timeout , forceReload : false , async, cancellationToken ) . ConfigureAwait ( false ) ;
493502
@@ -510,6 +519,8 @@ internal async Task Open(NpgsqlTimeout timeout, bool async, CancellationToken ca
510519 // It is intentionally not awaited and will run as long as the connector is alive.
511520 // The CommandsInFlightWriter channel is completed in Cleanup, which should cause this task
512521 // to complete.
522+ // Make sure we do not flow AsyncLocals like Activity.Current
523+ using var __ = ExecutionContext . SuppressFlow ( ) ;
513524 _ = Task . Run ( MultiplexingReadLoop , CancellationToken . None )
514525 . ContinueWith ( t =>
515526 {
@@ -540,7 +551,7 @@ internal async Task Open(NpgsqlTimeout timeout, bool async, CancellationToken ca
540551 {
541552 if ( async)
542553 await DataSource . ConnectionInitializerAsync ( tempConnection ) . ConfigureAwait ( false ) ;
543- else if ( ! async )
554+ else
544555 DataSource . ConnectionInitializer ( tempConnection ) ;
545556 }
546557 finally
@@ -553,26 +564,31 @@ internal async Task Open(NpgsqlTimeout timeout, bool async, CancellationToken ca
553564 }
554565 }
555566
567+ if ( activity is not null )
568+ NpgsqlActivitySource . CommandStop ( activity ) ;
569+
556570 LogMessages . OpenedPhysicalConnection (
557- ConnectionLogger , Host , Port , Database , UserFacingConnectionString , ( long ) Stopwatch . GetElapsedTime ( startOpenTimestamp ) . TotalMilliseconds , Id ) ;
571+ ConnectionLogger , Host , Port , Database , UserFacingConnectionString ,
572+ ( long ) Stopwatch . GetElapsedTime ( startOpenTimestamp ) . TotalMilliseconds , Id ) ;
558573 }
559574 catch ( Exception e )
560575 {
576+ if ( activity is not null )
577+ NpgsqlActivitySource . SetException ( activity , e ) ;
561578 Break ( e ) ;
562579 throw ;
563580 }
564581
565582 static async Task OpenCore (
566583 NpgsqlConnector conn ,
584+ string username ,
567585 SslMode sslMode ,
568586 NpgsqlTimeout timeout ,
569587 bool async ,
570588 CancellationToken cancellationToken )
571589 {
572590 await conn . RawOpen ( sslMode , timeout , async , cancellationToken ) . ConfigureAwait( false) ;
573591
574- var username = await conn. GetUsernameAsync ( async, cancellationToken ) . ConfigureAwait ( false ) ;
575-
576592 timeout. CheckAndApply( conn) ;
577593 conn. WriteStartupMessage( username) ;
578594 await conn . Flush ( async , cancellationToken ) . ConfigureAwait( false) ;
@@ -595,6 +611,7 @@ static async Task OpenCore(
595611 // If Allow was specified and we failed (without SSL), retry with SSL
596612 await OpenCore (
597613 conn ,
614+ username ,
598615 sslMode == SslMode . Prefer ? SslMode . Disable : SslMode . Require ,
599616 timeout ,
600617 async,
@@ -754,6 +771,8 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
754771 else
755772 Connect ( timeout ) ;
756773
774+ ConnectionLogger . LogTrace ( "Socket connected to {Host}:{Port}" , Host , Port ) ;
775+
757776 _baseStream = new NetworkStream ( _socket , true ) ;
758777 _stream = _baseStream ;
759778
@@ -810,8 +829,6 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
810829 if ( ReadBuffer . ReadBytesLeft > 0 )
811830 throw new NpgsqlException( "Additional unencrypted data received after SSL negotiation - this should never happen, and may be an indication of a man-in-the-middle attack." ) ;
812831 }
813-
814- ConnectionLogger. LogTrace ( "Socket connected to {Host}:{Port}" , Host , Port ) ;
815832 }
816833 catch
817834 {
0 commit comments