-
Notifications
You must be signed in to change notification settings - Fork 3
feat(router): Hive Console Usage Reporting #499
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
ardatan
wants to merge
13
commits into
main
Choose a base branch
from
hive-usage-reporting
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
13 commits
Select commit
Hold shift + click to select a range
c397bf7
feat(router): Hive Console Usage Reporting
ardatan 878cd90
Use the published package
ardatan 609e3af
Go
ardatan add93b7
Fix
ardatan 0c5cff8
More
ardatan 9e0f5d1
..
ardatan 887e891
Update readme
ardatan b3d239f
Enabled
ardatan 92e7fa5
Enabled
ardatan 065f151
Improvements
ardatan cd15aa5
Go
ardatan 2a7e914
Update
ardatan 83384de
match patterns
ardatan 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
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,105 @@ | ||
| use std::{ | ||
| sync::Arc, | ||
| time::{Duration, SystemTime, UNIX_EPOCH}, | ||
| }; | ||
|
|
||
| use async_trait::async_trait; | ||
| use graphql_parser::schema::Document; | ||
| use hive_console_sdk::agent::{ExecutionReport, UsageAgent}; | ||
| use hive_router_config::usage_reporting::UsageReportingConfig; | ||
| use hive_router_plan_executor::execution::{ | ||
| client_request_details::ClientRequestDetails, plan::PlanExecutionOutput, | ||
| }; | ||
| use ntex::web::HttpRequest; | ||
| use rand::Rng; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| use crate::{ | ||
| background_tasks::{BackgroundTask, BackgroundTasksManager}, | ||
| consts::ROUTER_VERSION, | ||
| }; | ||
|
|
||
| pub fn init_hive_user_agent( | ||
| bg_tasks_manager: &mut BackgroundTasksManager, | ||
| usage_config: &UsageReportingConfig, | ||
| ) -> Arc<UsageAgent> { | ||
| let user_agent = format!("hive-router/{}", ROUTER_VERSION); | ||
| let hive_user_agent = hive_console_sdk::agent::UsageAgent::new( | ||
| usage_config.access_token.clone(), | ||
| usage_config.endpoint.clone(), | ||
| usage_config.target_id.clone(), | ||
| usage_config.buffer_size, | ||
| usage_config.connect_timeout, | ||
| usage_config.request_timeout, | ||
| usage_config.accept_invalid_certs, | ||
| usage_config.flush_interval, | ||
| user_agent, | ||
| ); | ||
| let hive_user_agent_arc = Arc::new(hive_user_agent); | ||
| bg_tasks_manager.register_task(hive_user_agent_arc.clone()); | ||
| hive_user_agent_arc | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn collect_usage_report( | ||
| schema: Arc<Document<'static, String>>, | ||
| duration: Duration, | ||
| req: &HttpRequest, | ||
| client_request_details: &ClientRequestDetails, | ||
| hive_usage_agent: &UsageAgent, | ||
| usage_config: &UsageReportingConfig, | ||
| execution_result: &PlanExecutionOutput, | ||
| ) { | ||
| let mut rng = rand::rng(); | ||
| let sampled = rng.random::<f64>() < usage_config.sample_rate.as_f64(); | ||
| if !sampled { | ||
| return; | ||
| } | ||
| if client_request_details | ||
| .operation | ||
| .name | ||
| .is_some_and(|op_name| usage_config.exclude.contains(&op_name.to_string())) | ||
| { | ||
| return; | ||
| } | ||
| let client_name = get_header_value(req, &usage_config.client_name_header); | ||
| let client_version = get_header_value(req, &usage_config.client_version_header); | ||
| let timestamp = SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .unwrap() | ||
| .as_millis() as u64; | ||
| let execution_report = ExecutionReport { | ||
| schema, | ||
| client_name: client_name.map(|s| s.to_owned()), | ||
| client_version: client_version.map(|s| s.to_owned()), | ||
| timestamp, | ||
| duration, | ||
| ok: execution_result.error_count == 0, | ||
| errors: execution_result.error_count, | ||
| operation_body: client_request_details.operation.query.to_owned(), | ||
| operation_name: client_request_details | ||
| .operation | ||
| .name | ||
| .map(|op_name| op_name.to_owned()), | ||
| persisted_document_hash: None, | ||
| }; | ||
|
|
||
| if let Err(err) = hive_usage_agent.add_report(execution_report) { | ||
| tracing::error!("Failed to send usage report: {}", err); | ||
| } | ||
| } | ||
|
|
||
| fn get_header_value<'req>(req: &'req HttpRequest, header_name: &str) -> Option<&'req str> { | ||
| req.headers().get(header_name).and_then(|v| v.to_str().ok()) | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl BackgroundTask for UsageAgent { | ||
| fn id(&self) -> &str { | ||
| "hive_console_usage_report_task" | ||
| } | ||
|
|
||
| async fn run(&self, token: CancellationToken) { | ||
| self.start_flush_interval(Some(token)).await | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be
as_millisinstead of sec*1000There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍