diff --git a/packages/web/src/index.ts b/packages/web/src/index.ts index c3edf503..d46abff1 100644 --- a/packages/web/src/index.ts +++ b/packages/web/src/index.ts @@ -434,10 +434,13 @@ export const SplunkRum: SplunkOtelWebType = { // Splunk specific attributes 'splunk.rumVersion': VERSION, 'splunk.scriptInstance': instanceId, - 'browser.instance.id': BrowserInstanceService.id, 'app': applicationName, }; + if(BrowserInstanceService.id) { + resourceAttrs['browser.instance.id'] = BrowserInstanceService.id + } + const syntheticsRunId = getSyntheticsRunId(); if (syntheticsRunId) { resourceAttrs[SYNTHETICS_RUN_ID_ATTRIBUTE] = syntheticsRunId; diff --git a/packages/web/src/services/BrowserInstanceService.ts b/packages/web/src/services/BrowserInstanceService.ts index 0329b6c0..97f50122 100644 --- a/packages/web/src/services/BrowserInstanceService.ts +++ b/packages/web/src/services/BrowserInstanceService.ts @@ -31,22 +31,29 @@ const BROWSER_INSTANCE_ID_KEY = 'browser_instance_id'; * This is not implemented yet as requires bigger refactoring. */ export class BrowserInstanceService { - static _id: string | undefined = undefined; + // `undefined` represents the case when the storage is inaccessible. + static _id: string | undefined | null = null; - static get id(): string { - if(this._id) { + static get id(): string | undefined { + if(this._id !== null) { return this._id; } // Check if the ID is already stored in the session storage. It might be generated by another frame/context. let browserInstanceId = safelyGetSessionStorage(BROWSER_INSTANCE_ID_KEY); - if(!browserInstanceId) { + if(browserInstanceId) { + this._id = browserInstanceId; + } else if(browserInstanceId === null) { + // Storage is accessible but the ID is not stored yet. browserInstanceId = generateId(64); + this._id = browserInstanceId; safelySetSessionStorage(BROWSER_INSTANCE_ID_KEY, browserInstanceId); + } else { + // Storage is not accessible. + this._id = undefined; } - this._id = browserInstanceId; return this._id; } diff --git a/packages/web/src/utils/storage.ts b/packages/web/src/utils/storage.ts index 53914e73..f996ae0b 100644 --- a/packages/web/src/utils/storage.ts +++ b/packages/web/src/utils/storage.ts @@ -14,15 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -export const safelyGetSessionStorage = (key: string): string | null => { - let value = null; +export const safelyGetSessionStorage = (key: string): string | null | undefined => { try { - value = window.sessionStorage.getItem(key); + return window.sessionStorage.getItem(key); } catch { + return undefined // sessionStorage not accessible probably user is in incognito-mode // or set "Block third-party cookies" option in browser settings } - return value; }; export const safelySetSessionStorage = (key: string, value: string): boolean => {