Skip to content

Commit 36d5ae7

Browse files
committed
more unit tests
1 parent 6093ab6 commit 36d5ae7

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed

packages/opentelemetry/src/utils/parseSpanDescription.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ export function getSanitizedUrl(
264264
return { urlPath: undefined, url, query, fragment, hasRoute: false };
265265
}
266266

267-
function getOriginalName(name: string, attributes: Attributes): string {
267+
/**
268+
* Because Otel decided to mutate span names via `span.updateName`, the only way to ensure
269+
* that a user-set span name is preserved is to store it as a tmp attribute on the span.
270+
* We delete this attribute once we're done with it when preparing the event envelope.
271+
* @internal
272+
*/
273+
export function getOriginalName(name: string, attributes: Attributes): string {
268274
return attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom' &&
269275
attributes['_sentry_span_name_set_by_user'] &&
270276
typeof attributes['_sentry_span_name_set_by_user'] === 'string'

packages/opentelemetry/test/utils/parseSpanDescription.test.ts

+113-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import {
1616
} from '@opentelemetry/semantic-conventions';
1717

1818
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
19-
import { descriptionForHttpMethod, getSanitizedUrl, parseSpanDescription } from '../../src/utils/parseSpanDescription';
19+
import {
20+
descriptionForHttpMethod,
21+
getOriginalName,
22+
getSanitizedUrl,
23+
parseSpanDescription,
24+
} from '../../src/utils/parseSpanDescription';
2025

2126
describe('parseSpanDescription', () => {
2227
it.each([
@@ -97,6 +102,22 @@ describe('parseSpanDescription', () => {
97102
source: 'custom',
98103
},
99104
],
105+
[
106+
'works with db system and custom source and custom name',
107+
{
108+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
109+
[SEMATTRS_DB_SYSTEM]: 'mysql',
110+
[SEMATTRS_DB_STATEMENT]: 'SELECT * from users',
111+
['_sentry_span_name_set_by_user']: 'custom name',
112+
},
113+
'test name',
114+
SpanKind.CLIENT,
115+
{
116+
description: 'custom name',
117+
op: 'db',
118+
source: 'custom',
119+
},
120+
],
100121
[
101122
'works with db system without statement',
102123
{
@@ -137,6 +158,21 @@ describe('parseSpanDescription', () => {
137158
source: 'custom',
138159
},
139160
],
161+
[
162+
'works with rpc service and custom source and custom name',
163+
{
164+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
165+
[SEMATTRS_RPC_SERVICE]: 'rpc-test-service',
166+
['_sentry_span_name_set_by_user']: 'custom name',
167+
},
168+
'test name',
169+
undefined,
170+
{
171+
description: 'custom name',
172+
op: 'rpc',
173+
source: 'custom',
174+
},
175+
],
140176
[
141177
'works with messaging system',
142178
{
@@ -164,6 +200,21 @@ describe('parseSpanDescription', () => {
164200
source: 'custom',
165201
},
166202
],
203+
[
204+
'works with messaging system and custom source and custom name',
205+
{
206+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
207+
[SEMATTRS_MESSAGING_SYSTEM]: 'test-messaging-system',
208+
['_sentry_span_name_set_by_user']: 'custom name',
209+
},
210+
'test name',
211+
undefined,
212+
{
213+
description: 'custom name',
214+
op: 'message',
215+
source: 'custom',
216+
},
217+
],
167218
[
168219
'works with faas trigger',
169220
{
@@ -191,6 +242,21 @@ describe('parseSpanDescription', () => {
191242
source: 'custom',
192243
},
193244
],
245+
[
246+
'works with faas trigger and custom source and custom name',
247+
{
248+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
249+
[SEMATTRS_FAAS_TRIGGER]: 'test-faas-trigger',
250+
['_sentry_span_name_set_by_user']: 'custom name',
251+
},
252+
'test name',
253+
undefined,
254+
{
255+
description: 'custom name',
256+
op: 'test-faas-trigger',
257+
source: 'custom',
258+
},
259+
],
194260
])('%s', (_, attributes, name, kind, expected) => {
195261
const actual = parseSpanDescription({ attributes, kind, name } as unknown as Span);
196262
expect(actual).toEqual(expected);
@@ -309,7 +375,7 @@ describe('descriptionForHttpMethod', () => {
309375
},
310376
],
311377
[
312-
"doesn't overwrite name with source custom",
378+
"doesn't overwrite span name with source custom",
313379
'GET',
314380
{
315381
[SEMATTRS_HTTP_METHOD]: 'GET',
@@ -329,6 +395,28 @@ describe('descriptionForHttpMethod', () => {
329395
source: 'custom',
330396
},
331397
],
398+
[
399+
'takes user-passed span name (with source custom)',
400+
'GET',
401+
{
402+
[SEMATTRS_HTTP_METHOD]: 'GET',
403+
[SEMATTRS_HTTP_URL]: 'https://www.example.com/my-path/123',
404+
[SEMATTRS_HTTP_TARGET]: '/my-path/123',
405+
[ATTR_HTTP_ROUTE]: '/my-path/:id',
406+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
407+
['_sentry_span_name_set_by_user']: 'custom name',
408+
},
409+
'test name',
410+
SpanKind.CLIENT,
411+
{
412+
op: 'http.client',
413+
description: 'custom name',
414+
data: {
415+
url: 'https://www.example.com/my-path/123',
416+
},
417+
source: 'custom',
418+
},
419+
],
332420
])('%s', (_, httpMethod, attributes, name, kind, expected) => {
333421
const actual = descriptionForHttpMethod({ attributes, kind, name }, httpMethod);
334422
expect(actual).toEqual(expected);
@@ -482,3 +570,26 @@ describe('getSanitizedUrl', () => {
482570
expect(actual).toEqual(expected);
483571
});
484572
});
573+
574+
describe('getOriginalName', () => {
575+
it('returns param name if source is not custom', () => {
576+
expect(getOriginalName('base name', {})).toBe('base name');
577+
});
578+
579+
it('returns param name if `_sentry_span_name_set_by_user` attribute is not set', () => {
580+
expect(getOriginalName('base name', { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' })).toBe('base name');
581+
});
582+
583+
it('returns param name if `_sentry_span_name_set_by_user` attribute is not a string', () => {
584+
expect(getOriginalName('base name', { ['_sentry_span_name_set_by_user']: 123 })).toBe('base name');
585+
});
586+
587+
it('returns `_sentry_span_name_set_by_user` attribute if is a string and source is custom', () => {
588+
expect(
589+
getOriginalName('base name', {
590+
['_sentry_span_name_set_by_user']: 'custom name',
591+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
592+
}),
593+
).toBe('custom name');
594+
});
595+
});

0 commit comments

Comments
 (0)