Browser monitoring supports the uploading of source maps, which are used to un-minify error stack traces on the JS errors page. This document explains how to use the API to publish (upload) source maps to browser.
Prepare for using the source map API
In order to upload source maps to browser via the API, you'll need this information:
Optionally, if the JavaScript URL doesn't automatically have release info appended to it, the release name and ID
Every time the agent captures an error in your code, it's associated with the URL of the JavaScript in which it occurred. This is the src attribute of the script tag in your HTML. This full JavaScript URL is required when sending source maps to browser.
You can find the URL for an error's JavaScript file in browser, on the JS errors page. See Browser monitoring source maps for more on finding these errors in the UI.
Many organizations include a version number or hash in the JavaScript URL. This is generally added to "bust" caches to ensure your users get the most recent version of your code. This type of URL might look something like:
If your app's URLs automatically have the version info appended to it, the browser agent has everything it needs in order to match errors with your code. You can move ahead to generating source maps.
If this doesn't apply to you, and JS URLs do not have version info appended, you’ll have to assist the agent by specifying a release name and ID with the API.
If you're interested in connecting New Relic to your CodeStream organization when this feature is available, your repository URL or build commit hash are required. Otherwise, this is optional.
If you're interested in learning more about this, you can read about how CodeStream and New Relic work together to make it easier to identify errors and collaborate on fixing them.
There is no limit to the overall number of source maps you can upload. However, the API is rate-limited per account:
You can upload a maximum of 1000 source maps per minute.
You can upload a maximum of 15,000 source maps per day.
Only one source map can be uploaded or published per API request.
Source map files can be a maximum of 50Mb in size.
Push source maps to New Relic
Now that you have one or more source maps, you are ready to publish it to browser. You can use any of these methods to send source maps to browser:
Use the New Relic npm module with the API via the command line or via a client-side JavaScript build/deploy script like Gulp or Grunt.
Use npm module via command line or client-side script
The easiest and recommended way to upload source maps to browser is to use the our new @newrelic/publish-sourcemap npm module. It provides a command line tool and Javascript API to accomplish this task. More documentation is available in the npm repo.
Here are some examples of using the npm module via the command line.
Important
The following examples are for US accounts. For EU accounts, the endpoint is https://sourcemaps.service.eu.newrelic.com. For more information, see Introduction to the EU region data center.
Here's an example of uploading source maps using the npm module via the command line. Note that the source map can come from a local file or a remote URL.
Here's an example of listing published source maps:
list-sourcemaps --applicationId=YOUR_APP_ID --apiKey=YOUR_NEW_RELIC_USER_KEY
Options:
--applicationId Browser application id
--apiKey New Relic user API key
Here's an example of deleting a source map:
delete-sourcemap --applicationId=YOUR_APP_ID --apiKey=YOUR_NEW_RELIC_USER_API_KEY --sourcemapId=YOUR_SOURCE_MAP_ID
Options:
--applicationId Browser application id
--apiKey New Relic user API key
--sourcemapId Unique id generated for a source map
Here are some examples of using the npm module to publish from client-side JavaScript:
Here's an example of publishing a source map via a Node.js script:
Here's an example of listing all published source maps:
var listSourcemaps = require(‘@newrelic/publish-sourcemap’).listSourcemaps
listSourcemaps({
applicationId: YOUR_NEW_RELIC_APP_ID,
apiKey: 'YOUR_NEW_RELIC_USER_API_KEY',
limit: [Max number of results to return || 20]: ,
offset: [Number of results to skip before returning || 0]: ,
}, function (err, res) { console.log(err || res.sourcemaps)})
Here's an example of deleting a source map file via a Node.js script:
var deleteSourcemap = require(‘@newrelic/publish-sourcemap’).deleteSourcemap
deleteSourcemap({
sourcemapId: 'SOURCE_MAP_ID',
applicationId: YOUR_NEW_RELIC_APP_ID,
apiKey: 'YOUR_NEW_RELIC_USER_API_KEY',
}, function (err) { console.log(err || 'Deleted source map')})
When you're done, go to the JS errors page in browser, select an error grouping, and see if your error stack traces have been un-minified.
Use API via curl
Below are some examples of using curl to publish, list, and delete source maps:
An example of using API via curl to publish maps to browser:
Below is an example of how to get a list of source maps previously uploaded to New Relic via curl. New Relic returns the source map's unique SOURCEMAP_ID and its components:
When you're done, go to the JS errors page in browser, select an error grouping, and see if your error stack traces have been un-minified.
Troubleshoot source maps
If you are having trouble generating source maps from your build system, or if your errors in browser are remaining minified, see the source maps troubleshooting documentation.