@@ -40,10 +40,7 @@ public ServerSession(ConnectionPool? pool, int poolGeneration, int id)
40
40
PoolGeneration = poolGeneration ;
41
41
HostName = "" ;
42
42
m_logArguments = new object ? [ ] { "{0}" . FormatInvariant ( Id ) , null } ;
43
- m_activityTags = new ActivityTagsCollection
44
- {
45
- { ActivitySourceHelper . DatabaseSystemTagName , ActivitySourceHelper . DatabaseSystemValue } ,
46
- } ;
43
+ m_activityTags = new ActivityTagsCollection ( ) ;
47
44
Log . Trace ( "Session{0} created new session" , m_logArguments ) ;
48
45
}
49
46
@@ -394,7 +391,7 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
394
391
m_state = State . Closed ;
395
392
}
396
393
397
- public async Task < string ? > ConnectAsync ( ConnectionSettings cs , MySqlConnection connection , int startTickCount , ILoadBalancer ? loadBalancer , IOBehavior ioBehavior , CancellationToken cancellationToken )
394
+ public async Task < string ? > ConnectAsync ( ConnectionSettings cs , MySqlConnection connection , int startTickCount , ILoadBalancer ? loadBalancer , Activity ? activity , IOBehavior ioBehavior , CancellationToken cancellationToken )
398
395
{
399
396
string ? statusInfo = null ;
400
397
@@ -406,6 +403,24 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
406
403
m_state = State . Connecting ;
407
404
}
408
405
406
+ // set activity tags
407
+ {
408
+ var connectionString = cs . ConnectionStringBuilder . GetConnectionString ( cs . ConnectionStringBuilder . PersistSecurityInfo ) ;
409
+ m_activityTags . Add ( ActivitySourceHelper . DatabaseSystemTagName , ActivitySourceHelper . DatabaseSystemValue ) ;
410
+ m_activityTags . Add ( ActivitySourceHelper . DatabaseConnectionStringTagName , connectionString ) ;
411
+ m_activityTags . Add ( ActivitySourceHelper . DatabaseUserTagName , cs . UserID ) ;
412
+ if ( cs . Database . Length != 0 )
413
+ m_activityTags . Add ( ActivitySourceHelper . DatabaseNameTagName , cs . Database ) ;
414
+ if ( activity is { IsAllDataRequested : true } )
415
+ {
416
+ activity . SetTag ( ActivitySourceHelper . DatabaseSystemTagName , ActivitySourceHelper . DatabaseSystemValue ) ;
417
+ activity . SetTag ( ActivitySourceHelper . DatabaseConnectionStringTagName , connectionString ) ;
418
+ activity . SetTag ( ActivitySourceHelper . DatabaseUserTagName , cs . UserID ) ;
419
+ if ( cs . Database . Length != 0 )
420
+ activity . SetTag ( ActivitySourceHelper . DatabaseNameTagName , cs . Database ) ;
421
+ }
422
+ }
423
+
409
424
// TLS negotiation should automatically fall back to the best version supported by client and server. However,
410
425
// Windows Schannel clients will fail to connect to a yaSSL-based MySQL Server if TLS 1.2 is requested and
411
426
// have to use only TLS 1.1: https://github.com/mysql-net/MySqlConnector/pull/101
@@ -424,11 +439,11 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
424
439
425
440
var connected = false ;
426
441
if ( cs . ConnectionProtocol == MySqlConnectionProtocol . Sockets )
427
- connected = await OpenTcpSocketAsync ( cs , loadBalancer ?? throw new ArgumentNullException ( nameof ( loadBalancer ) ) , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
442
+ connected = await OpenTcpSocketAsync ( cs , loadBalancer ?? throw new ArgumentNullException ( nameof ( loadBalancer ) ) , activity , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
428
443
else if ( cs . ConnectionProtocol == MySqlConnectionProtocol . UnixSocket )
429
- connected = await OpenUnixSocketAsync ( cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
444
+ connected = await OpenUnixSocketAsync ( cs , activity , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
430
445
else if ( cs . ConnectionProtocol == MySqlConnectionProtocol . NamedPipe )
431
- connected = await OpenNamedPipeAsync ( cs , startTickCount , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
446
+ connected = await OpenNamedPipeAsync ( cs , startTickCount , activity , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
432
447
if ( ! connected )
433
448
{
434
449
lock ( m_lock )
@@ -465,6 +480,14 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
465
480
m_useCompression = cs . UseCompression && ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Compress ) != 0 ;
466
481
CancellationTimeout = cs . CancellationTimeout ;
467
482
483
+ // set activity tags
484
+ {
485
+ var connectionId = ConnectionId . ToString ( CultureInfo . InvariantCulture ) ;
486
+ m_activityTags [ ActivitySourceHelper . DatabaseConnectionIdTagName ] = connectionId ;
487
+ if ( activity is { IsAllDataRequested : true } )
488
+ activity . SetTag ( ActivitySourceHelper . DatabaseConnectionIdTagName , connectionId ) ;
489
+ }
490
+
468
491
m_supportsComMulti = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . MariaDbComMulti ) != 0 ;
469
492
m_supportsConnectionAttributes = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . ConnectionAttributes ) != 0 ;
470
493
m_supportsDeprecateEof = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . DeprecateEof ) != 0 ;
@@ -569,12 +592,6 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
569
592
await GetRealServerDetailsAsync ( ioBehavior , CancellationToken . None ) . ConfigureAwait ( false ) ;
570
593
571
594
m_payloadHandler . ByteHandler . RemainingTimeout = Constants . InfiniteTimeout ;
572
-
573
- m_activityTags . Add ( ActivitySourceHelper . DatabaseConnectionIdTagName , ConnectionId . ToString ( CultureInfo . InvariantCulture ) ) ;
574
- m_activityTags . Add ( ActivitySourceHelper . DatabaseConnectionStringTagName , cs . ConnectionStringBuilder . GetConnectionString ( cs . ConnectionStringBuilder . PersistSecurityInfo ) ) ;
575
- m_activityTags . Add ( ActivitySourceHelper . DatabaseUserTagName , cs . UserID ) ;
576
- if ( cs . Database . Length != 0 )
577
- m_activityTags . Add ( ActivitySourceHelper . DatabaseNameTagName , cs . Database ) ;
578
595
}
579
596
catch ( ArgumentException ex )
580
597
{
@@ -1013,8 +1030,22 @@ private void VerifyConnected()
1013
1030
}
1014
1031
}
1015
1032
1016
- private async Task < bool > OpenTcpSocketAsync ( ConnectionSettings cs , ILoadBalancer loadBalancer , IOBehavior ioBehavior , CancellationToken cancellationToken )
1033
+ private async Task < bool > OpenTcpSocketAsync ( ConnectionSettings cs , ILoadBalancer loadBalancer , Activity ? activity , IOBehavior ioBehavior , CancellationToken cancellationToken )
1017
1034
{
1035
+ // set activity tags for TCP/IP
1036
+ {
1037
+ m_activityTags . Add ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportTcpIpValue ) ;
1038
+ string ? port = cs . Port == 3306 ? default : cs . Port . ToString ( CultureInfo . InvariantCulture ) ;
1039
+ if ( port is not null )
1040
+ m_activityTags . Add ( ActivitySourceHelper . NetPeerPortTagName , port ) ;
1041
+ if ( activity is { IsAllDataRequested : true } )
1042
+ {
1043
+ activity . SetTag ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportTcpIpValue ) ;
1044
+ if ( port is not null )
1045
+ activity . SetTag ( ActivitySourceHelper . NetPeerPortTagName , port ) ;
1046
+ }
1047
+ }
1048
+
1018
1049
var hostNames = loadBalancer . LoadBalance ( cs . HostNames ! ) ;
1019
1050
for ( var hostNameIndex = 0 ; hostNameIndex < hostNames . Count ; hostNameIndex ++ )
1020
1051
{
@@ -1041,8 +1072,28 @@ private async Task<bool> OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer
1041
1072
for ( var ipAddressIndex = 0 ; ipAddressIndex < ipAddresses . Length ; ipAddressIndex ++ )
1042
1073
{
1043
1074
var ipAddress = ipAddresses [ ipAddressIndex ] ;
1075
+ var ipAddressString = ipAddress . ToString ( ) ;
1044
1076
if ( Log . IsTraceEnabled ( ) )
1045
- Log . Trace ( "Session{0} connecting to IpAddress {1} ({2} of {3}) for HostName '{4}' ({5} of {6})" , m_logArguments [ 0 ] , ipAddress , ipAddressIndex + 1 , ipAddresses . Length , hostName , hostNameIndex + 1 , hostNames . Count ) ;
1077
+ Log . Trace ( "Session{0} connecting to IpAddress {1} ({2} of {3}) for HostName '{4}' ({5} of {6})" , m_logArguments [ 0 ] , ipAddressString , ipAddressIndex + 1 , ipAddresses . Length , hostName , hostNameIndex + 1 , hostNames . Count ) ;
1078
+
1079
+ // set activity tags for the current IP address/hostname
1080
+ {
1081
+ m_activityTags [ ActivitySourceHelper . NetPeerIpTagName ] = ipAddressString ;
1082
+ if ( ipAddressString != hostName )
1083
+ m_activityTags [ ActivitySourceHelper . NetPeerNameTagName ] = hostName ;
1084
+ else
1085
+ m_activityTags . Remove ( ActivitySourceHelper . NetPeerNameTagName ) ;
1086
+
1087
+ if ( activity is { IsAllDataRequested : true } )
1088
+ {
1089
+ activity . SetTag ( ActivitySourceHelper . NetPeerIpTagName , ipAddressString ) ;
1090
+ if ( ipAddressString != hostName )
1091
+ activity . SetTag ( ActivitySourceHelper . NetPeerNameTagName , hostName ) ;
1092
+ else
1093
+ activity . SetTag ( ActivitySourceHelper . NetPeerNameTagName , null ) ;
1094
+ }
1095
+ }
1096
+
1046
1097
TcpClient ? tcpClient = null ;
1047
1098
try
1048
1099
{
@@ -1118,14 +1169,6 @@ private async Task<bool> OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer
1118
1169
m_socket . NoDelay = true ;
1119
1170
m_stream = m_tcpClient . GetStream ( ) ;
1120
1171
m_socket . SetKeepAlive ( cs . Keepalive ) ;
1121
-
1122
- m_activityTags . Add ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportTcpIpValue ) ;
1123
- var ipAddressString = ipAddress . ToString ( ) ;
1124
- m_activityTags . Add ( ActivitySourceHelper . NetPeerIpTagName , ipAddressString ) ;
1125
- if ( ipAddressString != hostName )
1126
- m_activityTags . Add ( ActivitySourceHelper . NetPeerNameTagName , hostName ) ;
1127
- if ( cs . Port != 3306 )
1128
- m_activityTags . Add ( ActivitySourceHelper . NetPeerPortTagName , cs . Port . ToString ( CultureInfo . InvariantCulture ) ) ;
1129
1172
}
1130
1173
catch ( ObjectDisposedException ) when ( cancellationToken . IsCancellationRequested )
1131
1174
{
@@ -1145,10 +1188,22 @@ private async Task<bool> OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer
1145
1188
return false ;
1146
1189
}
1147
1190
1148
- private async Task < bool > OpenUnixSocketAsync ( ConnectionSettings cs , IOBehavior ioBehavior , CancellationToken cancellationToken )
1191
+ private async Task < bool > OpenUnixSocketAsync ( ConnectionSettings cs , Activity ? activity , IOBehavior ioBehavior , CancellationToken cancellationToken )
1149
1192
{
1150
1193
m_logArguments [ 1 ] = cs . UnixSocket ;
1151
1194
Log . Trace ( "Session{0} connecting to UNIX Socket '{1}'" , m_logArguments ) ;
1195
+
1196
+ // set activity tags
1197
+ {
1198
+ m_activityTags . Add ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportUnixValue ) ;
1199
+ m_activityTags . Add ( ActivitySourceHelper . NetPeerNameTagName , cs . UnixSocket ) ;
1200
+ if ( activity is { IsAllDataRequested : true } )
1201
+ {
1202
+ activity . SetTag ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportUnixValue ) ;
1203
+ activity . SetTag ( ActivitySourceHelper . NetPeerNameTagName , cs . UnixSocket ) ;
1204
+ }
1205
+ }
1206
+
1152
1207
var socket = new Socket ( AddressFamily . Unix , SocketType . Stream , ProtocolType . IP ) ;
1153
1208
var unixEp = new UnixDomainSocketEndPoint ( cs . UnixSocket ! ) ;
1154
1209
try
@@ -1183,9 +1238,6 @@ private async Task<bool> OpenUnixSocketAsync(ConnectionSettings cs, IOBehavior i
1183
1238
m_socket = socket ;
1184
1239
m_stream = new NetworkStream ( socket ) ;
1185
1240
1186
- m_activityTags . Add ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportUnixValue ) ;
1187
- m_activityTags . Add ( ActivitySourceHelper . NetPeerNameTagName , cs . UnixSocket ) ;
1188
-
1189
1241
lock ( m_lock )
1190
1242
m_state = State . Connected ;
1191
1243
return true ;
@@ -1194,10 +1246,24 @@ private async Task<bool> OpenUnixSocketAsync(ConnectionSettings cs, IOBehavior i
1194
1246
return false ;
1195
1247
}
1196
1248
1197
- private async Task < bool > OpenNamedPipeAsync ( ConnectionSettings cs , int startTickCount , IOBehavior ioBehavior , CancellationToken cancellationToken )
1249
+ private async Task < bool > OpenNamedPipeAsync ( ConnectionSettings cs , int startTickCount , Activity ? activity , IOBehavior ioBehavior , CancellationToken cancellationToken )
1198
1250
{
1199
1251
if ( Log . IsTraceEnabled ( ) )
1200
1252
Log . Trace ( "Session{0} connecting to NamedPipe '{1}' on Server '{2}'" , m_logArguments [ 0 ] , cs . PipeName , cs . HostNames ! [ 0 ] ) ;
1253
+
1254
+ // set activity tags
1255
+ {
1256
+ // see https://docs.microsoft.com/en-us/windows/win32/ipc/pipe-names for pipe name format
1257
+ var pipeName = @"\\" + cs . HostNames ! [ 0 ] + @"\pipe\" + cs . PipeName ;
1258
+ m_activityTags . Add ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportNamedPipeValue ) ;
1259
+ m_activityTags . Add ( ActivitySourceHelper . NetPeerNameTagName , pipeName ) ;
1260
+ if ( activity is { IsAllDataRequested : true } )
1261
+ {
1262
+ activity . SetTag ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportNamedPipeValue ) ;
1263
+ activity . SetTag ( ActivitySourceHelper . NetPeerNameTagName , pipeName ) ;
1264
+ }
1265
+ }
1266
+
1201
1267
var namedPipeStream = new NamedPipeClientStream ( cs . HostNames ! [ 0 ] , cs . PipeName , PipeDirection . InOut , PipeOptions . Asynchronous ) ;
1202
1268
var timeout = Math . Max ( 1 , cs . ConnectionTimeoutMilliseconds - unchecked ( Environment . TickCount - startTickCount ) ) ;
1203
1269
try
@@ -1228,10 +1294,6 @@ private async Task<bool> OpenNamedPipeAsync(ConnectionSettings cs, int startTick
1228
1294
{
1229
1295
m_stream = namedPipeStream ;
1230
1296
1231
- // see https://docs.microsoft.com/en-us/windows/win32/ipc/pipe-names for pipe name format
1232
- m_activityTags . Add ( ActivitySourceHelper . NetTransportTagName , ActivitySourceHelper . NetTransportNamedPipeValue ) ;
1233
- m_activityTags . Add ( ActivitySourceHelper . NetPeerNameTagName , @"\\" + cs . HostNames ! [ 0 ] + @"\pipe\" + cs . PipeName ) ;
1234
-
1235
1297
lock ( m_lock )
1236
1298
m_state = State . Connected ;
1237
1299
return true ;
@@ -1695,7 +1757,6 @@ private void ShutdownSocket()
1695
1757
SafeDispose ( ref m_socket ) ;
1696
1758
Utility . Dispose ( ref m_clientCertificate ) ;
1697
1759
m_activityTags . Clear ( ) ;
1698
- m_activityTags . Add ( ActivitySourceHelper . DatabaseSystemTagName , ActivitySourceHelper . DatabaseSystemValue ) ;
1699
1760
}
1700
1761
1701
1762
/// <summary>
0 commit comments