diff --git a/src/telemetry/exporter.rs b/src/telemetry/exporter.rs new file mode 100644 index 00000000..50b5db3f --- /dev/null +++ b/src/telemetry/exporter.rs @@ -0,0 +1,34 @@ +// Copyright (c) 2026 Databricks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::models::TelemetryRequest; +use std::future::Future; +use std::pin::Pin; + +pub(crate) type TelemetryExportResult = Result<(), TelemetryExportError>; + +#[derive(Debug, thiserror::Error)] +pub(crate) enum TelemetryExportError { + #[error("telemetry serialization failed: {0}")] + Serialization(#[from] serde_json::Error), + #[error("telemetry export failed: {0}")] + Export(String), +} + +pub(crate) trait TelemetryExporter: Send + Sync { + fn export<'a>( + &'a self, + request: TelemetryRequest, + ) -> Pin + Send + 'a>>; +} diff --git a/src/telemetry/mod.rs b/src/telemetry/mod.rs index 4ab628a3..1c1988f5 100644 --- a/src/telemetry/mod.rs +++ b/src/telemetry/mod.rs @@ -12,101 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Telemetry and metrics collection for the Databricks ADBC driver. +//! Telemetry model and serialization support. +//! +//! These types mirror the SQL driver telemetry envelope used by the ADBC and +//! JDBC drivers. Phase 1 intentionally stops at model/serialization boundaries; +//! later phases add lifecycle observers and production export. -use std::time::{Duration, Instant}; +#![allow(dead_code)] -/// Configuration for telemetry collection. -/// -/// Telemetry is disabled by default and requires explicit opt-in. -/// When enabled, the driver collects anonymous usage metrics such as -/// query execution times and error counts. No query content or -/// personally identifiable information is collected. -#[derive(Debug, Clone, Default)] -pub struct TelemetryConfig { - /// Whether telemetry collection is enabled (default: false). - pub enabled: bool, -} - -/// Collects and reports driver telemetry data. -#[derive(Debug)] -pub struct TelemetryCollector { - config: TelemetryConfig, -} - -impl TelemetryCollector { - /// Creates a new telemetry collector with the given configuration. - pub fn new(config: TelemetryConfig) -> Self { - Self { config } - } - - /// Returns whether telemetry is enabled. - pub fn is_enabled(&self) -> bool { - self.config.enabled - } - - /// Records a query execution metric. - pub fn record_query_execution(&self, _duration: Duration, _rows_fetched: u64) { - if self.config.enabled { - // TODO: Implement telemetry reporting - } - } - - /// Records an error occurrence. - pub fn record_error(&self, _error_type: &str) { - if self.config.enabled { - // TODO: Implement telemetry reporting - } - } -} - -impl Default for TelemetryCollector { - fn default() -> Self { - Self::new(TelemetryConfig::default()) - } -} - -/// A timer for measuring operation durations. -#[derive(Debug)] -pub struct Timer { - start: Instant, -} - -impl Timer { - /// Starts a new timer. - pub fn start() -> Self { - Self { - start: Instant::now(), - } - } - - /// Returns the elapsed duration since the timer was started. - pub fn elapsed(&self) -> Duration { - self.start.elapsed() - } -} +mod exporter; +mod models; #[cfg(test)] -mod tests { - use super::*; - use std::thread::sleep; - - #[test] - fn test_telemetry_config_default_disabled() { - let config = TelemetryConfig::default(); - assert!(!config.enabled); - } - - #[test] - fn test_telemetry_collector_disabled() { - let collector = TelemetryCollector::new(TelemetryConfig { enabled: false }); - assert!(!collector.is_enabled()); - } - - #[test] - fn test_timer() { - let timer = Timer::start(); - sleep(Duration::from_millis(10)); - assert!(timer.elapsed() >= Duration::from_millis(10)); - } -} +pub(crate) mod test_support; diff --git a/src/telemetry/models.rs b/src/telemetry/models.rs new file mode 100644 index 00000000..bbb5d871 --- /dev/null +++ b/src/telemetry/models.rs @@ -0,0 +1,566 @@ +// Copyright (c) 2026 Databricks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use serde::{Deserialize, Serialize}; +use std::time::{SystemTime, UNIX_EPOCH}; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct TelemetryRequest { + pub upload_time: i64, + pub proto_logs: Vec, +} + +impl TelemetryRequest { + pub(crate) fn from_logs(logs: &[TelemetryFrontendLog]) -> Result { + let proto_logs = logs + .iter() + .map(serde_json::to_string) + .collect::, _>>()?; + Ok(Self { + upload_time: unix_time_millis(), + proto_logs, + }) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub(crate) struct TelemetryFrontendLog { + pub workspace_id: i64, + pub frontend_log_event_id: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub context: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub entry: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct FrontendLogContext { + #[serde(skip_serializing_if = "Option::is_none")] + pub client_context: Option, + pub timestamp_millis: i64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct TelemetryClientContext { + #[serde(skip_serializing_if = "Option::is_none")] + pub user_agent: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct FrontendLogEntry { + #[serde(skip_serializing_if = "Option::is_none")] + pub sql_driver_log: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub(crate) struct OssSqlDriverTelemetryLog { + #[serde(skip_serializing_if = "Option::is_none")] + pub session_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub sql_statement_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub system_configuration: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub driver_connection_params: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub auth_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub vol_operation: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub sql_operation: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub error_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub operation_latency_ms: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub(crate) struct DriverSystemConfiguration { + #[serde(skip_serializing_if = "Option::is_none")] + pub driver_version: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub runtime_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub runtime_version: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub runtime_vendor: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub os_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub os_version: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub os_arch: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub driver_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub client_app_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub locale_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub char_set_encoding: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub process_name: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub(crate) struct DriverConnectionParameters { + #[serde(skip_serializing_if = "Option::is_none")] + pub http_path: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub mode: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub host_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub use_proxy: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub auth_mech: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub auth_flow: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub auth_scope: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub use_system_proxy: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub non_proxy_hosts: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + pub use_cf_proxy: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub proxy_host_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub cf_proxy_host_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub discovery_mode_enabled: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub discovery_url: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub use_empty_metadata: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub support_many_parameters: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub ssl_trust_store_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub check_certificate_revocation: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub accept_undetermined_certificate_revocation: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub google_service_account: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub google_credential_file_path: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub http_connection_pool_size: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_sea_hybrid_results: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_complex_datatype_support: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub allow_self_signed_support: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub use_system_trust_store: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub rows_fetched_per_block: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub azure_workspace_resource_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub azure_tenant_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub string_column_length: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_token_cache: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub token_endpoint: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub auth_endpoint: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_arrow: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_direct_results: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_jwt_assertion: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub jwt_key_file: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub jwt_algorithm: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub socket_timeout: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub allowed_volume_ingestion_paths: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub async_poll_interval_millis: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub query_tags: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_metric_view_metadata: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub auto_commit: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub fetch_auto_commit_from_server: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub enable_geospatial_support: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct HostDetails { + #[serde(skip_serializing_if = "Option::is_none")] + pub host_url: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub port: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub proxy_auth_type: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct DriverErrorInfo { + #[serde(skip_serializing_if = "Option::is_none")] + pub error_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub stack_trace: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct VolumeOperationEvent { + #[serde(skip_serializing_if = "Option::is_none")] + pub volume_operation_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub volume_path: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub local_file: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub(crate) struct SqlExecutionEvent { + #[serde(skip_serializing_if = "Option::is_none")] + pub statement_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub is_compressed: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub execution_result: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub chunk_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub retry_count: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub chunk_details: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub result_latency: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub operation_detail: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub java_uses_patched_arrow: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct OperationDetail { + #[serde(skip_serializing_if = "Option::is_none")] + pub n_operation_status_calls: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub operation_status_latency_millis: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub operation_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub is_internal_call: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct ResultLatency { + #[serde(skip_serializing_if = "Option::is_none")] + pub result_set_ready_latency_millis: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub result_set_consumption_latency_millis: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct ChunkDetails { + #[serde(skip_serializing_if = "Option::is_none")] + pub initial_chunk_latency_millis: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub slowest_chunk_latency_millis: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub total_chunks_present: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub total_chunks_iterated: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub sum_chunks_download_time_millis: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum DriverMode { + TypeUnspecified, + Thrift, + Sea, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum StatementType { + TypeUnspecified, + Query, + Sql, + Update, + Metadata, + Volume, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum OperationType { + TypeUnspecified, + CreateSession, + DeleteSession, + ExecuteStatement, + ExecuteStatementAsync, + CloseStatement, + CancelStatement, + ListTypeInfo, + ListCatalogs, + ListSchemas, + ListTables, + ListTableTypes, + ListColumns, + ListFunctions, + ListPrimaryKeys, + ListImportedKeys, + ListExportedKeys, + ListCrossReferences, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum ExecutionResultFormat { + FormatUnspecified, + InlineArrow, + InlineJson, + ExternalLinks, + ColumnarInline, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum VolumeOperationType { + TypeUnspecified, + Put, + Get, + Delete, + List, + Query, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum DriverProxyAuth { + AuthUnspecified, + None, + Basic, + Spnego, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum DriverAuthMech { + TypeUnspecified, + Other, + Pat, + Oauth, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum DriverAuthFlow { + TypeUnspecified, + TokenPassthrough, + ClientCredentials, + BrowserBasedAuthentication, +} + +pub(super) fn unix_time_millis() -> i64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_millis() as i64 +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::telemetry::exporter::TelemetryExporter; + use crate::telemetry::test_support::CapturingTelemetryExporter; + use serde_json::Value; + + #[test] + fn telemetry_request_serializes_with_backend_shape() { + let request = TelemetryRequest { + upload_time: 42, + proto_logs: vec!["{\"workspace_id\":1}".to_string()], + }; + + let json = serde_json::to_value(&request).unwrap(); + + assert_eq!(json["uploadTime"], 42); + assert_eq!(json["protoLogs"][0], "{\"workspace_id\":1}"); + assert!(json.get("upload_time").is_none()); + assert!(json.get("proto_logs").is_none()); + } + + #[test] + fn telemetry_frontend_log_serializes_with_snake_case_fields() { + let log = sample_frontend_log(); + + let json = serde_json::to_value(&log).unwrap(); + + assert_eq!(json["workspace_id"], 12345); + assert_eq!(json["frontend_log_event_id"], "event-1"); + assert_eq!(json["context"]["timestamp_millis"], 1000); + assert_eq!( + json["context"]["client_context"]["user_agent"], + "databricks-sql-kernel/0.2.0" + ); + assert!(json.get("workspaceId").is_none()); + assert!(json.get("frontendLogEventId").is_none()); + } + + #[test] + fn telemetry_request_from_logs_serializes_frontend_logs_as_strings() { + let request = TelemetryRequest::from_logs(&[sample_frontend_log()]).unwrap(); + + assert_eq!(request.proto_logs.len(), 1); + let nested: Value = serde_json::from_str(&request.proto_logs[0]).unwrap(); + + assert_eq!(nested["workspace_id"], 12345); + assert_eq!(nested["entry"]["sql_driver_log"]["session_id"], "session-1"); + assert!(request.upload_time > 0); + } + + #[test] + fn oss_sql_driver_telemetry_log_has_proto_field_names() { + let log = OssSqlDriverTelemetryLog { + session_id: Some("session-1".to_string()), + sql_statement_id: Some("statement-1".to_string()), + system_configuration: Some(DriverSystemConfiguration { + driver_version: Some("0.2.0".to_string()), + runtime_name: Some("rust".to_string()), + runtime_version: Some("1.0".to_string()), + runtime_vendor: Some("rust-lang".to_string()), + os_name: Some("linux".to_string()), + os_version: Some("6.0".to_string()), + os_arch: Some("x86_64".to_string()), + driver_name: Some("Databricks SQL Kernel".to_string()), + client_app_name: Some("test-app".to_string()), + locale_name: Some("en_US".to_string()), + char_set_encoding: Some("UTF-8".to_string()), + process_name: Some("cargo".to_string()), + }), + driver_connection_params: Some(DriverConnectionParameters { + http_path: Some("/sql/1.0/warehouses/abc".to_string()), + mode: Some(DriverMode::Sea), + host_info: Some(HostDetails { + host_url: Some("https://example.databricks.com".to_string()), + port: Some(443), + proxy_auth_type: Some(DriverProxyAuth::None), + }), + auth_mech: Some(DriverAuthMech::Pat), + auth_flow: Some(DriverAuthFlow::ClientCredentials), + enable_direct_results: Some(true), + async_poll_interval_millis: Some(1000), + auto_commit: Some(true), + ..Default::default() + }), + auth_type: Some("pat".to_string()), + vol_operation: Some(VolumeOperationEvent { + volume_operation_type: Some(VolumeOperationType::List), + volume_path: Some("/Volumes/catalog/schema/volume".to_string()), + local_file: None, + }), + sql_operation: Some(SqlExecutionEvent { + statement_type: Some(StatementType::Sql), + execution_result: Some(ExecutionResultFormat::ExternalLinks), + operation_detail: Some(OperationDetail { + n_operation_status_calls: Some(2), + operation_status_latency_millis: Some(10), + operation_type: Some(OperationType::ExecuteStatement), + is_internal_call: Some(false), + }), + result_latency: Some(ResultLatency { + result_set_ready_latency_millis: Some(5), + result_set_consumption_latency_millis: Some(20), + }), + chunk_details: Some(ChunkDetails { + initial_chunk_latency_millis: Some(1), + slowest_chunk_latency_millis: Some(2), + total_chunks_present: Some(3), + total_chunks_iterated: Some(3), + sum_chunks_download_time_millis: Some(6), + }), + ..Default::default() + }), + error_info: Some(DriverErrorInfo { + error_name: Some("InvalidState".to_string()), + stack_trace: Some("sanitized".to_string()), + }), + operation_latency_ms: Some(25), + }; + + let json = serde_json::to_value(&log).unwrap(); + + for field in [ + "session_id", + "sql_statement_id", + "system_configuration", + "driver_connection_params", + "auth_type", + "vol_operation", + "sql_operation", + "error_info", + "operation_latency_ms", + ] { + assert!(json.get(field).is_some(), "missing field {field}"); + } + assert_eq!(json["driver_connection_params"]["mode"], "SEA"); + assert_eq!(json["driver_connection_params"]["auth_mech"], "PAT"); + assert_eq!(json["sql_operation"]["execution_result"], "EXTERNAL_LINKS"); + } + + #[tokio::test] + async fn capturing_exporter_records_requests() { + let exporter = CapturingTelemetryExporter::default(); + let request = TelemetryRequest { + upload_time: 42, + proto_logs: vec!["{}".to_string()], + }; + + exporter.export(request.clone()).await.unwrap(); + + assert_eq!(exporter.requests(), vec![request]); + } + + fn sample_frontend_log() -> TelemetryFrontendLog { + TelemetryFrontendLog { + workspace_id: 12345, + frontend_log_event_id: "event-1".to_string(), + context: Some(FrontendLogContext { + client_context: Some(TelemetryClientContext { + user_agent: Some("databricks-sql-kernel/0.2.0".to_string()), + }), + timestamp_millis: 1000, + }), + entry: Some(FrontendLogEntry { + sql_driver_log: Some(OssSqlDriverTelemetryLog { + session_id: Some("session-1".to_string()), + operation_latency_ms: Some(10), + ..Default::default() + }), + }), + } + } +} diff --git a/src/telemetry/test_support.rs b/src/telemetry/test_support.rs new file mode 100644 index 00000000..a0142201 --- /dev/null +++ b/src/telemetry/test_support.rs @@ -0,0 +1,48 @@ +// Copyright (c) 2026 Databricks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::exporter::{TelemetryExportResult, TelemetryExporter}; +use super::models::TelemetryRequest; +use std::future::Future; +use std::pin::Pin; +use std::sync::{Arc, Mutex}; + +#[derive(Debug, Default, Clone)] +pub(crate) struct CapturingTelemetryExporter { + requests: Arc>>, +} + +impl CapturingTelemetryExporter { + pub(crate) fn requests(&self) -> Vec { + self.requests + .lock() + .expect("poisoned telemetry capture") + .clone() + } +} + +impl TelemetryExporter for CapturingTelemetryExporter { + fn export<'a>( + &'a self, + request: TelemetryRequest, + ) -> Pin + Send + 'a>> { + Box::pin(async move { + self.requests + .lock() + .expect("poisoned telemetry capture") + .push(request); + Ok(()) + }) + } +}