Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is there any global way to capture the name of called/executed functions? #57

Open
helio-frota opened this issue Jun 14, 2024 · 2 comments

Comments

@helio-frota
Copy link

I found this crate cool thanks.

I have 2 use cases that helps me

a) to print the name of executed function

let rt = emit::setup()
        .emit_to(emit::emitter::from_fn(|evt| println!("{}", evt.msg())))
        .and_emit_to(emit_file::set("./target/logs/AAAAAAAAAAAA.log").spawn()?)
        .init();

b) to send the function args content to log file with

pub fn similar(blocks: Vec<String>, threshold: f64) -> Vec<(String, String, f64)> {
    emit::info!("{#[emit::as_serde] blocks}");

in case of a) I need to add #[emit::span("similar")] before each function to grab the executed function name

for example:

#[emit::span("similar")]
pub fn similar(blocks: Vec<String>, threshold: f64) -> Vec<(String, String, f64)> {

So my question is related to this ^ situation ... I was reading the docs and the code but I have no idea if that is possible to do.
thanks.

@KodrAus
Copy link
Contributor

KodrAus commented Jun 14, 2024

Hi @helio-frota! 👋

If I understand correctly, you want to be able to emit the names and arguments of functions as they execute, without necessarily having to annotate them yourself? There isn't any built-in way to do this unfortunately, emit only interacts with functions through its macros. I think it would be possible with debug info and a debugger, the Sentry folks may have built something along those lines, but I haven't looked into it myself.

I am interested in trying to come up with a nice macro for annotating function calls so they produce useful flame-graph-like events though. Until then, I would probably use the #[emit::span] macro like this to annotate function calls:

extern crate emit;
extern crate emit_file;
extern crate emit_term;

use std::time::Duration;

#[emit::span("{fn_name}({#[emit::as_serde] blocks})", fn_name: "similar")]
fn similar(blocks: Vec<String>) -> Vec<(String, String, f64)> {
    vec![]
}

fn main() {
    let rt = emit::setup()
        .emit_to(emit_term::stdout())
        .and_emit_to(emit_file::set("./target/logs/log.txt").spawn().unwrap())
        .init();

    similar(vec![
        "a".to_owned(),
        "b".to_owned(),
        "c".to_owned(),
    ]);

    rt.blocking_flush(Duration::from_secs(5));
}

That will produce output like this to the console:

Screenshot 2024-06-15 at 8 33 31 am

and like this to the rolling file:

{"ts_start":"2024-06-14T22:25:29.198058000Z","ts":"2024-06-14T22:25:29.198731000Z","msg":"similar([\"a\", \"b\", \"c\"])","tpl":"{fn_name}({blocks})","blocks":["a","b","c"],"event_kind":"span","fn_name":"similar","span_id":"797146cc6879de98","span_name":"{fn_name}({blocks})","trace_id":"74d5777c80b9fcf64d85deb6efd6ce31"}

With a nicer macro, I think we could get that annotation down to something like this:

// Doesn't exist yet
#[emit_fn::span]
fn similar(blocks: Vec<String>) -> Vec<(String, String, f64)> {
    vec![]
}

Would that kind of approach work for you?

@helio-frota
Copy link
Author

If I understand correctly, you want to be able to emit the names and arguments of functions as they execute, without necessarily having to annotate them yourself?

yeah that's the idea

// Doesn't exist yet
#[emit_fn::span]

Would that kind of approach work for you?

yes 🎉

This already would helps a lot as we could change all the pub functions with the following script for example

#!/bin/bash
root_dir="."
find "$root_dir" -name "*.rs" | while read -r file; do
  if grep -q "pub fn" "$file"; then
    sed -i '/pub fn/ i #[emit_fn::span]' "$file"
  fi
done

thanks for the response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants