-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
We've just added an endpoint matched event to diagnostic listeners with #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.
// Getting gRPC method name from metadata
var methodName = HttpContext.GetEndpoint().Metadata.GetMetadata<GrpcMethodMetadata>().Method.FullName;We could make this easier by having some generic diagnostic metadata that app frameworks set when building endpoints:
public class EndpointDiagnosticsMetadata
{
public Dictionary<string, string> Diagnostics { get; set; }
}EndpointBuilder endpointBuilder = CreateGrpcEndpoint(method);
// Loosely typed diagnostics values
var diagnostics = new Dictionary<string, string>
{
["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.