Git Product home page Git Product logo

spandoc's Introduction

spandoc

Build Status Latest Version Rust Documentation

Attribute macro that transforms doc comments in functions into tracing spans.

Details

All doc comments intended to be transformed into spans must begin with SPANDOC: :

use spandoc::spandoc;
use tracing::info;

#[spandoc]
fn foo() {
    /// SPANDOC: this will be converted into a span
    info!("event 1");

    /// this will be ignored and produce a warning for an unused doc comment
    info!("event 2");
}

The spans that are created by spandoc are explicitly scoped to the expression they're associated with.

use spandoc::spandoc;
use tracing::info;

#[spandoc]
fn main() {
    tracing_subscriber::fmt::init();
    let local = 4;

    /// SPANDOC: Emit a tracing info event {?local}
    info!("event 1");

    info!("event 2");
}

Running the above example will produce the following output

spandoc on ๎‚  await-support [!+] is ๐Ÿ“ฆ v0.1.3 via ๐Ÿฆ€ v1.44.1
โฏ cargo run --example scoped
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/examples/scoped`
Jul 09 12:42:43.691  INFO main::comment{local=4 text=Emit a tracing info event}: scoped: event 1
Jul 09 12:42:43.691  INFO scoped: event 2

Local variables can be associated with the generated spans by adding a trailing block to the doc comment. The syntax for fields in the span is the same as in tracing.

use spandoc::spandoc;
use tracing::info;

#[spandoc]
fn foo() {
    let path = "fake.txt";
    /// SPANDOC: going to load config {?path}
    info!("event 1");

    /// this will be ignored and produce a warning for an unused doc comment
    info!("event 2");
}

When applied to expressions that contain awaits spandoc will correctly use instrument() and exit/enter the span when suspending and resuming the future. If there are multiple await expressions inside of the annotated expression it will instrument each expression with the same span. The macro will not recurse into async blocks.

use std::future::Future;
use spandoc::spandoc;
use tracing::info;

fn make_liz() -> impl Future<Output = Result<(), ()>> {
    info!("this will be printed in the span from `clever_girl`");

    liz()
}

async fn liz() -> Result<(), ()> {
    info!("this will also be printed in the span from `clever_girl`");

    // return a result so we can call map outside of the scope of the future
    Ok(())
}

#[spandoc]
async fn clever_girl() {
    // This span will be entered before the await, exited correctly when the
    // future suspends, and instrument the future returned from `liz` with
    // `tracing-futures`
    /// SPANDOC: clever_girl async span
    make_liz().await.map(|()| info!("this will also be printed in the span"));
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

spandoc's People

Contributors

ben0x539 avatar yaahc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.