-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: instrument spawned tasks with current tracing span when tracing
feature is enabled
#14547
Open
geoffreyclaude
wants to merge
1
commit into
apache:main
Choose a base branch
from
geoffreyclaude:feat/trace-span-propagation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+445
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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. | ||
|
||
//! This example demonstrates the trace feature in DataFusion’s runtime. | ||
//! When the `trace` feature is enabled, spawned tasks in DataFusion (such as those | ||
//! created during repartitioning or when reading Parquet files) are instrumented | ||
//! with the current tracing span, allowing to propagate any existing tracing context. | ||
//! | ||
//! In this example we create a session configured to use multiple partitions, | ||
//! register a Parquet table (based on the `alltypes_tiny_pages_plain.parquet` file), | ||
//! and run a query that should trigger parallel execution on multiple threads. | ||
//! We wrap the entire query execution within a custom span and log messages. | ||
//! By inspecting the tracing output, we should see that the tasks spawned | ||
//! internally inherit the span context. | ||
|
||
use arrow::util::pretty::pretty_format_batches; | ||
use datafusion::arrow::record_batch::RecordBatch; | ||
use datafusion::datasource::file_format::parquet::ParquetFormat; | ||
use datafusion::datasource::listing::ListingOptions; | ||
use datafusion::error::Result; | ||
use datafusion::prelude::*; | ||
use datafusion::test_util::parquet_test_data; | ||
use std::sync::Arc; | ||
use tracing::{info, Level, instrument}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Initialize a tracing subscriber that prints to stdout. | ||
tracing_subscriber::fmt() | ||
.with_thread_ids(true) | ||
.with_thread_names(true) | ||
.with_max_level(Level::DEBUG) | ||
.init(); | ||
|
||
log::info!("Starting example, this log is not captured by tracing"); | ||
|
||
// execute the query within a tracing span | ||
let result = run_instrumented_query().await; | ||
|
||
info!( | ||
"Finished example. Check the logs above for tracing span details showing \ | ||
that tasks were spawned within the 'run_instrumented_query' span on different threads." | ||
); | ||
|
||
result | ||
} | ||
|
||
#[instrument(level = "info")] | ||
async fn run_instrumented_query() -> Result<()> { | ||
info!("Starting query execution within the custom tracing span"); | ||
|
||
// The default session will set the number of partitions to `std::thread::available_parallelism()`. | ||
let ctx = SessionContext::new(); | ||
|
||
// Get the path to the test parquet data. | ||
let test_data = parquet_test_data(); | ||
// Build listing options that pick up only the "alltypes_tiny_pages_plain.parquet" file. | ||
let file_format = ParquetFormat::default().with_enable_pruning(true); | ||
let listing_options = ListingOptions::new(Arc::new(file_format)) | ||
.with_file_extension("alltypes_tiny_pages_plain.parquet"); | ||
|
||
info!("Registering Parquet table 'alltypes' from {test_data} in {listing_options:?}"); | ||
|
||
// Register a listing table using an absolute URL. | ||
let table_path = format!("file://{test_data}/"); | ||
ctx.register_listing_table( | ||
"alltypes", | ||
&table_path, | ||
listing_options.clone(), | ||
None, | ||
None, | ||
) | ||
.await | ||
.expect("register_listing_table failed"); | ||
|
||
info!("Registered Parquet table 'alltypes' from {table_path}"); | ||
|
||
// Run a query that will trigger parallel execution on multiple threads. | ||
let sql = "SELECT COUNT(*), bool_col, date_string_col, string_col | ||
FROM ( | ||
SELECT bool_col, date_string_col, string_col FROM alltypes | ||
UNION ALL | ||
SELECT bool_col, date_string_col, string_col FROM alltypes | ||
) AS t | ||
GROUP BY bool_col, date_string_col, string_col | ||
ORDER BY 1,2,3,4 DESC | ||
LIMIT 5;"; | ||
info!(%sql, "Executing SQL query"); | ||
let df = ctx.sql(sql).await?; | ||
|
||
let results: Vec<RecordBatch> = df.collect().await?; | ||
info!("Query execution complete"); | ||
|
||
// Print out the results and tracing output. | ||
datafusion::common::assert_batches_eq!( | ||
[ | ||
"+----------+----------+-----------------+------------+", | ||
"| count(*) | bool_col | date_string_col | string_col |", | ||
"+----------+----------+-----------------+------------+", | ||
"| 2 | false | 01/01/09 | 9 |", | ||
"| 2 | false | 01/01/09 | 7 |", | ||
"| 2 | false | 01/01/09 | 5 |", | ||
"| 2 | false | 01/01/09 | 3 |", | ||
"| 2 | false | 01/01/09 | 1 |", | ||
"+----------+----------+-----------------+------------+", | ||
], | ||
&results | ||
); | ||
|
||
info!("Query results:\n{}", pretty_format_batches(&results)?); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Question: Is the new
tracing
feature required? Or should we just make it the default?