Skip to content

Commit

Permalink
add sentry support
Browse files Browse the repository at this point in the history
  • Loading branch information
lwalejko committed Feb 6, 2024
1 parent b9486ad commit 26eecda
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 9 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Suggests:
glue,
jsonlite,
purrr
sentryR
RdMacros: mathjaxr
Config/testthat/edition: 3
Encoding: UTF-8
Expand Down
66 changes: 66 additions & 0 deletions R/run-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
#'
#' @export
run_unbiased <- function() {
tryCatch(
{
rlang::global_entrace()
},
error = function(e) {
message("Error setting up global_entrace, it is expected in testing environment: ", e$message)
}
)
setup_sentry()
host <- Sys.getenv("UNBIASED_HOST", "0.0.0.0")
port <- as.integer(Sys.getenv("UNBIASED_PORT", "3838"))
assign("db_connection_pool",
Expand All @@ -38,3 +47,60 @@ run_unbiased <- function() {
plumber::pr_run(host = host, port = port)
}
}


#' setup_sentry function
#'
#' This function is used to configure Sentry, a service for real-time error tracking.
#' It uses the sentryR package to set up Sentry based on environment variables.
#'
#' @param None
#'
#' @return None. If the SENTRY_DSN environment variable is not set, the function will
#' return a message and stop execution.
#'
#' @examples
#' setup_sentry()
#'
#' @details
#' The function first checks if the SENTRY_DSN environment variable is set. If not, it
#' returns a message and stops execution.
#' If SENTRY_DSN is set, it uses the sentryR::configure_sentry function to set up Sentry with
#' the following parameters:
#' - dsn: The Data Source Name (DSN) is retrieved from the SENTRY_DSN environment variable.
#' - app_name: The application name is set to "unbiased".
#' - app_version: The application version is retrieved from the GITHUB_SHA environment variable.
#' If not set, it defaults to "unspecified".
#' - environment: The environment is retrieved from the SENTRY_ENVIRONMENT environment variable.
#' If not set, it defaults to "development".
#' - release: The release is retrieved from the SENTRY_RELEASE environment variable.
#' If not set, it defaults to "unspecified".
#'
#' @seealso \url{https://docs.sentry.io/}
#'
#' @export
setup_sentry <- function() {
sentry_dsn <- Sys.getenv("SENTRY_DSN")
if (sentry_dsn == "") {
message("SENTRY_DSN not set, skipping Sentry setup")
return()
}

sentryR::configure_sentry(
dsn = sentry_dsn,
app_name = "unbiased",
app_version = Sys.getenv("GITHUB_SHA", "unspecified"),
environment = Sys.getenv("SENTRY_ENVIRONMENT", "development"),
release = Sys.getenv("SENTRY_RELEASE", "unspecified")
)

globalCallingHandlers(
error = global_calling_handler
)
}

global_calling_handler <- function(error) {
error$function_calls <- sys.calls()
sentryR::capture_exception(error)
signalCondition(error)
}
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ To calculate code coverage, you will need to install the `covr` package. Once in
- `covr::report()`: This method runs all tests and generates a detailed coverage report in HTML format.
- `covr::package_coverage()`: This method provides a simpler, text-based code coverage report.

Alternatively, you can use the provided `run_tests_with_coverage.sh` script to run Unbiased tests with code coverage.
Alternatively, you can use the provided `run_tests_with_coverage.sh` script to run Unbiased tests with code coverage.

# Configuring Sentry
The Unbiased server offers robust error reporting capabilities through the integration of the Sentry service. To activate Sentry, simply set the `SENTRY_DSN` environment variable. Additionally, you have the flexibility to customize the setup further by configuring the following environment variables:

* `SENTRY_ENVIRONMENT` This is used to set the environment (e.g., "production", "staging", "development"). If not set, the environment defaults to "development".

* `SENTRY_RELEASE` This is used to set the release in Sentry. If not set, the release defaults to "unspecified".
4 changes: 2 additions & 2 deletions inst/plumber/unbiased_api/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#* @tag other
#* @get /sha
#* @serializer unboxedJSON
function(res) {
sentryR::with_captured_calls(function(req, res) {
sha <- Sys.getenv("GITHUB_SHA", unset = "NULL")
if (sha == "NULL") {
res$status <- 404
return(c(error = "SHA not found"))
} else {
return(sha)
}
}
})
6 changes: 4 additions & 2 deletions inst/plumber/unbiased_api/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#*
#* @plumber
function(api) {
meta <- plumber::pr("meta.R")
study <- plumber::pr("study.R")
meta <- plumber::pr("meta.R") |>
plumber::pr_set_error(sentryR::sentry_error_handler)
study <- plumber::pr("study.R") |>
plumber::pr_set_error(sentryR::sentry_error_handler)

api |>
plumber::pr_mount("/meta", meta) |>
Expand Down
10 changes: 6 additions & 4 deletions inst/plumber/unbiased_api/study.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
#* @post /minimisation_pocock
#* @serializer unboxedJSON
#*
function(identifier, name, method, arms, covariates, p, req, res) {
sentryR::with_captured_calls(function(
identifier, name, method, arms, covariates, p, req, res
) {
return(
unbiased:::api__minimization_pocock(
identifier, name, method, arms, covariates, p, req, res
)
)
}
})

#* Randomize one patient
#*
Expand All @@ -37,8 +39,8 @@ function(identifier, name, method, arms, covariates, p, req, res) {
#* @serializer unboxedJSON
#*

function(study_id, current_state, req, res) {
sentryR::with_captured_calls(function(study_id, current_state, req, res) {
return(
unbiased:::api__randomize_patient(study_id, current_state, req, res)
)
}
})
25 changes: 25 additions & 0 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,21 @@
],
"Hash": "168f9353c76d4c4b0a0bbf72e2c2d035"
},
"sentryR": {
"Package": "sentryR",
"Version": "1.1.2",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
"httr",
"jsonlite",
"stats",
"stringr",
"tibble",
"uuid"
],
"Hash": "f37e91d605fbf665d7b5467ded4e539e"
},
"sessioninfo": {
"Package": "sessioninfo",
"Version": "1.2.2",
Expand Down Expand Up @@ -1506,6 +1521,16 @@
],
"Hash": "1fe17157424bb09c48a8b3b550c753bc"
},
"uuid": {
"Package": "uuid",
"Version": "1.2-0",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
"R"
],
"Hash": "303c19bfd970bece872f93a824e323d9"
},
"vctrs": {
"Package": "vctrs",
"Version": "0.6.4",
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/setup-testing-environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ setup_test_db_connection_pool <- function(envir = parent.frame()) {
)
}

# Make sure to disable Sentry during testing
withr::local_envvar(
SENTRY_DSN = NULL
)

# We will always run the API on the localhost
# and on a random port
Expand Down

0 comments on commit 26eecda

Please sign in to comment.