-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgetRequestSpanData.test.ts
80 lines (65 loc) · 2.22 KB
/
getRequestSpanData.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* eslint-disable deprecation/deprecation */
import type { Span } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { getRequestSpanData } from '../../src/utils/getRequestSpanData';
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
import { setupOtel } from '../helpers/initOtel';
import { cleanupOtel } from '../helpers/mockSdkInit';
describe('getRequestSpanData', () => {
let provider: BasicTracerProvider | undefined;
beforeEach(() => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1 }));
provider = setupOtel(client);
});
afterEach(() => {
cleanupOtel(provider);
});
function createSpan(name: string): Span {
return trace.getTracer('test').startSpan(name);
}
it('works with basic span', () => {
const span = createSpan('test-span');
const data = getRequestSpanData(span);
expect(data).toEqual({});
});
it('works with http span', () => {
const span = createSpan('test-span');
span.setAttributes({
[SEMATTRS_HTTP_URL]: 'http://example.com?foo=bar#baz',
[SEMATTRS_HTTP_METHOD]: 'GET',
});
const data = getRequestSpanData(span);
expect(data).toEqual({
url: 'http://example.com',
'http.method': 'GET',
'http.query': '?foo=bar',
'http.fragment': '#baz',
});
});
it('works without method', () => {
const span = createSpan('test-span');
span.setAttributes({
[SEMATTRS_HTTP_URL]: 'http://example.com',
});
const data = getRequestSpanData(span);
expect(data).toEqual({
url: 'http://example.com',
'http.method': 'GET',
});
});
it('works with incorrect URL', () => {
const span = createSpan('test-span');
span.setAttributes({
[SEMATTRS_HTTP_URL]: 'malformed-url-here',
[SEMATTRS_HTTP_METHOD]: 'GET',
});
const data = getRequestSpanData(span);
expect(data).toEqual({
url: 'malformed-url-here',
'http.method': 'GET',
});
});
});