Skip to content

Commit 861bed8

Browse files
committed
feat: send id only when storage is accessible
1 parent 35fe31c commit 861bed8

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

packages/web/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,13 @@ export const SplunkRum: SplunkOtelWebType = {
434434
// Splunk specific attributes
435435
'splunk.rumVersion': VERSION,
436436
'splunk.scriptInstance': instanceId,
437-
'browser.instance.id': BrowserInstanceService.id,
438437
'app': applicationName,
439438
};
440439

440+
if(BrowserInstanceService.id) {
441+
resourceAttrs['browser.instance.id'] = BrowserInstanceService.id
442+
}
443+
441444
const syntheticsRunId = getSyntheticsRunId();
442445
if (syntheticsRunId) {
443446
resourceAttrs[SYNTHETICS_RUN_ID_ATTRIBUTE] = syntheticsRunId;

packages/web/src/services/BrowserInstanceService.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,29 @@ const BROWSER_INSTANCE_ID_KEY = 'browser_instance_id';
3131
* This is not implemented yet as requires bigger refactoring.
3232
*/
3333
export class BrowserInstanceService {
34-
static _id: string | undefined = undefined;
34+
// `undefined` represents the case when the storage is inaccessible.
35+
static _id: string | undefined | null = null;
3536

36-
static get id(): string {
37-
if(this._id) {
37+
static get id(): string | undefined {
38+
if(this._id !== null) {
3839
return this._id;
3940
}
4041

4142

4243
// Check if the ID is already stored in the session storage. It might be generated by another frame/context.
4344
let browserInstanceId = safelyGetSessionStorage(BROWSER_INSTANCE_ID_KEY);
44-
if(!browserInstanceId) {
45+
if(browserInstanceId) {
46+
this._id = browserInstanceId;
47+
} else if(browserInstanceId === null) {
48+
// Storage is accessible but the ID is not stored yet.
4549
browserInstanceId = generateId(64);
50+
this._id = browserInstanceId;
4651
safelySetSessionStorage(BROWSER_INSTANCE_ID_KEY, browserInstanceId);
52+
} else {
53+
// Storage is not accessible.
54+
this._id = undefined;
4755
}
4856

49-
this._id = browserInstanceId;
5057

5158
return this._id;
5259
}

packages/web/src/utils/storage.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
export const safelyGetSessionStorage = (key: string): string | null => {
18-
let value = null;
17+
export const safelyGetSessionStorage = (key: string): string | null | undefined => {
1918
try {
20-
value = window.sessionStorage.getItem(key);
19+
return window.sessionStorage.getItem(key);
2120
} catch {
21+
return undefined
2222
// sessionStorage not accessible probably user is in incognito-mode
2323
// or set "Block third-party cookies" option in browser settings
2424
}
25-
return value;
2625
};
2726

2827
export const safelySetSessionStorage = (key: string, value: string): boolean => {

0 commit comments

Comments
 (0)