Skip to content

Commit d0251e9

Browse files
committed
Send along the session duration as extra
1 parent 9e8ff97 commit d0251e9

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/raven.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var _Raven = window.Raven,
2525
authQueryString,
2626
isRavenInstalled = false,
2727

28-
objectPrototype = Object.prototype;
28+
objectPrototype = Object.prototype,
29+
startTime = now();
2930

3031
/*
3132
* The core Raven singleton
@@ -623,6 +624,10 @@ function truncate(str, max) {
623624
return str.length <= max ? str : str.substr(0, max) + '\u2026';
624625
}
625626

627+
function now() {
628+
return +new Date();
629+
}
630+
626631
function getHttpData() {
627632
var http = {
628633
url: document.location.href,
@@ -654,9 +659,13 @@ function send(data) {
654659
data.tags = objectMerge(globalOptions.tags, data.tags);
655660
data.extra = objectMerge(globalOptions.extra, data.extra);
656661

662+
// Send along our own collected metadata with extra
663+
data.extra = objectMerge({
664+
'session:duration': now() - startTime,
665+
}, data.extra);
666+
657667
// If there are no tags/extra, strip the key from the payload alltogther.
658668
if (isEmptyObject(data.tags)) delete data.tags;
659-
if (isEmptyObject(data.extra)) delete data.extra;
660669

661670
if (globalUser) {
662671
// sentry.interfaces.User

test/raven.test.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ function flushRavenState() {
1616
maxMessageLength: 100,
1717
tags: {},
1818
extra: {}
19-
};
19+
},
20+
startTime = 0
21+
;
22+
2023
Raven.uninstall();
2124
}
2225

@@ -41,6 +44,11 @@ function uuid4() {
4144
return 'abc123';
4245
}
4346

47+
// patched to be predictable
48+
function now() {
49+
return 100;
50+
}
51+
4452
describe('TraceKit', function(){
4553
describe('error notifications', function(){
4654
var testMessage = "__mocha_ignore__";
@@ -815,7 +823,8 @@ describe('globals', function() {
815823
}
816824
},
817825
event_id: 'abc123',
818-
foo: 'bar'
826+
foo: 'bar',
827+
extra: {'session:duration': 100}
819828
});
820829
});
821830

@@ -851,7 +860,8 @@ describe('globals', function() {
851860
user: {
852861
name: 'Matt'
853862
},
854-
foo: 'bar'
863+
foo: 'bar',
864+
extra: {'session:duration': 100}
855865
}]);
856866
});
857867

@@ -884,7 +894,8 @@ describe('globals', function() {
884894
}
885895
},
886896
event_id: 'abc123',
887-
tags: {tag1: 'value1', tag2: 'value2'}
897+
tags: {tag1: 'value1', tag2: 'value2'},
898+
extra: {'session:duration': 100}
888899
}]);
889900
});
890901

@@ -917,7 +928,7 @@ describe('globals', function() {
917928
}
918929
},
919930
event_id: 'abc123',
920-
extra: {key1: 'value1', key2: 'value2'}
931+
extra: {key1: 'value1', key2: 'value2', 'session:duration': 100}
921932
}]);
922933
});
923934

@@ -943,7 +954,7 @@ describe('globals', function() {
943954
}]);
944955
});
945956

946-
it('should strip empty tags/extra', function() {
957+
it('should strip empty tags', function() {
947958
this.sinon.stub(window, 'isSetup').returns(true);
948959
this.sinon.stub(window, 'makeRequest');
949960
this.sinon.stub(window, 'getHttpData').returns({
@@ -955,8 +966,7 @@ describe('globals', function() {
955966
projectId: 2,
956967
logger: 'javascript',
957968
site: 'THE BEST',
958-
tags: {},
959-
extra: {}
969+
tags: {}
960970
};
961971

962972
send({foo: 'bar', tags: {}, extra: {}});
@@ -972,7 +982,8 @@ describe('globals', function() {
972982
}
973983
},
974984
event_id: 'abc123',
975-
foo: 'bar'
985+
foo: 'bar',
986+
extra: {'session:duration': 100}
976987
});
977988
});
978989
});

0 commit comments

Comments
 (0)