Skip to content

[API Proposal]: make payload types in System.Net.Http.DiagnosticsHandler to be public #82910

Open

Description

@xakep139

Background and motivation

Currently all folks who want to listen to HttpClient events with DiagnosticListener, need to use reflection to get request/response data from a payload. Here's an example of a class used to get these properties in OpenTelemetry repo: PropertyFetcher
This applies to both .NET Core/.NET 5+ and .NET Framework (HttpHandlerDiagnosticListener and System.Net.Http.Desktop listeners respectively).

API Proposal

To improve both user experience and performance, these types can be made public:

For .NET Framework, anonymous types used for payload can be also done as regular public types:

API Usage

var observer = new HttpClientDiagnosticListener();
using var listenerSubscription = DiagnosticListener.AllListeners.Subscribe(observer);

// ...

class HttpClientDiagnosticListener : IObserver<DiagnosticListener>, IObserver<KeyValuePair<string, object?>>, IDisposable
{
    private IDisposable? _listenerSubscription;

    public void Dispose() => _listenerSubscription?.Dispose();

    public void OnCompleted() => /* ... */;

    public void OnError(Exception error) => /* ... */;

    public void OnNext(DiagnosticListener listener)
    {
        if (listener.Name == "HttpHandlerDiagnosticListener")
        {
            _listenerSubscription?.Dispose();
            _listenerSubscription = listener.Subscribe(this);
        }
    }

    public void OnNext(KeyValuePair<string, object?> @event)
    {
        // NB: No reflection here, we just cast payload:
        switch (@event.Key)
        {
            case "System.Net.Http.Request":
                if (@event.Value is RequestData request)
                {
                    // ...
                }
                break;

            case "System.Net.Http.Response":
                if (@event.Value is ResponseData response)
                {
                    // ...
                }
                break;

            case "System.Net.Http.Exception":
                if (@event.Value is RequestData request)
                {
                    // ...
                }
                break;
        }
    }
}

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Mar 2, 2023
  2. ghost added
    untriaged
    on Mar 2, 2023
  3. dpk83 commented on Mar 2, 2023

    @dpk83
  4. tarekgh commented on Mar 2, 2023

    @tarekgh
    Member

    CC @karelz @ericstj @davidfowl

    Currently System.Net.Http library is shipped as inbox only and not released anymore as OOB in NuGet. Changing anything for .NET Framework will be challenging.

  5. tarekgh commented on Mar 2, 2023

    @tarekgh
    Member

    Looks the part need to be exposed for .NET Framework is in the System.Diagnostics.DiagnosticSource and not in System.Net.Http. So, supporting the .NET Framework part will not be difficult.

    Now, we need to look if the request is reasonable or should we have it.

  6. tarekgh commented on Mar 2, 2023

    @tarekgh
    Member
  7. noahfalk commented on Mar 3, 2023

    @noahfalk
    Member

    I think exposing a strong type in .NET Core is reasonable as long as the owners of HttpClient believe they can offer the same level of back compatibility for that data as we would offer for other APIs.

    For desktop we made a choice in the past not to do new feature work in the HttpHandlerDiagnosticsListener both to focus effort on .NET Core and to reduce the chance of accidentally breaking compatibility. When OpenTelemetry last asked for new features in that code we suggested they fork it and add any features they wanted that way: https://github.com/open-telemetry/opentelemetry-dotnet/blob/ab3e40790dde1d4cbc990e1871d831820430781d/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs#L33. Any other project that needs specific new desktop .NET functionality for that type could do something similar if desired.

  8. ManickaP commented on Mar 21, 2023

    @ManickaP
    Member

    Triage: This looks useful for instrumentation like OpenTelemetry to avoid unnecessary reflection. It is nice to have and not critical for 8.0, planning for future.

    As for Framework, we will not expose any new APIs there.

  9. removed
    untriaged
    on Mar 21, 2023
  10. added this to the Future milestone on Mar 21, 2023
  11. eerhardt commented on Jun 2, 2023

    @eerhardt
    Member

    Triage: This looks useful for instrumentation like OpenTelemetry to avoid unnecessary reflection. It is nice to have and not critical for 8.0, planning for future.

    We have a goal of getting OpenTelemetry AOT-compatible in .NET 8. See the "Stage 2" section of EPIC: Support publishing ASP.NET Core API apps with Native AOT. And open-telemetry/opentelemetry-dotnet#3429.

    @vitek-karas - given your work in trying to make OpenTelemetry AOT compatible, do you see this as a "nice to have"? Or is doing something here to eliminate Reflection critical to get OpenTelemetry AOT-compatible?

  12. vitek-karas commented on Jun 2, 2023

    @vitek-karas
    Member

    See #87008 for another request on this.

    I don't think this is essential for making OpenTelemetry AOT-compatible, because the current annotations in the HTTP will preserve the necessary properties. That said, the system is VERY fragile and unverifiable by tooling. It forces warning suppression in the consumer library. See #87008 for more details on the reasoning.

    I also think a big benefit of doing this would be that we codify the fact that it's a public contract which we should not break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Net.Http

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions