| | | 1 | | using System.Diagnostics; |
| | | 2 | | using AsiBackbone.AspNetCore.DependencyInjection; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | using Microsoft.AspNetCore.Routing; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | using Microsoft.Extensions.Primitives; |
| | | 7 | | |
| | | 8 | | namespace AsiBackbone.AspNetCore.Correlation; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Resolves safe request correlation data from the current ASP.NET Core HTTP context. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class HttpContextAsiBackboneRequestCorrelationResolver : IAsiBackboneHttpRequestCorrelationResolver |
| | | 14 | | { |
| | | 15 | | private readonly IHttpContextAccessor httpContextAccessor; |
| | | 16 | | private readonly AsiBackboneAspNetCoreOptions options; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="HttpContextAsiBackboneRequestCorrelationResolver" /> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="httpContextAccessor">The ASP.NET Core HTTP context accessor.</param> |
| | | 22 | | /// <param name="options">The request correlation options.</param> |
| | 39 | 23 | | public HttpContextAsiBackboneRequestCorrelationResolver( |
| | 39 | 24 | | IHttpContextAccessor httpContextAccessor, |
| | 39 | 25 | | IOptions<AsiBackboneAspNetCoreOptions> options) |
| | | 26 | | { |
| | 39 | 27 | | this.httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); |
| | 39 | 28 | | this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 39 | 29 | | this.options.Validate(); |
| | 39 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public AsiBackboneHttpRequestCorrelation ResolveRequestCorrelation() |
| | | 34 | | { |
| | 37 | 35 | | HttpContext? httpContext = httpContextAccessor.HttpContext; |
| | | 36 | | |
| | 37 | 37 | | return httpContext is null |
| | 37 | 38 | | ? new AsiBackboneHttpRequestCorrelation(traceId: Activity.Current?.Id) |
| | 37 | 39 | | : new AsiBackboneHttpRequestCorrelation( |
| | 37 | 40 | | ResolveCorrelationId(httpContext), |
| | 37 | 41 | | ResolveTraceId(httpContext), |
| | 37 | 42 | | ResolveMetadata(httpContext)); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private string? ResolveCorrelationId(HttpContext httpContext) |
| | | 46 | | { |
| | 258 | 47 | | foreach (string headerName in options.CorrelationIdHeaderNames) |
| | | 48 | | { |
| | 97 | 49 | | if (string.IsNullOrWhiteSpace(headerName)) |
| | | 50 | | { |
| | | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 95 | 54 | | if (httpContext.Request.Headers.TryGetValue(headerName, out StringValues values)) |
| | | 55 | | { |
| | 16 | 56 | | string? value = values.FirstOrDefault(static candidate => !string.IsNullOrWhiteSpace(candidate)); |
| | 8 | 57 | | if (!string.IsNullOrWhiteSpace(value)) |
| | | 58 | | { |
| | 6 | 59 | | return value.Trim(); |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 29 | 64 | | return options.UseHttpContextTraceIdentifierAsCorrelationId |
| | 29 | 65 | | ? httpContext.TraceIdentifier |
| | 29 | 66 | | : null; |
| | 6 | 67 | | } |
| | | 68 | | |
| | | 69 | | private static string? ResolveTraceId(HttpContext httpContext) |
| | | 70 | | { |
| | 35 | 71 | | return Activity.Current?.Id ?? httpContext.TraceIdentifier; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | private Dictionary<string, string> ResolveMetadata(HttpContext httpContext) |
| | | 75 | | { |
| | 35 | 76 | | Dictionary<string, string> metadata = new(StringComparer.Ordinal); |
| | | 77 | | |
| | 35 | 78 | | if (options.IncludeRequestMethod && !string.IsNullOrWhiteSpace(httpContext.Request.Method)) |
| | | 79 | | { |
| | 4 | 80 | | metadata[AsiBackboneHttpRequestMetadataKeys.Method] = httpContext.Request.Method.Trim(); |
| | | 81 | | } |
| | | 82 | | |
| | 35 | 83 | | if (options.IncludeRequestPath && httpContext.Request.Path.HasValue) |
| | | 84 | | { |
| | 2 | 85 | | metadata[AsiBackboneHttpRequestMetadataKeys.Path] = httpContext.Request.Path.Value; |
| | | 86 | | } |
| | | 87 | | |
| | 35 | 88 | | Endpoint? endpoint = httpContext.GetEndpoint(); |
| | 35 | 89 | | var routeEndpoint = endpoint as RouteEndpoint; |
| | | 90 | | |
| | 35 | 91 | | if (options.IncludeEndpointDisplayName && !string.IsNullOrWhiteSpace(endpoint?.DisplayName)) |
| | | 92 | | { |
| | 4 | 93 | | metadata[AsiBackboneHttpRequestMetadataKeys.EndpointDisplayName] = endpoint.DisplayName.Trim(); |
| | | 94 | | } |
| | | 95 | | |
| | 35 | 96 | | if (options.IncludeRoutePattern && !string.IsNullOrWhiteSpace(routeEndpoint?.RoutePattern.RawText)) |
| | | 97 | | { |
| | 4 | 98 | | metadata[AsiBackboneHttpRequestMetadataKeys.RoutePattern] = routeEndpoint.RoutePattern.RawText.Trim(); |
| | | 99 | | } |
| | | 100 | | |
| | 35 | 101 | | if (options.IncludeRouteValues) |
| | | 102 | | { |
| | 82 | 103 | | foreach (KeyValuePair<string, object?> routeValue in httpContext.Request.RouteValues) |
| | | 104 | | { |
| | 8 | 105 | | if (routeValue.Value is null || string.IsNullOrWhiteSpace(routeValue.Key)) |
| | | 106 | | { |
| | | 107 | | continue; |
| | | 108 | | } |
| | | 109 | | |
| | 4 | 110 | | metadata[$"{AsiBackboneHttpRequestMetadataKeys.RouteValuePrefix}{routeValue.Key.Trim()}"] = |
| | 4 | 111 | | routeValue.Value.ToString()?.Trim() ?? string.Empty; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | 35 | 115 | | return metadata; |
| | | 116 | | } |
| | | 117 | | } |