apollo-router 3.0.0-alpha.2

A configurable, high-performance routing runtime for Apollo Federation 🚀
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
//! Blocking-safe Tokio runtime for OpenTelemetry SDK background tasks.
//!
//! Both `PeriodicReader` (metrics) and `BatchSpanProcessor` (tracing) drive their background
//! tasks via `futures_executor::block_on` — a real synchronous block, not just an await.
//! Running that on a shared async worker thread can starve other work on the same thread;
//! under high concurrency it can even deadlock (every thread occupied by a blocked caller,
//! none left to run the background task they are waiting on).
//!
//! [`BlockingSafeTokioRuntime`] spawns onto `tokio::task::spawn_blocking`'s dedicated thread
//! pool instead, where blocking is expected and safe.
use std::fmt::Debug;
use std::future::Future;
use std::time::Duration;

use opentelemetry_sdk::runtime::Runtime;
use opentelemetry_sdk::runtime::RuntimeChannel;
use opentelemetry_sdk::runtime::TrySend;
use opentelemetry_sdk::runtime::TrySendError;

/// Blocking-safe Tokio runtime for OpenTelemetry SDK background tasks.
///
/// Both `PeriodicReader` (metrics) and `BatchSpanProcessor` (tracing) drive their background
/// tasks via `futures_executor::block_on` — a real synchronous block, not just an await.
/// This runtime spawns those tasks onto `tokio::task::spawn_blocking`'s dedicated thread pool,
/// where blocking is expected and safe.
///
/// Use [`new_for_tracing`][Self::new_for_tracing] for `BatchSpanProcessor` (emits
/// `apollo.router.telemetry.batch_processor.errors` metrics when spans are dropped) and
/// [`new_for_metrics`][Self::new_for_metrics] for `PeriodicReader` (no channel name needed,
/// as `PeriodicReader` does not use the `RuntimeChannel` interface).
#[derive(Debug, Clone)]
pub(crate) struct BlockingSafeTokioRuntime {
    /// Exporter name used in error metrics; `None` for the metrics-reader variant.
    name: Option<&'static str>,
}

impl BlockingSafeTokioRuntime {
    /// Creates a runtime for a `BatchSpanProcessor`.
    ///
    /// `name` is attached to `apollo.router.telemetry.batch_processor.errors` metrics
    /// when spans are dropped because the channel is full or closed.
    pub(crate) fn new_for_tracing(name: &'static str) -> Self {
        Self { name: Some(name) }
    }

    /// Creates a runtime for a `PeriodicReader`.
    ///
    /// No name is required: `PeriodicReader` only calls [`Runtime::spawn`] and
    /// [`Runtime::delay`] — it never calls [`RuntimeChannel::batch_message_channel`].
    pub(crate) fn new_for_metrics() -> Self {
        Self { name: None }
    }
}

impl Runtime for BlockingSafeTokioRuntime {
    fn spawn<F>(&self, future: F)
    where
        F: Future<Output = ()> + Send + 'static,
    {
        let handle = tokio::runtime::Handle::current();
        tokio::task::spawn_blocking(move || {
            handle.block_on(future);
        });
    }

    fn delay(&self, duration: Duration) -> impl Future<Output = ()> + Send + 'static {
        tokio::time::sleep(duration)
    }
}

impl RuntimeChannel for BlockingSafeTokioRuntime {
    type Receiver<T: Debug + Send> = tokio_stream::wrappers::ReceiverStream<T>;
    type Sender<T: Debug + Send> = BatchSender<T>;

    fn batch_message_channel<T: Debug + Send>(
        &self,
        capacity: usize,
    ) -> (Self::Sender<T>, Self::Receiver<T>) {
        let (sender, receiver) = tokio::sync::mpsc::channel(capacity);
        (
            BatchSender::new(self.name, sender),
            tokio_stream::wrappers::ReceiverStream::new(receiver),
        )
    }
}

/// Sender for the batch processor channel that optionally emits metrics on send failures.
///
/// When `name` is `Some` (i.e. created via [`BlockingSafeTokioRuntime::new_for_tracing`]),
/// records `apollo.router.telemetry.batch_processor.errors` labelled with the exporter name
/// when the channel is full or closed.  Metrics are skipped for the nameless metrics-reader
/// variant.
#[derive(Debug)]
pub(crate) struct BatchSender<T> {
    name: Option<&'static str>,
    sender: tokio::sync::mpsc::Sender<T>,
}

impl<T: Send> BatchSender<T> {
    fn new(name: Option<&'static str>, sender: tokio::sync::mpsc::Sender<T>) -> Self {
        Self { name, sender }
    }
}

impl<T: Send> TrySend for BatchSender<T> {
    type Message = T;

    fn try_send(&self, item: Self::Message) -> Result<(), TrySendError> {
        self.sender.try_send(item).map_err(|err| {
            let error = match &err {
                tokio::sync::mpsc::error::TrySendError::Full(_) => "channel full",
                tokio::sync::mpsc::error::TrySendError::Closed(_) => "channel closed",
            };

            if let Some(name) = self.name {
                u64_counter!(
                    "apollo.router.telemetry.batch_processor.errors",
                    "Errors when sending to a batch processor",
                    1,
                    "name" = name,
                    "error" = error
                );
                TrySendError::Other(
                    format!(
                        "cannot send message to batch processor '{name}' as the channel is {error}"
                    )
                    .into(),
                )
            } else {
                TrySendError::Other(format!("batch processor channel {error}").into())
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use opentelemetry_sdk::runtime::Tokio;

    use super::*;
    use crate::metrics::FutureMetricsExt;

    // ── PeriodicReader tests (Runtime only) ─────────────────────────────────

    /// Use a single worker thread to prove that `new_for_metrics` doesn't prevent other
    /// work from going through.
    #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
    async fn spawn_does_not_block_other_tasks_on_the_same_worker() {
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();

        BlockingSafeTokioRuntime::new_for_metrics().spawn(async {
            // Mirrors `PeriodicReader`'s real, synchronous block inside its background
            // loop - not just an await.
            std::thread::sleep(Duration::from_millis(300));
        });

        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(10)).await;
            let _ = tx.send(());
        });

        tokio::time::timeout(Duration::from_millis(100), rx)
            .await
            .expect(
                "lightweight task did not complete within 100 ms: \
                 the blocking spawn may have starved the worker thread",
            )
            .expect("oneshot sender dropped before sending");
    }

    /// Shared helper: spawns `concurrent_readers` `PeriodicReader`s all calling
    /// `force_flush` concurrently on a runtime capped to `worker_threads` threads, then
    /// checks whether they all complete within `bound`.
    fn metrics_force_flush_completes_within<RT>(
        runtime: RT,
        worker_threads: usize,
        concurrent_readers: usize,
        bound: Duration,
    ) -> bool
    where
        RT: Runtime + Clone,
    {
        use opentelemetry_sdk::metrics::InMemoryMetricExporter;
        use opentelemetry_sdk::metrics::SdkMeterProvider;
        use opentelemetry_sdk::metrics::periodic_reader_with_async_runtime::PeriodicReader;
        use opentelemetry_sdk::metrics::reader::MetricReader;

        let (done_tx, done_rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let rt = tokio::runtime::Builder::new_multi_thread()
                .worker_threads(worker_threads)
                .enable_all()
                .build()
                .expect("failed to build tokio runtime");
            rt.block_on(async move {
                // Keep every provider alive for the duration - dropping one would tear
                // down its reader early, rather than leaving it competing for threads.
                let mut providers = Vec::with_capacity(concurrent_readers);
                let mut flushes = Vec::with_capacity(concurrent_readers);
                for _ in 0..concurrent_readers {
                    let exporter = InMemoryMetricExporter::default();
                    let reader = PeriodicReader::builder(exporter, runtime.clone())
                        .with_interval(Duration::from_secs(3600))
                        .build();
                    providers.push(
                        SdkMeterProvider::builder()
                            .with_reader(reader.clone())
                            .build(),
                    );
                    flushes.push(tokio::spawn(async move { reader.force_flush() }));
                }
                for flush in flushes {
                    let _ = flush.await;
                }
            });
            let _ = done_tx.send(());
        });

        done_rx.recv_timeout(bound).is_ok()
    }

    #[test]
    fn regular_tokio_runtime_deadlocks_metrics_force_flush_on_a_single_worker_thread() {
        assert!(
            !metrics_force_flush_completes_within(Tokio, 1, 1, Duration::from_secs(2)),
            "expected the plain opentelemetry_sdk::runtime::Tokio to deadlock force_flush \
             on a single-worker-thread runtime - if this fails, either the upstream SDK \
             changed its blocking behavior, or this test is unreliable"
        );
    }

    #[test]
    fn blocking_safe_tokio_runtime_does_not_deadlock_metrics_force_flush_on_a_single_worker_thread()
    {
        assert!(
            metrics_force_flush_completes_within(
                BlockingSafeTokioRuntime::new_for_metrics(),
                1,
                1,
                Duration::from_secs(2)
            ),
            "BlockingSafeTokioRuntime should not deadlock PeriodicReader::force_flush \
             on a single-worker-thread runtime"
        );
    }

    #[test]
    fn regular_tokio_runtime_deadlocks_metrics_force_flush_when_demand_exceeds_a_larger_pool() {
        assert!(
            !metrics_force_flush_completes_within(Tokio, 4, 8, Duration::from_secs(2)),
            "expected the plain opentelemetry_sdk::runtime::Tokio to deadlock force_flush \
             when 8 readers concurrently flush on a 4-worker-thread runtime - if this fails, \
             either the upstream SDK changed, or this test itself is unreliable"
        );
    }

    #[test]
    fn blocking_safe_tokio_runtime_does_not_deadlock_metrics_force_flush_when_demand_exceeds_a_larger_pool()
     {
        assert!(
            metrics_force_flush_completes_within(
                BlockingSafeTokioRuntime::new_for_metrics(),
                4,
                8,
                Duration::from_secs(2)
            ),
            "BlockingSafeTokioRuntime should not deadlock PeriodicReader::force_flush \
             even when 8 readers concurrently flush on a 4-worker-thread runtime"
        );
    }

    // ── BatchSpanProcessor tests (RuntimeChannel) ────────────────────────────

    /// Shared helper: spawns `concurrent_processors` `BatchSpanProcessor`s all calling
    /// `force_flush` concurrently on a runtime capped to `worker_threads` threads, then
    /// checks whether they all complete within `bound`.
    fn tracing_force_flush_completes_within<R>(
        runtime: R,
        worker_threads: usize,
        concurrent_processors: usize,
        bound: Duration,
    ) -> bool
    where
        R: RuntimeChannel + Clone,
    {
        use opentelemetry_sdk::trace::InMemorySpanExporterBuilder;
        use opentelemetry_sdk::trace::SpanProcessor;
        use opentelemetry_sdk::trace::span_processor_with_async_runtime::BatchSpanProcessor;

        let (done_tx, done_rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let rt = tokio::runtime::Builder::new_multi_thread()
                .worker_threads(worker_threads)
                .enable_all()
                .build()
                .expect("failed to build tokio runtime");
            rt.block_on(async move {
                let mut flushes = Vec::with_capacity(concurrent_processors);
                for _ in 0..concurrent_processors {
                    let exporter = InMemorySpanExporterBuilder::new().build();
                    let processor = BatchSpanProcessor::builder(exporter, runtime.clone()).build();
                    flushes.push(tokio::spawn(async move { processor.force_flush() }));
                }
                for flush in flushes {
                    let _ = flush.await;
                }
            });
            let _ = done_tx.send(());
        });

        done_rx.recv_timeout(bound).is_ok()
    }

    #[test]
    fn regular_tokio_runtime_deadlocks_tracing_force_flush_on_a_single_worker_thread() {
        assert!(
            !tracing_force_flush_completes_within(Tokio, 1, 1, Duration::from_secs(2)),
            "expected the plain opentelemetry_sdk::runtime::Tokio to deadlock force_flush \
             on a single-worker-thread runtime - if this fails, either the upstream SDK \
             changed its blocking behavior, or this test is unreliable"
        );
    }

    #[test]
    fn blocking_safe_tokio_runtime_does_not_deadlock_tracing_force_flush_on_a_single_worker_thread()
    {
        assert!(
            tracing_force_flush_completes_within(
                BlockingSafeTokioRuntime::new_for_tracing("test"),
                1,
                1,
                Duration::from_secs(2)
            ),
            "BlockingSafeTokioRuntime should not deadlock BatchSpanProcessor::force_flush \
             on a single-worker-thread runtime"
        );
    }

    #[test]
    fn regular_tokio_runtime_deadlocks_tracing_force_flush_when_demand_exceeds_a_larger_pool() {
        assert!(
            !tracing_force_flush_completes_within(Tokio, 4, 8, Duration::from_secs(2)),
            "expected the plain opentelemetry_sdk::runtime::Tokio to deadlock force_flush \
             when 8 processors concurrently flush on a 4-worker-thread runtime - if this \
             fails, either the upstream SDK changed, or this test itself is unreliable"
        );
    }

    #[test]
    fn blocking_safe_tokio_runtime_does_not_deadlock_tracing_force_flush_when_demand_exceeds_a_larger_pool()
     {
        assert!(
            tracing_force_flush_completes_within(
                BlockingSafeTokioRuntime::new_for_tracing("test"),
                4,
                8,
                Duration::from_secs(2)
            ),
            "BlockingSafeTokioRuntime should not deadlock BatchSpanProcessor::force_flush \
             even when 8 processors concurrently flush on a 4-worker-thread runtime"
        );
    }

    // ── BatchSender channel error metrics ────────────────────────────────────

    #[tokio::test]
    async fn batch_sender_channel_full_emits_metric() {
        async {
            let runtime = BlockingSafeTokioRuntime::new_for_tracing("test_processor");
            let (sender, _receiver) = runtime.batch_message_channel::<&str>(1);

            sender.try_send("first").expect("should send first message");
            let result = sender.try_send("second");
            assert!(result.is_err());

            assert_counter!(
                "apollo.router.telemetry.batch_processor.errors",
                1,
                "name" = "test_processor",
                "error" = "channel full"
            );
        }
        .with_metrics()
        .await;
    }

    #[tokio::test]
    async fn batch_sender_channel_closed_emits_metric() {
        async {
            let runtime = BlockingSafeTokioRuntime::new_for_tracing("test_processor");
            let (sender, receiver) = runtime.batch_message_channel::<&str>(1);

            drop(receiver);
            let result = sender.try_send("message");
            assert!(result.is_err());

            assert_counter!(
                "apollo.router.telemetry.batch_processor.errors",
                1,
                "name" = "test_processor",
                "error" = "channel closed"
            );
        }
        .with_metrics()
        .await;
    }

    #[tokio::test]
    async fn batch_sender_successful_send_emits_no_metric() {
        async {
            let runtime = BlockingSafeTokioRuntime::new_for_tracing("test_processor");
            let (sender, _receiver) = runtime.batch_message_channel::<&str>(1);

            assert!(sender.try_send("message").is_ok());

            let metrics = crate::metrics::collect_metrics();
            assert!(
                metrics
                    .find("apollo.router.telemetry.batch_processor.errors")
                    .is_none()
            );
        }
        .with_metrics()
        .await;
    }

    #[tokio::test]
    async fn batch_sender_for_metrics_variant_emits_no_metric_on_failure() {
        async {
            let runtime = BlockingSafeTokioRuntime::new_for_metrics();
            let (sender, receiver) = runtime.batch_message_channel::<&str>(1);

            drop(receiver);
            let result = sender.try_send("message");
            assert!(result.is_err());

            // No metrics for the nameless metrics-reader variant.
            let metrics = crate::metrics::collect_metrics();
            assert!(
                metrics
                    .find("apollo.router.telemetry.batch_processor.errors")
                    .is_none()
            );
        }
        .with_metrics()
        .await;
    }
}