Consider adding a telemetry event for failed HTTP connection attempts

Open
#110,351 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start by reading the connection-failure paths in HttpConnectionPool.Http1.cs, HttpConnectionPool.Http2.cs, and HttpConnectionPool.Http3.cs at the referenced lines, then inspect the System.Net.Http EventSource implementation. The work is done when the telemetry design and coverage for connection-establishment failures are agreed and implemented across the relevant protocol paths.

Written by the indexing model from the issue text.

Description

area-System.Net.Http enhancement

With how requests are split from connections in SocketsHttpHandler, the user may not always see the errors that occurred during connection establishment (dns + tcp + tls + http/2 handshake).
The errors may be swallowed if we already served the initiating request with a different connection, or if that request timed out.
There are therefore situations where you may have poor visibility into what's happening.

One way of gaining the info is to inject a custom ConnectCallback along the lines of

handler.ConnectCallback = async (context, ct) =>
{
    var stopwatch = Stopwatch.StartNew();
    var socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
    try
    {
        await socket.ConnectAsync(context.DnsEndPoint, ct);
        return new NetworkStream(socket, ownsSocket: true);
    }
    catch (Exception ex)
    {
        socket.Dispose();
        Console.WriteLine($"Failed to connect to {context.DnsEndPoint} after {stopwatch.ElapsedMilliseconds:N2}: {ex}");
        throw;
    }
};

This gives you visibility into failures when trying to connect, but not into TLS failures/timeouts.
It's possible to also perform the TLS handshake in the callback, but we don't always make it trivial (how do you pick the host, how do you handle ALPN, potential custom ssl settings, ...).


It seems rather simple for us to add an EventSource event to System.Net.Http that just logs all such failures, corresponding to
https://github.com/dotnet/runtime/blob/05fa8813a7774890b46e60109d8cd6d4c853a014/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.Http1.cs#L307-L309
https://github.com/dotnet/runtime/blob/05fa8813a7774890b46e60109d8cd6d4c853a014/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.Http2.cs#L271-L273
https://github.com/dotnet/runtime/blob/05fa8813a7774890b46e60109d8cd6d4c853a014/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.Http3.cs#L325-L329

Maybe something like

[Event(42, Level = EventLevel.Error)]
public void ConnectionFailed(byte versionMajor, byte versionMinor, string scheme, string host, int port, string? remoteAddress, double elapsedMilliseconds, string exception);

Does this seem reasonable @antonfirsov, maybe we have better ways of exposing such data now?

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.