Endpoint diagnostics metadata
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
We've just added an endpoint matched event to diagnostic listeners with https://github.com/aspnet/AspNetCore/pull/11685
From this event AppInsights/OpenTelemetry can get the HttpContext, and the matched endpoint via `HttpContext.GetEndpoint()` and then look at endpoint metadata. Endpoints produced by app frameworks are adding typed metadata about the endpoint to `Endpoint.Metadata`. For example, gRPC adds `GrpcMethodMetadata` to gRPC endpoints, MVC adds `ActionDescriptor` to MVC endpoints, etc. Using this metadata telemetry frameworks can figure out what the request is for.
The problem with this is the metadata is all strongly typed, which means telemetry frameworks will either need complex reflection or dependencies to query it.
```cs
// Getting gRPC method name from metadata
var methodName = HttpContext.GetEndpoint().Metadata.GetMetadata().Method.FullName;
```
We could make this easier by having some generic diagnostic metadata that app frameworks set when building endpoints:
```cs
public class EndpointDiagnosticsMetadata
{
public Dictionary Diagnostics { get; set; }
}
```
```cs
EndpointBuilder endpointBuilder = CreateGrpcEndpoint(method);
// Loosely typed diagnostics values
var diagnostics = new Dictionary
{
["grpc.method_name"] = method.FullName,
["grpc.method_type"] = method.Type.ToString(),
["grpc.service"] = typeof(TService).FullName,
};
endpointBuilder.Metadata.Add(new EndpointDiagnosticsMetadata(diagnostics));
// Continue to add strongly typed endpoint metadata
endpointBuilder.Metadata.Add(new GrpcMethodMetadata(method));
var endpoint = endpointBuilder.Build();
```
Now telemetry frameworks can simply copy name/values from the diagnostics collection.
Benefits:
* Remove the need for complex reflection or dependencies in telemetry frameworks
* New and community web frameworks could add diagnostics about endpoints that would automatically light up in telemetry frameworks. They don't need to add support for app frameworks one-by-one.
@davidfowl @rynowak @lmolkova @SergeyKanzhelev
Contributor guide
Assessment
This issue has not been assessed yet.