Skip to main content

datafusion_datasource/file_stream/
metrics.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use datafusion_common::instant::Instant;
6use datafusion_physical_plan::metrics::{
7    Count, ExecutionPlanMetricsSet, MetricBuilder, MetricCategory, Time,
8};
9
10/// A timer that can be started and stopped.
11pub struct StartableTime {
12    pub metrics: Time,
13    // use for record each part cost time, will eventually add into 'metrics'.
14    pub start: Option<Instant>,
15}
16
17impl StartableTime {
18    pub fn start(&mut self) {
19        assert!(self.start.is_none());
20        self.start = Some(Instant::now());
21    }
22
23    pub fn stop(&mut self) {
24        if let Some(start) = self.start.take() {
25            self.metrics.add_elapsed(start);
26        }
27    }
28}
29
30/// Metrics for [`FileStream`]
31///
32/// Note that all of these metrics are in terms of wall clock time
33/// (not cpu time) so they include time spent waiting on I/O as well
34/// as other operators.
35///
36/// [`FileStream`]: <https://github.com/apache/datafusion/blob/main/datafusion/datasource/src/file_stream.rs>
37pub struct FileStreamMetrics {
38    /// Wall clock time elapsed for file opening.
39    ///
40    /// Time between when [`FileOpener::open`] is called and when the
41    /// [`FileStream`] receives a stream for reading.
42    ///
43    /// [`FileStream`]: crate::file_stream::FileStream
44    /// [`FileOpener::open`]: crate::file_stream::FileOpener::open
45    pub time_opening: StartableTime,
46    /// Wall clock time elapsed for file scanning + first record batch of decompression + decoding
47    ///
48    /// Time between when the [`FileStream`] requests data from the
49    /// stream and when the first [`RecordBatch`] is produced.
50    ///
51    /// [`FileStream`]: crate::file_stream::FileStream
52    /// [`RecordBatch`]: arrow::record_batch::RecordBatch
53    pub time_scanning_until_data: StartableTime,
54    /// Total elapsed wall clock time for scanning + record batch decompression / decoding
55    ///
56    /// Sum of time between when the [`FileStream`] requests data from
57    /// the stream and when a [`RecordBatch`] is produced for all
58    /// record batches in the stream. Note that this metric also
59    /// includes the time of the parent operator's execution.
60    ///
61    /// [`FileStream`]: crate::file_stream::FileStream
62    /// [`RecordBatch`]: arrow::record_batch::RecordBatch
63    pub time_scanning_total: StartableTime,
64    /// Wall clock time elapsed for data decompression + decoding
65    ///
66    /// Time spent waiting for the FileStream's input.
67    pub time_processing: Time,
68    /// Count of errors opening file.
69    ///
70    /// If using `OnError::Skip` this will provide a count of the number of files
71    /// which were skipped and will not be included in the scan results.
72    pub file_open_errors: Count,
73    /// Count of errors scanning file
74    ///
75    /// If using `OnError::Skip` this will provide a count of the number of files
76    /// which were skipped and will not be included in the scan results.
77    pub file_scan_errors: Count,
78    /// Count of files successfully opened or evaluated for processing.
79    /// At t=end (completion of a query) this is equal to `files_opened`, and both values are equal
80    /// to the total number of files in the query; unless the query itself fails.
81    /// This value will always be greater than or equal to `files_open`.
82    /// Note that this value does *not* mean the file was actually scanned.
83    /// We increment this value for any processing of a file, even if that processing is
84    /// discarding it because we hit a `LIMIT` (in this case `files_opened` and `files_processed` are both incremented at the same time).
85    pub files_opened: Count,
86    /// Count of files completely processed / closed (opened, pruned, or skipped due to limit).
87    /// At t=0 (the beginning of a query) this is 0.
88    /// At t=end (completion of a query) this is equal to `files_opened`, and both values are equal
89    /// to the total number of files in the query; unless the query itself fails.
90    /// This value will always be less than or equal to `files_open`.
91    /// We increment this value for any processing of a file, even if that processing is
92    /// discarding it because we hit a `LIMIT` (in this case `files_opened` and `files_processed` are both incremented at the same time).
93    pub files_processed: Count,
94}
95
96impl FileStreamMetrics {
97    pub fn new(metrics: &ExecutionPlanMetricsSet, partition: usize) -> Self {
98        let time_opening = StartableTime {
99            metrics: MetricBuilder::new(metrics)
100                .subset_time("time_elapsed_opening", partition),
101            start: None,
102        };
103
104        let time_scanning_until_data = StartableTime {
105            metrics: MetricBuilder::new(metrics)
106                .subset_time("time_elapsed_scanning_until_data", partition),
107            start: None,
108        };
109
110        let time_scanning_total = StartableTime {
111            metrics: MetricBuilder::new(metrics)
112                .subset_time("time_elapsed_scanning_total", partition),
113            start: None,
114        };
115
116        let time_processing =
117            MetricBuilder::new(metrics).subset_time("time_elapsed_processing", partition);
118
119        let file_open_errors = MetricBuilder::new(metrics)
120            .with_category(MetricCategory::Rows)
121            .counter("file_open_errors", partition);
122
123        let file_scan_errors = MetricBuilder::new(metrics)
124            .with_category(MetricCategory::Rows)
125            .counter("file_scan_errors", partition);
126
127        let files_opened = MetricBuilder::new(metrics)
128            .with_category(MetricCategory::Rows)
129            .counter("files_opened", partition);
130
131        let files_processed = MetricBuilder::new(metrics)
132            .with_category(MetricCategory::Rows)
133            .counter("files_processed", partition);
134
135        Self {
136            time_opening,
137            time_scanning_until_data,
138            time_scanning_total,
139            time_processing,
140            file_open_errors,
141            file_scan_errors,
142            files_opened,
143            files_processed,
144        }
145    }
146}