Skip to content

Commit

Permalink
MWPW-159555 Add support for lana-sample query param (#3045)
Browse files Browse the repository at this point in the history
* MWPW-159555 Add support for lana-sample query param

The `lana-sample` query param will override any sample rate defined elsewhere.  Setting it to "100" will force lana to send everything to splunk.

* Disable es-lint on lana.js

* Use explicit eslint labels

* Fix lana test to not intermittently fail

* Add invalid value test
  • Loading branch information
chrischrischris authored Oct 28, 2024
1 parent febe065 commit 56ed952
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libs/utils/lana.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(function () {
/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
(function iife() {
const MSG_LIMIT = 2000;

const defaultOptions = {
Expand Down Expand Up @@ -49,10 +51,6 @@
}, {});
}

function sendUnhandledError(e) {
log(e.reason || e.error || e.message, { errorType: 'i' });
}

function log(msg, options) {
msg = msg && msg.stack ? msg.stack : (msg || '');
if (msg.length > MSG_LIMIT) {
Expand All @@ -65,7 +63,8 @@
return;
}

const sampleRate = o.errorType === 'i' ? o.implicitSampleRate : o.sampleRate;
const sampleRateParam = parseInt(new URL(window.location).searchParams.get('lana-sample'), 10);
const sampleRate = sampleRateParam || (o.errorType === 'i' ? o.implicitSampleRate : o.sampleRate);

if (!w.lana.debug && !w.lana.localhost && sampleRate <= Math.random() * 100) return;

Expand Down Expand Up @@ -95,10 +94,15 @@
}
xhr.open('GET', `${endpoint}?${queryParams.join('&')}`);
xhr.send();
// eslint-disable-next-line consistent-return
return xhr;
}
}

function sendUnhandledError(e) {
log(e.reason || e.error || e.message, { errorType: 'i' });
}

function hasDebugParam() {
return w.location.search.toLowerCase().indexOf('lanadebug') !== -1;
}
Expand Down
3 changes: 3 additions & 0 deletions libs/utils/lana.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Note that any errors that happen on a `*.corp.adobe.com` domain will automatical
* same as `sampleRate`
* `useProd` - bool: Defaults to `true`
* Toggle between prod and stage endpoints

## Helper Functions

The helper functions have been deprecated in favor of the options being passed with every lana call.
Expand All @@ -52,6 +53,8 @@ Note that the default option state is stored in `window.lana.options` and can be
Debugging mode can be enabled by setting a `lanaDebug` query param. This sends lana messages with debug enabled and will console.log the responses.
Alternatively, the `window.lana.debug` property can be set (the query param also sets this property).

The Lana logging sample rate can be overridden with the `lana-sample` query param. It should be set as an integer between 0-100. This will set the sample rate for all lana messages regardless of other sample rates set.

## localhost

When on a url that contains "localhost", logged messages will not be sent to the server and will be displayed in the console. However if debug is enabled, then messages will be sent.
Expand Down
51 changes: 51 additions & 0 deletions test/utils/lana.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,55 @@ describe('LANA', () => {
'https://www.stage.adobe.com/lana/ll?m=only%20the%20clientId%20set%20in%20window.lana.options&c=blah&s=100&t=e',
);
});

it('The lana-sample query param overrides existing sampleRate', () => {
window.lana.options = {
clientId: 'blah',
sampleRate: 0,
implicitSampleRate: 0,
};
const originalUrl = window.location.href;

window.lana.log('Sample rate is zero so should not be logged');
expect(xhrRequests.length).to.equal(0);

// create a new url based on the current url that adds a query param for lana-sample
const newUrl = new URL(window.location.href);
newUrl.searchParams.set('lana-sample', '100');

window.history.pushState({ path: newUrl.toString() }, '', newUrl.toString());

window.lana.log('lana-sample query param');
expect(xhrRequests.length).to.equal(1);
expect(xhrRequests[0].method).to.equal('GET');
expect(xhrRequests[0].url).to.equal(
'https://www.stage.adobe.com/lana/ll?m=lana-sample%20query%20param&c=blah&s=100&t=e',
);

window.history.pushState({ path: originalUrl }, '', originalUrl);
});

it('Invalid lana-sample query params are ignored', () => {
window.lana.options = {
clientId: 'blah',
sampleRate: 100,
implicitSampleRate: 100,
};
const originalUrl = window.location.href;

// create a new url based on the current url that adds a query param for lana-sample
const newUrl = new URL(window.location.href);
newUrl.searchParams.set('lana-sample', 'notvalid');

window.history.pushState({ path: newUrl.toString() }, '', newUrl.toString());

window.lana.log('lana-sample query param');
expect(xhrRequests.length).to.equal(1);
expect(xhrRequests[0].method).to.equal('GET');
expect(xhrRequests[0].url).to.equal(
'https://www.stage.adobe.com/lana/ll?m=lana-sample%20query%20param&c=blah&s=100&t=e',
);

window.history.pushState({ path: originalUrl }, '', originalUrl);
});
});

0 comments on commit 56ed952

Please sign in to comment.