emit_otlp 2.22.1

Emit diagnostic events to an OpenTelemetry-compatible collector.
Documentation
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*!
Channel infrastructure for the OTLP emitter.

Events are sent from the caller thread through a [`Channel`] to a background
[`SignalWorker`], which serializes and ships them via HTTP.
*/

use std::{
    collections::HashMap,
    fmt,
    future::Future,
    hash::{BuildHasher, Hash},
    ops::ControlFlow,
};

use fnv::FnvBuildHasher;
use hashbrown::{HashTable, hash_table};

use emit_batcher::BatchError;

use crate::{
    InternalMetrics,
    data::{EncodedEvent, EncodedScopeItems},
};

pub(crate) async fn batch<F>(
    mut channel: Channel,
    max_request_size: usize,
    metrics: &InternalMetrics,
    mut encode_event: impl FnMut(&ChannelEvent) -> Option<EncodedEvent>,
    mut send_batch: impl FnMut(&EncodedScopeItems) -> F,
) -> Result<(), BatchError<Channel>>
where
    F: Future<Output = Result<(), BatchError<()>>>,
{
    let mut batch = EncodedScopeItems::new();

    let mut scope_index = channel.cursor.scope_index;
    let mut event_index = channel.cursor.event_index;
    let mut remaining_items = channel.cursor.remaining_items;

    // Split our channel into batches roughly by request size
    // OTLP requires we collect events under the same scope together so we work a scope at a time
    while scope_index < channel.scopes.len() {
        let events = &channel.scopes[scope_index].1;

        while event_index < events.len() {
            let event = &events[event_index];

            event_index += 1;
            remaining_items -= 1;

            let Some(encoded) = encode_event(event) else {
                metrics.event_encoding_failed.increment();
                continue;
            };

            if batch.total_items() > 0
                && batch.total_size_bytes() + encoded.size_bytes() > max_request_size
            {
                // We've reached the maximum size of a single batch; send it then start a new one
                match send_batch(&batch).await {
                    Ok(()) => {
                        batch.clear();

                        channel.cursor = ChannelCursor {
                            scope_index,
                            event_index,
                            remaining_items,
                        };
                    }
                    Err(e) => return Err(e.map_retryable(|r| r.map(|_| channel))),
                }
            }

            batch.push(encoded);
        }

        event_index = 0;
        scope_index += 1;
    }

    // Send the final batch
    if batch.total_items() > 0 {
        match send_batch(&batch).await {
            Ok(()) => Ok(()),
            Err(e) => Err(e.map_retryable(|r| r.map(|_| channel))),
        }
    } else {
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ChannelCursor {
    scope_index: usize,
    event_index: usize,
    remaining_items: usize,
}

#[derive(Clone)]
pub(crate) struct Channel {
    scopes: Vec<(emit::Path<'static>, Vec<ChannelEvent>)>,
    scopes_by_key: HashTable<usize>,
    cursor: ChannelCursor,
}

impl fmt::Debug for Channel {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Channel")
            .field("cursor", &self.cursor)
            .finish_non_exhaustive()
    }
}

impl Default for Channel {
    fn default() -> Self {
        Channel {
            scopes: Default::default(),
            scopes_by_key: Default::default(),
            cursor: ChannelCursor {
                scope_index: 0,
                event_index: 0,
                remaining_items: 0,
            },
        }
    }
}

#[derive(Clone)]
pub(crate) struct ChannelEvent(emit::Event<'static, ChannelProps>);

#[derive(Clone, Default)]
pub(crate) struct ChannelProps {
    evt_kind: Option<emit::Kind>,
    lvl: Option<emit::Level>,
    trace_id: Option<emit::TraceId>,
    span_parent: Option<emit::SpanId>,
    span_id: Option<emit::SpanId>,
    span_kind: Option<emit::SpanKind>,
    rest: HashMap<emit::Str<'static>, emit::value::OwnedValue>,
}

impl ChannelEvent {
    pub(crate) fn from_evt(evt: emit::Event<impl emit::Props>) -> Self {
        ChannelEvent(emit::Event::new(
            evt.mdl().to_owned(),
            evt.tpl().to_owned(),
            evt.extent().cloned(),
            evt.props().collect(),
        ))
    }

    pub(crate) fn get<'a>(&'a self) -> &'a emit::Event<'a, impl emit::Props> {
        &self.0
    }
}

impl<'kv> emit::props::FromProps<'kv> for ChannelProps {
    fn from_props<P: emit::Props + ?Sized>(props: &'kv P) -> Self {
        let mut owned = ChannelProps::default();

        let _ = props.for_each(|k, v| {
            match k.get() {
                // Well-known props
                emit::well_known::KEY_LVL => {
                    if let Some(v) = v.by_ref().cast() {
                        owned.lvl = Some(v);

                        return ControlFlow::Continue(());
                    }
                }
                emit::well_known::KEY_TRACE_ID => {
                    if let Some(v) = v.by_ref().cast() {
                        owned.trace_id = Some(v);

                        return ControlFlow::Continue(());
                    }
                }
                emit::well_known::KEY_SPAN_ID => {
                    if let Some(v) = v.by_ref().cast() {
                        owned.span_id = Some(v);

                        return ControlFlow::Continue(());
                    }
                }
                emit::well_known::KEY_SPAN_PARENT => {
                    if let Some(v) = v.by_ref().cast() {
                        owned.span_parent = Some(v);

                        return ControlFlow::Continue(());
                    }
                }
                emit::well_known::KEY_SPAN_KIND => {
                    if let Some(v) = v.by_ref().cast() {
                        owned.span_kind = Some(v);

                        return ControlFlow::Continue(());
                    }
                }
                emit::well_known::KEY_EVT_KIND => {
                    if let Some(v) = v.by_ref().cast() {
                        owned.evt_kind = Some(v);

                        return ControlFlow::Continue(());
                    }
                }
                _ => (),
            }

            // Insert other values
            owned.rest.insert(k.to_owned(), v.to_owned());

            ControlFlow::Continue(())
        });

        owned
    }
}

impl emit::Props for ChannelProps {
    fn for_each<'a, F: FnMut(emit::Str<'a>, emit::Value<'a>) -> ControlFlow<()>>(
        &'a self,
        mut for_each: F,
    ) -> ControlFlow<()> {
        use emit::{str::ToStr as _, value::ToValue as _};

        let ChannelProps {
            evt_kind,
            lvl,
            trace_id,
            span_parent,
            span_id,
            span_kind,
            rest,
        } = self;

        if let Some(evt_kind) = evt_kind {
            for_each(emit::well_known::KEY_EVT_KIND.to_str(), evt_kind.to_value())?;
        }
        if let Some(lvl) = lvl {
            for_each(emit::well_known::KEY_LVL.to_str(), lvl.to_value())?;
        }
        if let Some(trace_id) = trace_id {
            for_each(emit::well_known::KEY_TRACE_ID.to_str(), trace_id.to_value())?;
        }
        if let Some(span_parent) = span_parent {
            for_each(
                emit::well_known::KEY_SPAN_PARENT.to_str(),
                span_parent.to_value(),
            )?;
        }
        if let Some(span_id) = span_id {
            for_each(emit::well_known::KEY_SPAN_ID.to_str(), span_id.to_value())?;
        }
        if let Some(span_kind) = span_kind {
            for_each(
                emit::well_known::KEY_SPAN_KIND.to_str(),
                span_kind.to_value(),
            )?;
        }

        emit::Props::for_each(rest, for_each)
    }

    fn get<'v, K: emit::str::ToStr>(&'v self, key: K) -> Option<emit::Value<'v>> {
        use emit::value::ToValue as _;

        let ChannelProps {
            evt_kind,
            lvl,
            trace_id,
            span_parent,
            span_id,
            span_kind,
            rest,
        } = self;

        match key.to_str().get() {
            emit::well_known::KEY_EVT_KIND => evt_kind.as_ref().map(|v| v.to_value()),
            emit::well_known::KEY_LVL => lvl.as_ref().map(|v| v.to_value()),
            emit::well_known::KEY_TRACE_ID => trace_id.as_ref().map(|v| v.to_value()),
            emit::well_known::KEY_SPAN_ID => span_id.as_ref().map(|v| v.to_value()),
            emit::well_known::KEY_SPAN_PARENT => span_parent.as_ref().map(|v| v.to_value()),
            emit::well_known::KEY_SPAN_KIND => span_kind.as_ref().map(|v| v.to_value()),
            key => emit::Props::get(rest, key),
        }
    }

    fn is_unique(&self) -> bool {
        true
    }
}

impl emit_batcher::Channel for Channel {
    type Item = ChannelEvent;

    fn new() -> Self {
        Default::default()
    }

    fn with_capacity(capacity_hint: usize) -> Self {
        let mut channel = Self::new();

        channel.scopes = Vec::with_capacity(capacity_hint);

        channel
    }

    fn push(&mut self, item: Self::Item) {
        assert_eq!(
            0, self.cursor.scope_index,
            "attempt to push to a channel that's already being drained"
        );
        assert_eq!(
            0, self.cursor.event_index,
            "attempt to push to a channel that's already being drained"
        );

        let scope = item.get().mdl();

        match self.scopes_by_key.entry(
            hash(&scope),
            |idx| self.scopes[*idx].0 == scope,
            |idx| hash(&self.scopes[*idx].0),
        ) {
            hash_table::Entry::Occupied(entry) => {
                self.scopes[*entry.get()].1.push(item);
            }
            hash_table::Entry::Vacant(entry) => {
                let idx = self.scopes.len();

                self.scopes.push((scope.to_owned(), vec![item]));
                entry.insert(idx);
            }
        }

        self.cursor.remaining_items += 1;
    }

    fn len(&self) -> usize {
        self.cursor.remaining_items
    }

    fn clear(&mut self) {
        *self = Default::default();
    }
}

#[inline]
fn hash(v: &(impl Hash + ?Sized)) -> u64 {
    FnvBuildHasher::default().hash_one(v)
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::{
        io,
        sync::{Arc, Mutex},
    };

    use crate::data::{EventEncoder, Json, logs};

    use emit_batcher::Channel as _;

    #[tokio::test]
    async fn channel_splits_batches_by_size() {
        let mut channel = Channel::default();

        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("a"), "Event 1"),
        ));
        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("a"), "Event 2"),
        ));
        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("b"), "Event 3"),
        ));
        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("c"), "Event 4"),
        ));

        assert_eq!(4, channel.len());

        for (case, expected) in [
            (0, 4),
            (
                {
                    logs::LogsEventEncoder::default()
                        .encode_event::<Json>(&emit::evt!(mdl: emit::path!("a"), "Event 1"))
                        .unwrap()
                        .size_bytes()
                        * 2
                },
                2,
            ),
            (usize::MAX, 1),
        ] {
            let channel = channel.clone();

            let calls = Arc::new(Mutex::new(0));

            batch(
                channel,
                case,
                &Default::default(),
                |evt| logs::LogsEventEncoder::default().encode_event::<Json>(evt.get()),
                |_| async {
                    *calls.lock().unwrap() += 1;

                    Ok(())
                },
            )
            .await
            .unwrap();

            assert_eq!(expected, *calls.lock().unwrap(), "max batch size {case}");
        }
    }

    #[tokio::test]
    async fn channel_retry_resumes_from_cursor() {
        let mut channel = Channel::default();

        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("a"), "Event 1"),
        ));
        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("a"), "Event 2"),
        ));
        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("b"), "Event 3"),
        ));
        channel.push(ChannelEvent::from_evt(
            emit::evt!(mdl: emit::path!("c"), "Event 4"),
        ));

        assert_eq!(4, channel.len());

        let calls = Arc::new(Mutex::new(0));

        // This first call will fail
        let channel = batch(
            channel,
            0,
            &Default::default(),
            |evt| logs::LogsEventEncoder::default().encode_event::<Json>(evt.get()),
            |_| async {
                let mut calls = calls.lock().unwrap();

                *calls += 1;

                if *calls == 2 {
                    return Err(BatchError::retry(
                        io::Error::new(io::ErrorKind::Other, "explicit failure"),
                        (),
                    ));
                }

                Ok(())
            },
        )
        .await
        .unwrap_err()
        .into_retryable()
        .unwrap();

        assert_eq!(0, channel.cursor.scope_index);
        assert_eq!(2, channel.cursor.event_index);

        assert_eq!(2, channel.len());

        // This second call will succeed
        batch(
            channel,
            0,
            &Default::default(),
            |evt| logs::LogsEventEncoder::default().encode_event::<Json>(evt.get()),
            |_| async { Ok(()) },
        )
        .await
        .unwrap();
    }
}