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
#![allow(clippy::needless_doctest_main)]
//! A [tracing](https://github.com/tokio-rs/tracing/) [Layer][`GraphLayer`] for generating a call graphs.
//!
//! # Overview
//!
//! [`tracing`] is a framework for instrumenting Rust programs to collect
//! scoped, structured, and async-aware diagnostics. `tracing-callgraph` provides helpers
//! for consuming `tracing` instrumentation that can later be visualized as a
//! call graph in [Graphviz](http://www.graphviz.org/) `dot` representation.
//!
//! ## Layer Setup
//!
//! ```rust
//! use tracing_callgraph::GraphLayer;
//! use tracing_subscriber::{registry::Registry, prelude::*};
//!
//! fn setup_global_subscriber() -> impl Drop {
//!     let (graph_layer, _guard) = GraphLayer::with_file("./output.dot").unwrap();
//!     let subscriber = Registry::default().with(graph_layer);
//!
//!     tracing::subscriber::set_global_default(subscriber).expect("Could not set global default");
//!     _guard
//! }
//!
//! #[tracing::instrument]
//! fn outer_a() {
//!     inner()
//! }
//!
//! #[tracing::instrument]
//! fn outer_b() {
//!     inner()
//! }
//!
//! #[tracing::instrument]
//! fn inner() {}
//!
//! fn main() {
//!     let _ = setup_global_subscriber();
//!     outer_a();
//!     outer_b();
//! }
//! ```
//!
#![warn(
    missing_debug_implementations,
    missing_docs,
    rust_2018_idioms,
    unreachable_pub,
    bad_style,
    const_err,
    dead_code,
    improper_ctypes,
    non_shorthand_field_patterns,
    no_mangle_generic_items,
    overflowing_literals,
    path_statements,
    patterns_in_fns_without_body,
    private_in_public,
    unconditional_recursion,
    unused,
    unused_allocation,
    unused_comparisons,
    unused_parens,
    while_true
)]

pub use error::Error;

use error::Kind;
use petgraph::{dot::Dot, graphmap::GraphMap, Directed};
use std::{
    fs::File,
    io::{BufWriter, Write},
    path::Path,
    sync::{Arc, Mutex},
};
use tracing::{span, Subscriber};
use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer};

mod error;

type CallGraph = GraphMap<&'static str, usize, Directed>;

/// A `Layer` that records span open events as directed edges in a call graph.
///
/// # Dropping and Flushing
///
/// To ensure all data is flushed when the program exits, `GraphLayer` exposes
/// the [`flush_on_drop`] function, which returns a [`FlushGuard`]. The [`FlushGuard`]
/// will flush the writer when it is dropped. If necessary, it can also be used to manually
/// flush the writer.
#[derive(Clone, Debug)]
pub struct GraphLayer {
    graph: Arc<Mutex<CallGraph>>,
    top_node: Option<&'static str>,
}

impl GraphLayer {
    /// Add a top node to the graph.
    #[allow(clippy::clone_double_ref)]
    pub fn enable_top_node(mut self, name: &'static str) -> Self {
        self = self.disable_top_node();
        self.top_node = Some(name.clone());
        self.graph.lock().unwrap().add_node(name);
        self
    }

    /// Remove the top node to the graph.
    pub fn disable_top_node(mut self) -> Self {
        if let Some(name) = self.top_node.take() {
            self.graph.lock().unwrap().remove_node(name);
        }
        self
    }
}

/// An RAII guard for flushing a writer.
#[must_use]
#[derive(Debug)]
pub struct FlushGuard<W>
where
    W: Write + 'static,
{
    graph: Arc<Mutex<CallGraph>>,
    writer: W,
}

impl<W> FlushGuard<W>
where
    W: Write + 'static,
{
    /// Flush the internal writer, ensuring that the graph is written.
    pub fn flush(&mut self) -> Result<(), Error> {
        let graph = match self.graph.lock() {
            Ok(graph) => graph,
            Err(e) => {
                if !std::thread::panicking() {
                    panic!("{}", e);
                } else {
                    return Ok(());
                }
            }
        };
        writeln!(self.writer, "{:?}", Dot::new(&*graph))
            .map_err(Kind::FlushFile)
            .map_err(Error)?;

        self.writer.flush().map_err(Kind::FlushFile).map_err(Error)
    }
}

impl<W> Drop for FlushGuard<W>
where
    W: Write + 'static,
{
    fn drop(&mut self) {
        match self.flush() {
            Ok(_) => (),
            Err(e) => e.report(),
        }
    }
}

impl Default for GraphLayer {
    fn default() -> Self {
        let graph = CallGraph::new();
        Self {
            graph: Arc::new(Mutex::new(graph)),
            top_node: None,
        }
    }
}

impl GraphLayer {
    /// Returns a new [`GraphLayer`] which constructs the call graph.
    pub fn new() -> Self {
        Default::default()
    }

    /// Returns a [`FlushGuard`] which will flush the `GraphLayer`'s writer when
    /// it is dropped, or when `flush` is manually invoked on the guard.
    pub fn flush_on_drop<W>(&self, writer: W) -> FlushGuard<W>
    where
        W: Write + 'static,
    {
        FlushGuard {
            graph: self.graph.clone(),
            writer,
        }
    }
}

impl GraphLayer {
    /// Constructs a `GraphLayer` that constructs the call graph, and a
    /// `FlushGuard` which writes the graph to a `dot` file when dropped.
    pub fn with_file(path: impl AsRef<Path>) -> Result<(Self, FlushGuard<BufWriter<File>>), Error> {
        let path = path.as_ref();
        let file = File::create(path)
            .map_err(|source| Kind::CreateFile {
                path: path.into(),
                source,
            })
            .map_err(Error)?;
        let writer = BufWriter::new(file);
        let layer = Self::new();
        let guard = layer.flush_on_drop(writer);
        Ok((layer, guard))
    }
}

impl<S> Layer<S> for GraphLayer
where
    S: Subscriber + for<'span> LookupSpan<'span>,
{
    fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
        let mut locked = self.graph.lock().unwrap();

        // Add node
        let first = ctx.span(id).expect("expected: span id exists in registry");
        let node_b = first.name();
        locked.add_node(node_b);

        // Find parent node
        let node_a = if let Some(parent) = first.parent() {
            parent.name()
        } else if let Some(name) = self.top_node {
            name
        } else {
            return;
        };

        if let Some(weight) = locked.edge_weight_mut(node_a, node_b) {
            // Increase edge weight
            *weight += 1;
        } else {
            // Add edge
            locked.add_edge(node_a, node_b, 1);
        }
    }
}