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

feat: configurable path parameters cardinality #73

Merged
merged 8 commits into from
Dec 1, 2023

Conversation

lefuturiste
Copy link
Contributor

@lefuturiste lefuturiste commented Aug 7, 2023

the idea for this MR was originally discussed in #72

A change that allow developer to change the cardinality of a given param name for a specific route.

To define a config that goes down the line, we need to use actix extensions_mut() method. The external/user developer need to define a middleware that will pass some data down the middleware stack.

Example when defining a route:

    web::resource("/{source_ids}/{z}/{x}/{y}")
        .wrap_fn(|req, srv| {
            req.extensions_mut().insert::<MetricsConfig>(
                MetricsConfig { cardinality_keep_params: vec!["source_ids".to_string()] }
            );
            srv.call(req)
        })
        .route(web::get().to(get_tile_handler))

Feel free to comment or help.

@lefuturiste
Copy link
Contributor Author

@nlopes You may take a look at this

@lefuturiste lefuturiste marked this pull request as ready for review September 6, 2023 09:33
@lefuturiste lefuturiste changed the title WIP: configurable for path cardinality feat: configurable for path cardinality Sep 6, 2023
@lefuturiste lefuturiste changed the title feat: configurable for path cardinality feat: configurable path params cardinality Sep 6, 2023
@lefuturiste lefuturiste changed the title feat: configurable path params cardinality feat: configurable path parameters cardinality Sep 6, 2023
@nlopes
Copy link
Owner

nlopes commented Sep 6, 2023

Thanks for sending over. I'll try to take a look sometime this week.

@lefuturiste
Copy link
Contributor Author

Thanks for sending over. I'll try to take a look sometime this week.

Hello, I'm kindly asking if you have some update on this, thanks! 😄

@nlopes
Copy link
Owner

nlopes commented Oct 8, 2023

Apologies for taking so long. Can you modify your example please? As it stands I don't think it actually exemplifies your change. Or am I misunderstanding it?

@lefuturiste
Copy link
Contributor Author

lefuturiste commented Oct 8, 2023

Apologies for taking so long. Can you modify your example please? As it stands I don't think it actually exemplifies your change. Or am I misunderstanding it?

Yes you are correct, I will update the example.

the main purpose for me is to have an example on which I can test with
configurable params cardinality feature.
Allow developer to externally set which on route params they want
to keep in the metrics (as metrics label cardinality) on a
particular route.
@lefuturiste
Copy link
Contributor Author

lefuturiste commented Oct 9, 2023

In theory I should have rebased this fork, and now I added an example. I also need to fix the code so that 404 are not counting as new labels values.

TODO:

  • clarify example
  • add unit test
  • fix 404 behavior
  • add test for 404 behavior

@lefuturiste lefuturiste changed the title feat: configurable path parameters cardinality WIP: feat: configurable path parameters cardinality Oct 9, 2023
@lefuturiste lefuturiste changed the title WIP: feat: configurable path parameters cardinality feat: configurable path parameters cardinality Oct 10, 2023
@lefuturiste
Copy link
Contributor Author

lefuturiste commented Oct 10, 2023

@nlopes can you take a look at this? It should be better now

@lefuturiste
Copy link
Contributor Author

Hello @nlopes sorry to ask you again, what can be done to get this forward? I know that maintaining opensource software is a burden 😢 .

Copy link
Owner

@nlopes nlopes left a comment

Choose a reason for hiding this comment

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

Almost there. Apologies, I've forgotten about this.

README.md Outdated
### Configurable routes pattern cardinality

Let's say you have on your app a route to fetch posts by language and by slug `GET /posts/{language}/{slug}`.
By default, actix-web-prom will provide metrics for the whole route with the label `endpoint` set to the pattern `/posts/{locale}/{slug}`.
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
By default, actix-web-prom will provide metrics for the whole route with the label `endpoint` set to the pattern `/posts/{locale}/{slug}`.
By default, actix-web-prom will provide metrics for the whole route with the label `endpoint` set to the pattern `/posts/{language}/{slug}`.

src/lib.rs Outdated
|| self.exclude_status.contains(&status)
{
return;
}

let final_pattern =
if fallback_pattern != pattern && (status == 404 || status == 405) { fallback_pattern }
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add a comment here on why this is necessary? I'm pretty sure we'll all forget why at some point.

src/lib.rs Outdated
match strfmt(&full_pattern, &params) {
Ok(mixed_cardinality_pattern) => mixed_cardinality_pattern,
Err(_) => {
// may be we need to log something to warn that we didn't managed to build the pattern?
Copy link
Owner

Choose a reason for hiding this comment

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

Yes, probably useful to bring in the log package.

fix: add warn log when failed to build mixed cardinality pattern
docs: typo fix

(apply review suggestions)
@lefuturiste
Copy link
Contributor Author

Almost there. Apologies, I've forgotten about this.

Thanks for your review, I've fix something

@nlopes nlopes merged commit 817fe1e into nlopes:main Dec 1, 2023
3 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.

2 participants