-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Report served-request usage #19
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
Open
gagantrivedi
wants to merge
22
commits into
main
Choose a base branch
from
feat/usage-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e2f646a
feat: report served-request usage
gagantrivedi 3a3c672
refactor: parking_lot mutex for usage counts, name the map by its key
gagantrivedi 174e1c1
fix: count usage from the response, only for 2xx
gagantrivedi 7c2c853
fix: refuse cross-origin pagination links
gagantrivedi 027803f
fix: resend a failed usage batch unchanged under an idempotency key
gagantrivedi 78d2f69
fix: wait a full interval after a slow usage flush
gagantrivedi 0444a39
docs: drop the UsageCounts doc comment
gagantrivedi efa494e
docs: drop the Resource doc comment
gagantrivedi c6bb20d
test: aggregate across two client keys
gagantrivedi 85eba1b
docs: drop the UsageBatch doc comment
gagantrivedi 583a101
docs: drop the parse_next_link doc comment
gagantrivedi 3507a91
refactor: move the layers into a middleware module
gagantrivedi 3595450
refactor: name the usage middleware module usage
gagantrivedi 3e1d7f2
docs: one-line MAX_ROWS_PER_FLUSH comment
gagantrivedi b62d9fd
docs: drop the pending-batch comment
gagantrivedi a91c7ed
docs: drop the track_usage doc comment
gagantrivedi 1168a6e
fix: bill every served environment the same once a proxy key is set
gagantrivedi feb2b49
chore: drop the bench dependency committed by mistake
gagantrivedi e2d65c5
refactor: move usage counting and reporting into UsageProcessor
gagantrivedi 1bfece5
refactor: UsageProcessor::record is track
gagantrivedi 7b97c21
refactor: rename pending to pending_batches
gagantrivedi 27d28db
fix: never follow redirects on requests carrying keys
gagantrivedi 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
File renamed without changes.
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,2 @@ | ||
| pub mod cors; | ||
| pub mod usage; |
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,40 @@ | ||
| use crate::routes::{ENVIRONMENT_DOCUMENT_PATH, FLAGS_PATH, IDENTITIES_PATH}; | ||
| use crate::state::AppState; | ||
| use crate::usage::Resource; | ||
| use axum::extract::{MatchedPath, Request, State}; | ||
| use axum::middleware::Next; | ||
| use axum::response::Response; | ||
|
|
||
| /// Count a request once the handler has answered: only a 2xx is usage. | ||
| pub async fn track_usage(State(state): State<AppState>, request: Request, next: Next) -> Response { | ||
| let resource = request | ||
| .extensions() | ||
| .get::<MatchedPath>() | ||
| .and_then(|path| resource_for(path.as_str())); | ||
| let environment_key = request | ||
| .headers() | ||
| .get("X-Environment-Key") | ||
| .and_then(|value| value.to_str().ok()) | ||
| .map(str::to_owned); | ||
|
|
||
| let response = next.run(request).await; | ||
|
|
||
| if !response.status().is_success() { | ||
| return response; | ||
| } | ||
| if let (Some(resource), Some(environment_key)) = (resource, environment_key) { | ||
| if let Some(keys) = state.environments.resolve(&environment_key) { | ||
| state.usage.track(&keys.client_key, resource); | ||
| } | ||
| } | ||
| response | ||
| } | ||
|
|
||
| fn resource_for(route: &str) -> Option<Resource> { | ||
| match route { | ||
| FLAGS_PATH => Some(Resource::Flags), | ||
| IDENTITIES_PATH => Some(Resource::Identities), | ||
| ENVIRONMENT_DOCUMENT_PATH => Some(Resource::EnvironmentDocument), | ||
| _ => None, | ||
| } | ||
| } | ||
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
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
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
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
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.