-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmapStatus.test.ts
130 lines (110 loc) · 5.88 KB
/
mapStatus.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* 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_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
import type { SpanStatus } from '@sentry/core';
import { SPAN_STATUS_ERROR, SPAN_STATUS_OK } from '@sentry/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { mapStatus } from '../../src/utils/mapStatus';
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
import { setupOtel } from '../helpers/initOtel';
import { cleanupOtel } from '../helpers/mockSdkInit';
describe('mapStatus', () => {
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);
}
const statusTestTable: [undefined | number | string, undefined | string, SpanStatus][] = [
// http codes
[400, undefined, { code: SPAN_STATUS_ERROR, message: 'invalid_argument' }],
[401, undefined, { code: SPAN_STATUS_ERROR, message: 'unauthenticated' }],
[403, undefined, { code: SPAN_STATUS_ERROR, message: 'permission_denied' }],
[404, undefined, { code: SPAN_STATUS_ERROR, message: 'not_found' }],
[409, undefined, { code: SPAN_STATUS_ERROR, message: 'already_exists' }],
[429, undefined, { code: SPAN_STATUS_ERROR, message: 'resource_exhausted' }],
[499, undefined, { code: SPAN_STATUS_ERROR, message: 'cancelled' }],
[500, undefined, { code: SPAN_STATUS_ERROR, message: 'internal_error' }],
[501, undefined, { code: SPAN_STATUS_ERROR, message: 'unimplemented' }],
[503, undefined, { code: SPAN_STATUS_ERROR, message: 'unavailable' }],
[504, undefined, { code: SPAN_STATUS_ERROR, message: 'deadline_exceeded' }],
[999, undefined, { code: SPAN_STATUS_ERROR, message: 'unknown_error' }],
// grpc codes
[undefined, '1', { code: SPAN_STATUS_ERROR, message: 'cancelled' }],
[undefined, '2', { code: SPAN_STATUS_ERROR, message: 'unknown_error' }],
[undefined, '3', { code: SPAN_STATUS_ERROR, message: 'invalid_argument' }],
[undefined, '4', { code: SPAN_STATUS_ERROR, message: 'deadline_exceeded' }],
[undefined, '5', { code: SPAN_STATUS_ERROR, message: 'not_found' }],
[undefined, '6', { code: SPAN_STATUS_ERROR, message: 'already_exists' }],
[undefined, '7', { code: SPAN_STATUS_ERROR, message: 'permission_denied' }],
[undefined, '8', { code: SPAN_STATUS_ERROR, message: 'resource_exhausted' }],
[undefined, '9', { code: SPAN_STATUS_ERROR, message: 'failed_precondition' }],
[undefined, '10', { code: SPAN_STATUS_ERROR, message: 'aborted' }],
[undefined, '11', { code: SPAN_STATUS_ERROR, message: 'out_of_range' }],
[undefined, '12', { code: SPAN_STATUS_ERROR, message: 'unimplemented' }],
[undefined, '13', { code: SPAN_STATUS_ERROR, message: 'internal_error' }],
[undefined, '14', { code: SPAN_STATUS_ERROR, message: 'unavailable' }],
[undefined, '15', { code: SPAN_STATUS_ERROR, message: 'data_loss' }],
[undefined, '16', { code: SPAN_STATUS_ERROR, message: 'unauthenticated' }],
[undefined, '999', { code: SPAN_STATUS_ERROR, message: 'unknown_error' }],
// http takes precedence over grpc
[400, '2', { code: SPAN_STATUS_ERROR, message: 'invalid_argument' }],
];
it.each(statusTestTable)('works with httpCode=%s, grpcCode=%s', (httpCode, grpcCode, expected) => {
const span = createSpan('test-span');
span.setStatus({ code: 0 }); // UNSET
if (httpCode) {
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, httpCode);
}
if (grpcCode) {
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, grpcCode);
}
const actual = mapStatus(span);
expect(actual).toEqual(expected);
});
it('works with string SEMATTRS_HTTP_STATUS_CODE xxx', () => {
const span = createSpan('test-span');
span.setStatus({ code: 0 }); // UNSET
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, '400');
const actual = mapStatus(span);
expect(actual).toEqual({ code: SPAN_STATUS_ERROR, message: 'invalid_argument' });
});
it('returns ok span status when is UNSET present on span', () => {
const span = createSpan('test-span');
span.setStatus({ code: 0 }); // UNSET
expect(mapStatus(span)).toEqual({ code: SPAN_STATUS_OK });
});
it('returns ok span status when already present on span', () => {
const span = createSpan('test-span');
span.setStatus({ code: 1 }); // OK
expect(mapStatus(span)).toEqual({ code: SPAN_STATUS_OK });
});
it('returns error status when span already has error status', () => {
const span = createSpan('test-span');
span.setStatus({ code: 2, message: 'invalid_argument' }); // ERROR
expect(mapStatus(span)).toEqual({ code: SPAN_STATUS_ERROR, message: 'invalid_argument' });
});
it('returns error status when span already has error status without message', () => {
const span = createSpan('test-span');
span.setStatus({ code: 2 }); // ERROR
expect(mapStatus(span)).toEqual({ code: SPAN_STATUS_ERROR, message: 'unknown_error' });
});
it('infers error status form attributes when span already has error status without message', () => {
const span = createSpan('test-span');
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, 500);
span.setStatus({ code: 2 }); // ERROR
expect(mapStatus(span)).toEqual({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
});
it('returns unknown error status when code is unknown', () => {
const span = createSpan('test-span');
span.setStatus({ code: -1 as 0 });
expect(mapStatus(span)).toEqual({ code: SPAN_STATUS_ERROR, message: 'unknown_error' });
});
});