|
| 1 | +use std::{ |
| 2 | + sync::Arc, |
| 3 | + time::{Duration, SystemTime, UNIX_EPOCH}, |
| 4 | +}; |
| 5 | + |
| 6 | +use async_trait::async_trait; |
| 7 | +use graphql_parser::schema::Document; |
| 8 | +use hive_console_sdk::agent::{ExecutionReport, UsageAgent}; |
| 9 | +use hive_router_config::usage_reporting::UsageReportingConfig; |
| 10 | +use hive_router_plan_executor::execution::{ |
| 11 | + client_request_details::ClientRequestDetails, plan::PlanExecutionOutput, |
| 12 | +}; |
| 13 | +use ntex::web::HttpRequest; |
| 14 | +use rand::Rng; |
| 15 | +use tokio_util::sync::CancellationToken; |
| 16 | + |
| 17 | +use crate::{ |
| 18 | + background_tasks::{BackgroundTask, BackgroundTasksManager}, |
| 19 | + consts::ROUTER_VERSION, |
| 20 | +}; |
| 21 | + |
| 22 | +pub fn init_hive_user_agent( |
| 23 | + bg_tasks_manager: &mut BackgroundTasksManager, |
| 24 | + usage_config: &UsageReportingConfig, |
| 25 | +) -> Arc<UsageAgent> { |
| 26 | + let user_agent = format!("hive-router/{}", ROUTER_VERSION); |
| 27 | + let hive_user_agent = hive_console_sdk::agent::UsageAgent::new( |
| 28 | + usage_config.access_token.clone(), |
| 29 | + usage_config.endpoint.clone(), |
| 30 | + usage_config.target_id.clone(), |
| 31 | + usage_config.buffer_size, |
| 32 | + usage_config.connect_timeout, |
| 33 | + usage_config.request_timeout, |
| 34 | + usage_config.accept_invalid_certs, |
| 35 | + usage_config.flush_interval, |
| 36 | + user_agent, |
| 37 | + ); |
| 38 | + let hive_user_agent_arc = Arc::new(hive_user_agent); |
| 39 | + bg_tasks_manager.register_task(hive_user_agent_arc.clone()); |
| 40 | + hive_user_agent_arc |
| 41 | +} |
| 42 | + |
| 43 | +#[inline] |
| 44 | +pub fn collect_usage_report( |
| 45 | + schema: Arc<Document<'static, String>>, |
| 46 | + duration: Duration, |
| 47 | + req: &HttpRequest, |
| 48 | + client_request_details: &ClientRequestDetails, |
| 49 | + hive_usage_agent: &UsageAgent, |
| 50 | + usage_config: &UsageReportingConfig, |
| 51 | + execution_result: &PlanExecutionOutput, |
| 52 | +) { |
| 53 | + let mut rng = rand::rng(); |
| 54 | + let sampled = rng.random::<f64>() < usage_config.sample_rate.as_f64(); |
| 55 | + if !sampled { |
| 56 | + return; |
| 57 | + } |
| 58 | + if client_request_details |
| 59 | + .operation |
| 60 | + .name |
| 61 | + .is_some_and(|op_name| usage_config.exclude.contains(&op_name.to_string())) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + let client_name = get_header_value(req, &usage_config.client_name_header); |
| 66 | + let client_version = get_header_value(req, &usage_config.client_version_header); |
| 67 | + let timestamp = SystemTime::now() |
| 68 | + .duration_since(UNIX_EPOCH) |
| 69 | + .unwrap() |
| 70 | + .as_millis() as u64; |
| 71 | + let execution_report = ExecutionReport { |
| 72 | + schema, |
| 73 | + client_name: client_name.map(|s| s.to_owned()), |
| 74 | + client_version: client_version.map(|s| s.to_owned()), |
| 75 | + timestamp, |
| 76 | + duration, |
| 77 | + ok: execution_result.error_count == 0, |
| 78 | + errors: execution_result.error_count, |
| 79 | + operation_body: client_request_details.operation.query.to_owned(), |
| 80 | + operation_name: client_request_details |
| 81 | + .operation |
| 82 | + .name |
| 83 | + .map(|op_name| op_name.to_owned()), |
| 84 | + persisted_document_hash: None, |
| 85 | + }; |
| 86 | + |
| 87 | + if let Err(err) = hive_usage_agent.add_report(execution_report) { |
| 88 | + tracing::error!("Failed to send usage report: {}", err); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn get_header_value<'req>(req: &'req HttpRequest, header_name: &str) -> Option<&'req str> { |
| 93 | + req.headers().get(header_name).and_then(|v| v.to_str().ok()) |
| 94 | +} |
| 95 | + |
| 96 | +#[async_trait] |
| 97 | +impl BackgroundTask for UsageAgent { |
| 98 | + fn id(&self) -> &str { |
| 99 | + "hive_console_usage_report_task" |
| 100 | + } |
| 101 | + |
| 102 | + async fn run(&self, token: CancellationToken) { |
| 103 | + self.start_flush_interval(Some(token)).await |
| 104 | + } |
| 105 | +} |
0 commit comments