-
-
Notifications
You must be signed in to change notification settings - Fork 880
Implement OpenTelemetry Instrumentation #2422
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7f60fda
OpenTelemetry concept
shikokuchuo dc34771
Add device as attribute
shikokuchuo d6c830f
Add processing and output spans
shikokuchuo 8c4b925
Add tests and `otel_refresh_tracer()` test helper
shikokuchuo e12f93a
Label chunk attributes as such
shikokuchuo 84b314e
Add more tests
shikokuchuo 8506e1d
Add chunk label to attributes
shikokuchuo e165f5a
Add news item
shikokuchuo 0310388
Cleanup tests
shikokuchuo fa4bf37
Refactor to use generic `otel_local_active_span()`
shikokuchuo 5185ccf
Format consistently with codebase
shikokuchuo 3a914a9
Correct scope
shikokuchuo 9ebec1d
Rename function to `otel_active_span()` and add comment
shikokuchuo c8f7071
Final formatting pass
shikokuchuo 0c34a77
use the `%n%` operator [ci skip]
yihui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| otel_tracer_name = 'org.yihui.knitr' | ||
| otel_tracer = NULL | ||
| otel_is_tracing = FALSE | ||
|
|
||
| # generic otel helpers: | ||
|
|
||
| # - without specifying `scope`, the span ends when this function returns; | ||
| # to make this a local span (last as long as the function it is called from), | ||
| # specify `scope = environment()` | ||
| # - arguments remain unevaluated on early return | ||
| otel_active_span = function( | ||
| name, | ||
| label, | ||
| attributes = list(), | ||
| scope = environment() | ||
| ) { | ||
| otel_is_tracing || return() | ||
| otel::start_local_active_span( | ||
| name = sprintf('%s %s', name, label), | ||
| attributes = otel::as_attributes(attributes), | ||
| tracer = otel_tracer, | ||
| activation_scope = scope | ||
| ) | ||
| } | ||
|
|
||
| otel_cache_tracer = function() { | ||
| requireNamespace('otel', quietly = TRUE) || return() | ||
| otel_tracer <<- otel::get_tracer(otel_tracer_name) | ||
| otel_is_tracing <<- tracer_enabled(otel_tracer) | ||
| } | ||
|
|
||
| tracer_enabled = function(tracer) { | ||
| .subset2(tracer, 'is_enabled')() | ||
| } | ||
|
|
||
| otel_refresh_tracer = function(pkgname) { | ||
| requireNamespace('otel', quietly = TRUE) || return() | ||
| ns = getNamespace(pkgname) | ||
| do.call(unlockBinding, list('otel_is_tracing', ns)) # do.call for R CMD Check | ||
| do.call(unlockBinding, list('otel_tracer', ns)) | ||
| otel_tracer = otel::get_tracer() | ||
| ns[['otel_is_tracing']] = tracer_enabled(otel_tracer) | ||
| ns[['otel_tracer']] = otel_tracer | ||
| lockBinding('otel_is_tracing', ns) | ||
| lockBinding('otel_tracer', ns) | ||
| } | ||
|
|
||
| # knitr-specific helpers: | ||
|
|
||
| make_chunk_attributes = function(options) { | ||
| list( | ||
| knitr.chunk.device = options$dev, | ||
| knitr.chunk.echo = options$echo, | ||
| knitr.chunk.engine = options$engine, | ||
| knitr.chunk.eval = options$eval, | ||
| knitr.chunk.label = options$label | ||
| ) | ||
| } | ||
|
|
||
| make_knitr_attributes = function() { | ||
| list( | ||
| knitr.format = out_format(), | ||
| knitr.input = get_knitr_concord('infile'), | ||
| knitr.output = get_knitr_concord('outfile') | ||
| ) | ||
| } | ||
|
|
||
| # safe version that always returns a string | ||
| get_knitr_concord = function(name) { | ||
| item = knit_concord$get(name) | ||
| is.null(item) && return('') | ||
| item | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| library(testit) | ||
|
|
||
| if (requireNamespace('otelsdk', quietly = TRUE)) { | ||
|
|
||
| record = otelsdk::with_otel_record({ | ||
| # refresh tracer within the `with_otel_record()` scope | ||
| otel_refresh_tracer('knitr') | ||
|
|
||
| knit( | ||
| text = c('<<tidy=FALSE, eval=1:2, echo=FALSE, results="asis">>=', '1', '1+', '1', '1', '@'), | ||
| quiet = TRUE | ||
| ) | ||
| }) | ||
|
|
||
| traces = record$traces | ||
|
|
||
| assert('otel tracing works on text input', { | ||
| (length(traces) %==% 3L) | ||
| (startsWith(traces[[1L]]$name, 'knitr processing')) | ||
| (traces[[1L]]$attributes$knitr.format %==% 'latex') | ||
| (traces[[1L]]$attributes$knitr.input %==% '') | ||
| (traces[[1L]]$attributes$knitr.output %==% '') | ||
| (traces[[2L]]$name %==% 'knit unnamed-chunk-1') | ||
| (traces[[2L]]$attributes$knitr.chunk.device %==% 'pdf') | ||
| (traces[[2L]]$attributes$knitr.chunk.echo %==% FALSE) | ||
| (traces[[2L]]$attributes$knitr.chunk.engine %==% 'R') | ||
| (traces[[2L]]$attributes$knitr.chunk.eval %==% c(1, 2)) | ||
| (traces[[2L]]$attributes$knitr.chunk.label %==% 'unnamed-chunk-1') | ||
| (startsWith(traces[[3L]]$name, 'knitr output')) | ||
| (traces[[3L]]$attributes$knitr.format %==% 'latex') | ||
| (traces[[3L]]$attributes$knitr.input %==% '') | ||
| (traces[[3L]]$attributes$knitr.output %==% '') | ||
| }) | ||
|
|
||
| record = otelsdk::with_otel_record({ | ||
| otel_refresh_tracer('knitr') | ||
|
|
||
| local({ | ||
| env = new.env() | ||
| env$y = 1:3 | ||
| z = 5 | ||
| on.exit(file.remove('knit-envir.md')) | ||
| knit('knit-envir.Rmd', envir = env, quiet = TRUE) | ||
| }) | ||
| }) | ||
|
|
||
| traces = record$traces | ||
|
|
||
| assert('otel tracing works when knitting files', { | ||
| (length(traces) %==% 3L) | ||
| (traces[[1L]]$name %==% 'knitr processing knit-envir.Rmd') | ||
| (traces[[1L]]$attributes$knitr.format %==% 'markdown') | ||
| (traces[[1L]]$attributes$knitr.input %==% 'knit-envir.Rmd') | ||
| (traces[[1L]]$attributes$knitr.output %==% 'knit-envir.md') | ||
| (traces[[2L]]$name %==% 'knit test') | ||
| (traces[[2L]]$attributes$knitr.chunk.device %==% 'png') | ||
| (traces[[2L]]$attributes$knitr.chunk.echo %==% TRUE) | ||
| (traces[[2L]]$attributes$knitr.chunk.engine %==% 'R') | ||
| (traces[[2L]]$attributes$knitr.chunk.eval %==% TRUE) | ||
| (traces[[2L]]$attributes$knitr.chunk.label %==% 'test') | ||
| (traces[[3L]]$name %==% 'knitr output knit-envir.md') | ||
| (traces[[3L]]$attributes$knitr.format %==% 'markdown') | ||
| (traces[[3L]]$attributes$knitr.input %==% 'knit-envir.Rmd') | ||
| (traces[[3L]]$attributes$knitr.output %==% 'knit-envir.md') | ||
| }) | ||
|
|
||
| # reset tracer after tests | ||
| otel_refresh_tracer('knitr') | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.