mesh_tracing/
lib.rs

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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Mesh tracing backend.

#![expect(missing_docs)]

mod bounded;

use self::bounded::BoundedReceiver;
use self::bounded::BoundedSender;
use self::bounded::bounded;
use anyhow::Context as _;
use futures::FutureExt;
use futures::Stream;
use futures::future::join_all;
use guid::Guid;
use inspect::InspectMut;
use mesh::MeshPayload;
use mesh::rpc::Rpc;
use mesh::rpc::RpcSend;
use pal_async::task::Spawn;
use pal_async::task::Task;
use std::fs::File;
use std::future::Future;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use tracing_subscriber::Layer;
use tracing_subscriber::filter::Filtered;
use tracing_subscriber::filter::Targets;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::reload;

#[derive(Debug, MeshPayload)]
pub struct RemoteTracer {
    pub trace_writer: TraceWriter,
    pub trace_filter: MeshFilter,
    pub perf_trace_filter: MeshFilter,
    pub perf_trace_file: File,
    pub perf_trace_flush: mesh::Receiver<Rpc<(), ()>>,
}

#[derive(Debug, MeshPayload, Clone)]
pub struct MeshFilter {
    filter: mesh::Cell<String>,
}

impl MeshFilter {
    /// Wraps `layer` in a filter that will be dynamically updated by incoming
    /// mesh messages.
    pub fn apply<L, S>(
        self,
        spawn: impl Spawn,
        layer: L,
    ) -> anyhow::Result<reload::Layer<Filtered<L, Targets, S>, S>>
    where
        L: Layer<S> + Send + Sync,
        S: tracing::Subscriber + for<'span> LookupSpan<'span>,
    {
        let targets: Targets = self
            .filter
            .with(|filter| filter.parse())
            .context("failed to parse filter")?;

        let (layer, reload_handle) = reload::Layer::new(layer.with_filter(targets));

        let mut filter_cell = self.filter;
        spawn
            .spawn("tracing filter refresh", async move {
                loop {
                    filter_cell.wait_next().await;
                    filter_cell.with(|filter| match filter.parse::<Targets>() {
                        Ok(new_targets) => {
                            let _ = reload_handle.modify(|layer| *layer.filter_mut() = new_targets);
                            tracing::info!(filter = filter.as_str(), "updated trace filter");
                        }
                        Err(err) => {
                            tracing::error!(
                                error = &err as &dyn std::error::Error,
                                "failed to update filter"
                            );
                        }
                    })
                }
            })
            .detach();

        Ok(layer)
    }
}

#[derive(Debug)]
struct MeshFilterUpdater {
    updater: mesh::CellUpdater<String>,
}

impl MeshFilterUpdater {
    fn get(&self) -> &str {
        self.updater.get()
    }

    fn update(&mut self, filter: &str) -> anyhow::Result<()> {
        // Validate the filter.
        let _: Targets = filter.parse().context("invalid filter")?;
        self.updater.set(filter.into()).now_or_never();
        Ok(())
    }
}

impl InspectMut for MeshFilterUpdater {
    fn inspect_mut(&mut self, req: inspect::Request<'_>) {
        match req.update() {
            Ok(req) => match self.update(req.new_value()) {
                Ok(()) => req.succeed(self.get().into()),
                Err(err) => req.fail(err),
            },
            Err(req) => req.value(self.get().into()),
        }
    }
}

fn filter(initial: String) -> anyhow::Result<(MeshFilterUpdater, MeshFilter)> {
    // Validate the filter.
    let _: Targets = initial.parse().context("invalid filter")?;
    let (updater, cell) = mesh::cell(initial);
    Ok((MeshFilterUpdater { updater }, MeshFilter { filter: cell }))
}

struct MeshFlusher {
    spawn: Box<dyn Spawn>,
    remotes: Vec<mesh::Sender<Rpc<(), ()>>>,
}

impl MeshFlusher {
    fn add(&mut self) -> mesh::Receiver<Rpc<(), ()>> {
        let (send, recv) = mesh::channel();
        self.remotes.retain(|s| !s.is_closed());
        self.remotes.push(send);
        recv
    }
}

impl InspectMut for MeshFlusher {
    fn inspect_mut(&mut self, req: inspect::Request<'_>) {
        match req.update() {
            Ok(req) => {
                let join = join_all(self.remotes.iter().map(|r| r.call(|rpc| rpc, ())));
                let req = req.defer();
                self.spawn
                    .spawn("trace-flush", async move {
                        let _ = join.await;
                        req.succeed(true.into());
                    })
                    .detach();
            }
            Err(req) => req.value(false.into()),
        }
    }
}

#[derive(Debug, MeshPayload)]
pub struct TracingRequest {
    pub log_type: Type,
    pub timestamp: u64,
    pub level: Level,
    pub name: Option<Vec<u8>>,
    pub target: Option<Vec<u8>>,
    pub fields: Option<Vec<u8>>,
    pub activity_id: Option<Guid>,
    pub related_activity_id: Option<Guid>,
    pub correlation_id: Option<Guid>,
    pub message: Vec<u8>,
}

#[derive(Debug, MeshPayload)]
pub enum Type {
    Event = 0,
    SpanEnter = 1,
    SpanExit = 2,
}

#[derive(Debug, MeshPayload)]
pub enum Level {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

#[derive(MeshPayload, Debug)]
pub struct TraceWriter(BoundedSender<TracingRequest>);

impl From<BoundedSender<TracingRequest>> for TraceWriter {
    fn from(sender: BoundedSender<TracingRequest>) -> Self {
        Self(sender)
    }
}

impl TraceWriter {
    pub fn send(
        &self,
        log_type: Type,
        timestamp: u64,
        level: Level,
        name: Option<Vec<u8>>,
        target: Option<Vec<u8>>,
        fields: Option<Vec<u8>>,
        activity_id: Option<Guid>,
        related_activity_id: Option<Guid>,
        correlation_id: Option<Guid>,
        message: Vec<u8>,
    ) -> bool {
        self.0
            .try_send(TracingRequest {
                log_type,
                timestamp,
                level,
                name,
                target,
                fields,
                activity_id,
                related_activity_id,
                correlation_id,
                message,
            })
            .is_ok()
    }
}

/// Object to configure and reconfigure tracing for Underhill.
#[derive(InspectMut)]
pub struct TracingBackend {
    #[inspect(skip)]
    state: BackendState,

    #[inspect(mut, safe)]
    filter: MeshFilterUpdater,
    #[inspect(rename = "perf/filter", mut)]
    perf_filter: MeshFilterUpdater,
    #[inspect(rename = "perf/flush", mut)]
    perf_flush: MeshFlusher,
}

struct BackendState {
    trace_writer: mesh::Sender<BoundedReceiver<TracingRequest>>,
    trace_filter: MeshFilter,
    perf_trace_filter: MeshFilter,
    perf_trace_file: File,
    flush_send: mesh::Sender<Rpc<(), ()>>,
    task: Task<()>,
}

pub struct TracingRequestStream {
    new_receivers: mesh::Receiver<BoundedReceiver<TracingRequest>>,
    receivers: Vec<BoundedReceiver<TracingRequest>>,
}

impl Stream for TracingRequestStream {
    type Item = TracingRequest;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        let mut i = 0;
        while let Poll::Ready(Some(recv)) = Pin::new(&mut this.new_receivers).poll_next(cx) {
            this.receivers.push(recv);
        }
        while i < this.receivers.len() {
            match Pin::new(&mut this.receivers[i]).poll_next(cx) {
                r @ Poll::Ready(Some(_)) => return r,
                Poll::Ready(None) => {
                    this.receivers.swap_remove(i);
                }
                Poll::Pending => {}
            }
            i += 1;
        }
        Poll::Pending
    }
}

impl TracingBackend {
    /// Spawns worker that sends traces to the host
    pub fn new<Fut, F>(
        driver: impl 'static + Spawn,
        trace_filter: String,
        perf_trace_filter: String,
        handle_requests: F,
    ) -> anyhow::Result<Self>
    where
        F: 'static + Send + FnOnce(TracingRequestStream, mesh::Receiver<Rpc<(), ()>>) -> Fut,
        Fut: 'static + Send + Future<Output = ()>,
    {
        let (send, recv) = mesh::channel();

        let (trace_filter_updater, trace_filter) = filter(trace_filter)?;
        let (perf_trace_filter_updater, perf_trace_filter) = filter(perf_trace_filter)?;

        // This perf trace file can be shared across all processes in the mesh,
        // without extra synchronization. This works because the file extending
        // writes are atomic.
        let perf_trace_file = File::options()
            .append(true)
            .create(true)
            .open("underhill.perfetto")
            .context("failed to open underhill.perfetto")?;

        let (flush_send, flush_recv) = mesh::channel();
        let task = driver.spawn(
            "log write",
            handle_requests(
                TracingRequestStream {
                    new_receivers: recv,
                    receivers: Vec::new(),
                },
                flush_recv,
            ),
        );
        Ok(Self {
            state: BackendState {
                trace_writer: send,
                trace_filter,
                perf_trace_filter,
                perf_trace_file,
                flush_send,
                task,
            },
            filter: trace_filter_updater,
            perf_filter: perf_trace_filter_updater,
            perf_flush: MeshFlusher {
                spawn: Box::new(driver),
                remotes: Vec::new(),
            },
        })
    }

    pub fn tracer(&mut self) -> RemoteTracer {
        let (send, recv) = bounded(256);
        self.state.trace_writer.send(recv);
        RemoteTracer {
            trace_writer: TraceWriter(send),
            trace_filter: self.state.trace_filter.clone(),
            perf_trace_filter: self.state.perf_trace_filter.clone(),
            perf_trace_file: self.state.perf_trace_file.try_clone().unwrap(),
            perf_trace_flush: self.perf_flush.add(),
        }
    }

    /// Requests that all sent log messages have been flushed.
    pub async fn flush(&mut self) {
        self.state.flush_send.call(|x| x, ()).await.ok();
    }

    /// Shuts down the tracing backend.
    ///
    /// This implicitly flushes any sent log messages.
    pub async fn shutdown(self) {
        drop(self.state.flush_send);
        self.state.task.await;
    }
}