|
| 1 | +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use anyhow::anyhow; |
| 5 | +use async_trait::async_trait; |
| 6 | +use cadence::{MetricSink, SinkStats}; |
| 7 | +use libdd_common::Endpoint; |
| 8 | +use libdd_shared_runtime::{worker::Worker, SharedRuntime, WorkerHandle}; |
| 9 | +use std::fmt; |
| 10 | +use std::io; |
| 11 | +use std::panic::RefUnwindSafe; |
| 12 | +use std::sync::Arc; |
| 13 | +use tokio::sync::mpsc; |
| 14 | +use tracing::error; |
| 15 | + |
| 16 | +use super::{sink, QUEUE_SIZE}; |
| 17 | + |
| 18 | +/// A [`MetricSink`] that offloads sent metrics to a [`SharedRuntime`]. |
| 19 | +#[derive(Clone)] |
| 20 | +pub struct SharedRuntimeMetricSink { |
| 21 | + sender: mpsc::Sender<String>, |
| 22 | + sink: Arc<dyn MetricSink + Send + Sync + RefUnwindSafe>, |
| 23 | +} |
| 24 | + |
| 25 | +impl fmt::Debug for SharedRuntimeMetricSink { |
| 26 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | + write!(f, "SharedRuntimeMetricSink {{ {:?} }}", self.sender) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl MetricSink for SharedRuntimeMetricSink { |
| 32 | + fn emit(&self, metric: &str) -> io::Result<usize> { |
| 33 | + let len = metric.len(); |
| 34 | + self.sender |
| 35 | + .try_send(metric.to_owned()) |
| 36 | + .map(|_| len) |
| 37 | + .map_err(|e| match e { |
| 38 | + mpsc::error::TrySendError::Full(_) => { |
| 39 | + io::Error::new(io::ErrorKind::WouldBlock, "dogstatsd channel full") |
| 40 | + } |
| 41 | + mpsc::error::TrySendError::Closed(_) => { |
| 42 | + io::Error::new(io::ErrorKind::BrokenPipe, "dogstatsd channel closed") |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + fn flush(&self) -> Result<(), std::io::Error> { |
| 48 | + self.sink.flush() |
| 49 | + } |
| 50 | + |
| 51 | + fn stats(&self) -> SinkStats { |
| 52 | + self.sink.stats() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// A [`Worker`] that drains metrics from a channel and forwards them to a |
| 57 | +/// wrapped [`MetricSink`] (e.g. `UdpMetricSink`). |
| 58 | +pub struct MetricSinkWorker { |
| 59 | + receiver: mpsc::Receiver<String>, |
| 60 | + sink: Arc<dyn MetricSink + Send + Sync + RefUnwindSafe>, |
| 61 | + pending: Option<String>, |
| 62 | +} |
| 63 | + |
| 64 | +impl std::fmt::Debug for MetricSinkWorker { |
| 65 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 66 | + f.debug_struct("MetricSinkWorker") |
| 67 | + .field("pending", &self.pending) |
| 68 | + .finish_non_exhaustive() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[async_trait] |
| 73 | +impl Worker for MetricSinkWorker { |
| 74 | + /// Awaits the next metric from the channel, storing it in `pending`. |
| 75 | + /// |
| 76 | + /// If the channel is closed this future never resolves, allowing the |
| 77 | + /// SharedRuntime to cancel it cleanly at the next yield point. |
| 78 | + async fn trigger(&mut self) { |
| 79 | + match self.receiver.recv().await { |
| 80 | + Some(metric) => self.pending = Some(metric), |
| 81 | + None => { |
| 82 | + // Channel closed — park until cancelled by the runtime. |
| 83 | + std::future::pending::<()>().await; |
| 84 | + } |
| 85 | + } |
| 86 | + println!("Received metric: {:#?}", self.pending); |
| 87 | + } |
| 88 | + |
| 89 | + /// Forwards the single metric stored by `trigger` to the wrapped sink. |
| 90 | + async fn run(&mut self) { |
| 91 | + if let Some(metric) = self.pending.take() { |
| 92 | + if let Err(e) = self.sink.emit(&metric) { |
| 93 | + error!(?e, "MetricSinkWorker: failed to emit metric"); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Drains any remaining queued metrics and flushes the wrapped sink. |
| 99 | + async fn shutdown(&mut self) { |
| 100 | + // Forward the metric that was sitting in `pending`, if any. |
| 101 | + if let Some(metric) = self.pending.take() { |
| 102 | + if let Err(e) = self.sink.emit(&metric) { |
| 103 | + error!(?e, "MetricSinkWorker: failed to emit metric on shutdown"); |
| 104 | + } |
| 105 | + } |
| 106 | + // Drain the channel. |
| 107 | + while let Ok(metric) = self.receiver.try_recv() { |
| 108 | + if let Err(e) = self.sink.emit(&metric) { |
| 109 | + error!(?e, "MetricSinkWorker: failed to emit metric on shutdown"); |
| 110 | + } |
| 111 | + } |
| 112 | + if let Err(e) = self.sink.flush() { |
| 113 | + error!(?e, "MetricSinkWorker: failed to flush sink on shutdown"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// Reset the worker in the child process after a fork. |
| 118 | + fn reset(&mut self) { |
| 119 | + self.pending = None; |
| 120 | + // Drain the channel |
| 121 | + while self.receiver.try_recv().is_ok() {} |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +pub fn create_shared_runtime_sink( |
| 126 | + endpoint: &Endpoint, |
| 127 | + runtime: &impl SharedRuntime, |
| 128 | +) -> anyhow::Result<(SharedRuntimeMetricSink, WorkerHandle)> { |
| 129 | + let (tx, rx) = mpsc::channel(QUEUE_SIZE); |
| 130 | + |
| 131 | + let sink: Arc<dyn MetricSink + Send + Sync + RefUnwindSafe> = match endpoint.url.scheme_str() { |
| 132 | + #[cfg(unix)] |
| 133 | + Some("unix") => Arc::new(sink::create_unix_sink(endpoint)?), |
| 134 | + _ => Arc::new(sink::create_udp_sink(endpoint)?), |
| 135 | + }; |
| 136 | + |
| 137 | + let sink_worker = MetricSinkWorker { |
| 138 | + receiver: rx, |
| 139 | + sink: sink.clone(), |
| 140 | + pending: None, |
| 141 | + }; |
| 142 | + |
| 143 | + let handle = runtime |
| 144 | + .spawn_worker(sink_worker, true) |
| 145 | + .map_err(|e| anyhow!("failed to spawn MetricSinkWorker: {e}"))?; |
| 146 | + |
| 147 | + let shared_sink = SharedRuntimeMetricSink { sender: tx, sink }; |
| 148 | + |
| 149 | + Ok((shared_sink, handle)) |
| 150 | +} |
0 commit comments