merni 0.1.3

A low overhead metrics implementation.
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::io::{self, Write};
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use reqwest::header;
use tokio::runtime::Handle;
use tokio::task::JoinHandle;
use zstd::stream::raw::{Encoder, Operation};
use zstd::zstd_safe::{InBuffer, OutBuffer};

use crate::{
    AggregatedMetric, AggregationSink, Aggregations, Dispatcher, MetricMeta, MetricType,
    MetricUnit, ThreadLocalAggregator, set_global_dispatcher,
};

type DatadogAggregator = Arc<ThreadLocalAggregator<io::Result<Vec<JoinHandle<()>>>>>;

/// Creates a [`DatadogBuilder`] with sensible defaults.
///
/// By default, it runs on the "current" tokio runtime, flushes metrics every 10 seconds,
/// and defaults to the `DD_API_KEY` env variable if no explicit Datadog API key has been given.
///
/// Calling [`try_init`](DatadogBuilder::try_init) will configure a global dispatcher and return a [`DatadogFlusher`].
pub fn datadog<'a>(api_key: impl Into<Option<&'a str>>) -> DatadogBuilder {
    let api_key = api_key
        .into()
        .map(|s| Ok(s.into()))
        .unwrap_or_else(|| std::env::var("DD_API_KEY").map_err(io::Error::other));
    let ddog_site = match std::env::var("DD_SITE") {
        Ok(site) => Ok(Some(format!("https://api.{site}"))),
        Err(std::env::VarError::NotPresent) => Ok(None),
        Err(err) => Err(io::Error::other(err)),
    };

    DatadogBuilder {
        runtime: None,
        flush_interval: Duration::from_secs(10),

        api_key,
        ddog_site,

        prefix: String::new(),
        global_tags: String::new(),
    }
}

/// A builder for configuring common datadog options.
pub struct DatadogBuilder {
    runtime: Option<Handle>,
    flush_interval: Duration,

    api_key: io::Result<String>,
    ddog_site: io::Result<Option<String>>,

    prefix: String,
    global_tags: String,
}

impl DatadogBuilder {
    /// Sets a global prefix to all the emitted metrics.
    ///
    /// For example, this could be `"myservice."`.
    pub fn prefix(mut self, prefix: &str) -> Self {
        self.prefix = prefix.into();
        self
    }

    /// Adds a global tag to all the emitted metrics.
    ///
    /// For example, this could be something like `"hostname"`, or similar.
    pub fn global_tag(mut self, key: &str, value: &str) -> Self {
        let formatted_tag = format!("{key}:{value}");
        if let Ok(formatted_tag) = serde_json::to_string(&formatted_tag) {
            if !self.global_tags.is_empty() {
                self.global_tags.push(',');
            }
            self.global_tags.push_str(&formatted_tag);
        }
        self
    }

    /// Explicitly sets a tokio runtime [`Handle`] to use for the flusher thread.
    pub fn runtime(mut self, runtime: Handle) -> Self {
        self.runtime = Some(runtime);
        self
    }

    /// Explicitly sets the upstream datadog site.
    ///
    /// This defaults to the `DD_SITE` env variable, or `https://api.datadoghq.com` otherwise.
    pub fn ddog_site(mut self, ddog_site: &str) -> Self {
        self.ddog_site = Ok(Some(ddog_site.into()));
        self
    }

    /// Explicitly sets a flush interval.
    ///
    /// This defaults to 10 seconds.
    pub fn flush_interval(mut self, flush_interval: Duration) -> Self {
        self.flush_interval = flush_interval;
        self
    }

    /// Turns the builder into a [`DatadogSink`].
    pub fn into_sink(self) -> io::Result<DatadogSink> {
        let runtime = self.runtime.unwrap_or_else(Handle::current);
        let api_key = self.api_key?;
        let ddog_site = self.ddog_site?;

        Ok(DatadogSink {
            runtime,
            client: reqwest::ClientBuilder::new()
                .build()
                .map_err(io::Error::other)?,
            api_key,
            ddog_site: ddog_site
                .as_deref()
                .unwrap_or(DD_SITE)
                .trim_end_matches('/')
                .into(),

            join_handles: Vec::new(),

            metric_buf: Vec::with_capacity(MAX_COMPRESSED),
            scratch_buf: String::new(),
            prefix: self.prefix,
            global_tags: self.global_tags,

            flush_interval_secs: self.flush_interval.as_secs(),
            next_flush_len: MAX_COMPRESSED - THRESHOLD,
            bytes_written: 0,
            cctx: Encoder::new(0)?,
            compression_buffer: Vec::with_capacity(MAX_COMPRESSED),
        })
    }

    /// Initializes the datadog sink and aggregator, registering it as a global dispatcher.
    pub fn try_init(self) -> io::Result<DatadogFlusher> {
        let flush_interval = self.flush_interval;
        let datadog = self.into_sink()?;

        let aggregator = Arc::new(ThreadLocalAggregator::new(flush_interval, datadog));
        let dispatcher = Dispatcher::new(Arc::clone(&aggregator));
        set_global_dispatcher(dispatcher)
            .map_err(|_| io::Error::other("unable to set global dispatcher"))?;
        Ok(DatadogFlusher { aggregator })
    }
}

/// This is a wrapper struct that allows flushing aggregated metrics to Datadog.
pub struct DatadogFlusher {
    aggregator: DatadogAggregator,
}
impl DatadogFlusher {
    /// Flushes aggregated metrics to datadog
    pub async fn flush(&self, timeout: Option<Duration>) -> io::Result<()> {
        let tasks = self.aggregator.flush(timeout).map_err(io::Error::other)??;
        for task in tasks {
            task.await.map_err(io::Error::other)?;
        }

        Ok(())
    }
}

/// An aggregator sink which pushes metrics to Datadog, using the Datadog API.
pub struct DatadogSink {
    runtime: Handle,
    client: reqwest::Client,
    api_key: String,
    ddog_site: String,

    join_handles: Vec<JoinHandle<()>>,

    metric_buf: Vec<u8>,
    scratch_buf: String,
    prefix: String,
    global_tags: String,

    flush_interval_secs: u64,
    next_flush_len: usize,
    bytes_written: usize,
    cctx: Encoder<'static>,
    compression_buffer: Vec<u8>,
}

impl AggregationSink for DatadogSink {
    type Output = io::Result<Vec<JoinHandle<()>>>;

    fn emit(&mut self, metrics: Aggregations) -> Self::Output {
        self.emit_metrics(metrics)
    }
}

const THRESHOLD: usize = 1024 * 4;
const MAX_COMPRESSED: usize = 512000;
const MAX_UNCOMPRESSED: usize = 5242880;
const BYTES_PER_POINT: usize = 20;

const DD_SITE: &str = "https://api.datadoghq.com";
const DISTRIBUTION_ENDPOINT: &str = "/api/v1/distribution_points";
const METRICS_ENDPOINT: &str = "/api/v2/series";

impl DatadogSink {
    fn emit_metrics(&mut self, metrics: Aggregations) -> io::Result<Vec<JoinHandle<()>>> {
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map_err(io::Error::other)?
            .as_secs();

        for (meta, value) in metrics.counters {
            self.push_metric(&meta, timestamp, value)?;
        }
        for (meta, value) in metrics.gauges {
            self.push_metric(&meta, timestamp, value.last)?;
        }
        self.flush(METRICS_ENDPOINT)?;

        for (meta, values) in metrics.distributions {
            let mut rest = values.values.as_slice();
            while !rest.is_empty() {
                let num_values = (self.next_flush_len / BYTES_PER_POINT).min(rest.len());
                if let Some(values) = rest.split_off(..num_values) {
                    self.push_distribution(&meta, timestamp, values)?;
                }
            }
        }
        self.flush(DISTRIBUTION_ENDPOINT)?;

        Ok(std::mem::take(&mut self.join_handles))
    }

    fn flush(&mut self, endpoint: &str) -> io::Result<()> {
        if self.metric_buf.is_empty() && self.bytes_written == 0 {
            return Ok(());
        }
        self.metric_buf.extend_from_slice(br#"]}"#);

        self.flush_to_zstd()?;
        self.do_flush(endpoint)?;

        Ok(())
    }
    fn maybe_flush(&mut self, endpoint: &str) -> io::Result<()> {
        if self.metric_buf.len() >= self.next_flush_len {
            self.flush_to_zstd()?;
        }

        let compressed_left = MAX_COMPRESSED
            .checked_sub(self.compression_buffer.len())
            .ok_or(io::ErrorKind::QuotaExceeded)?;
        let uncompressed_left = MAX_UNCOMPRESSED
            .checked_sub(self.bytes_written)
            .ok_or(io::ErrorKind::QuotaExceeded)?;

        self.next_flush_len = compressed_left
            .min(uncompressed_left)
            .saturating_sub(THRESHOLD);
        if self.next_flush_len < THRESHOLD {
            self.metric_buf.extend_from_slice(br#"]}"#);
            self.flush_to_zstd()?;
            self.do_flush(endpoint)?;
        }

        Ok(())
    }
    fn flush_to_zstd(&mut self) -> io::Result<()> {
        let mut input = InBuffer::around(&self.metric_buf);
        let output_len = self.compression_buffer.len();
        let mut output = OutBuffer::around_pos(&mut self.compression_buffer, output_len);

        self.cctx.run(&mut input, &mut output)?;

        self.bytes_written += self.metric_buf.len();
        self.metric_buf.clear();

        Ok(())
    }
    fn do_flush(&mut self, endpoint: &str) -> io::Result<()> {
        let output_len = self.compression_buffer.len();
        let mut output = OutBuffer::around_pos(&mut self.compression_buffer, output_len);
        self.cctx.finish(&mut output, true)?;
        self.cctx.reinit()?;

        let request = self
            .client
            .post(format!("{}{endpoint}", self.ddog_site))
            .header("DD-API-KEY", &self.api_key)
            .header(header::ACCEPT, "application/json")
            .header(header::CONTENT_ENCODING, "zstd")
            .header(header::CONTENT_TYPE, "application/json")
            .body(self.compression_buffer.clone())
            .send();

        self.join_handles.push(self.runtime.spawn(async move {
            let response = match request.await {
                Ok(resp) => resp,
                Err(err) => return eprintln!("merni: error submitting metrics to datadog (err={err})"),
            };

            if let Err(err) = response.error_for_status_ref() {
                let text_result = response.text().await;
                let response_text = text_result.as_deref().unwrap_or("<unable to read response>");

                eprintln!("merni: error submitting metrics to datadog (err={err}, response={response_text})");
            }
        }));

        self.bytes_written = 0;
        self.compression_buffer.clear();

        Ok(())
    }

    fn push_metric(
        &mut self,
        meta: &AggregatedMetric,
        timestamp: u64,
        value: f64,
    ) -> io::Result<()> {
        self.write_begin();
        self.write_meta(meta)?;
        self.write_type_and_unit(meta)?;

        self.metric_buf.write_fmt(format_args!(
            r#""points":[{{"timestamp":{timestamp},"value":{value}}}]}}"#
        ))?;

        self.maybe_flush(METRICS_ENDPOINT)
    }

    fn push_distribution(
        &mut self,
        meta: &AggregatedMetric,
        timestamp: u64,
        values: &[f64],
    ) -> io::Result<()> {
        self.write_begin();
        self.write_meta(meta)?;

        self.metric_buf
            .write_fmt(format_args!(r#""points":[[{timestamp},"#))?;
        serde_json::to_writer(&mut self.metric_buf, values).map_err(io::Error::other)?;
        self.metric_buf.extend_from_slice(br#"]]}"#);

        self.maybe_flush(DISTRIBUTION_ENDPOINT)
    }

    fn write_begin(&mut self) {
        if self.metric_buf.is_empty() && self.bytes_written == 0 {
            self.metric_buf.extend_from_slice(br#"{"series":["#);
        } else {
            self.metric_buf.push(b',');
        }
    }

    fn write_meta(&mut self, meta: &AggregatedMetric) -> io::Result<()> {
        self.scratch_buf.clear();
        self.scratch_buf.push_str(&self.prefix);
        self.scratch_buf.push_str(meta.key());

        self.metric_buf.extend_from_slice(br#"{"metric":"#);
        serde_json::to_writer(&mut self.metric_buf, &self.scratch_buf).map_err(io::Error::other)?;
        self.metric_buf.push(b',');

        if matches!(meta.meta.ty(), MetricType::Counter) {
            self.metric_buf
                .write_fmt(format_args!(r#""interval":{},"#, self.flush_interval_secs))?;
        }

        let tags = meta.tags();
        if tags.len() > 0 || !self.global_tags.is_empty() {
            self.metric_buf.extend_from_slice(br#""tags":["#);
            self.metric_buf
                .extend_from_slice(self.global_tags.as_bytes());
            if !self.global_tags.is_empty() && tags.len() > 0 {
                self.metric_buf.push(b',');
            }
            for (i, tag) in tags.enumerate() {
                self.scratch_buf.clear();
                self.scratch_buf.push_str(tag.0);
                self.scratch_buf.push(':');
                self.scratch_buf.push_str(tag.1);

                if i > 0 {
                    self.metric_buf.push(b',');
                }
                serde_json::to_writer(&mut self.metric_buf, &self.scratch_buf)
                    .map_err(io::Error::other)?;
            }
            self.metric_buf.extend_from_slice(br#"],"#);
        }

        Ok(())
    }

    fn write_type_and_unit(&mut self, meta: &MetricMeta) -> io::Result<()> {
        self.metric_buf.extend_from_slice(br#""type":"#);
        let ty = match meta.ty() {
            MetricType::Counter => b'1',
            MetricType::Gauge => b'3',
            _ => b'0',
        };
        self.metric_buf.push(ty);
        self.metric_buf.push(b',');
        if meta.unit() != MetricUnit::Unknown {
            self.metric_buf.extend_from_slice(br#""unit":"#);
            serde_json::to_writer(&mut self.metric_buf, &meta.unit()).map_err(io::Error::other)?;
            self.metric_buf.push(b',');
        }

        Ok(())
    }
}

// #[cfg(test)]
// mod tests {
//     use crate::tags::record_tags;

//     use super::*;

//     #[test]
//     fn builds_metrics_request() {
//         let mut sink = DatadogSink::new().unwrap();

//         sink.push_metric(
//             &AggregatedMetric {
//                 meta: MetricMeta::new(MetricType::Counter, MetricUnit::Bytes, "some.bytes"),
//                 tag_values: Default::default(),
//             },
//             12345,
//             123.45,
//         )
//         .unwrap();

//         sink.push_metric(
//             &AggregatedMetric {
//                 meta: MetricMeta::new(MetricType::Gauge, MetricUnit::Unknown, "a.gauge")
//                     .with_tags(&["a_tag"])
//                     .meta,
//                 tag_values: record_tags(&[&"a_value"]),
//             },
//             12346,
//             1234.567,
//         )
//         .unwrap();

//         sink.flush(METRICS_ENDPOINT).unwrap();

//         sink.push_distribution(
//             &AggregatedMetric {
//                 meta: MetricMeta::new(MetricType::Distribution, MetricUnit::Seconds, "a.timer"),
//                 tag_values: Default::default(),
//             },
//             12346,
//             &[1., 2., 3., 4.],
//         )
//         .unwrap();

//         sink.flush(DISTRIBUTION_ENDPOINT).unwrap();
//     }
// }