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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! A helper that scrapes materialized's prometheus metrics and produces
//! updates for the mz_metrics table.

use std::collections::HashMap;
use std::pin::Pin;

use anyhow::anyhow;
use chrono::{DateTime, Utc};
use futures::stream::{self, Stream, StreamExt};
use prometheus::proto::MetricType;
use tokio::time::{self, Duration};
use tokio_stream::wrappers::IntervalStream;

use ore::metrics::MetricsRegistry;
use ore::now::{self, SYSTEM_TIME};
use repr::{Datum, Diff, Row};

use crate::catalog::builtin::{
    BuiltinTable, MZ_PROMETHEUS_HISTOGRAMS, MZ_PROMETHEUS_METRICS, MZ_PROMETHEUS_READINGS,
};
use crate::catalog::BuiltinTableUpdate;
use crate::coord::{LoggingConfig, Message, TimestampedUpdate};

/// Scrapes the prometheus registry when asked and produces a batch of metric
/// data that can be inserted into the built-in `mz_metrics` table.
pub struct Scraper {
    interval: Option<Duration>,
    retain_for: u64,
    registry: MetricsRegistry,
    metadata: HashMap<Row, u64>,
}

fn convert_metrics_to_value_rows<
    'a,
    M: IntoIterator<Item = &'a prometheus::proto::MetricFamily>,
>(
    timestamp: DateTime<Utc>,
    families: M,
) -> (Vec<Row>, Vec<Row>) {
    let mut row_packer = Row::default();
    let mut rows: Vec<Row> = vec![];
    let mut metadata: Vec<Row> = vec![];

    for fam in families {
        let kind = fam.get_field_type();
        if kind != MetricType::COUNTER && kind != MetricType::GAUGE {
            continue;
        }

        metadata.push(metric_family_metadata(&fam));
        for metric in fam.get_metric() {
            let labels: Vec<_> = metric
                .get_label()
                .into_iter()
                .map(|pair| (pair.get_name(), Datum::from(pair.get_value())))
                .collect();
            row_packer.push(Datum::from(fam.get_name()));
            row_packer.push(Datum::from(timestamp));
            row_packer.push_dict(labels.iter().copied());
            row_packer.push(Datum::from(match kind {
                MetricType::COUNTER => metric.get_counter().get_value(),
                MetricType::GAUGE => metric.get_gauge().get_value(),
                _ => unreachable!("never hit for anything other than gauges & counters"),
            }));
            rows.push(row_packer.finish_and_reuse());
        }
    }
    (rows, metadata)
}

fn convert_metrics_to_histogram_rows<
    'a,
    M: IntoIterator<Item = &'a prometheus::proto::MetricFamily>,
>(
    timestamp: DateTime<Utc>,
    families: M,
) -> (Vec<Row>, Vec<Row>) {
    let mut row_packer = Row::default();
    let mut rows: Vec<Row> = vec![];
    let mut metadata: Vec<Row> = vec![];

    for fam in families {
        let name = fam.get_name();
        if fam.get_field_type() == MetricType::HISTOGRAM {
            metadata.push(metric_family_metadata(&fam));
            for metric in fam.get_metric() {
                let labels: Vec<_> = metric
                    .get_label()
                    .into_iter()
                    .map(|pair| (pair.get_name(), Datum::from(pair.get_value())))
                    .collect();
                for bucket in metric.get_histogram().get_bucket() {
                    row_packer.push(Datum::from(name));
                    row_packer.push(Datum::from(timestamp));
                    row_packer.push_dict(labels.iter().copied());
                    row_packer.push(Datum::from(bucket.get_upper_bound()));
                    row_packer.push(Datum::from(bucket.get_cumulative_count() as i64));
                    rows.push(row_packer.finish_and_reuse());
                }
            }
        }
    }
    (rows, metadata)
}

fn metric_family_metadata(family: &prometheus::proto::MetricFamily) -> Row {
    Row::pack(&[
        Datum::from(family.get_name()),
        Datum::from(match family.get_field_type() {
            MetricType::COUNTER => "counter",
            MetricType::GAUGE => "gauge",
            MetricType::HISTOGRAM => "histogram",
            MetricType::SUMMARY => "summary",
            MetricType::UNTYPED => "untyped",
        }),
        Datum::from(family.get_help()),
    ])
}

fn add_expiring_update(
    table: &BuiltinTable,
    updates: Vec<Row>,
    retain_for: u64,
    out: &mut Vec<TimestampedUpdate>,
) {
    // NB: This makes sure to send both records in the same message so we can
    // persist them atomically. Otherwise, we may end up with permanent orphans
    // if a restart/crash happens at the wrong time.
    let id = table.id;
    out.push(TimestampedUpdate {
        updates: updates
            .iter()
            .cloned()
            .map(|row| BuiltinTableUpdate { id, row, diff: 1 })
            .collect(),
        timestamp_offset: 0,
    });
    out.push(TimestampedUpdate {
        updates: updates
            .iter()
            .cloned()
            .map(|row| BuiltinTableUpdate { id, row, diff: -1 })
            .collect(),
        timestamp_offset: retain_for,
    });
}

fn add_metadata_update<I: IntoIterator<Item = Row>>(
    updates: I,
    diff: Diff,
    out: &mut Vec<TimestampedUpdate>,
) {
    let id = MZ_PROMETHEUS_METRICS.id;
    out.push(TimestampedUpdate {
        updates: updates
            .into_iter()
            .map(|row| BuiltinTableUpdate { id, row, diff })
            .collect(),
        timestamp_offset: 0,
    });
}

impl Scraper {
    /// Constructs a new metrics scraper for the specified registry.
    ///
    /// The logging configuration specifies what metrics to scrape and how long
    /// to retain them for. If the logging configuration is none, scraping is
    /// disabled.
    pub fn new(
        logging_config: Option<&LoggingConfig>,
        registry: MetricsRegistry,
    ) -> Result<Scraper, anyhow::Error> {
        let (interval, retain_for) = match logging_config {
            Some(config) => {
                let interval = config.metrics_scraping_interval;
                let retain_for = u64::try_from(config.retain_readings_for.as_millis())
                    .map_err(|_| anyhow!("scraper retention duration does not fit in an i64"))?;
                (interval, retain_for)
            }
            None => (None, 0),
        };
        Ok(Scraper {
            interval,
            retain_for,
            registry,
            metadata: HashMap::new(),
        })
    }

    /// Produces a stream that yields a `Message::ScrapeMetrics` at the desired
    /// scrape frequency.
    ///
    /// If the scraper is disabled, this stream will yield no items.
    pub fn tick_stream(&self) -> Pin<Box<dyn Stream<Item = Message> + Send>> {
        match self.interval {
            None => stream::empty().boxed(),
            Some(interval) => IntervalStream::new(time::interval(interval))
                .map(|_| Message::ScrapeMetrics)
                .boxed(),
        }
    }

    /// Scrapes the metrics once, producing a batch of updates that should be
    /// inserted into the `mz_metrics` table.
    pub fn scrape_once(&mut self) -> Vec<TimestampedUpdate> {
        let now = SYSTEM_TIME();
        let timestamp = now::to_datetime(now);

        let metric_fams = self.registry.gather();

        let mut out = vec![];

        let (value_readings, meta_value) =
            convert_metrics_to_value_rows(timestamp, metric_fams.iter());
        add_expiring_update(
            &MZ_PROMETHEUS_READINGS,
            value_readings,
            self.retain_for,
            &mut out,
        );

        let (histo_readings, meta_histo) =
            convert_metrics_to_histogram_rows(timestamp, metric_fams.iter());
        add_expiring_update(
            &MZ_PROMETHEUS_HISTOGRAMS,
            histo_readings,
            self.retain_for,
            &mut out,
        );

        // Find any metric metadata we need to add:
        let retain_for = self.retain_for;
        let missing = meta_value
            .into_iter()
            .chain(meta_histo.into_iter())
            .filter(|metric| {
                self.metadata
                    .insert(metric.clone(), now + retain_for)
                    .is_none()
            });
        add_metadata_update(missing, 1, &mut out);

        // Expire any that can now go (I would love HashMap.drain_filter here):
        add_metadata_update(
            self.metadata
                .iter()
                .filter(|(_, &retention)| retention <= now)
                .map(|(row, _)| row)
                .cloned(),
            -1,
            &mut out,
        );
        self.metadata.retain(|_, &mut retention| retention > now);

        out
    }
}