-
Couldn't load subscription status.
- 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
base: main
Are you sure you want to change the base?
Changes from all commits
71d3fad
61308f1
df7b84d
897fc6e
9b86fa0
cc2e86b
e0fd297
File filter
Filter by extension
Conversations
Jump to
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| 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, consts::ROUTER_VERSION}; | ||
|
|
||
| pub fn create_hive_user_agent(usage_config: &UsageReportingConfig) -> UsageAgent { | ||
| let user_agent = format!("hive-router/{}", ROUTER_VERSION); | ||
| 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.as_secs(), | ||
| usage_config.request_timeout.as_secs(), | ||
| usage_config.accept_invalid_certs, | ||
| usage_config.flush_interval, | ||
| user_agent, | ||
| ) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn collect_usage_report( | ||
| schema: Arc<Document<'static, String>>, | ||
| duration: Duration, | ||
| req: &HttpRequest, | ||
| client_request_details: &ClientRequestDetails, | ||
| usage_agent: &UsageAgent, | ||
| usage_config: &UsageReportingConfig, | ||
| execution_result: &PlanExecutionOutput, | ||
| ) { | ||
| let mut rng = rand::rng(); | ||
| let sampled = rng.random::<f64>() < usage_config.sample_rate; | ||
| 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() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be |
||
| .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) = 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 | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
why use the custom one? we dropped it on purpose.
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.
SDK uses this one.
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.
So we need to fix that in SDK. I don't think the SDK has any reason to still use it now.
Router shouldn't use this custom one