|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! This example demonstrates the trace feature in DataFusion’s runtime. |
| 19 | +//! When the `trace` feature is enabled, spawned tasks in DataFusion (such as those |
| 20 | +//! created during repartitioning or when reading Parquet files) are instrumented |
| 21 | +//! with the current tracing span, allowing to propagate any existing tracing context. |
| 22 | +//! |
| 23 | +//! In this example we create a session configured to use multiple partitions, |
| 24 | +//! register a Parquet table (based on the `alltypes_tiny_pages_plain.parquet` file), |
| 25 | +//! and run a query that should trigger parallel execution on multiple threads. |
| 26 | +//! We wrap the entire query execution within a custom span and log messages. |
| 27 | +//! By inspecting the tracing output, we should see that the tasks spawned |
| 28 | +//! internally inherit the span context. |
| 29 | +
|
| 30 | +use arrow::util::pretty::pretty_format_batches; |
| 31 | +use datafusion::arrow::record_batch::RecordBatch; |
| 32 | +use datafusion::datasource::file_format::parquet::ParquetFormat; |
| 33 | +use datafusion::datasource::listing::ListingOptions; |
| 34 | +use datafusion::error::Result; |
| 35 | +use datafusion::prelude::*; |
| 36 | +use datafusion::test_util::parquet_test_data; |
| 37 | +use std::sync::Arc; |
| 38 | +use tracing::{info, Level, instrument}; |
| 39 | + |
| 40 | +#[tokio::main] |
| 41 | +async fn main() -> Result<()> { |
| 42 | + // Initialize a tracing subscriber that prints to stdout. |
| 43 | + tracing_subscriber::fmt() |
| 44 | + .with_thread_ids(true) |
| 45 | + .with_thread_names(true) |
| 46 | + .with_max_level(Level::DEBUG) |
| 47 | + .init(); |
| 48 | + |
| 49 | + log::info!("Starting example, this log is not captured by tracing"); |
| 50 | + |
| 51 | + // execute the query within a tracing span |
| 52 | + let result = run_instrumented_query().await; |
| 53 | + |
| 54 | + info!( |
| 55 | + "Finished example. Check the logs above for tracing span details showing \ |
| 56 | +that tasks were spawned within the 'run_instrumented_query' span on different threads." |
| 57 | + ); |
| 58 | + |
| 59 | + result |
| 60 | +} |
| 61 | + |
| 62 | +#[instrument(level = "info")] |
| 63 | +async fn run_instrumented_query() -> Result<()> { |
| 64 | + info!("Starting query execution within the custom tracing span"); |
| 65 | + |
| 66 | + // The default session will set the number of partitions to `std::thread::available_parallelism()`. |
| 67 | + let ctx = SessionContext::new(); |
| 68 | + |
| 69 | + // Get the path to the test parquet data. |
| 70 | + let test_data = parquet_test_data(); |
| 71 | + // Build listing options that pick up only the "alltypes_tiny_pages_plain.parquet" file. |
| 72 | + let file_format = ParquetFormat::default().with_enable_pruning(true); |
| 73 | + let listing_options = ListingOptions::new(Arc::new(file_format)) |
| 74 | + .with_file_extension("alltypes_tiny_pages_plain.parquet"); |
| 75 | + |
| 76 | + info!("Registering Parquet table 'alltypes' from {test_data} in {listing_options:?}"); |
| 77 | + |
| 78 | + // Register a listing table using an absolute URL. |
| 79 | + let table_path = format!("file://{test_data}/"); |
| 80 | + ctx.register_listing_table( |
| 81 | + "alltypes", |
| 82 | + &table_path, |
| 83 | + listing_options.clone(), |
| 84 | + None, |
| 85 | + None, |
| 86 | + ) |
| 87 | + .await |
| 88 | + .expect("register_listing_table failed"); |
| 89 | + |
| 90 | + info!("Registered Parquet table 'alltypes' from {table_path}"); |
| 91 | + |
| 92 | + // Run a query that will trigger parallel execution on multiple threads. |
| 93 | + let sql = "SELECT COUNT(*), bool_col, date_string_col, string_col |
| 94 | + FROM ( |
| 95 | + SELECT bool_col, date_string_col, string_col FROM alltypes |
| 96 | + UNION ALL |
| 97 | + SELECT bool_col, date_string_col, string_col FROM alltypes |
| 98 | + ) AS t |
| 99 | + GROUP BY bool_col, date_string_col, string_col |
| 100 | + ORDER BY 1,2,3,4 DESC |
| 101 | + LIMIT 5;"; |
| 102 | + info!(%sql, "Executing SQL query"); |
| 103 | + let df = ctx.sql(sql).await?; |
| 104 | + |
| 105 | + let results: Vec<RecordBatch> = df.collect().await?; |
| 106 | + info!("Query execution complete"); |
| 107 | + |
| 108 | + // Print out the results and tracing output. |
| 109 | + datafusion::common::assert_batches_eq!( |
| 110 | + [ |
| 111 | + "+----------+----------+-----------------+------------+", |
| 112 | + "| count(*) | bool_col | date_string_col | string_col |", |
| 113 | + "+----------+----------+-----------------+------------+", |
| 114 | + "| 2 | false | 01/01/09 | 9 |", |
| 115 | + "| 2 | false | 01/01/09 | 7 |", |
| 116 | + "| 2 | false | 01/01/09 | 5 |", |
| 117 | + "| 2 | false | 01/01/09 | 3 |", |
| 118 | + "| 2 | false | 01/01/09 | 1 |", |
| 119 | + "+----------+----------+-----------------+------------+", |
| 120 | + ], |
| 121 | + &results |
| 122 | + ); |
| 123 | + |
| 124 | + info!("Query results:\n{}", pretty_format_batches(&results)?); |
| 125 | + |
| 126 | + Ok(()) |
| 127 | +} |
0 commit comments