Code Monkey home page Code Monkey logo

sentryr's Introduction

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("jcpsantiago/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
    ...
}

Acknowledgements

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

Contributing

PRs and issues are welcome! ๐ŸŽ‰

sentryr's People

Contributors

ahmedsamy avatar andlla avatar egnor avatar ellisvalentiner avatar jcpsantiago avatar jwebbvalent avatar kirel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sentryr's Issues

Ambiguous error with Plumber

Implemented exactly as shown in the docs with plumber, I'm getting the following out of R when new requests come into plumber

Error in z(...): unused arguments (req = <environment>, res = <environment>)

I'm assuming something has changed with the payload that Sentry expects or something is now broken.

How to get DSN?

Thank you for creating this package. I have not used sentry before so I am not sure how to get started. I understand the instructions in the README. But what should I select as a platform while creating a new project on sentry website? R is not an option there. Without that, how do I get the DSN?

Error connecting to Sentry:{"error":"No JSON data was found"}

Hello, thanks for working on this package! I was trying to set it up and I'm running into this error when I make a request to my plumber API that intentionally creates an error:

Warning in sentryR::sentry.captureException(err, req) :
  Error connecting to Sentry:{"error":"No JSON data was found"}

Looking around, it seems like this is relevant: getsentry/sentry#6978

But, not sure how to apply it to my issue. Thanks!

Files and functions are scrambled in stack traces

DISCLAIMER: I'm new to R and could be totally off base. But I believe this code is badly erroneous:

  to_keep <- !(funs %in% c(
    "stop", ".handleSimpleError", "h",
    "doTryCatch", "tryCatchList", "tryCatchOne"
  ))

  funs_to_keep <- funs[to_keep]

  names(srcrefs) <- funs_to_keep
  names(srcfiles) <- funs_to_keep

  srcrefs <- srcrefs[to_keep]

  srcfiles <- srcfiles[to_keep]

  full_function_call <- as.character(calls)
  names(full_function_call) <- funs_to_keep
  full_function_call <- full_function_call[to_keep]

Specifically, to_keep is a boolean vector of the length of the original funs identifying ones to keep; funs_to_keep is a shorter vector of just the kept ones. Reassigning names(srcrefs) <- funs_to_keep before subsetting srcrefs assigns the wrong names to the wrong references; then subsetting them leaves a smattering of nonsense function names. I think it should be:

  full_function_call <- as.character(calls)

  to_keep <- [same as before...]
  funs <- funs[to_keep]
  srcrefs <- srcrefs[to_keep]
  srcfiles <- srcfiles[to_keep]
  full_function_call <- full_function_call[to_keep]
  
  names(srcrefs) <- funs
  names(srcfiles) <- funs
  names(full_function_call) <- funs

(If you made it into a tibble or dataframe before filtering out uninteresting functions, and then added the extra columns, this would all be a lot easier and less error-prone?)

I suspect this is related to how I'm getting completely wrong source context in my stack traces...?

Default `sentry_error_handler` errors in R 4

Attempting to use sentryR with a plumber project in R 4 throws the following error when using the sentry_error_handler: missing value where TRUE/FALSE needed.

This appears to be because within the handler, req$HTTP_CONTENT_TYPE == "application/json" returns a logical(0) and not a boolean.

Can't configure app context with configure_sentry()

In this code, any extra parameters are appended to the payload skeleton which is used as the base for requests... BUT if additional contexts (especially contexts$app) are added, they will be appended to the existing contexts in the list, and the JSON serializer will rename them and thus they will get lost.

Probably better to use utils::modifyList to append the user's parameters to the system defaults, rather than just including the ... at the end of the system default list?

What is the `bunny` package?

In ghcard.R (one copy, another copy), reference is made to a bunny library:

library(magick)
library(bunny)

img_hex_gh <- image_read("man/figures/logo.png") %>%
  image_scale("400x400")

gh_logo <- bunny::github %>%
  image_scale("50x50")
...

I cannot find this bunny library on CRAN or elsewhere? Of course this file appears to exist only to regenerate the logo image, which is also checked in, so it's not really a big deal, but renv flagged it as a reference that isn't listed as a dependency and I wasn't sure what that's all about.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.