A single pane of glass for Telemetry data from your R app
All transactions through R Shiny. Breakdowns, errors highlighted, perf stats
All Logs in context, fast, easy to query, no indexing necessary, create alerts from UI
Flexible data representation - bring custom Event data too
Compare performance across processing nodes
This demo application shows sending a regular distributed trace to New Relic. There are not any child spans added in the demo yet. You must add your Insert API Key to r-dist-trace.R
The browser monitoring requires your Browser snippet to be added to www/newrelic.js taking care to have removed the <script> tags.
Use the tracer()and errorTracer()functions in your R scripts by referencing the r-dist-trace.Rfile. Do this by adding source("r-dist-trace.R")into every file to reference.
All arguments are optional, the more you can add the more useful your traces will become:
serviceNamepass the name of the service. This will default to Custom R Service. You can also override this globally by setting the variable RServiceName in the distributed trace source file.hostNamepass this to override the name of the host. This will automatically be set with the nodename of the host running the R script.UUIDthis is used as the Trace ID. This will by default be generated. If you need to update a span later, reference the same UUID for the new span to be added to the trace.spanIDthis is the Span ID. This will by default be generated.durationthis is the time, in milliseconds, the span taken. By default this is 1 millisecond, you should look to control the timer yourself for this is not yet part of this solution.namethis is the name of the operation. This will default to Unnamed Calldescriptionsome description of the call. This will default to Sent using ...userSessionIdthe ID of the user running the operation. This will default to an instruction placeholder. In all of your calls, you should passsession$tokenwhich will pass the Session ID generated by R Shiny to the trace and can be useful in debugging.
New: use trace.start() at the beginning of your transaction and trace.end() at the end for a timed trace. Save your trace.start() as a variable and pass it to trace.end() to accomplish this. E.g:
traceStart = trace.start()
<<>>
trace.end(timeStart = traceStart, userSessionId = session$token, name = "MyTransaction", description = "blah blah blah")
Error traces use all of the same as above, but is called using errorTracer() and includes two additional arguments - along with all of the above. Those new arguments are:
errorNamethe name of the error. This will default to Unknown errorerrorTextthe message of the error, or additional context. This will default to This error is unspecified...
Include your Browser snippet in newrelic.js - make sure to remove trhe HTML Script tags!
This script includes metadata from R Shiny, and begins to send those attributes to New Relic Browser after Shiny has completed its own work in the webpage. This is done by checking that Shiny exists as an object in the window.
Variables that are captured out of the box:
webSocketURL: queried in the browser fromShiny.shinyapp.$socket.urlwebSocketState: queried in the browser fromShiny.shinyapp.$socket.readyStatewebSocketBufferedAmount: queried in the browser fromShiny.shinyapp.$socket.bufferedAmountshinyErrors: queried in the browser fromShiny.shinyapp.$errorsshinyConditionals: queried in the browser fromShiny.shinyapp.$conditionalssessionID: queried in the browser fromShiny.shinyapp.config.sessionIdworkerID: queried in the browser fromShiny.shinyapp.config.workerId
You can optionally confugure the script to configure the output variables of your script, those that are sent to the browser (already available in the Shiny.shinyapp.$values collection). Inside here, after $values append the example values, e.g. eventValue and value to the name of yours. This will look like in your code as output$value where you are interested in the name after $
You can optionally but usefully, add in New Relic Browser Page Actions. This should correspond to your R Shiny form inputs. In the example app, find these correspond to the buttons by ID go and event. This is exactly the name for the input you provided in your R code. For instance actionButton("go", "Send a trace") where go is the input name, and Shiny keeps this as the name on the client side.
This allows you to set the name of the Page Action in New Relic. You should use the detail of the operation the input triggers. The event listener also triggers checkShiny() again after 1 second. If you have any new values in the page, these will be fetched as a result of that. So if you need to capture variables that are present after an input is triggered, you can add them within the checkShiny() function.
FYI
If you are uploading to say, shiny apps.io then the reference to the contents in www/ must be appended with the name of the application. For instance, uploading my project to gspncr.shinyapps.io/newR, any files I am referencing in www/ must be referenced such as: /newR/newrelic.js
Send log messages through nrLogger() function. If you pass the ID returned when you call tracer() or errorTracer() then the Log message will be linked to the trace. If you are sending a Log message without an associated trace, then a new trace ID will be generated for you. You can later link these to new traces.
All arguments apart from message can be automatically created. Even then, a placeholder is passed for message but there is not much point calling this function if you have no message to send.
serviceNamepass the name of the service. This will default to Custom R Service. You can also override this globally by setting the variable RServiceName in the distributed trace source file.hostNamepass this to override the name of the host. This will automatically be set with the nodename of the host running the R script.traceIdpass this to reference an existing trace. This will automatically create a new UUID if you do not pass a traceId - returned bytrace()orerrorTrace()functions.timestamppass this in EPOCH MS time. This will automatically be set to the current EPOCH time, according to the timezone of the running node.messagepass this to include the log message. This will be set with placeholder text if not set.
The script will write to debug.log and info.log. These can be monitored using New Relic Logs for complete visibility. The outputs into those log files, are particularly for tracing. You can however use New Relic Logs to of course monitor any other Log files you desire.
You can use the function newRMetric() to send timeseries dimensional metrics to New Relic. There is a new series of arguments that are supported for metric data:
metricNamethe name of the metric. This will default to Custom R MetricmetricValuethe integer or double value of the metric. This will default to 0hostNamepass this to override the name of the host. This will automatically be set with the nodename of the host running the R script.serviceNamepass the name of the service. This will default to Custom R Service. You can also override this globally by setting the variable RServiceName in the distributed trace source file.timestamppass this in EPOCH MS time. This will automatically be set to the current EPOCH time, according to the timezone of the running node.
Metrics in the demo app
In the demo app, I have added plot_the_dots by NHS-R Community. This is used for the example of sending Metrics into New Relic. For the demonstration purpose, the demo app takes the values to be plot (out of the Values column in the example spreadsheet) and they are sent into New Relic. This is not really a use case itself for Metrics one might typically come across, but it is nice enough for us to demonstrate.
You can use the function newREvent() to send custom Events to New Relic. Note, this function is designed with the demo app in mind - you should modify this to cater for your own use case. There is a series of arguments that are supported:
eventTypethe name of the Event as should appear in New Relic. You will find this in the data explorer view. This will default to RCustomEventRTestNamethis is specific to the demo apps use case. This defaults to Plot the Dots ExampleRTestResultthis is specific to the demo apps use case. This defaults to SuccessRTestTimeTakenthis is specific to the demo apps use case. This defaults to a random integer.hostNameset this to override. This is automatically detected as the nodes name running the R script.sessionIDset this using session$token per other areas of the library. Otherwise a UUID will be generated.serviceNamethis is the name of the service to appear. This is automatically set with the default of the source file, Custom R Service