Skip to content

Record fetch requests in breadcrumb #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"bundle-collapser": "^1.2.1",
"chai": "2.3.0",
"derequire": "2.0.3",
"es6-promise": "^4.0.5",
"grunt": "^0.4.5",
"grunt-browserify": "^4.0.1",
"grunt-cli": "^0.1.13",
Expand All @@ -43,7 +44,8 @@
"proxyquireify": "^3.0.2",
"sinon": "1.7.3",
"through2": "^2.0.0",
"typescript": "^1.8.10"
"typescript": "^1.8.10",
"whatwg-fetch": "^1.0.0"
},
"keywords": [
"exceptions",
Expand Down
39 changes: 38 additions & 1 deletion src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Raven.prototype = {
xhr: true,
console: true,
dom: true,
location: true
location: true,
};

var autoBreadcrumbs = globalOptions.autoBreadcrumbs;
Expand Down Expand Up @@ -971,6 +971,43 @@ Raven.prototype = {
}, wrappedBuiltIns);
}

if (autoBreadcrumbs.xhr && 'fetch' in _window) {
fill(_window, 'fetch', function(origFetch) {
return function (fn, t) { // preserve arity
// Make a copy of the arguments to prevent deoptimization
// https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}

var method = 'GET';

if (args[1] && args[1].method) {
method = args[1].method;
}

var fetchData = {
method: method,
url: args[0],
status_code: null
};

self.captureBreadcrumb({
type: 'http',
category: 'fetch',
data: fetchData
});

return origFetch.apply(this, args).then(function (response) {
fetchData.status_code = response.status;

return response;
});
};
}, wrappedBuiltIns);
}

// Capture breadcrumbs from any click that is unhandled / bubbled up all the way
// to the document. Do this before we instrument addEventListener.
if (autoBreadcrumbs.dom && this._hasDocument) {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<head>
<meta charset="utf-8">
<title></title>
<script src="../../node_modules/es6-promise/dist/es6-promise.auto.js"></script>
<script src="../../node_modules/whatwg-fetch/fetch.js"></script>
<script>
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
Expand Down
41 changes: 41 additions & 0 deletions test/integration/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ describe('integration', function () {
assert.equal(breadcrumbs.length, 1);

assert.equal(breadcrumbs[0].type, 'http');
assert.equal(breadcrumbs[0].category, 'xhr');
assert.equal(breadcrumbs[0].data.method, 'GET');
// NOTE: not checking status code because we seem to get
// statusCode 0/undefined from Phantom when fetching
Expand Down Expand Up @@ -441,6 +442,46 @@ describe('integration', function () {
);
});

it('should record a fetch request', function (done) {
var iframe = this.iframe;

iframeExecute(iframe, done,
function () {
// some browsers trigger onpopstate for load / reset breadcrumb state
Raven._breadcrumbs = [];

fetch('/test/integration/example.json').then(function () {
setTimeout(done);
}, function () {
setTimeout(done);
});
},
function () {
var Raven = iframe.contentWindow.Raven,
breadcrumbs = Raven._breadcrumbs;

if ('fetch' in window) {
assert.equal(breadcrumbs.length, 1);

assert.equal(breadcrumbs[0].type, 'http');
assert.equal(breadcrumbs[0].category, 'fetch');
assert.equal(breadcrumbs[0].data.method, 'GET');
} else {
// otherwise we use a fetch polyfill based on xhr
assert.equal(breadcrumbs.length, 2);

assert.equal(breadcrumbs[0].type, 'http');
assert.equal(breadcrumbs[0].category, 'fetch');
assert.equal(breadcrumbs[0].data.method, 'GET');

assert.equal(breadcrumbs[1].type, 'http');
assert.equal(breadcrumbs[1].category, 'xhr');
assert.equal(breadcrumbs[1].data.method, 'GET');
}
}
);
});

it('should record a mouse click on element WITH click handler present', function (done) {
var iframe = this.iframe;

Expand Down