r-lib / debugme

Easy and efficient debugging for R packages
https://r-lib.github.io/debugme/
Other
149 stars 10 forks source link

Problem configuring debugme for plumber application #30

Closed nuest closed 6 years ago

nuest commented 6 years ago

I want to use DEBUG for a plumber application. You can find it here: https://github.com/nuest/o2r-inspecter/tree/debugme-issue-report

I would like to configure the debugging using the package name inspecter, but there might be an issue with the environments or with the way my package is set up.

To find out more I added logs of the default values used in debug::debugme() at three times:

This is what I get (without a DEBUGME environment variable set):

R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
[...]

> library("inspecter", verbose = TRUE)
onLoad: libname: /home/daniel/R/x86_64-pc-linux-gnu-library/3.4 environment: base 
<environment: namespace:base>

Attaching package: ‘inspecter’

The following object is masked from ‘package:stats’:

    start

> inspecter::start()
start: pkg: R_GlobalEnv 
<environment: R_GlobalEnv>
Starting server to listen on port 8091
Running the swagger UI at http://127.0.0.1:8091/__swagger__/

Enabling debugging for all environments does the trick for now:

> Sys.setenv(DEBUGME = "R_GlobalEnv,base,inspecter")
> library("inspecter", verbose = TRUE)
onLoad: libname: /home/daniel/R/x86_64-pc-linux-gnu-library/3.4 environment: base 
<environment: namespace:base>

Attaching package: ‘inspecter’

The following object is masked from ‘package:stats’:

    start

> inspecter::start()
start: pkg: R_GlobalEnv 
<environment: R_GlobalEnv>
inspecter starting inspecter +24095ms 
inspecter initialize plumber with route definitions from /home/daniel/R/x86_64-pc-linux-gnu-library/3.4/inspecter/api.R +1ms 
inspecter run plumber +22ms 
Starting server to listen on port 8091
Running the swagger UI at http://127.0.0.1:8091/__swagger__/
inspecter +-2017-12-15 11:23:52 | GET /status | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/63.0.3239.84 Chrome/63.0.3239.84 Safari/537.36  from  127.0.0.1 +8336ms 
inspecter +-2017-12-15 11:23:52 | GET /status | response sent: 200 | size: 344 B +22ms

Any help or ideas appreciated, if this setup is too complex for you to evaluate I can try to work out a minimal reprex.

nuest commented 6 years ago

A short update: it actually works with only DEBUGME=inspecter when setting it as an env var outside of R. Does this clarify anything?

DEBUGME=inspecter R -q -e 'library("inspecter"); inspecter::start()'
> library("inspecter"); inspecter::start()
onLoad: libname: /home/daniel/R/x86_64-pc-linux-gnu-library/3.4 environment: base
<environment: namespace:base>

Attaching package: ‘inspecter’

The following object is masked from ‘package:stats’:

    start

start: pkg: R_GlobalEnv
<environment: R_GlobalEnv>
inspecter starting...
inspecter initialize plumber with route definitions from /home/daniel/R/x86_64-pc-linux-gnu-library/3.4/inspecter/api.R +29ms
init: pkg: R_GlobalEnv
<environment: R_GlobalEnv>
inspecter run plumber +201ms
Starting server to listen on port 8091
gaborcsardi commented 6 years ago

Debugging is turned on/off when you load your package. So setting the env var after loading it, will not change anything.

nuest commented 6 years ago

Sorry, I don't know what changed, I failed to re-test the combinations of values for DEBUGME:

> Sys.setenv(DEBUGME = "inspecter")
> library(inspecter)
onLoad: libname: /home/daniel/R/x86_64-pc-linux-gnu-library/3.4 environment: base 
<environment: namespace:base>

Attaching package: ‘inspecter’

The following object is masked from ‘package:stats’:

    start

> inspecter::start()
start: pkg: R_GlobalEnv 
<environment: R_GlobalEnv>
inspecter starting... 
inspecter initialize plumber with route definitions from /home/daniel/R/x86_64-pc-linux-gnu-library/3.4/inspecter/api.R +35ms 
inspecter run plumber +275ms 
Starting server to listen on port 8091
Running the swagger UI at http://127.0.0.1:8091/__swagger__/
nuest commented 6 years ago

Apologies for the back and forth. The problem remains for logs from the file inst/api.R.

@trestletech can you shed light on how plumber::plumb(file = "inst/api.R") loads the file, i.e. what environment it should be in?

trestletech commented 6 years ago

The runtime and environments for plumber are documented here: https://www.rplumber.io/docs/runtime.html

nuest commented 6 years ago

@trestletech Thanks, that helps. So I could define my routers programmatically instead of using annotations and then set my own environment. This line also explains why logging works when I enable it for the the environment R_GlobalEnv,base. I tried to fix this by creating a new environment with my package environment as parent and initiating my router with it, but that did not work.

Eventually I switched from annotations to defining my endpoints with pr$handle().

Hope this helps other people logging plumber with debugme.