Skip to content

Commit

Permalink
Fix sonar warnings in Obervability for AB#16872.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMaki committed Jan 7, 2025
1 parent 1855630 commit ff13af7
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions Apps/Common/src/AspNetConfiguration/Modules/Observability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public static IApplicationBuilder UseDefaultHttpRequestLogging(this IApplication
opts =>
{
opts.IncludeQueryInRequestPath = true;

// ReSharper disable once RedundantDelegateCreation
opts.GetLevel = new Func<HttpContext, double, Exception?, LogEventLevel>((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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -169,7 +167,8 @@ public static IServiceCollection AddOpenTelemetryDefaults(this IServiceCollectio
builder
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation();
.AddRuntimeInstrumentation()
;

if (otlpConfig.MetricsConsoleExporterEnabled)
{
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit ff13af7

Please sign in to comment.