Repository navigation
Replace static parameters in route pattern for telemetry #64854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e2153b
Replace constant parameters in telemetry route pattern
JamesNK f1f3274
Clean up
JamesNK ec8c576
Clean up
JamesNK 6fd922c
Preserve forward slash
JamesNK f6fbcf8
Clean up
JamesNK 89ee7c9
Reduce test duplication
JamesNK bc94108
Fix test
JamesNK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/Http/Routing/src/Patterns/RoutePatternDebugStringFormatter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.AspNetCore.Routing.Patterns; | ||
|
|
||
| internal static class RoutePatternDebugStringFormatter | ||
| { | ||
| private const char Separator = '/'; | ||
| private const string SeparatorString = "/"; | ||
|
|
||
| public static string Format(RoutePattern pattern) | ||
| { | ||
| // If there are no required values that match parameters, use the simple approach | ||
| if (pattern.RawText is { Length: > 0 } rawText && !HasMatchingRequiredValues(pattern)) | ||
| { | ||
| return rawText; | ||
| } | ||
|
|
||
| // Build the string replacing parameters with their required values when available | ||
| var segments = new string[pattern.PathSegments.Count]; | ||
| for (var i = 0; i < pattern.PathSegments.Count; i++) | ||
| { | ||
| var segment = pattern.PathSegments[i]; | ||
| var segmentString = GetSegmentDebuggerToString(pattern, segment); | ||
| segments[i] = segmentString; | ||
| } | ||
|
|
||
| var result = string.Join(Separator, segments); | ||
|
|
||
| // Preserve leading slash from raw text | ||
| if (pattern.RawText is { Length: > 0 } rt && rt[0] == Separator) | ||
| { | ||
| result = Separator + result; | ||
| } | ||
|
|
||
| // Return "/" for empty results | ||
| if (result.Length == 0) | ||
| { | ||
| return SeparatorString; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static bool HasMatchingRequiredValues(RoutePattern pattern) | ||
| { | ||
| if (pattern.RequiredValues.Count == 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| for (var i = 0; i < pattern.Parameters.Count; i++) | ||
| { | ||
| if (TryGetRequiredValue(pattern, pattern.Parameters[i].Name, out _)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static string GetSegmentDebuggerToString(RoutePattern pattern, RoutePatternPathSegment segment) | ||
| { | ||
| // Simple segment with single parameter that has a required value - just return the required value | ||
| if (segment.IsSimple && segment.Parts[0] is RoutePatternParameterPart parameter) | ||
| { | ||
| if (TryGetRequiredValue(pattern, parameter.Name, out var requiredValue)) | ||
| { | ||
| return requiredValue; | ||
| } | ||
| } | ||
|
|
||
| // For complex segments, build the string part by part | ||
| var parts = new string[segment.Parts.Count]; | ||
| for (var i = 0; i < segment.Parts.Count; i++) | ||
| { | ||
| var part = segment.Parts[i]; | ||
| parts[i] = part is RoutePatternParameterPart paramPart && TryGetRequiredValue(pattern, paramPart.Name, out var value) | ||
| ? value | ||
| : part.DebuggerToString(); | ||
| } | ||
|
|
||
| return string.Join(string.Empty, parts); | ||
| } | ||
|
|
||
| private static bool TryGetRequiredValue(RoutePattern pattern, string parameterName, [NotNullWhen(true)] out string? value) | ||
| { | ||
| if (pattern.RequiredValues.TryGetValue(parameterName, out var requiredValue) && | ||
| requiredValue is not null && | ||
| !RoutePattern.IsRequiredValueAny(requiredValue) && | ||
| requiredValue.ToString() is { Length: > 0 } v) | ||
| { | ||
| value = v; | ||
| return true; | ||
| } | ||
|
|
||
| value = null; | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.