|
| 1 | +// Copyright 2021-Present Datadog, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// See https://prometheus.io/docs/practices/naming/ |
| 16 | + |
| 17 | +use std::pin::Pin; |
| 18 | +use std::task::{Context, Poll, ready}; |
| 19 | +use std::time::Instant; |
| 20 | + |
| 21 | +use pin_project::{pin_project, pinned_drop}; |
| 22 | +use quickwit_proto::search::LeafSearchResponse; |
| 23 | + |
| 24 | +use crate::SearchError; |
| 25 | +use crate::metrics::SEARCH_METRICS; |
| 26 | + |
| 27 | +// root |
| 28 | + |
| 29 | +pub enum RootSearchMetricsStep { |
| 30 | + Plan, |
| 31 | + Exec { num_targeted_splits: usize }, |
| 32 | +} |
| 33 | + |
| 34 | +/// Wrapper around the plan and search futures to track metrics. |
| 35 | +#[pin_project(PinnedDrop)] |
| 36 | +pub struct RootSearchMetricsFuture<F> { |
| 37 | + #[pin] |
| 38 | + pub tracked: F, |
| 39 | + pub start: Instant, |
| 40 | + pub step: RootSearchMetricsStep, |
| 41 | + pub is_success: Option<bool>, |
| 42 | +} |
| 43 | + |
| 44 | +#[pinned_drop] |
| 45 | +impl<F> PinnedDrop for RootSearchMetricsFuture<F> { |
| 46 | + fn drop(self: Pin<&mut Self>) { |
| 47 | + let (num_targeted_splits, status) = match (&self.step, self.is_success) { |
| 48 | + // is is a partial success, actual success is recorded during the search step |
| 49 | + (RootSearchMetricsStep::Plan, Some(true)) => return, |
| 50 | + (RootSearchMetricsStep::Plan, Some(false)) => (0, "plan-error"), |
| 51 | + (RootSearchMetricsStep::Plan, None) => (0, "plan-cancelled"), |
| 52 | + ( |
| 53 | + RootSearchMetricsStep::Exec { |
| 54 | + num_targeted_splits, |
| 55 | + }, |
| 56 | + Some(true), |
| 57 | + ) => (*num_targeted_splits, "success"), |
| 58 | + ( |
| 59 | + RootSearchMetricsStep::Exec { |
| 60 | + num_targeted_splits, |
| 61 | + }, |
| 62 | + Some(false), |
| 63 | + ) => (*num_targeted_splits, "error"), |
| 64 | + ( |
| 65 | + RootSearchMetricsStep::Exec { |
| 66 | + num_targeted_splits, |
| 67 | + }, |
| 68 | + None, |
| 69 | + ) => (*num_targeted_splits, "cancelled"), |
| 70 | + }; |
| 71 | + |
| 72 | + let label_values = [status]; |
| 73 | + SEARCH_METRICS |
| 74 | + .root_search_requests_total |
| 75 | + .with_label_values(label_values) |
| 76 | + .inc(); |
| 77 | + SEARCH_METRICS |
| 78 | + .root_search_request_duration_seconds |
| 79 | + .with_label_values(label_values) |
| 80 | + .observe(self.start.elapsed().as_secs_f64()); |
| 81 | + SEARCH_METRICS |
| 82 | + .root_search_targeted_splits |
| 83 | + .with_label_values(label_values) |
| 84 | + .observe(num_targeted_splits as f64); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<F, R, E> Future for RootSearchMetricsFuture<F> |
| 89 | +where F: Future<Output = Result<R, E>> |
| 90 | +{ |
| 91 | + type Output = Result<R, E>; |
| 92 | + |
| 93 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 94 | + let this = self.project(); |
| 95 | + let response = ready!(this.tracked.poll(cx)); |
| 96 | + *this.is_success = Some(response.is_ok()); |
| 97 | + Poll::Ready(Ok(response?)) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// leaf |
| 102 | + |
| 103 | +/// Wrapper around the search future to track metrics. |
| 104 | +#[pin_project(PinnedDrop)] |
| 105 | +pub struct LeafSearchMetricsFuture<F> |
| 106 | +where F: Future<Output = Result<LeafSearchResponse, SearchError>> |
| 107 | +{ |
| 108 | + #[pin] |
| 109 | + pub tracked: F, |
| 110 | + pub start: Instant, |
| 111 | + pub targeted_splits: usize, |
| 112 | + pub status: Option<&'static str>, |
| 113 | +} |
| 114 | + |
| 115 | +#[pinned_drop] |
| 116 | +impl<F> PinnedDrop for LeafSearchMetricsFuture<F> |
| 117 | +where F: Future<Output = Result<LeafSearchResponse, SearchError>> |
| 118 | +{ |
| 119 | + fn drop(self: Pin<&mut Self>) { |
| 120 | + let label_values = [self.status.unwrap_or("cancelled")]; |
| 121 | + SEARCH_METRICS |
| 122 | + .leaf_search_requests_total |
| 123 | + .with_label_values(label_values) |
| 124 | + .inc(); |
| 125 | + SEARCH_METRICS |
| 126 | + .leaf_search_request_duration_seconds |
| 127 | + .with_label_values(label_values) |
| 128 | + .observe(self.start.elapsed().as_secs_f64()); |
| 129 | + SEARCH_METRICS |
| 130 | + .leaf_search_targeted_splits |
| 131 | + .with_label_values(label_values) |
| 132 | + .observe(self.targeted_splits as f64); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl<F> Future for LeafSearchMetricsFuture<F> |
| 137 | +where F: Future<Output = Result<LeafSearchResponse, SearchError>> |
| 138 | +{ |
| 139 | + type Output = Result<LeafSearchResponse, SearchError>; |
| 140 | + |
| 141 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 142 | + let this = self.project(); |
| 143 | + let response = ready!(this.tracked.poll(cx)); |
| 144 | + *this.status = if response.is_ok() { |
| 145 | + Some("success") |
| 146 | + } else { |
| 147 | + Some("error") |
| 148 | + }; |
| 149 | + Poll::Ready(Ok(response?)) |
| 150 | + } |
| 151 | +} |
0 commit comments