1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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 product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2025 Datadog, Inc.
//! DataFusion Tracing is an extension for [Apache DataFusion](https://datafusion.apache.org/) that helps you monitor and debug queries. It uses [`tracing`](https://docs.rs/tracing/latest/tracing/) and [OpenTelemetry](https://opentelemetry.io/) to gather DataFusion metrics, trace execution steps, and preview partial query results.
//!
//! **Note:** This is not an official Apache Software Foundation release.
//!
//! # Overview
//!
//! When you run queries with DataFusion Tracing enabled, it automatically adds tracing around execution steps, records all native DataFusion metrics such as execution time and output row count, lets you preview partial results for easier debugging, and integrates with OpenTelemetry for distributed tracing. This makes it simpler to understand and improve query performance.
//!
//! ## See it in action
//!
//! Here's what DataFusion Tracing can look like in practice:
//!
//! <details>
//! <summary>Jaeger UI</summary>
//!
//! 
//! </details>
//!
//! <details>
//! <summary>DataDog UI</summary>
//!
//! 
//! </details>
//!
//! # Getting Started
//!
//! ## Installation
//!
//! Include DataFusion Tracing in your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! datafusion = "53.0.0"
//! datafusion-tracing = "53.0.0"
//! ```
//!
//! ## Quick Start Example
//!
//! ```rust
//! use datafusion::{
//! arrow::{array::RecordBatch, util::pretty::pretty_format_batches},
//! error::Result,
//! execution::SessionStateBuilder,
//! prelude::*,
//! };
//! use datafusion_tracing::{
//! instrument_rules_with_info_spans, instrument_with_info_spans,
//! pretty_format_compact_batch, InstrumentationOptions, RuleInstrumentationOptions,
//! };
//! use std::sync::Arc;
//! use tracing::field;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! // Initialize tracing subscriber as usual
//! // (See examples/otlp.rs for a complete example).
//!
//! // Set up execution plan tracing options (you can customize these).
//! let exec_options = InstrumentationOptions::builder()
//! .record_metrics(true)
//! .preview_limit(5)
//! .preview_fn(Arc::new(|batch: &RecordBatch| {
//! pretty_format_compact_batch(batch, 64, 3, 10).map(|fmt| fmt.to_string())
//! }))
//! .add_custom_field("env", "production")
//! .add_custom_field("region", "us-west")
//! .build();
//!
//! let instrument_rule = instrument_with_info_spans!(
//! options: exec_options,
//! env = field::Empty,
//! region = field::Empty,
//! );
//!
//! let session_state = SessionStateBuilder::new()
//! .with_default_features()
//! .with_physical_optimizer_rule(instrument_rule)
//! .build();
//!
//! // Instrument all rules (analyzer, logical optimizer, physical optimizer)
//! // Physical plan creation tracing is automatically enabled when physical_optimizer is set
//! let rule_options = RuleInstrumentationOptions::full().with_plan_diff();
//! let session_state = instrument_rules_with_info_spans!(
//! options: rule_options,
//! state: session_state
//! );
//!
//! let ctx = SessionContext::new_with_state(session_state);
//!
//! // Execute a query - the entire lifecycle is now traced:
//! // SQL Parsing -> Logical Plan -> Analyzer Rules -> Optimizer Rules ->
//! // Physical Plan Creation -> Physical Optimizer Rules -> Execution
//! let results = ctx.sql("SELECT 1").await?.collect().await?;
//! println!(
//! "Query Results:\n{}",
//! pretty_format_batches(results.as_slice())?
//! );
//!
//! Ok(())
//! }
//! ```
//!
//! A more complete example can be found in the [examples directory](https://github.com/datafusion-contrib/datafusion-tracing/tree/main/examples).
//!
// Execution plan instrumentation (wraps ExecutionPlan nodes with tracing)
// Rule instrumentation (wraps analyzer/optimizer/physical optimizer rules with tracing)
// Shared utilities
// Hide implementation details from documentation.
// These functions are only public because they need to be accessed by the macros,
// but they're not intended for direct use by consumers of this crate.
pub use new_instrument_rule;
pub use instrument_session_state;
pub use InstrumentationOptions;
pub use pretty_format_compact_batch;
pub use RuleInstrumentationOptions;