Skip to content

Commit

Permalink
Adding Kestrel connection start/stop events (#2360)
Browse files Browse the repository at this point in the history
* connection start/stop events for kestrel

* addressing PR comments. 1. Made the params nullable 2. change case of LocalEndPoint var
  • Loading branch information
ajay-sainy authored Dec 20, 2023
1 parent ae0c844 commit 9134c14
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/TelemetryConsumption/Kestrel/IKestrelTelemetryConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ namespace Yarp.Telemetry.Consumption;
/// </summary>
public interface IKestrelTelemetryConsumer
{
/// <summary>
/// Called at the start of a connection.
/// </summary>
/// <param name="timestamp">Timestamp when the event was fired.</param>
/// <param name="connectionId">ID of the connection.</param>
/// <param name="localEndPoint">Local endpoint for the connection.</param>
/// <param name="remoteEndPoint">Remote endpoint for the connection.</param>
void OnConnectionStart(DateTime timestamp, string connectionId, string? localEndPoint, string? remoteEndPoint) { }

/// <summary>
/// Called at the end of a connection.
/// </summary>
/// <param name="timestamp">Timestamp when the event was fired.</param>
/// <param name="connectionId">ID of the connection.</param>
void OnConnectionStop(DateTime timestamp, string connectionId) { }

/// <summary>
/// Called at the start of a request.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/TelemetryConsumption/Kestrel/KestrelEventListenerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ protected override void OnEvent(IKestrelTelemetryConsumer[] consumers, EventWrit

switch (eventData.EventId)
{
case 1:
Debug.Assert(eventData.EventName == "ConnectionStart" && payload.Count == 3);
{
var connectionId = (string)payload[0];
var localEndPoint = (string?)payload[1];
var remoteEndPoint = (string?)payload[2];
foreach (var consumer in consumers)
{
consumer.OnConnectionStart(eventData.TimeStamp, connectionId, localEndPoint, remoteEndPoint);
}
}
break;

case 2:
Debug.Assert(eventData.EventName == "ConnectionStop" && payload.Count == 1);
{
var connectionId = (string)payload[0];
foreach (var consumer in consumers)
{
consumer.OnConnectionStop(eventData.TimeStamp, connectionId);
}
}
break;

case 3:
Debug.Assert(eventData.EventName == "RequestStart" && payload.Count == 5);
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ await test.Invoke(async uri =>

var expected = new[]
{
"OnConnectionStart-Kestrel",
"OnRequestStart-Kestrel",
"OnForwarderInvoke",
"OnForwarderStart",
Expand All @@ -144,6 +145,7 @@ await test.Invoke(async uri =>
"OnContentTransferred",
"OnForwarderStop",
"OnRequestStop-Kestrel",
"OnConnectionStop-Kestrel",
};

if (!useHttpsOnDestination)
Expand Down Expand Up @@ -283,8 +285,10 @@ public void OnRequestStart(DateTime timestamp, string scheme, string host, int p
public void OnConnectStart(DateTime timestamp, string address) => AddStage(nameof(OnConnectStart), timestamp);
public void OnConnectStop(DateTime timestamp) => AddStage(nameof(OnConnectStop), timestamp);
public void OnConnectFailed(DateTime timestamp, SocketError error, string exceptionMessage) => AddStage(nameof(OnConnectFailed), timestamp);
public void OnConnectionStart(DateTime timestamp, string connectionId, string localEndPoint, string remoteEndPoint) => AddStage($"{nameof(OnConnectionStart)}-Kestrel", timestamp);
public void OnRequestStart(DateTime timestamp, string connectionId, string requestId, string httpVersion, string path, string method) => AddStage($"{nameof(OnRequestStart)}-Kestrel", timestamp);
public void OnRequestStop(DateTime timestamp, string connectionId, string requestId, string httpVersion, string path, string method) => AddStage($"{nameof(OnRequestStop)}-Kestrel", timestamp);
public void OnConnectionStop(DateTime timestamp, string connectionId) => AddStage($"{nameof(OnConnectionStop)}-Kestrel", timestamp);
public void OnRedirect(DateTime timestamp, string redirectUri) => AddStage(nameof(OnRedirect), timestamp);
}

Expand Down

0 comments on commit 9134c14

Please sign in to comment.