Skip to content

Commit

Permalink
feat: send id only when storage is accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
Joozty committed Nov 4, 2024
1 parent 35fe31c commit 861bed8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 441 in packages/web/src/index.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Missing semicolon
}

const syntheticsRunId = getSyntheticsRunId();
if (syntheticsRunId) {
resourceAttrs[SYNTHETICS_RUN_ID_ATTRIBUTE] = syntheticsRunId;
Expand Down
17 changes: 12 additions & 5 deletions packages/web/src/services/BrowserInstanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Check failure on line 53 in packages/web/src/services/BrowserInstanceService.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Expected indentation of 6 spaces but found 8
this._id = undefined;

Check failure on line 54 in packages/web/src/services/BrowserInstanceService.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Expected indentation of 6 spaces but found 8
}

this._id = browserInstanceId;

return this._id;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/web/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 21 in packages/web/src/utils/storage.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Missing semicolon
// 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 => {
Expand Down

0 comments on commit 861bed8

Please sign in to comment.