MyNixOS website logo
Description

Send Errors and Messages to Sentry.

Unofficial client for 'Sentry' <https://sentry.io>, a self-hosted or cloud-based error-monitoring service. It will inform about errors in real-time, and includes integration with the 'Plumber' package.

R-CMD-check CRAN status Lifecycle: stable License: MIT

sentryR

sentryR is an unofficial R client for Sentry. It includes an error handler for Plumber for uncaught exceptions.

Installation

You can install the latest development version of sentryR with:

# install.packages("remotes")
remotes::install_github("ozean12/sentryR")

or the stable version in CRAN with:

install.packages("sentryR")

Using sentryR

configure_sentry and capture are the two core functions of sentryR. The first sets up an isolated environment with your Sentry project's DSN, optionally your app's name, version and the environment it's running in. Both configure_sentry and any of the capture_ functions accept additional fields to pass on to Sentry as named lists. NULLifying a field will remove it.

library(sentryR)

configure_sentry(dsn = Sys.getenv("SENTRY_DSN"),
                 app_name = "myapp", app_version = "8.8.8",
                 environment = Sys.getenv("APP_ENV"),
                 tags = list(foo = "tag1", bar = "tag2"),
                 runtime = NULL)

capture(message = "my message", level = "info")

You are encouraged to use the two wrappers around capture included: capture_exception, which handles the error object and then reports the error to Sentry, and capture_message for transmitting messages. Refer to the Sentry docs for a full list of available fields.

By default sentryR will send the following fields to Sentry:

list(
  logger = "R",
  platform = "R", # Sentry will ignore this for now
  sdk = list(
    name = "SentryR",
    version = ...
  ),
  contexts = list(
    os = list(
      name = ...,
      version = ...,
      kernel_version = ...
    ),
    runtime = list(
      version = ...,
      type = "runtime",
      name = "R",
      build = ...
    )
  ),
  timestamp = ...,
  event_id = ...
)

capture_exception further adds the exception field to the payload.

Example with Plumber

In a Plumber API, besides the initial configuration for Sentry, you'll also have to set the error handler.

sentryR ships with the default plumber error handler wrapped in the convenience function sentry_error_handler, but you can use your own function and wrap it as below:

library(plumber)
library(sentryR)

# add list of installed packages and their versions.
# this can be slow in systems with a high number of packages installed,
# so it is not the default behavior
installed_pkgs_df <- as.data.frame(utils::installed.packages(),
stringsAsFactors = FALSE
)
versions <- installed_pkgs_df$Version
names(versions) <- installed_pkgs_df$Package
packages <- as.list(versions)

configure_sentry(dsn = Sys.getenv('SENTRY_DSN'), 
                 app_name = "myapp", app_version = "1.0.0",
                 modules = packages)

my_sentry_error_handler <- wrap_error_handler_with_sentry(my_error_handler)

r <- plumb("R/api.R")
r$setErrorHandler(my_sentry_error_handler)
r$run(host = "0.0.0.0", port = 8000)

and wrap your endpoint functions with with_captured_calls

#* @get /error
api_error <- with_captured_calls(function(res, req){
  stop("error")
})

once this is done, Plumber will handle any errors, send them to Sentry using capture_exception, and respond with status 500 and the error message. You don't need to do any further configuration.

Example with Shiny

You can also use sentryR to capture exceptions in your Shiny applications by providing a callback function to the shiny.error option.

library(shiny)
library(sentryR)

configure_sentry(dsn = Sys.getenv('SENTRY_DSN'), 
                 app_name = "myapp", app_version = "1.0.0",
		 modules = packages)

error_handler <- function() {
    capture_exception(error = geterrmessage())
}

options(shiny.error = error_handler)

shinyServer(function(input, output) {
    # Define server logic
    ...
}

TODO

  • test the error handling functions, needs mocking?
  • posting to sentry asynchronously
  • vignettes

Acknowledgements

sentryR took inspiration from raven-clj a Clojure interface to Sentry.

Contributing

PRs and issues are welcome! :tada:

Metadata

Version

1.1.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows