Skip to content

Commit 9937ab3

Browse files
committed
test(telemetry): cover list tables metadata telemetry
Signed-off-by: Jay Xiao <jay.xiao@databricks.com>
1 parent 4773450 commit 9937ab3

2 files changed

Lines changed: 199 additions & 4 deletions

File tree

‎src/metadata_v0.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ impl Metadata {
680680
.map_err(|e| Error::data_loss().message(format!("merge tables: {e}")))?;
681681
Some(thrift_shape::sort_tables_batch(&concatenated)?)
682682
};
683+
// The fan-out performs real per-catalog server statements, but the
684+
// caller receives one merged in-memory result and there is no single
685+
// statement id to attribute. Record that aggregate as one synthetic
686+
// inline LIST_TABLES event; the internal catalog enumeration uses
687+
// noop telemetry to avoid double-counting a LIST_CATALOGS operation.
683688
Self::observe_inline_result(telemetry);
684689
ResultStream::from_reader(Box::new(InMemoryReader::new(
685690
GET_TABLES_SCHEMA.clone(),

‎src/session.rs‎

Lines changed: 194 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,15 +1852,15 @@ mod is_open_tests {
18521852
use crate::telemetry::test_support::CapturingTelemetryExporter;
18531853
use crate::telemetry::{TelemetryExportResult, TelemetryExporter, TelemetryRequest};
18541854
use crate::ResultBatch;
1855-
use arrow_array::{Int64Array, RecordBatch, StringArray};
1855+
use arrow_array::{BooleanArray, Int64Array, RecordBatch, StringArray};
18561856
use arrow_ipc::writer::StreamWriter;
18571857
use arrow_schema::{DataType, Field, Schema};
18581858
use base64::{engine::general_purpose::STANDARD, Engine};
18591859
use serde_json::json;
18601860
use serde_json::Value;
18611861
use std::future::Future;
18621862
use std::pin::Pin;
1863-
use wiremock::matchers::{method, path, path_regex};
1863+
use wiremock::matchers::{body_string_contains, method, path, path_regex};
18641864
use wiremock::{Mock, MockServer, ResponseTemplate};
18651865

18661866
/// `Session::is_open` reports true for a freshly-opened session and
@@ -2734,6 +2734,139 @@ mod is_open_tests {
27342734
);
27352735
}
27362736

2737+
#[tokio::test]
2738+
async fn metadata_list_tables_fanout_exports_one_synthetic_event() {
2739+
let server = MockServer::start().await;
2740+
mount_session_create_delete(&server, "sess-telemetry-tables-fanout").await;
2741+
mount_close_any_statement(&server).await;
2742+
mount_execute_for_sql(
2743+
&server,
2744+
"SHOW CATALOGS",
2745+
succeeded_typed_inline_body(
2746+
"stmt-telemetry-tables-catalogs",
2747+
&string_batch("catalog", vec!["cat_a", "cat_b", "other"]),
2748+
),
2749+
)
2750+
.await;
2751+
mount_execute_for_sql(
2752+
&server,
2753+
"SHOW TABLES IN CATALOG `cat_a`",
2754+
succeeded_typed_inline_body(
2755+
"stmt-telemetry-tables-cat-a",
2756+
&server_tables_batch(vec![("s1", "t1", "cat_a", "TABLE")]),
2757+
),
2758+
)
2759+
.await;
2760+
mount_execute_for_sql(
2761+
&server,
2762+
"SHOW TABLES IN CATALOG `cat_b`",
2763+
succeeded_typed_inline_body(
2764+
"stmt-telemetry-tables-cat-b",
2765+
&server_tables_batch(vec![("s2", "v1", "cat_b", "VIEW")]),
2766+
),
2767+
)
2768+
.await;
2769+
let exporter = CapturingTelemetryExporter::default();
2770+
let telemetry = TelemetryConfig {
2771+
enabled: true,
2772+
..Default::default()
2773+
}
2774+
.with_exporter(std::sync::Arc::new(exporter.clone()));
2775+
let session =
2776+
telemetry_test_session(&server, "sess-telemetry-tables-fanout", telemetry).await;
2777+
2778+
let catalog_pattern =
2779+
crate::metadata_v0::LikePattern::new("cat\\_%").expect("valid catalog pattern");
2780+
let mut stream = session
2781+
.metadata()
2782+
.list_tables(Some(catalog_pattern), None, None, None)
2783+
.await
2784+
.expect("list_tables fan-out succeeds");
2785+
assert!(stream.next_batch().await.expect("first batch").is_some());
2786+
assert!(stream
2787+
.next_batch()
2788+
.await
2789+
.expect("stream exhausted")
2790+
.is_none());
2791+
drop(stream);
2792+
session.close().await.expect("close succeeds");
2793+
2794+
let logs = exported_logs(&exporter);
2795+
let list_tables_logs = logs
2796+
.iter()
2797+
.filter(|log| operation_type(log) == Some("LIST_TABLES"))
2798+
.collect::<Vec<_>>();
2799+
assert_eq!(
2800+
list_tables_logs.len(),
2801+
1,
2802+
"fan-out should export exactly one synthetic LIST_TABLES event"
2803+
);
2804+
assert!(
2805+
logs.iter()
2806+
.all(|log| operation_type(log) != Some("LIST_CATALOGS")),
2807+
"internal catalog enumeration should use noop telemetry"
2808+
);
2809+
let sql_log = &list_tables_logs[0]["entry"]["sql_driver_log"];
2810+
assert_eq!(sql_log["sql_operation"]["statement_type"], "METADATA");
2811+
assert_eq!(sql_log["sql_operation"]["execution_result"], "INLINE_ARROW");
2812+
assert!(
2813+
sql_log.get("sql_statement_id").is_none(),
2814+
"merged fan-out result has no single server statement id"
2815+
);
2816+
}
2817+
2818+
#[tokio::test]
2819+
async fn metadata_list_tables_single_catalog_exports_server_result_metadata() {
2820+
let server = MockServer::start().await;
2821+
mount_session_create_delete(&server, "sess-telemetry-tables-single").await;
2822+
mount_close_any_statement(&server).await;
2823+
mount_execute(
2824+
&server,
2825+
succeeded_typed_inline_body(
2826+
"stmt-telemetry-tables-single",
2827+
&server_tables_batch(vec![("default", "t1", "main", "TABLE")]),
2828+
),
2829+
)
2830+
.await;
2831+
let exporter = CapturingTelemetryExporter::default();
2832+
let telemetry = TelemetryConfig {
2833+
enabled: true,
2834+
..Default::default()
2835+
}
2836+
.with_exporter(std::sync::Arc::new(exporter.clone()));
2837+
let session =
2838+
telemetry_test_session(&server, "sess-telemetry-tables-single", telemetry).await;
2839+
2840+
let catalog = crate::metadata_v0::LikePattern::new("main").expect("valid catalog pattern");
2841+
let mut stream = session
2842+
.metadata()
2843+
.list_tables(Some(catalog), None, None, None)
2844+
.await
2845+
.expect("list_tables single catalog succeeds");
2846+
assert!(stream.next_batch().await.expect("first batch").is_some());
2847+
assert!(stream
2848+
.next_batch()
2849+
.await
2850+
.expect("stream exhausted")
2851+
.is_none());
2852+
drop(stream);
2853+
session.close().await.expect("close succeeds");
2854+
2855+
let list_tables_logs = exported_logs(&exporter)
2856+
.into_iter()
2857+
.filter(|log| operation_type(log) == Some("LIST_TABLES"))
2858+
.collect::<Vec<_>>();
2859+
assert_eq!(
2860+
list_tables_logs.len(),
2861+
1,
2862+
"single-catalog path should export one LIST_TABLES event"
2863+
);
2864+
let sql_log = &list_tables_logs[0]["entry"]["sql_driver_log"];
2865+
assert_eq!(sql_log["sql_statement_id"], "stmt-telemetry-tables-single");
2866+
assert_eq!(sql_log["sql_operation"]["statement_type"], "METADATA");
2867+
assert_eq!(sql_log["sql_operation"]["execution_result"], "INLINE_ARROW");
2868+
}
2869+
27372870
#[tokio::test]
27382871
async fn metadata_argument_error_exports_sanitized_error_telemetry() {
27392872
let server = MockServer::start().await;
@@ -2871,6 +3004,15 @@ mod is_open_tests {
28713004
.await;
28723005
}
28733006

3007+
async fn mount_execute_for_sql(server: &MockServer, sql_substr: &str, body: String) {
3008+
Mock::given(method("POST"))
3009+
.and(path("/api/2.0/sql/statements"))
3010+
.and(body_string_contains(sql_substr))
3011+
.respond_with(ResponseTemplate::new(200).set_body_string(body))
3012+
.mount(server)
3013+
.await;
3014+
}
3015+
28743016
async fn mount_status(server: &MockServer, statement_id: &str, body: String) {
28753017
Mock::given(method("GET"))
28763018
.and(path(format!("/api/2.0/sql/statements/{statement_id}")))
@@ -2907,6 +3049,36 @@ mod is_open_tests {
29073049
.expect("valid batch")
29083050
}
29093051

3052+
fn server_tables_batch(rows: Vec<(&str, &str, &str, &str)>) -> RecordBatch {
3053+
let schema = std::sync::Arc::new(Schema::new(vec![
3054+
Field::new("namespace", DataType::Utf8, false),
3055+
Field::new("tableName", DataType::Utf8, false),
3056+
Field::new("isTemporary", DataType::Boolean, false),
3057+
Field::new("information", DataType::Utf8, true),
3058+
Field::new("catalogName", DataType::Utf8, false),
3059+
Field::new("tableType", DataType::Utf8, false),
3060+
Field::new("remarks", DataType::Utf8, true),
3061+
]));
3062+
let namespaces = rows.iter().map(|row| row.0).collect::<Vec<_>>();
3063+
let table_names = rows.iter().map(|row| row.1).collect::<Vec<_>>();
3064+
let catalog_names = rows.iter().map(|row| row.2).collect::<Vec<_>>();
3065+
let table_types = rows.iter().map(|row| row.3).collect::<Vec<_>>();
3066+
let row_count = rows.len();
3067+
RecordBatch::try_new(
3068+
schema,
3069+
vec![
3070+
std::sync::Arc::new(StringArray::from(namespaces)),
3071+
std::sync::Arc::new(StringArray::from(table_names)),
3072+
std::sync::Arc::new(BooleanArray::from(vec![false; row_count])),
3073+
std::sync::Arc::new(StringArray::from(vec![Some(""); row_count])),
3074+
std::sync::Arc::new(StringArray::from(catalog_names)),
3075+
std::sync::Arc::new(StringArray::from(table_types)),
3076+
std::sync::Arc::new(StringArray::from(vec![Some(""); row_count])),
3077+
],
3078+
)
3079+
.expect("valid server tables batch")
3080+
}
3081+
29103082
fn arrow_ipc_bytes(batch: &RecordBatch) -> Vec<u8> {
29113083
let mut buf = Vec::new();
29123084
{
@@ -2918,6 +3090,23 @@ mod is_open_tests {
29183090
}
29193091

29203092
fn succeeded_inline_body(statement_id: &str, batch: &RecordBatch) -> String {
3093+
succeeded_inline_body_with_column_types(statement_id, batch, |_| ("BIGINT", "BIGINT"))
3094+
}
3095+
3096+
fn succeeded_typed_inline_body(statement_id: &str, batch: &RecordBatch) -> String {
3097+
succeeded_inline_body_with_column_types(statement_id, batch, |data_type| match data_type {
3098+
DataType::Boolean => ("BOOLEAN", "BOOLEAN"),
3099+
DataType::Int64 => ("BIGINT", "BIGINT"),
3100+
DataType::Utf8 => ("STRING", "STRING"),
3101+
other => panic!("test helper does not map Arrow type {other:?}"),
3102+
})
3103+
}
3104+
3105+
fn succeeded_inline_body_with_column_types(
3106+
statement_id: &str,
3107+
batch: &RecordBatch,
3108+
column_type: impl Fn(&DataType) -> (&'static str, &'static str),
3109+
) -> String {
29213110
let ipc = arrow_ipc_bytes(batch);
29223111
let attachment = STANDARD.encode(&ipc);
29233112
let columns: Vec<_> = batch
@@ -2926,10 +3115,11 @@ mod is_open_tests {
29263115
.iter()
29273116
.enumerate()
29283117
.map(|(i, f)| {
3118+
let (type_name, type_text) = column_type(f.data_type());
29293119
json!({
29303120
"name": f.name(),
2931-
"type_name": "BIGINT",
2932-
"type_text": "BIGINT",
3121+
"type_name": type_name,
3122+
"type_text": type_text,
29333123
"position": i as i32,
29343124
})
29353125
})

0 commit comments

Comments
 (0)