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
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Sentry.getGlobalScope().setAttribute('array_attr', [1, 2, 3]);
// global scope, log attribute
Sentry.logger.info('log_after_global_scope', { log_attr: 'log_attr_2' });

Sentry.withIsolationScope(isolationScope => {
isolationScope.setAttribute('isolation_scope_1_attr', { value: 100, unit: 'millisecond' });
Sentry.withIsolationScope(() => {
Sentry.setAttribute('isolation_scope_1_attr', { value: 100, unit: 'millisecond' });

// global scope, isolation scope, log attribute
Sentry.logger.info('log_with_isolation_scope', { log_attr: 'log_attr_3' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ Sentry.startSpan({ name: 'test-span', op: 'test' }, () => {
Sentry.setUser({ id: 'user-123', email: 'test@example.com', username: 'testuser' });
Sentry.metrics.count('test.user.counter', 1, { attributes: { action: 'click' } });

Sentry.withScope(scope => {
scope.setAttribute('scope_attribute_1', 1);
scope.setAttributes({ scope_attribute_2: { value: 'test' }, scope_attribute_3: { value: 38, unit: 'gigabyte' } });
Sentry.metrics.count('test.scope.attributes.counter', 1, { attributes: { action: 'click' } });
});
Sentry.setAttribute('scope_attribute_1', 1);
Sentry.setAttributes({ scope_attribute_2: { value: 'test' }, scope_attribute_3: { value: 38, unit: 'gigabyte' } });
Sentry.metrics.count('test.scope.attributes.counter', 1, { attributes: { action: 'click' } });

Sentry.flush();
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ async function run(): Promise<void> {
Sentry.setUser({ id: 'user-123', email: 'test@example.com', username: 'testuser' });
Sentry.metrics.count('test.user.counter', 1, { attributes: { action: 'click' } });

Sentry.withScope(scope => {
scope.setAttribute('scope_attribute_1', 1);
scope.setAttributes({ scope_attribute_2: { value: 'test' }, scope_attribute_3: { value: 38, unit: 'gigabyte' } });
Sentry.metrics.count('test.scope.attributes.counter', 1, { attributes: { action: 'click' } });
});
Sentry.setAttribute('scope_attribute_1', 1);
Sentry.setAttributes({ scope_attribute_2: { value: 'test' }, scope_attribute_3: { value: 38, unit: 'gigabyte' } });
Sentry.metrics.count('test.scope.attributes.counter', 1, { attributes: { action: 'click' } });

await Sentry.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function run(): Promise<void> {
// global scope, log attribute
Sentry.logger.info('log_after_global_scope', { log_attr: 'log_attr_2' }, {});

Sentry.withIsolationScope(isolationScope => {
isolationScope.setAttribute('isolation_scope_1_attr', { value: 100, unit: 'millisecond' });
Sentry.withIsolationScope(() => {
Sentry.setAttribute('isolation_scope_1_attr', { value: 100, unit: 'millisecond' });

// global scope, isolation scope, log attribute
Sentry.logger.info('log_with_isolation_scope', { log_attr: 'log_attr_3' }, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ async function run(): Promise<void> {
Sentry.setUser({ id: 'user-123', email: 'test@example.com', username: 'testuser' });
Sentry.metrics.count('test.user.counter', 1, { attributes: { action: 'click' } });

Sentry.withScope(scope => {
scope.setAttribute('scope_attribute_1', 1);
scope.setAttributes({ scope_attribute_2: { value: 'test' }, scope_attribute_3: { value: 38, unit: 'gigabyte' } });
Sentry.metrics.count('test.scope.attributes.counter', 1, { attributes: { action: 'click' } });
});
Sentry.setAttribute('scope_attribute_1', 1);
Sentry.setAttributes({ scope_attribute_2: { value: 'test' }, scope_attribute_3: { value: 38, unit: 'gigabyte' } });
Sentry.metrics.count('test.scope.attributes.counter', 1, { attributes: { action: 'click' } });

await Sentry.flush();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export {
setMeasurement,
setTag,
setTags,
setAttribute,
setAttributes,
setupConnectErrorHandler,
setupExpressErrorHandler,
setupHapiErrorHandler,
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
setConversationId,
withScope,
Expand Down
2 changes: 2 additions & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
setConversationId,
getSpanStatusFromHttpCode,
Expand Down
46 changes: 46 additions & 0 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AttributeObject, RawAttribute, RawAttributes } from './attributes';
import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } from './currentScopes';
import { DEBUG_BUILD } from './debug-build';
import type { CaptureContext } from './scope';
Expand Down Expand Up @@ -103,6 +104,51 @@ export function setTag(key: string, value: Primitive): void {
getIsolationScope().setTag(key, value);
}

/**
* Sets attributes on the isolation scope.
*
* These attributes are applied to logs, metrics and streamed spans.
*
* Supported attribute value types are `string`, `number`, `boolean`, `string[]`, `number[]` and `boolean[]`.
*
* @param attributes - The attributes to set on the scope, as key-value pairs.
*
* @example
* ```typescript
* Sentry.setAttributes({
* is_admin: true,
* payment_selection: 'credit_card',
* render_duration: 150,
* });
* ```
*/
export function setAttributes<T extends Record<string, unknown>>(attributes: RawAttributes<T>): void {
getIsolationScope().setAttributes(attributes);
}

/**
* Sets an attribute on the isolation scope.
*
* These attributes are applied to logs, metrics and streamed spans.
*
* Supported attribute value types are `string`, `number`, `boolean`, `string[]`, `number[]` and `boolean[]`.
*
* @param key - The attribute key.
* @param value - The attribute value.
*
* @example
* ```typescript
* Sentry.setAttribute('is_admin', true);
* Sentry.setAttribute('render_duration', 150);
* ```
*/
export function setAttribute<
// oxlint-disable-next-line typescript-eslint/no-explicit-any
T extends RawAttribute<T> extends { value: any } | { unit: any } ? AttributeObject : unknown,
>(key: string, value: RawAttribute<T>): void {
getIsolationScope().setAttribute(key, value);
}

/**
* Updates user context information for future events.
*
Expand Down
24 changes: 8 additions & 16 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,22 +323,18 @@ export class Scope {
/**
* Sets attributes onto the scope.
*
* These attributes are currently applied to logs and metrics.
* In the future, they will also be applied to spans.
* These attributes are applied to logs, metrics and streamed spans.
*
* Important: For now, only strings, numbers and boolean attributes are supported, despite types allowing for
* more complex attribute types. We'll add this support in the future but already specify the wider type to
* avoid a breaking change in the future.
* Supported attribute value types are `string`, `number`, `boolean`, `string[]`, `number[]` and `boolean[]`.
*
* @param newAttributes - The attributes to set on the scope. You can either pass in key-value pairs, or
* an object with a `value` and an optional `unit` (if applicable to your attribute).
* @param newAttributes - The attributes to set on the scope, as key-value pairs.
*
* @example
* ```typescript
* scope.setAttributes({
* is_admin: true,
* payment_selection: 'credit_card',
* render_duration: { value: 'render_duration', unit: 'ms' },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Is that on purpose a number now?

@Lms24 Lms24 Jun 23, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, documentation was a bit off, since this was a string previously. Also, I opted to remove the unit documentation since the product doesn't yet show units

* render_duration: 150,
* });
* ```
*/
Expand All @@ -355,21 +351,17 @@ export class Scope {
/**
* Sets an attribute onto the scope.
*
* These attributes are currently applied to logs and metrics.
* In the future, they will also be applied to spans.
* These attributes are applied to logs, metrics and streamed spans.
*
* Important: For now, only strings, numbers and boolean attributes are supported, despite types allowing for
* more complex attribute types. We'll add this support in the future but already specify the wider type to
* avoid a breaking change in the future.
* Supported attribute value types are `string`, `number`, `boolean`, `string[]`, `number[]` and `boolean[]`.
*
* @param key - The attribute key.
* @param value - the attribute value. You can either pass in a raw value, or an attribute
* object with a `value` and an optional `unit` (if applicable to your attribute).
* @param value - The attribute value.
*
* @example
* ```typescript
* scope.setAttribute('is_admin', true);
* scope.setAttribute('render_duration', { value: 'render_duration', unit: 'ms' });
* scope.setAttribute('render_duration', 150);
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shared-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
setConversationId,
isInitialized,
Expand Down
2 changes: 2 additions & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/elysia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/node-core/src/common-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
Expand Down
2 changes: 2 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
setConversationId,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
Expand Down
2 changes: 2 additions & 0 deletions packages/remix/src/cloudflare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
2 changes: 2 additions & 0 deletions packages/remix/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export {
setMeasurement,
setTag,
setTags,
setAttribute,
setAttributes,
setupConnectErrorHandler,
setupExpressErrorHandler,
setupHapiErrorHandler,
Expand Down
2 changes: 2 additions & 0 deletions packages/solidstart/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export {
setMeasurement,
setTag,
setTags,
setAttribute,
setAttributes,
setupConnectErrorHandler,
setupExpressErrorHandler,
setupHapiErrorHandler,
Expand Down
2 changes: 2 additions & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export {
setMeasurement,
setTag,
setTags,
setAttribute,
setAttributes,
setupConnectErrorHandler,
setupExpressErrorHandler,
setupHapiErrorHandler,
Expand Down
2 changes: 2 additions & 0 deletions packages/sveltekit/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export {
setMeasurement,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
spanToBaggageHeader,
spanToJSON,
Expand Down
2 changes: 2 additions & 0 deletions packages/vercel-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export {
setExtras,
setTag,
setTags,
setAttribute,
setAttributes,
setUser,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
Loading