Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions packages/core/src/js/scopeSync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Breadcrumb, Scope } from '@sentry/core';
import { debug } from '@sentry/core';
import { logger } from '@sentry/react';
import { DEFAULT_BREADCRUMB_LEVEL } from './breadcrumb';
import { fillTyped } from './utils/fill';
Expand Down Expand Up @@ -81,26 +82,30 @@ export function enableSyncToNative(scope: Scope): void {
});

fillTyped(scope, 'setAttribute', original => (key: string, value: unknown): Scope => {
debug.warn('This feature is currently not supported.');
// Only sync primitive types
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
NATIVE.setAttribute(key, value);
}
// Native layer still not supported
// if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
// NATIVE.setAttribute(key, value);
// }
return original.call(scope, key, value);
});

fillTyped(scope, 'setAttributes', original => (attributes: Record<string, unknown>): Scope => {
// Native layer not supported
debug.warn('This feature is currently not supported.');
// Filter to only primitive types
const primitiveAttrs: Record<string, string | number | boolean> = {};
Object.keys(attributes).forEach(key => {
const value = attributes[key];
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
primitiveAttrs[key] = value;
}
});

if (Object.keys(primitiveAttrs).length > 0) {
NATIVE.setAttributes(primitiveAttrs);
}
// const primitiveAttrs: Record<string, string | number | boolean> = {};
// Object.keys(attributes).forEach(key => {
// const value = attributes[key];
// if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
// primitiveAttrs[key] = value;
// }
// });
//
// if (Object.keys(primitiveAttrs).length > 0) {
// NATIVE.setAttributes(primitiveAttrs);
// }
return original.call(scope, attributes);
});
}
6 changes: 6 additions & 0 deletions packages/core/test/scopeSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ describe('ScopeSync', () => {
let setExtrasScopeSpy: jest.SpyInstance;
let addBreadcrumbScopeSpy: jest.SpyInstance;
let setContextScopeSpy: jest.SpyInstance;
/*
TODO: Uncomment once Native setattribute is implemented.
let setAttributeScopeSpy: jest.SpyInstance;
let setAttributesScopeSpy: jest.SpyInstance;
*/

beforeAll(() => {
const testScope = SentryCore.getIsolationScope();
Expand Down Expand Up @@ -219,6 +222,8 @@ describe('ScopeSync', () => {
expect(setContextScopeSpy).toHaveBeenCalledExactlyOnceWith('key', { key: 'value' });
});

/*
TODO: uncomment tests once native implementation is done.
it('setAttribute', () => {
expect(SentryCore.getIsolationScope().setAttribute).not.toBe(setAttributeScopeSpy);

Expand Down Expand Up @@ -283,5 +288,6 @@ describe('ScopeSync', () => {
});
expect(NATIVE.setAttributes).not.toHaveBeenCalled();
});
*/
});
});
Loading