1
1
/* eslint-disable deprecation/deprecation */
2
+ import type { Span } from '@opentelemetry/api' ;
3
+ import { trace } from '@opentelemetry/api' ;
4
+ import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base' ;
2
5
import { SEMATTRS_HTTP_STATUS_CODE , SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions' ;
3
- import { SPAN_STATUS_ERROR , SPAN_STATUS_OK } from '@sentry/core' ;
4
6
import type { SpanStatus } from '@sentry/core' ;
5
- import { describe , expect , it } from 'vitest ' ;
6
-
7
+ import { SPAN_STATUS_ERROR , SPAN_STATUS_OK } from '@sentry/core ' ;
8
+ import { afterEach , beforeEach , describe , expect , it } from 'vitest' ;
7
9
import { mapStatus } from '../../src/utils/mapStatus' ;
8
- import { createSpan } from '../helpers/createSpan' ;
10
+ import { TestClient , getDefaultTestClientOptions } from '../helpers/TestClient' ;
11
+ import { setupOtel } from '../helpers/initOtel' ;
12
+ import { cleanupOtel } from '../helpers/mockSdkInit' ;
9
13
10
14
describe ( 'mapStatus' , ( ) => {
15
+ let provider : BasicTracerProvider | undefined ;
16
+
17
+ beforeEach ( ( ) => {
18
+ const client = new TestClient ( getDefaultTestClientOptions ( { tracesSampleRate : 1 } ) ) ;
19
+ provider = setupOtel ( client ) ;
20
+ } ) ;
21
+
22
+ afterEach ( ( ) => {
23
+ cleanupOtel ( provider ) ;
24
+ } ) ;
25
+
26
+ function createSpan ( name : string ) : Span {
27
+ return trace . getTracer ( 'test' ) . startSpan ( name ) ;
28
+ }
29
+
11
30
const statusTestTable : [ undefined | number | string , undefined | string , SpanStatus ] [ ] = [
12
31
// http codes
13
32
[ 400 , undefined , { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ] ,
@@ -23,19 +42,6 @@ describe('mapStatus', () => {
23
42
[ 504 , undefined , { code : SPAN_STATUS_ERROR , message : 'deadline_exceeded' } ] ,
24
43
[ 999 , undefined , { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ] ,
25
44
26
- [ '400' , undefined , { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ] ,
27
- [ '401' , undefined , { code : SPAN_STATUS_ERROR , message : 'unauthenticated' } ] ,
28
- [ '403' , undefined , { code : SPAN_STATUS_ERROR , message : 'permission_denied' } ] ,
29
- [ '404' , undefined , { code : SPAN_STATUS_ERROR , message : 'not_found' } ] ,
30
- [ '409' , undefined , { code : SPAN_STATUS_ERROR , message : 'already_exists' } ] ,
31
- [ '429' , undefined , { code : SPAN_STATUS_ERROR , message : 'resource_exhausted' } ] ,
32
- [ '499' , undefined , { code : SPAN_STATUS_ERROR , message : 'cancelled' } ] ,
33
- [ '500' , undefined , { code : SPAN_STATUS_ERROR , message : 'internal_error' } ] ,
34
- [ '501' , undefined , { code : SPAN_STATUS_ERROR , message : 'unimplemented' } ] ,
35
- [ '503' , undefined , { code : SPAN_STATUS_ERROR , message : 'unavailable' } ] ,
36
- [ '504' , undefined , { code : SPAN_STATUS_ERROR , message : 'deadline_exceeded' } ] ,
37
- [ '999' , undefined , { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ] ,
38
-
39
45
// grpc codes
40
46
[ undefined , '1' , { code : SPAN_STATUS_ERROR , message : 'cancelled' } ] ,
41
47
[ undefined , '2' , { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ] ,
@@ -56,11 +62,11 @@ describe('mapStatus', () => {
56
62
[ undefined , '999' , { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ] ,
57
63
58
64
// http takes precedence over grpc
59
- [ ' 400' , '2' , { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ] ,
65
+ [ 400 , '2' , { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ] ,
60
66
] ;
61
67
62
68
it . each ( statusTestTable ) ( 'works with httpCode=%s, grpcCode=%s' , ( httpCode , grpcCode , expected ) => {
63
- const span = createSpan ( ) ;
69
+ const span = createSpan ( 'test-span' ) ;
64
70
span . setStatus ( { code : 0 } ) ; // UNSET
65
71
66
72
if ( httpCode ) {
@@ -75,39 +81,49 @@ describe('mapStatus', () => {
75
81
expect ( actual ) . toEqual ( expected ) ;
76
82
} ) ;
77
83
84
+ it ( 'works with string SEMATTRS_HTTP_STATUS_CODE xxx' , ( ) => {
85
+ const span = createSpan ( 'test-span' ) ;
86
+
87
+ span . setStatus ( { code : 0 } ) ; // UNSET
88
+ span . setAttribute ( SEMATTRS_HTTP_STATUS_CODE , '400' ) ;
89
+
90
+ const actual = mapStatus ( span ) ;
91
+ expect ( actual ) . toEqual ( { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ) ;
92
+ } ) ;
93
+
78
94
it ( 'returns ok span status when is UNSET present on span' , ( ) => {
79
- const span = createSpan ( ) ;
95
+ const span = createSpan ( 'test-span' ) ;
80
96
span . setStatus ( { code : 0 } ) ; // UNSET
81
97
expect ( mapStatus ( span ) ) . toEqual ( { code : SPAN_STATUS_OK } ) ;
82
98
} ) ;
83
99
84
100
it ( 'returns ok span status when already present on span' , ( ) => {
85
- const span = createSpan ( ) ;
101
+ const span = createSpan ( 'test-span' ) ;
86
102
span . setStatus ( { code : 1 } ) ; // OK
87
103
expect ( mapStatus ( span ) ) . toEqual ( { code : SPAN_STATUS_OK } ) ;
88
104
} ) ;
89
105
90
106
it ( 'returns error status when span already has error status' , ( ) => {
91
- const span = createSpan ( ) ;
107
+ const span = createSpan ( 'test-span' ) ;
92
108
span . setStatus ( { code : 2 , message : 'invalid_argument' } ) ; // ERROR
93
109
expect ( mapStatus ( span ) ) . toEqual ( { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ) ;
94
110
} ) ;
95
111
96
112
it ( 'returns error status when span already has error status without message' , ( ) => {
97
- const span = createSpan ( ) ;
113
+ const span = createSpan ( 'test-span' ) ;
98
114
span . setStatus ( { code : 2 } ) ; // ERROR
99
115
expect ( mapStatus ( span ) ) . toEqual ( { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ) ;
100
116
} ) ;
101
117
102
118
it ( 'infers error status form attributes when span already has error status without message' , ( ) => {
103
- const span = createSpan ( ) ;
119
+ const span = createSpan ( 'test-span' ) ;
104
120
span . setAttribute ( SEMATTRS_HTTP_STATUS_CODE , 500 ) ;
105
121
span . setStatus ( { code : 2 } ) ; // ERROR
106
122
expect ( mapStatus ( span ) ) . toEqual ( { code : SPAN_STATUS_ERROR , message : 'internal_error' } ) ;
107
123
} ) ;
108
124
109
125
it ( 'returns unknown error status when code is unknown' , ( ) => {
110
- const span = createSpan ( ) ;
126
+ const span = createSpan ( 'test-span' ) ;
111
127
span . setStatus ( { code : - 1 as 0 } ) ;
112
128
expect ( mapStatus ( span ) ) . toEqual ( { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ) ;
113
129
} ) ;
0 commit comments