-
Notifications
You must be signed in to change notification settings - Fork 15
/
core.R
353 lines (303 loc) · 8.69 KB
/
core.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#' Parse a Sentry DSN into its components
#'
#' @param dsn the DSN of a Sentry project.
#'
#' @return a named list with parsed elements of the DSN
#' @export
#'
#' @examples
#' parse_dsn("https://1234@sentry.io/1")
parse_dsn <- function(dsn) {
if (!is.character(dsn)) {
stop(
paste("dsn must be a character string, not", class(dsn))
)
}
dsn_fields <- stats::setNames(
as.list(stringr::str_match(dsn, stringr::regex("(.*)://(\\w*)(:(\\w*))?@(.*)/(.*)"))),
c("dsn", "protocol", "public_key", "ignore", "secret_key", "host", "project_id")
)
if (is.na(dsn_fields$dsn)) {
stop(
"Invalid DSN! Expected format is 'https://<public_key>@<host>/<project_id>' ",
"but received '", dsn, "' instead."
)
}
mandatory_fields_present <- sapply(
c("public_key", "host", "project_id"),
function(x) !is.na(dsn_fields[[x]]) & dsn_fields[[x]] != ""
)
if (!all(mandatory_fields_present)) {
stop(
"Expected fields 'https://<public_key>@<host>/<project_id>', but can't find ",
paste(names(mandatory_fields_present)[!mandatory_fields_present],
collapse = ", "
), " in '", dsn, "'. Please check your DSN."
)
}
return(dsn_fields)
}
#' Configure Sentry
#'
#' @param dsn the DSN of a Sentry project.
#' @param app_name name of your application (optional). Default: NULL
#' @param app_version version of your application (optional). Default: NULL
#' @param environment the environment name, such as production or staging (optional). Default: NULL
#' @param ... named lists as extra parameters for the Sentry payload
#'
#' @return populates the .sentry_env environment with character strings
#'
#' @export
#'
#' @examples
#' \dontrun{
#' configure_sentry("https://12345abcddbc45e49773bb1ca8d9c533@sentry.io/1234567")
#' sentry_env$host # sentry.io
#' }
configure_sentry <- function(dsn, app_name = NULL, app_version = NULL,
environment = NULL, ...) {
if (length(dsn) > 1) {
stop("Expected one dsn, but received ", length(dsn), " instead.")
}
# TODO: more happpy pathing here
dsn_vars <- parse_dsn(dsn)
skeleton <- list(
environment = environment,
contexts = list(
app = list(
app_name = app_name,
app_version = app_version
)
)
)
.sentry_env$payload_skeleton <- utils::modifyList(skeleton, list(...))
invisible(list2env(dsn_vars, .sentry_env))
}
#' Check if Sentry is configured
#'
#' @return boolean
#'
#' @export
#'
#' @examples
#' \dontrun{
#' configure_sentry("https://12345abcddbc45e49773bb1ca8d9c533@sentry.io/1234567")
#' is_sentry_configured() # TRUE
#' }
is_sentry_configured <- function() {
mandatory_fields <- sapply(
c("public_key", "host", "project_id"),
function(x) exists(x, envir = .sentry_env)
)
all_fields_present <- all(mandatory_fields) && all(!is.na(c(
.sentry_env$public_key,
.sentry_env$host,
.sentry_env$project_id
)))
if (!all_fields_present) {
message(
paste0(
"Expected public_key, host and project_id to be present but can't find ",
paste(names(mandatory_fields)[!mandatory_fields],
collapse = ", "
), "."
)
)
return(FALSE)
}
return(all_fields_present)
}
#' Prepare JSON payload for Sentry
#'
#' @param ... named parameters
#'
#' @return a JSON character string
#' @export
#'
#' @examples
#' \dontrun{
#' prepare_payload() # return only the core parameters
#' prepare_payload(tags = list(foo = 123, bar = "meh")) # add tags
#' }
prepare_payload <- function(...) {
# FIXME: don't allow unnamed lists e.g. prepare_paylaod(list(foo = 12, bar = 45))
if (any(names(c(...)) == "")) {
stop("All elements must be named!")
}
# TODO: check that user_inputs contains only valid names
# according to Sentry's documentation
# Hexadecimal string representing a uuid4 value.
# The length is exactly 32 characters. Dashes are not allowed.
uuid <- gsub(
pattern = "-", replacement = "",
x = uuid::UUIDgenerate(use.time = TRUE)
)
sys_info <- Sys.info()
system_parameters <- list(
logger = "R",
platform = "R", # Sentry will ignore this for now
sdk = list(
name = "SentryR",
version = .sentry_env$pkg_version
),
contexts = list(
os = list(
name = sys_info[["sysname"]],
version = sys_info[["release"]],
kernel_version = sys_info[["version"]]
),
runtime = list(
version = sprintf("%s.%s", R.version$major, R.version$minor),
type = "runtime",
name = "R",
build = R.version$version.string
)
),
# Sentry will treat the timezone as UTC/GMT by default
timestamp = strftime(as.POSIXlt(Sys.time(), tz = "GMT"), "%Y-%m-%dT%H:%M:%SZ"),
event_id = uuid
)
overrides <- list(...)
system_parameters <- utils::modifyList(
system_parameters,
overrides
)
if (!is.null(.sentry_env$payload_skeleton)) {
with_all_fields <- utils::modifyList(
.sentry_env$payload_skeleton,
system_parameters
)
without_nulls <- rm_null_obs(with_all_fields)
} else {
without_nulls <- rm_null_obs(system_parameters)
}
# rm_null_obs transforms everything into a list, and we need
# the stacktrace as a data.frame/tibble
without_nulls$exception$stacktrace <- system_parameters$exception$stacktrace
payload <- jsonlite::toJSON(
without_nulls,
auto_unbox = TRUE,
na = "null"
)
return(payload)
}
#' Send a message to a Sentry server
#'
#' @param ... named parameters
#'
#' @return sends message to Sentry
#' @export
#'
#' @examples
#' \dontrun{
#' capture(message = "oh hai there!") # send message to sentry
#' }
capture <- function(...) {
if (!is_sentry_configured()) {
warning("Sentry is not configured. Nothing was reported!")
return()
}
payload <- prepare_payload(...)
resp <- httr::POST(
url = sentry_url(),
body = payload, encode = "json",
httr::add_headers(.headers = sentry_headers())
)
resp_status_code <- httr::status_code(resp)
if (resp_status_code == 201 || resp_status_code == 200) {
resp_body <- httr::content(resp)
message("Your event was recorded in Sentry with ID ", resp_body$id)
} else {
warning(
"Error connecting to Sentry:",
httr::content(resp, "text", encoding = "UTF-8")
)
}
}
#' Report a message to Sentry
#'
#' @param message message text
#' @param ... optional additional named parameters
#' @param level the level of the message. Default: "info"
#'
#' @return nothing; sends message to Sentry
#' @export
#'
#' @examples
#' \dontrun{
#' capture_message("this is an important message", logger = "my.logger")
#' }
capture_message <- function(message, ..., level = "info") {
# TODO: hello? happy path?
capture(message = message, ..., level = level)
}
#' Report an error or exception object
#'
#' @param error an error object
#' @param ... optional additional named parameters
#' @param level the level of the message. Default: "error"
#'
#' @return nothing; sends error to Sentry
#' @export
#'
#' @examples
#' \dontrun{
#' capture_exception(simpleError("foo"), tags = list(version = "1.0"))
#' }
capture_exception <- function(error, ..., level = "error") {
# TODO: hello? happy path?
# TODO: wrangling the error should be a pure function prepare_error()
if ("function_calls" %in% names(error)) {
stacktrace <- calls_to_stacktrace(error$function_calls)
} else {
stacktrace <- calls_to_stacktrace(sys.calls())
}
error_type <- class(error)[[1]]
error_message <- gsub('(\\n|\\")', "", as.character(error))
# tibble allows list-columns, which jsonlite translate to array of maps
exception_payload <- list(
type = error_type,
value = error_message,
stacktrace = list(
frames = stacktrace
)
)
capture(exception = exception_payload, ..., level = level)
}
#' Build the sentry.io call URL
#'
#' @export
#'
#' @return a character string
sentry_url <- function() {
sprintf(
"%s://%s/api/%s/store/",
.sentry_env$protocol, .sentry_env$host, .sentry_env$project_id
)
}
#' Set the sentry.io call header
#'
#' @export
#'
#' @return a character vector
sentry_headers <- function() {
if (is.null(.sentry_env$secret_key)) {
stop("No secret key available. Did you set the DSN with configure_sentry?")
}
if (!is.na(.sentry_env$secret_key)) {
c("X-Sentry-Auth" = sprintf(
"Sentry sentry_version=7,sentry_client=sentryR/%s,sentry_timestamp=%s,sentry_key=%s,sentry_secret=%s",
.sentry_env$pkg_version,
as.integer(Sys.time()),
.sentry_env$public_key,
.sentry_env$secret_key
))
} else {
c("X-Sentry-Auth" = sprintf(
"Sentry sentry_version=7,sentry_client=sentryR/%s,sentry_timestamp=%s,sentry_key=%s",
.sentry_env$pkg_version,
as.integer(Sys.time()),
.sentry_env$public_key
))
}
}