c-cube / ocaml-trace

Common interface for tracing/instrumentation libraries in OCaml
https://c-cube.github.io/ocaml-trace/
22 stars 6 forks source link

Suggestion to use `__FUNCTION__` #6

Closed lindig closed 1 year ago

lindig commented 1 year ago

I have not yet used the library but assume that most users at least initially would instrument functions by saying:

Trace.with_span ~__FILE__ ~__LINE__ __FUNCTION__ @@ fun _sp ->

Only when you need to trace parts inside a function, you would need a different name. There is also __LOC__ available. I was wondering, why you didn't suggest to use __FUNCTION__? The code above could be easily inserted using search/replace in an editor.

Addendum: I looked at the implementation and indeed it encourages to use __FUNCTION__:

let[@inline] with_span ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name f =

So you can ignore my comment.

c-cube commented 1 year ago

Indeed! However it's up to collectors to make use of this option, so far none of them do. __FILE__ and __LINE__ are directly used by Tracy (which can then display source code), but otherwise it remains to be seen what use can be made of __FUNCTION__.

For example I think the trace-tef collector could, if __FUNCTION__ is passed, add it as a key/value metadata.

ELLIOTTCABLE commented 1 year ago

One thing worth noting, is that "span name" should probably contain additional data, besides __FUNCTION__ — as a random example, consider an http_handle function; having every single span have the same name of Web.http_handle (or whatever) would be fairly useless. To some extent, the point-of-instrumentation, and the "what does this do, at a glance" are two reasonably different things (in particular, the latter is likely to rely on runtime state.)

Just a tip, hope that's helpful.

c-cube commented 1 year ago

And that's what the ?data argument is all about, indeed :-). For an instrumentation middleware in an http service, you'd typically include the query path and maybe some headers in there.