Skip to content
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

KTOR-8183 Add a simplified route string #4691

Merged
merged 8 commits into from
Feb 28, 2025

Conversation

adriandieter
Copy link
Contributor

@adriandieter adriandieter commented Feb 21, 2025

Subsystem
Server, server-core & metrics-micrometer

Motivation
KTOR-8183 Add a simplified route string

Solution
Add path extension property to RoutingNode. It returns a String representation of the path matched by the route. RoutingNodes not related to the path, i.e. from the authorization plugin or the http method are ommited here.

Make the route label string of micrometer metrics configurable with the transformRoute function in MicrometerMetricsConfig and use RoutingNode.path as default.

This fixes the issue of unwanted path elements, like /(authenticate "default")/uri, in the metrics route label.
The original behaviour can be configured by setting

transformRoute { 
    parent.toString() 
}

in the MicrometerMetricsConfig.

@e5l e5l self-assigned this Feb 21, 2025
@e5l e5l self-requested a review February 21, 2025 05:49
@e5l
Copy link
Member

e5l commented Feb 21, 2025

Hey @adriandieter, thanks you for the PR. Nice start! Some minor things need to be fixed, and maybe we can avoid making a new API so it can go to the patch release. Could you please take a look?

@adriandieter adriandieter force-pushed the KTOR-8183-simplified-route-string branch from c4be08d to 7d2709c Compare February 26, 2025 09:07
@adriandieter
Copy link
Contributor Author

One point I was not sure about, is if we should set the new path property as default value for the metrics route label or keep the original behavior as default.

@adriandieter adriandieter marked this pull request as ready for review February 26, 2025 11:16
@adriandieter adriandieter requested a review from e5l February 26, 2025 11:16
@adriandieter adriandieter changed the title KTOR-8183 Add RoutingNode.path extension property KTOR-8183 Add a simplified route string Feb 26, 2025
Copy link
Member

@osipxd osipxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adriandieter, thank you for the PR! I have a couple of comments.
Please, cherry-pick the changes from this PR on top of 3.2.0-eap branch. It is required as this PR introduces new APIs. After that, please fix the issues found by linter and update API dump using the following command:

./gradlew :ktor-server:ktor-server-core:apiDump :ktor-server:ktor-server-plugins:ktor-server-metrics-micrometer:apiDump

@@ -119,6 +119,34 @@ public class MicrometerMetricsConfig {
public fun timers(block: Timer.Builder.(ApplicationCall, Throwable?) -> Unit) {
timerBuilder = block
}

internal var transformRoute: RoutingNode.() -> String = { path }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: We should mark this issue as "impactful change" as it requires migration for those who relies on the old behavior

@adriandieter adriandieter force-pushed the KTOR-8183-simplified-route-string branch from 7d2709c to edf2d1b Compare February 26, 2025 16:05
@adriandieter adriandieter changed the base branch from main to 3.2.0-eap February 26, 2025 16:05
@adriandieter adriandieter force-pushed the KTOR-8183-simplified-route-string branch from 6d60bf3 to abaec70 Compare February 26, 2025 16:59
@adriandieter adriandieter force-pushed the KTOR-8183-simplified-route-string branch from abaec70 to ec00f45 Compare February 26, 2025 17:00
@adriandieter
Copy link
Contributor Author

Thanks for the quick review @osipxd! Addressed all comments.

@e5l e5l requested review from osipxd and e5l and removed request for e5l February 27, 2025 08:37
Copy link
Member

@e5l e5l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osipxd, could you check?

@e5l e5l self-requested a review February 27, 2025 08:38
Copy link
Member

@osipxd osipxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adriandieter, thank you! Looks good to me 👍

Comment on lines +372 to +377
is PathSegmentConstantRouteSelector,
is PathSegmentParameterRouteSelector,
is PathSegmentOptionalParameterRouteSelector,
is PathSegmentTailcardRouteSelector,
is PathSegmentWildcardRouteSelector,
is PathSegmentRegexRouteSelector -> toString()
Copy link
Member

@osipxd osipxd Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could introduce a supertype PathSegmentRouteSelector for these selectors to make it possible to create custom route selectors appearing in path. This change should be backward compatible.
@bjhham, what do you think?

@e5l e5l merged commit 1242eb2 into ktorio:3.2.0-eap Feb 28, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants