| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | |
| | | 4 | | namespace Intentum.AspNetCore; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Middleware that automatically observes HTTP request behaviors into a BehaviorSpace. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class BehaviorObservationMiddleware |
| | | 10 | | { |
| | | 11 | | private readonly RequestDelegate _next; |
| | | 12 | | private readonly BehaviorSpace _behaviorSpace; |
| | | 13 | | private readonly BehaviorObservationOptions _options; |
| | | 14 | | |
| | 0 | 15 | | public BehaviorObservationMiddleware( |
| | 0 | 16 | | RequestDelegate next, |
| | 0 | 17 | | BehaviorSpace behaviorSpace, |
| | 0 | 18 | | BehaviorObservationOptions? options = null) |
| | 0 | 19 | | { |
| | 0 | 20 | | _next = next ?? throw new ArgumentNullException(nameof(next)); |
| | 0 | 21 | | _behaviorSpace = behaviorSpace ?? throw new ArgumentNullException(nameof(behaviorSpace)); |
| | 0 | 22 | | _options = options ?? new BehaviorObservationOptions(); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task InvokeAsync(HttpContext context) |
| | 0 | 26 | | { |
| | 0 | 27 | | if (_options.Enabled) |
| | 0 | 28 | | { |
| | 0 | 29 | | var actor = _options.GetActor(context); |
| | 0 | 30 | | var action = _options.GetAction(context); |
| | | 31 | | |
| | 0 | 32 | | var metadata = new Dictionary<string, object> |
| | 0 | 33 | | { |
| | 0 | 34 | | { "path", context.Request.Path.Value ?? "" }, |
| | 0 | 35 | | { "method", context.Request.Method }, |
| | 0 | 36 | | { "statusCode", context.Response.StatusCode } |
| | 0 | 37 | | }; |
| | | 38 | | |
| | 0 | 39 | | if (_options.IncludeHeaders && context.Request.Headers.Any()) |
| | 0 | 40 | | { |
| | 0 | 41 | | metadata["headers"] = context.Request.Headers |
| | 0 | 42 | | .ToDictionary(h => h.Key, h => (object)h.Value.ToString()); |
| | 0 | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | _behaviorSpace.Observe(new BehaviorEvent( |
| | 0 | 46 | | actor, |
| | 0 | 47 | | action, |
| | 0 | 48 | | DateTimeOffset.UtcNow, |
| | 0 | 49 | | metadata)); |
| | 0 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | await _next(context); |
| | 0 | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Options for behavior observation middleware. |
| | | 58 | | /// </summary> |
| | | 59 | | public sealed class BehaviorObservationOptions |
| | | 60 | | { |
| | | 61 | | /// <summary>Whether observation is enabled. Defaults to true.</summary> |
| | | 62 | | public bool Enabled { get; set; } = true; |
| | | 63 | | |
| | | 64 | | /// <summary>Whether to include request headers in metadata. Defaults to false.</summary> |
| | | 65 | | public bool IncludeHeaders { get; set; } = false; |
| | | 66 | | |
| | | 67 | | /// <summary>Function to extract actor from HTTP context. Defaults to "http".</summary> |
| | | 68 | | public Func<HttpContext, string> GetActor { get; set; } = _ => "http"; |
| | | 69 | | |
| | | 70 | | /// <summary>Function to extract action from HTTP context. Defaults to method + path.</summary> |
| | | 71 | | public Func<HttpContext, string> GetAction { get; set; } = ctx => |
| | | 72 | | $"{ctx.Request.Method.ToLowerInvariant()}_{ctx.Request.Path.Value?.Replace("/", "_").TrimStart('_') ?? "root"}"; |
| | | 73 | | } |