Skip to content

Commit

Permalink
Use SplunkRumNative.getNativeSessionId when present (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbley authored Oct 1, 2021
1 parent f7fc9bd commit d12ddba
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions integration-tests/tests/native/native.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Native</title>
<script>
window.SplunkRumNative = {
getNativeSessionId: function() {
return '12341234123412341234123412341234';
}
};
</script>

<%- renderAgent({debug: true}) %>

<style>
#container {
height: 200px;
width: 400px;
}
</style>
</head>
<body>
<h1>Native</h1>
</body>
</html>
29 changes: 29 additions & 0 deletions integration-tests/tests/native/native.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2020 Splunk Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

module.exports = {
'native session id integration': async function(browser) {
const url = browser.globals.getUrl('/native/native.ejs');
await browser.url(url);

const anySpan = await browser.globals.findSpan(span => span.tags['splunk.rumSessionId'] !== undefined);

await browser.assert.ok(anySpan);

await browser.assert.strictEqual(anySpan.tags['splunk.rumSessionId'], '12341234123412341234123412341234');
await browser.globals.assertNoErrorSpans();
}
};
18 changes: 18 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { findCookieValue, generateId, isIframe } from './utils';
Future work can add more fields though note that the fact that the value doesn't change
once created makes this very robust when used in multiple tabs/windows - tabs don't compete/
race to do anything but set the max-age.
Finally, if SplunkRumNative exists, use its session ID exclusively and don't bother
with setting cookies, checking for inactivity, etc.
*/


Expand Down Expand Up @@ -127,11 +130,23 @@ export function updateSessionStatus(): void {
recentActivity = false;
}

function hasNativeSessionId(): boolean {
return window['SplunkRumNative'] && window['SplunkRumNative'].getNativeSessionId;
}

export function initSessionTracking(
instanceId: SessionIdType,
newEventTarget: InternalEventTarget,
domain?: string,
): { deinit: () => void; } {
if (hasNativeSessionId()) {
// short-circuit and bail out - don't create cookie, watch for inactivity, or anything
return {
deinit: () => {
rumSessionId = undefined;
}
};
}
if (domain) {
cookieDomain = domain;
}
Expand Down Expand Up @@ -160,5 +175,8 @@ export function initSessionTracking(
}

export function getRumSessionId(): SessionIdType {
if (hasNativeSessionId()) {
return window['SplunkRumNative'].getNativeSessionId();
}
return rumSessionId;
}

0 comments on commit d12ddba

Please sign in to comment.