R - Plumber Monitoring
I didn't find any OpenTelemetry packages for R, so for this to work, I've made use of the openmetrics package that is essentially a Prometheus client for R using the OpenMetrics format.
Since we are dealing with Prometheus, I've also made use of the OpenTelemetry Contrib Collector, which has a Prometheus receiver built-in. Note, the base OpenTelemetry Collector does not have a Prometheus receiver built-in.
Prerequsites
\1. Download and install R.
\2. For Windows: Add this to a Path
environment variable (change version as needed)
C:\Program Files\R\R-4.3.0\bin
\3. Launch R.exe
or R.app
and install the following packages:
install.packages("openmetrics")
install.packages("plumber")
install.packages("httr")
\4. Download otelcol-contrib
and extract somewhere.
Plumber
Before starting, save these files, as we'll be using them as an example.
plumber.R
# plumber.R
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg="") {
list(msg = paste0("The message is: '", msg, "'"))
}
#* Plot a histogram
#* @serializer png
#* @get /plot
function() {
rand <- rnorm(100)
hist(rand)
}
#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b) {
as.numeric(a) + as.numeric(b)
}
#* Return the n-th Fibonacci number
#* @param n The n-th Fibonacci number
#* @get /fibonacci
fibonacci <- function(n="") {
if (n == 0) {
return(0)
} else if (n == 1) {
return(1)
} else {
return(fibonacci(as.numeric(n)-1) + fibonacci(as.numeric(n)-2))
}
}
plumberSrv.R
library(openmetrics)
library(plumber)
register_default_metrics()
srv <- plumber::plumb("plumber.R")
srv <- register_plumber_metrics(srv)
srv$run(host="0.0.0.0", port=c(8000))
\1. Run this in a terminal to start the Plumber server. You may need to allow this app over the firewall.
RScript plumberSrv.R
\2. Go to http://127.0.0.1:8000/__docs__/ to generate some traffic.
\3. Go to http://localhost:8000/metrics to see the following metrics.
metric name | metric type |
---|---|
process_cpu_seconds_total | counter |
process_cpu_seconds_total_created | gauge |
http_request_total | counter |
http_request_total_created | gauge |
http_request_duration_seconds | histogram |
http_request_duration_seconds_created | gauge |
\4. Save this YML file as otel-config_r-plumber.yml
. Since I'll be visualizing this data in New Relic, I've used an OTLP endpoint from New Relic, as well as an Ingest License Key
to send these metrics to New Relic.
receivers:
prometheus:
config:
scrape_configs:
- job_name: 'my-plumber-api'
scrape_interval: 5s
static_configs:
- targets: ['localhost:8000']
processors:
metricstransform/plumber:
transforms:
include: http_request_duration_seconds
action: update
new_name: http.server.duration
operations:
- action: experimental_scale_value
experimental_scale: 1000
- action: update_label
label: path
new_label: http.target
attributes/plumber:
actions:
- key: service.instance.id
action: insert
value: localhost-osx
- key: tags.team
action: insert
value: datacrunch
exporters:
otlp:
endpoint: https://otlp.nr-data.net:4317
headers:
api-key: ${NEW_RELIC_LICENSE_KEY}
service:
pipelines:
metrics:
receivers: [prometheus]
processors: [metricstransform/plumber, attributes/plumber]
exporters: [otlp]
\5. Run the OpenTelemetry Contrib Collector with the otel-config_r-plumber.yaml
./otelcol-contrib.exe --config=file:'./otel-config_r-plumber.yml'
New Relic Example
Go to APM & Services > OpenTelemetry to see the Plumber entity.
Check the Summary > Metrics page to see basic information
Click on Transactions
on the left to see additional metrics.
Finally, go to the Metrics Explorer
to see everything else.
Remarks
Using a similar methodology, I was able to repeat this for Shiny applications running with R, as well as batch jobs running in R. Check out these examples here:
Top comments (0)