From 390c76b1b0bb09f8377e40e9a08bb07056a17a03 Mon Sep 17 00:00:00 2001 From: BrianMaki Date: Mon, 6 Jan 2025 18:18:18 -0800 Subject: [PATCH] Renamed methods in Patient for AB#16872. --- .../Modules/Observability.cs | 40 ++++++++++++------- .../src/Controllers/PatientDataController.cs | 8 ++-- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Apps/Common/src/AspNetConfiguration/Modules/Observability.cs b/Apps/Common/src/AspNetConfiguration/Modules/Observability.cs index f3685235f6..eaf425ea26 100644 --- a/Apps/Common/src/AspNetConfiguration/Modules/Observability.cs +++ b/Apps/Common/src/AspNetConfiguration/Modules/Observability.cs @@ -84,9 +84,7 @@ public static IApplicationBuilder UseDefaultHttpRequestLogging(this IApplication opts => { opts.IncludeQueryInRequestPath = true; - - // ReSharper disable once RedundantDelegateCreation - opts.GetLevel = new Func((httpCtx, _, exception) => ExcludePaths(httpCtx, exception, excludePaths ?? [])); + opts.GetLevel = (httpCtx, _, exception) => ExcludePaths(httpCtx, exception, excludePaths ?? []); opts.EnrichDiagnosticContext = (diagCtx, httpCtx) => { diagCtx.Set("Host", httpCtx.Request.Host.Value); @@ -106,7 +104,7 @@ public static IApplicationBuilder UseDefaultHttpRequestLogging(this IApplication /// OpenTelemetry configuration values. /// The DI container. #pragma warning disable CA1506 //Avoid excessive class coupling - public static IServiceCollection AddOpenTelemetryDefaults(this IServiceCollection services, OpenTelemetryConfig otlpConfig) // NOSONAR + public static IServiceCollection AddOpenTelemetryDefaults(this IServiceCollection services, OpenTelemetryConfig otlpConfig) { if (string.IsNullOrEmpty(otlpConfig.ServiceName)) { @@ -129,14 +127,14 @@ public static IServiceCollection AddOpenTelemetryDefaults(this IServiceCollectio .AddAspNetCoreInstrumentation( options => { - // ReSharper disable once RedundantLambdaParameterType - options.Filter = (HttpContext httpContext) => !Array.Exists( + options.Filter = httpContext => !Array.Exists( otlpConfig.IgnorePathPrefixes, s => httpContext.Request.Path.ToString().StartsWith(s, StringComparison.OrdinalIgnoreCase)); }) .AddRedisInstrumentation() .AddEntityFrameworkCoreInstrumentation() - .AddNpgsql(); + .AddNpgsql() + ; foreach (string source in otlpConfig.Sources) { @@ -169,7 +167,8 @@ public static IServiceCollection AddOpenTelemetryDefaults(this IServiceCollectio builder .AddHttpClientInstrumentation() .AddAspNetCoreInstrumentation() - .AddRuntimeInstrumentation(); + .AddRuntimeInstrumentation() + ; if (otlpConfig.MetricsConsoleExporterEnabled) { @@ -271,16 +270,29 @@ private static LogEventLevel ExcludePaths(HttpContext ctx, Exception? ex, string return LogEventLevel.Error; } - bool isWildcardMatch = Array.Exists(excludedPaths, path => IsWildcardMatch(ctx.Request.Path, path)); - return isWildcardMatch ? LogEventLevel.Verbose : LogEventLevel.Information; + return Array.Exists(excludedPaths, path => IsWildcardMatch(ctx.Request.Path, path)) + ? LogEventLevel.Verbose + : LogEventLevel.Information; } private static bool IsWildcardMatch(PathString requestPath, string path) { - return requestPath.HasValue && - (path.StartsWith('*') - ? requestPath.Value!.EndsWith(path.Replace("*", string.Empty, StringComparison.InvariantCultureIgnoreCase), StringComparison.InvariantCultureIgnoreCase) - : requestPath.Equals(path, StringComparison.InvariantCultureIgnoreCase)); + if (!requestPath.HasValue) + { + return false; + } + + if (path.EndsWith('*')) + { + return requestPath.Value!.StartsWith(path.Replace("*", string.Empty, StringComparison.InvariantCultureIgnoreCase), StringComparison.InvariantCultureIgnoreCase); + } + + if (path.StartsWith('*')) + { + return requestPath.Value!.EndsWith(path.Replace("*", string.Empty, StringComparison.InvariantCultureIgnoreCase), StringComparison.InvariantCultureIgnoreCase); + } + + return requestPath.Equals(path, StringComparison.InvariantCultureIgnoreCase); } } } diff --git a/Apps/Patient/src/Controllers/PatientDataController.cs b/Apps/Patient/src/Controllers/PatientDataController.cs index f2ebc09547..112fbef691 100644 --- a/Apps/Patient/src/Controllers/PatientDataController.cs +++ b/Apps/Patient/src/Controllers/PatientDataController.cs @@ -64,7 +64,7 @@ public PatientDataController(IPatientDataService patientDataService) [ProducesResponseType(StatusCodes.Status502BadGateway, Type = typeof(ProblemDetails))] public async Task> Get(string hdid, [FromQuery] PatientDataType[] patientDataTypes, CancellationToken ct) { - ValidateGetRequest(hdid, patientDataTypes); + ValidateRequest(hdid, patientDataTypes); PatientDataQuery query = new(hdid, patientDataTypes); return await this.patientDataService.QueryAsync(query, ct); } @@ -86,13 +86,13 @@ public async Task> Get(string hdid, [FromQuery [ProducesResponseType(StatusCodes.Status502BadGateway, Type = typeof(ProblemDetails))] public async Task> GetFile(string hdid, string fileId, CancellationToken ct) { - ValidateGetFileRequest(hdid, fileId); + ValidateFileRequest(hdid, fileId); PatientFileQuery query = new(hdid, fileId); return await this.patientDataService.QueryAsync(query, ct) ?? throw new NotFoundException($"file {fileId} not found"); } - private static void ValidateGetRequest(string hdid, PatientDataType[] patientDataTypes) + private static void ValidateRequest(string hdid, PatientDataType[] patientDataTypes) { if (string.IsNullOrEmpty(hdid)) { @@ -105,7 +105,7 @@ private static void ValidateGetRequest(string hdid, PatientDataType[] patientDat } } - private static void ValidateGetFileRequest(string hdid, string fileId) + private static void ValidateFileRequest(string hdid, string fileId) { if (string.IsNullOrEmpty(hdid)) {