Skip to content

Commit

Permalink
feat: Support URL object in fetch / XHR telemetry (#1118)
Browse files Browse the repository at this point in the history
* feat: Support `URL` type in `XHR.open` telemetry

* feat: Support `URL` object in `fetch` telemetry

* test: XHR test

* test: Fetch telemetry tests
  • Loading branch information
dj-stormtrooper authored Aug 16, 2023
1 parent 6f83da1 commit 848d5f0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/browser/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ Instrumenter.prototype.instrumentNetwork = function() {
var xhrp = this._window.XMLHttpRequest.prototype;
replace(xhrp, 'open', function(orig) {
return function(method, url) {
if (_.isType(url, 'string')) {
var isUrlObject = _isUrlObject(url)
if (_.isType(url, 'string') || isUrlObject) {
url = isUrlObject ? url.toString() : url;
if (this.__rollbar_xhr) {
this.__rollbar_xhr.method = method;
this.__rollbar_xhr.url = url;
Expand Down Expand Up @@ -342,8 +344,9 @@ Instrumenter.prototype.instrumentNetwork = function() {
var input = args[0];
var method = 'GET';
var url;
if (_.isType(input, 'string')) {
url = input;
var isUrlObject = _isUrlObject(input)
if (_.isType(input, 'string') || isUrlObject) {
url = isUrlObject ? input.toString() : input;
} else if (input) {
url = input.url;
if (input.method) {
Expand Down Expand Up @@ -770,4 +773,8 @@ Instrumenter.prototype.removeListeners = function(section) {
}
};

function _isUrlObject(input) {
return typeof URL !== 'undefined' && input instanceof URL
}

module.exports = Instrumenter;
69 changes: 69 additions & 0 deletions test/browser.telemetry.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* globals expect */
/* globals describe */
/* globals it */
/* globals sinon */

var Instrumenter = require('../src/browser/telemetry');

describe('instrumentNetwork', function () {
it('should capture XHR requests with string URL', function (done) {
var callback = sinon.spy();
var windowMock = {
XMLHttpRequest: function () { }
}

windowMock.XMLHttpRequest.prototype.open = function () { }
windowMock.XMLHttpRequest.prototype.send = function () { }

var i = createInstrumenter(callback, windowMock)
i.instrumentNetwork()

var xhr = new windowMock.XMLHttpRequest();
xhr.open('GET', 'http://first.call')
xhr.send()
xhr.onreadystatechange()

expect(callback.callCount).to.eql(1)
expect(callback.args[0][0].url).to.eql('http://first.call')

i.deinstrumentNetwork()
i = createInstrumenter(callback, windowMock)
i.instrumentNetwork()
var xhr = new windowMock.XMLHttpRequest();
xhr.open('GET', new URL('http://second.call'))
xhr.send()
xhr.onreadystatechange()
expect(callback.callCount).to.eql(2)
expect(callback.args[1][0].url).to.eql('http://second.call/')

done()
})

it('should capture XHR requests with string URL', function (done) {
var callback = sinon.spy();
var windowMock = {
fetch: function () { return Promise.resolve() }
}

var i = createInstrumenter(callback, windowMock);
i.instrumentNetwork()

windowMock.fetch('http://first.call')
expect(callback.callCount).to.eql(1)
expect(callback.args[0][0].url).to.eql('http://first.call')

i.deinstrumentNetwork()
i = createInstrumenter(callback, windowMock)
i.instrumentNetwork()

windowMock.fetch(new URL('http://second.call'))
expect(callback.callCount).to.eql(2)
expect(callback.args[1][0].url).to.eql('http://second.call/')

done()
})
})

function createInstrumenter(callback, windowMock) {
return new Instrumenter({ scrubFields: [] }, { captureNetwork: callback }, { wrap: function () { }, client: { notifier: { diagnostic: {} } } }, windowMock);
}

0 comments on commit 848d5f0

Please sign in to comment.