Skip to content

Commit f49dfc2

Browse files
committed
better semantic attributes & fix tests
1 parent 72cde44 commit f49dfc2

File tree

40 files changed

+156
-124
lines changed

40 files changed

+156
-124
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import http from 'http';
2-
import { SentrySemanticAttributes } from '@sentry/core';
2+
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
33
import * as Sentry from '@sentry/node';
44
import * as Tracing from '@sentry/tracing';
55
import cors from 'cors';
@@ -34,7 +34,7 @@ app.get('/test/express', (_req, res) => {
3434
if (transaction) {
3535
// eslint-disable-next-line deprecation/deprecation
3636
transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd';
37-
transaction.setAttribute(SentrySemanticAttributes.Source, 'route');
37+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
3838
}
3939
const headers = http.get('http://somewhere.not.sentry/').getHeaders();
4040

packages/angular/src/tracing.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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 { SentrySemanticAttributes, spanToJSON } from '@sentry/core';
11+
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, spanToJSON } from '@sentry/core';
1212
import type { Span, Transaction, TransactionContext } from '@sentry/types';
1313
import { logger, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/utils';
1414
import type { Observable } from 'rxjs';
@@ -41,7 +41,7 @@ export function routingInstrumentation(
4141
op: 'pageload',
4242
origin: 'auto.pageload.angular',
4343
data: {
44-
[SentrySemanticAttributes.Source]: 'url',
44+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
4545
},
4646
});
4747
}
@@ -80,7 +80,7 @@ export class TraceService implements OnDestroy {
8080
op: 'navigation',
8181
origin: 'auto.navigation.angular',
8282
data: {
83-
[SentrySemanticAttributes.Source]: 'url',
83+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
8484
},
8585
});
8686
}
@@ -124,9 +124,9 @@ export class TraceService implements OnDestroy {
124124
const transaction = getActiveTransaction();
125125
// TODO (v8 / #5416): revisit the source condition. Do we want to make the parameterized route the default?
126126
const attributes = (transaction && spanToJSON(transaction).data) || {};
127-
if (transaction && attributes[SentrySemanticAttributes.Source] === 'url') {
127+
if (transaction && attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'url') {
128128
transaction.updateName(route);
129-
transaction.setAttribute(SentrySemanticAttributes.Source, 'route');
129+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
130130
}
131131
}),
132132
);

packages/angular/test/tracing.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component } from '@angular/core';
22
import type { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
3-
import { SentrySemanticAttributes } from '@sentry/core';
3+
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
44

55
import { TraceClassDecorator, TraceDirective, TraceMethodDecorator, instrumentAngularRouting } from '../src';
66
import { getParameterizedRouteFromSnapshot } from '../src/tracing';
@@ -15,7 +15,7 @@ const defaultStartTransaction = (ctx: any) => {
1515
setAttribute: jest.fn(),
1616
toJSON: () => ({
1717
data: {
18-
[SentrySemanticAttributes.Source]: 'custom',
18+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
1919
...ctx.data,
2020
},
2121
}),
@@ -52,7 +52,7 @@ describe('Angular Tracing', () => {
5252
name: '/',
5353
op: 'pageload',
5454
origin: 'auto.pageload.angular',
55-
data: { [SentrySemanticAttributes.Source]: 'url' },
55+
data: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' },
5656
});
5757
});
5858
});
@@ -117,7 +117,7 @@ describe('Angular Tracing', () => {
117117
toJSON: () => ({
118118
data: {
119119
...ctx.data,
120-
[SentrySemanticAttributes.Source]: 'custom',
120+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
121121
},
122122
}),
123123
metadata: ctx.metadata,
@@ -146,12 +146,12 @@ describe('Angular Tracing', () => {
146146
name: url,
147147
op: 'pageload',
148148
origin: 'auto.pageload.angular',
149-
data: { [SentrySemanticAttributes.Source]: 'url' },
149+
data: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' },
150150
});
151151

152152
expect(transaction.updateName).toHaveBeenCalledTimes(0);
153153
expect(transaction.name).toEqual(url);
154-
expect(transaction.toJSON().data).toEqual({ [SentrySemanticAttributes.Source]: 'custom' });
154+
expect(transaction.toJSON().data).toEqual({ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' });
155155

156156
env.destroy();
157157
});
@@ -337,10 +337,10 @@ describe('Angular Tracing', () => {
337337
name: url,
338338
op: 'navigation',
339339
origin: 'auto.navigation.angular',
340-
data: { [SentrySemanticAttributes.Source]: 'url' },
340+
data: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' },
341341
});
342342
expect(transaction.updateName).toHaveBeenCalledWith(result);
343-
expect(transaction.setAttribute).toHaveBeenCalledWith(SentrySemanticAttributes.Source, 'route');
343+
expect(transaction.setAttribute).toHaveBeenCalledWith(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
344344

345345
env.destroy();
346346
});

packages/astro/src/server/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SentrySemanticAttributes } from '@sentry/core';
1+
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
22
import {
33
captureException,
44
continueTrace,
@@ -129,7 +129,7 @@ async function instrumentRequest(
129129
data: {
130130
method,
131131
url: stripUrlQueryAndFragment(ctx.url.href),
132-
[SentrySemanticAttributes.Source]: source,
132+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
133133
...(ctx.url.search && { 'http.query': ctx.url.search }),
134134
...(ctx.url.hash && { 'http.fragment': ctx.url.hash }),
135135
...(options.trackHeaders && { headers: allHeaders }),

packages/astro/test/server/middleware.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SentrySemanticAttributes } from '@sentry/core';
1+
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
22
import * as SentryNode from '@sentry/node';
33
import type { Client } from '@sentry/types';
44
import { vi } from 'vitest';
@@ -58,7 +58,7 @@ describe('sentryMiddleware', () => {
5858
data: {
5959
method: 'GET',
6060
url: 'https://mydomain.io/users/123/details',
61-
[SentrySemanticAttributes.Source]: 'route',
61+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
6262
},
6363
metadata: {},
6464
name: 'GET /users/[id]/details',
@@ -94,7 +94,7 @@ describe('sentryMiddleware', () => {
9494
data: {
9595
method: 'GET',
9696
url: 'http://localhost:1234/a%xx',
97-
[SentrySemanticAttributes.Source]: 'url',
97+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
9898
},
9999
metadata: {},
100100
name: 'GET a%xx',
@@ -159,7 +159,7 @@ describe('sentryMiddleware', () => {
159159
expect(startSpanSpy).toHaveBeenCalledWith(
160160
expect.objectContaining({
161161
data: expect.objectContaining({
162-
[SentrySemanticAttributes.Source]: 'route',
162+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
163163
}),
164164
metadata: {
165165
dynamicSamplingContext: {

packages/bun/src/integrations/bunserver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
SentrySemanticAttributes,
2+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
33
Transaction,
44
captureException,
55
continueTrace,
@@ -55,7 +55,7 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
5555
const parsedUrl = parseUrl(request.url);
5656
const data: Record<string, unknown> = {
5757
'http.request.method': request.method || 'GET',
58-
[SentrySemanticAttributes.Source]: 'url',
58+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
5959
};
6060
if (parsedUrl.search) {
6161
data['http.query'] = parsedUrl.search;

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type { ServerRuntimeClientOptions } from './server-runtime-client';
55
export type { RequestDataIntegrationOptions } from './integrations/requestdata';
66

77
export * from './tracing';
8+
export * from './semanticAttributes';
89
export { createEventEnvelope, createSessionEnvelope } from './envelope';
910
export {
1011
addBreadcrumb,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Use this attribute to represent the source of a span.
3+
* Should be one of: custom, url, route, view, component, task, unknown
4+
*
5+
*/
6+
export const SEMANTIC_ATTRIBUTE_SENTRY_SOURCE = 'sentry.source';
7+
8+
/**
9+
* Use this attribute to represent the sample rate used for a span.
10+
*/
11+
export const SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE = 'sentry.sample_rate';

packages/core/src/tracing/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ export {
2121
} from './trace';
2222
export { getDynamicSamplingContextFromClient } from './dynamicSamplingContext';
2323
export { setMeasurement } from './measurement';
24-
export { SentrySemanticAttributes } from './spanAttributes';

packages/core/src/tracing/sampling.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { Options, SamplingContext } from '@sentry/types';
22
import { isNaN, logger } from '@sentry/utils';
33

44
import { DEBUG_BUILD } from '../debug-build';
5+
import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE } from '../semanticAttributes';
56
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
67
import { spanToJSON } from '../utils/spanUtils';
7-
import { SentrySemanticAttributes } from './spanAttributes';
88
import type { Transaction } from './transaction';
99

1010
/**
@@ -31,7 +31,8 @@ export function sampleTransaction<T extends Transaction>(
3131
// if the user has forced a sampling decision by passing a `sampled` value in their transaction context, go with that
3232
// eslint-disable-next-line deprecation/deprecation
3333
if (transaction.sampled !== undefined) {
34-
transaction.setAttribute(SentrySemanticAttributes.SampleRate, Number(transaction.sampled));
34+
// eslint-disable-next-line deprecation/deprecation
35+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, Number(transaction.sampled));
3536
return transaction;
3637
}
3738

@@ -40,16 +41,16 @@ export function sampleTransaction<T extends Transaction>(
4041
let sampleRate;
4142
if (typeof options.tracesSampler === 'function') {
4243
sampleRate = options.tracesSampler(samplingContext);
43-
transaction.setAttribute(SentrySemanticAttributes.SampleRate, Number(sampleRate));
44+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, Number(sampleRate));
4445
} else if (samplingContext.parentSampled !== undefined) {
4546
sampleRate = samplingContext.parentSampled;
4647
} else if (typeof options.tracesSampleRate !== 'undefined') {
4748
sampleRate = options.tracesSampleRate;
48-
transaction.setAttribute(SentrySemanticAttributes.SampleRate, Number(sampleRate));
49+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, Number(sampleRate));
4950
} else {
5051
// When `enableTracing === true`, we use a sample rate of 100%
5152
sampleRate = 1;
52-
transaction.setAttribute(SentrySemanticAttributes.SampleRate, sampleRate);
53+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, sampleRate);
5354
}
5455

5556
// Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The

0 commit comments

Comments
 (0)