Skip to content

Commit 4f1bd80

Browse files
committed
feat(core): Add spanGetMetadata() & spanSetMetadata() APIs
To replace `transaction.setMetadata()`.
1 parent a696aef commit 4f1bd80

File tree

38 files changed

+253
-114
lines changed

38 files changed

+253
-114
lines changed

MIGRATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
1717
* `span.setName(newName)`: Use `span.updateName(newName)` instead.
1818
* `span.toTraceparent()`: use `spanToTraceHeader(span)` util instead.
1919
* `span.getTraceContext()`: Use `spanToTraceContext(span)` utility function instead.
20+
* `transaction.setMetadata(metadata)`: Use `spanSetMetadata(span, metadata)` utility function instead.
21+
* `transaction.metadata`: Use `spanGetMetadata(span)` utility function instead.
2022

2123
## Deprecate `pushScope` & `popScope` in favor of `withScope`
2224

dev-packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http from 'http';
2+
import { spanSetMetadata } from '@sentry/core';
23
import * as Sentry from '@sentry/node';
34
import * as Tracing from '@sentry/tracing';
45
import cors from 'cors';
@@ -32,7 +33,7 @@ app.get('/test/express', (_req, res) => {
3233
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
3334
if (transaction) {
3435
transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd';
35-
transaction.setMetadata({ source: 'route' });
36+
spanSetMetadata(transaction, { source: 'route' });
3637
}
3738
const headers = http.get('http://somewhere.not.sentry/').getHeaders();
3839

packages/angular-ivy/ng-package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"entryFile": "src/index.ts",
66
"umdModuleIds": {
77
"@sentry/browser": "Sentry",
8-
"@sentry/utils": "Sentry.util"
8+
"@sentry/utils": "Sentry.util",
9+
"@sentry/core": "Sentry.core"
910
}
1011
},
11-
"allowedNonPeerDependencies": ["@sentry/browser", "@sentry/utils", "@sentry/types", "tslib"],
12+
"allowedNonPeerDependencies": ["@sentry/browser", "@sentry/core", "@sentry/utils", "@sentry/types", "tslib"],
1213
"assets": ["README.md", "LICENSE"]
1314
}

packages/angular-ivy/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"dependencies": {
2424
"@sentry/browser": "7.91.0",
25+
"@sentry/core": "7.91.0",
2526
"@sentry/types": "7.91.0",
2627
"@sentry/utils": "7.91.0",
2728
"tslib": "^2.4.1"

packages/angular/ng-package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"entryFile": "src/index.ts",
66
"umdModuleIds": {
77
"@sentry/browser": "Sentry",
8-
"@sentry/utils": "Sentry.util"
8+
"@sentry/utils": "Sentry.util",
9+
"@sentry/core": "Sentry.core"
910
}
1011
},
11-
"whitelistedNonPeerDependencies": ["@sentry/browser", "@sentry/utils", "@sentry/types", "tslib"],
12+
"whitelistedNonPeerDependencies": ["@sentry/browser", "@sentry/core", "@sentry/utils", "@sentry/types", "tslib"],
1213
"assets": ["README.md", "LICENSE"]
1314
}

packages/angular/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"dependencies": {
2424
"@sentry/browser": "7.91.0",
25+
"@sentry/core": "7.91.0",
2526
"@sentry/types": "7.91.0",
2627
"@sentry/utils": "7.91.0",
2728
"tslib": "^2.4.1"

packages/angular/src/tracing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ActivatedRouteSnapshot, Event, RouterState } from '@angular/router
88
import { NavigationCancel, NavigationError, Router } from '@angular/router';
99
import { NavigationEnd, NavigationStart, ResolveEnd } from '@angular/router';
1010
import { WINDOW, getCurrentScope } from '@sentry/browser';
11+
import { spanSetMetadata } from '@sentry/core';
1112
import type { Span, Transaction, TransactionContext } from '@sentry/types';
1213
import { logger, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/utils';
1314
import type { Observable } from 'rxjs';
@@ -119,7 +120,7 @@ export class TraceService implements OnDestroy {
119120
// TODO (v8 / #5416): revisit the source condition. Do we want to make the parameterized route the default?
120121
if (transaction && transaction.metadata.source === 'url') {
121122
transaction.updateName(route);
122-
transaction.setMetadata({ source: 'route' });
123+
spanSetMetadata(transaction, { source: 'route' });
123124
}
124125
}),
125126
);

packages/angular/test/tracing.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component } from '@angular/core';
22
import type { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
33

4+
import * as SentryCore from '@sentry/core';
45
import { TraceClassDecorator, TraceDirective, TraceMethodDecorator, instrumentAngularRouting } from '../src';
56
import { getParameterizedRouteFromSnapshot } from '../src/tracing';
67
import { AppComponent, TestEnv } from './utils/index';
@@ -11,7 +12,6 @@ const defaultStartTransaction = (ctx: any) => {
1112
transaction = {
1213
...ctx,
1314
updateName: jest.fn(name => (transaction.name = name)),
14-
setMetadata: jest.fn(),
1515
};
1616

1717
return transaction;
@@ -31,9 +31,12 @@ jest.mock('@sentry/browser', () => {
3131
};
3232
});
3333

34+
const mockSpanSetMetadata = jest.spyOn(SentryCore, 'spanSetMetadata');
35+
3436
describe('Angular Tracing', () => {
3537
beforeEach(() => {
3638
transaction = undefined;
39+
mockSpanSetMetadata.mockClear();
3740
});
3841

3942
describe('instrumentAngularRouting', () => {
@@ -329,7 +332,7 @@ describe('Angular Tracing', () => {
329332
metadata: { source: 'url' },
330333
});
331334
expect(transaction.updateName).toHaveBeenCalledWith(result);
332-
expect(transaction.setMetadata).toHaveBeenCalledWith({ source: 'route' });
335+
expect(mockSpanSetMetadata).toHaveBeenCalledWith(expect.anything(), { source: 'route' });
333336

334337
env.destroy();
335338
});

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export { createCheckInEnvelope } from './checkin';
7070
export { hasTracingEnabled } from './utils/hasTracingEnabled';
7171
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
7272
export { handleCallbackErrors } from './utils/handleCallbackErrors';
73-
export { spanToTraceHeader } from './utils/spanUtils';
73+
export { spanToTraceHeader, spanSetMetadata, spanGetMetadata } from './utils/spanUtils';
7474
export { DEFAULT_ENVIRONMENT } from './constants';
7575
export { ModuleMetadata } from './integrations/metadata';
7676
export { RequestData } from './integrations/requestdata';

packages/core/src/tracing/sampling.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isNaN, logger } from '@sentry/utils';
33

44
import { DEBUG_BUILD } from '../debug-build';
55
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
6+
import { spanSetMetadata } from '../utils/spanUtils';
67
import type { Transaction } from './transaction';
78

89
/**
@@ -27,7 +28,7 @@ export function sampleTransaction<T extends Transaction>(
2728

2829
// if the user has forced a sampling decision by passing a `sampled` value in their transaction context, go with that
2930
if (transaction.sampled !== undefined) {
30-
transaction.setMetadata({
31+
spanSetMetadata(transaction, {
3132
sampleRate: Number(transaction.sampled),
3233
});
3334
return transaction;
@@ -38,20 +39,20 @@ export function sampleTransaction<T extends Transaction>(
3839
let sampleRate;
3940
if (typeof options.tracesSampler === 'function') {
4041
sampleRate = options.tracesSampler(samplingContext);
41-
transaction.setMetadata({
42+
spanSetMetadata(transaction, {
4243
sampleRate: Number(sampleRate),
4344
});
4445
} else if (samplingContext.parentSampled !== undefined) {
4546
sampleRate = samplingContext.parentSampled;
4647
} else if (typeof options.tracesSampleRate !== 'undefined') {
4748
sampleRate = options.tracesSampleRate;
48-
transaction.setMetadata({
49-
sampleRate: Number(sampleRate),
49+
spanSetMetadata(transaction, {
50+
sampleRate,
5051
});
5152
} else {
5253
// When `enableTracing === true`, we use a sample rate of 100%
5354
sampleRate = 1;
54-
transaction.setMetadata({
55+
spanSetMetadata(transaction, {
5556
sampleRate,
5657
});
5758
}

0 commit comments

Comments
 (0)