Skip to content

Conversation

@shikokuchuo
Copy link
Contributor

@shikokuchuo shikokuchuo commented Oct 28, 2025

Closes #2421.

This PR implements basic OpenTelemetry instrumentation for knitr.

Abides by general otel semantic conventions in terms of what span names / attributes should look like. As knitr does not fit into any of the existing categories, there are no compulsory attributes to include.

For now, I've included 'engine', 'echo' and 'eval' options as attributes (metadata). Feel free to suggest others to add.

The following is an example screenshot of the spans created (from knitting purrr vignettes). You can see the mirai spans nested within the knitr chunk span.

Screenshot 2025-10-28 at 15 35 08

As per our video call, I've verified that this works fine for python chunks running reticulate.

We cache the otel tracer on package load - if it's not enabled then there is minimal overhead as otel_local_active_span() has an early return if the cached otel_is_tracing variable is FALSE.

Todo:

  • Consider other attributes
  • Add docs and tests

Feel free to ask any questions / point to other alternatives for implementation that I may not have thought of.

Thanks!

@CLAassistant
Copy link

CLAassistant commented Oct 28, 2025

CLA assistant check
All committers have signed the CLA.

@shikokuchuo shikokuchuo marked this pull request as draft October 28, 2025 15:52
Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Just let me know when you think it's ready and I'll merge it. Thanks!

Copy link
Collaborator

@cderv cderv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed really clean integration. Thank you!

@shikokuchuo shikokuchuo marked this pull request as ready for review October 29, 2025 20:28
@shikokuchuo
Copy link
Contributor Author

I've updated this PR, with the notable changes being:

  • Now there are knitr processing and knitr output spans in addition to the chunk spans
    Otel spans are meant to be short-lived, hence we create these to mark the start and end of a knit operation rather than have one span covering the whole thing
  • These spans have knitr.format, knitr.input and knitr.output (filename) attributes
  • Chunk attributes are now named knitr.chunk.* - with * being device, echo, engine, eval and label
  • Added tests

You can see a very small interactive example here (unfortunately it doesn't let me share multiple spans):
https://logfire-eu.pydantic.dev/public-trace/cdd13007-6045-486a-9c08-fb7658f9aa15?spanId=b5e798da01f6b252

I've added a news item, but wasn't sure if you'd want more documentation, or what form that would take. Happy to add something down the line if you let me know what you prefer.

@shikokuchuo shikokuchuo requested a review from yihui October 29, 2025 20:47
@atusy
Copy link
Collaborator

atusy commented Oct 29, 2025

How nice! I didn't know there is otel in R.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge this PR in a second. If you come up with anything else, please feel free to open subsequent PRs. Thanks!

@yihui yihui merged commit 9989924 into yihui:master Oct 31, 2025
1 check passed
@shikokuchuo shikokuchuo deleted the otel branch October 31, 2025 07:16
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

Successfully merging this pull request may close these issues.

Add OpenTelemetry

5 participants