Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
introduce & log connectionId
  • Loading branch information
antonfirsov committed Jul 10, 2023
commit 4409013d8200bed380e579649a58c43815882cf5
41 changes: 23 additions & 18 deletions src/libraries/System.Net.Http/src/System/Net/Http/HttpTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ private void RequestFailed(string exceptionMessage)
}

[Event(4, Level = EventLevel.Informational)]
private void ConnectionEstablished(byte versionMajor, byte versionMinor)
private void ConnectionEstablished(byte versionMajor, byte versionMinor, long connectionId)
{
WriteEvent(eventId: 4, versionMajor, versionMinor);
WriteEvent(eventId: 4, versionMajor, versionMinor, connectionId);
}

[Event(5, Level = EventLevel.Informational)]
private void ConnectionClosed(byte versionMajor, byte versionMinor)
private void ConnectionClosed(byte versionMajor, byte versionMinor, long connectionId)
{
WriteEvent(eventId: 5, versionMajor, versionMinor);
WriteEvent(eventId: 5, versionMajor, versionMinor, connectionId);
}

[Event(6, Level = EventLevel.Informational)]
Expand Down Expand Up @@ -163,48 +163,48 @@ private void RequestFailedDetailed(string exception)
}

[NonEvent]
public void Http11ConnectionEstablished()
public void Http11ConnectionEstablished(long connectionId)
{
Interlocked.Increment(ref _openedHttp11Connections);
ConnectionEstablished(versionMajor: 1, versionMinor: 1);
ConnectionEstablished(versionMajor: 1, versionMinor: 1, connectionId);
}

[NonEvent]
public void Http11ConnectionClosed()
public void Http11ConnectionClosed(long connectionId)
{
long count = Interlocked.Decrement(ref _openedHttp11Connections);
Debug.Assert(count >= 0);
ConnectionClosed(versionMajor: 1, versionMinor: 1);
ConnectionClosed(versionMajor: 1, versionMinor: 1, connectionId);
}

[NonEvent]
public void Http20ConnectionEstablished()
public void Http20ConnectionEstablished(long connectionId)
{
Interlocked.Increment(ref _openedHttp20Connections);
ConnectionEstablished(versionMajor: 2, versionMinor: 0);
ConnectionEstablished(versionMajor: 2, versionMinor: 0, connectionId);
}

[NonEvent]
public void Http20ConnectionClosed()
public void Http20ConnectionClosed(long connectionId)
{
long count = Interlocked.Decrement(ref _openedHttp20Connections);
Debug.Assert(count >= 0);
ConnectionClosed(versionMajor: 2, versionMinor: 0);
ConnectionClosed(versionMajor: 2, versionMinor: 0, connectionId);
}

[NonEvent]
public void Http30ConnectionEstablished()
public void Http30ConnectionEstablished(long connectionId)
{
Interlocked.Increment(ref _openedHttp30Connections);
ConnectionEstablished(versionMajor: 3, versionMinor: 0);
ConnectionEstablished(versionMajor: 3, versionMinor: 0, connectionId);
}

[NonEvent]
public void Http30ConnectionClosed()
public void Http30ConnectionClosed(long connectionId)
{
long count = Interlocked.Decrement(ref _openedHttp30Connections);
Debug.Assert(count >= 0);
ConnectionClosed(versionMajor: 3, versionMinor: 0);
ConnectionClosed(versionMajor: 3, versionMinor: 0, connectionId);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Expand Down Expand Up @@ -293,9 +293,9 @@ private unsafe void WriteEvent(int eventId, double arg1, byte arg2, byte arg3)
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Parameters to this method are primitive and are trimmer safe")]
[NonEvent]
private unsafe void WriteEvent(int eventId, byte arg1, byte arg2)
private unsafe void WriteEvent(int eventId, byte arg1, byte arg2, long arg3)
{
const int NumEventDatas = 2;
const int NumEventDatas = 3;
EventData* descrs = stackalloc EventData[NumEventDatas];

descrs[0] = new EventData
Expand All @@ -308,6 +308,11 @@ private unsafe void WriteEvent(int eventId, byte arg1, byte arg2)
DataPointer = (IntPtr)(&arg2),
Size = sizeof(byte)
};
descrs[2] = new EventData
{
DataPointer = (IntPtr)(&arg3),
Size = sizeof(long)
};

WriteEventCore(eventId, NumEventDatas, descrs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public Http2Connection(HttpConnectionPool pool, Stream stream)

if (HttpTelemetry.Log.IsEnabled())
{
HttpTelemetry.Log.Http20ConnectionEstablished();
HttpTelemetry.Log.Http20ConnectionEstablished(_id);
_markedByTelemetryStatus = TelemetryStatus_Opened;
}

Expand Down Expand Up @@ -1898,7 +1898,7 @@ private void FinalTeardown()
{
if (Interlocked.Exchange(ref _markedByTelemetryStatus, TelemetryStatus_Closed) == TelemetryStatus_Opened)
{
HttpTelemetry.Log.Http20ConnectionClosed();
HttpTelemetry.Log.Http20ConnectionClosed(_id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Http3Connection(HttpConnectionPool pool, HttpAuthority? origin, HttpAutho

if (HttpTelemetry.Log.IsEnabled())
{
HttpTelemetry.Log.Http30ConnectionEstablished();
HttpTelemetry.Log.Http30ConnectionEstablished(_id);
_markedByTelemetryStatus = TelemetryStatus_Opened;
}

Expand Down Expand Up @@ -172,7 +172,7 @@ private void CheckForShutdown()
{
if (Interlocked.Exchange(ref _markedByTelemetryStatus, TelemetryStatus_Closed) == TelemetryStatus_Opened)
{
HttpTelemetry.Log.Http30ConnectionClosed();
HttpTelemetry.Log.Http30ConnectionClosed(_id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public HttpConnection(

if (HttpTelemetry.Log.IsEnabled())
{
HttpTelemetry.Log.Http11ConnectionEstablished();
HttpTelemetry.Log.Http11ConnectionEstablished(_id);
_disposed = Status_NotDisposedAndTrackedByTelemetry;
}

Expand All @@ -116,7 +116,7 @@ private void Dispose(bool disposing)
// Only decrement the connection count if we counted this connection
if (HttpTelemetry.Log.IsEnabled() && previousValue == Status_NotDisposedAndTrackedByTelemetry)
{
HttpTelemetry.Log.Http11ConnectionClosed();
HttpTelemetry.Log.Http11ConnectionClosed(_id);
}

if (!_detachedFromPool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace System.Net.Http
{
internal abstract class HttpConnectionBase : IDisposable, IHttpTrace
{
private static long s_connectionCounter = -1;
protected readonly long _id = Interlocked.Increment(ref s_connectionCounter);

/// <summary>Cached string for the last Date header received on this connection.</summary>
private string? _lastDateHeaderValue;
/// <summary>Cached string for the last Server header received on this connection.</summary>
Expand Down Expand Up @@ -47,7 +50,7 @@ protected void TraceConnection(Stream stream)
if (stream is SslStream sslStream)
{
Trace(
$"{this}. " +
$"{this}. Id:{_id}, " +
$"SslProtocol:{sslStream.SslProtocol}, NegotiatedApplicationProtocol:{sslStream.NegotiatedApplicationProtocol}, " +
$"NegotiatedCipherSuite:{sslStream.NegotiatedCipherSuite}, CipherAlgorithm:{sslStream.CipherAlgorithm}, CipherStrength:{sslStream.CipherStrength}, " +
$"HashAlgorithm:{sslStream.HashAlgorithm}, HashStrength:{sslStream.HashStrength}, " +
Expand All @@ -56,7 +59,7 @@ protected void TraceConnection(Stream stream)
}
else
{
Trace($"{this}");
Trace($"{this}. Id:{_id}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,24 +449,26 @@ private static void ValidateStartFailedStopEvents(ConcurrentQueue<(EventWrittenE
}
}

private static void ValidateConnectionEstablishedClosed(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events, Version version, int count = 1)
private static void ValidateConnectionEstablishedClosed(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events, Version version, long connectionId = 0)
{
EventWrittenEventArgs[] connectionsEstablished = events.Select(e => e.Event).Where(e => e.EventName == "ConnectionEstablished").ToArray();
Assert.Equal(count, connectionsEstablished.Length);
Assert.Equal(1, connectionsEstablished.Length);
foreach (EventWrittenEventArgs connectionEstablished in connectionsEstablished)
{
Assert.Equal(2, connectionEstablished.Payload.Count);
Assert.Equal(3, connectionEstablished.Payload.Count);
Assert.Equal(version.Major, (byte)connectionEstablished.Payload[0]);
Assert.Equal(version.Minor, (byte)connectionEstablished.Payload[1]);
Assert.Equal(connectionId, (long)connectionEstablished.Payload[2]);
}

EventWrittenEventArgs[] connectionsClosed = events.Select(e => e.Event).Where(e => e.EventName == "ConnectionClosed").ToArray();
Assert.Equal(count, connectionsClosed.Length);
Assert.Equal(1, connectionsClosed.Length);
foreach (EventWrittenEventArgs connectionClosed in connectionsClosed)
{
Assert.Equal(2, connectionClosed.Payload.Count);
Assert.Equal(3, connectionClosed.Payload.Count);
Assert.Equal(version.Major, (byte)connectionClosed.Payload[0]);
Assert.Equal(version.Minor, (byte)connectionClosed.Payload[1]);
Assert.Equal(connectionId, (long)connectionClosed.Payload[2]);
}
}

Expand Down