11import { HTTP_CLIENT , HTTP_SERVER } from '@sentry/conventions/op' ;
22import { createTestServer } from '@sentry-internal/test-utils' ;
3- import { HTTP_RESPONSE_STATUS_CODE , SENTRY_OP , URL_FULL , URL_PATH } from '@sentry/conventions/attributes' ;
3+ import {
4+ CLIENT_ADDRESS ,
5+ CLIENT_PORT ,
6+ HTTP_REQUEST_METHOD ,
7+ HTTP_RESPONSE_STATUS_CODE ,
8+ HTTP_RESPONSE_STATUS_TEXT ,
9+ HTTP_ROUTE ,
10+ NETWORK_LOCAL_ADDRESS ,
11+ NETWORK_LOCAL_PORT ,
12+ NETWORK_PEER_ADDRESS ,
13+ NETWORK_PEER_PORT ,
14+ NETWORK_PROTOCOL_NAME ,
15+ NETWORK_PROTOCOL_VERSION ,
16+ NETWORK_TRANSPORT ,
17+ SENTRY_ENVIRONMENT ,
18+ SENTRY_IS_LOCALHOST ,
19+ SENTRY_KIND ,
20+ SENTRY_OP ,
21+ SENTRY_ORIGIN ,
22+ SENTRY_RELEASE ,
23+ SENTRY_SDK_INTEGRATIONS ,
24+ SENTRY_SDK_NAME ,
25+ SENTRY_SDK_VERSION ,
26+ SENTRY_SEGMENT_ID ,
27+ SENTRY_SEGMENT_NAME ,
28+ SENTRY_SEGMENT_NAME_SOURCE ,
29+ SENTRY_TRACE_LIFECYCLE ,
30+ SERVER_ADDRESS ,
31+ SERVER_PORT ,
32+ URL_FULL ,
33+ URL_PATH ,
34+ URL_SCHEME ,
35+ USER_AGENT_ORIGINAL ,
36+ USER_IP_ADDRESS ,
37+ } from '@sentry/conventions/attributes' ;
38+ import { SDK_VERSION , SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE } from '@sentry/core' ;
439import { afterAll , describe , expect , test } from 'vitest' ;
540import { cleanupChildProcesses , createEsmAndCjsTests , createRunner } from '../../../utils/runner' ;
6- import { supports } from '../../../utils' ;
41+ import { EXPECTED_SDK_NAME , supports } from '../../../utils' ;
42+
43+ const LOOPBACK_ADDRESS = / ^ ( : : 1 | 1 2 7 \. 0 \. 0 \. 1 | : : f f f f : 1 2 7 \. 0 \. 0 \. 1 ) $ / ;
44+
45+ // Attributes from the context of the host (app, device, OS, culture, runtime, cloud). Which of them
46+ // the SDK sends depends on the platform, so tests only check that they are well formed.
47+ const HOST_CONTEXT_ATTRIBUTE = / ^ ( a p p | d e v i c e | o s | c u l t u r e | p r o c e s s \. r u n t i m e | c l o u d ) \. / ;
48+
49+ function splitHostContextAttributes ( attributes : Record < string , unknown > ) : {
50+ hostContext : Record < string , unknown > ;
51+ rest : Record < string , unknown > ;
52+ } {
53+ const hostContext : Record < string , unknown > = { } ;
54+ const rest : Record < string , unknown > = { } ;
55+ for ( const [ key , value ] of Object . entries ( attributes ) ) {
56+ ( HOST_CONTEXT_ATTRIBUTE . test ( key ) ? hostContext : rest ) [ key ] = value ;
57+ }
58+ return { hostContext, rest } ;
59+ }
760
861function getCommonHttpRequestHeaders ( ) : Record < string , unknown > {
962 return {
@@ -22,30 +75,76 @@ describe('httpIntegration', () => {
2275 cleanupChildProcesses ( ) ;
2376 } ) ;
2477
25- describe ( 'onSpanCreated option ' , ( ) => {
78+ describe ( 'onSpanCreated and onSpanEnd options ' , ( ) => {
2679 createEsmAndCjsTests ( __dirname , 'server.mjs' , 'instrument-options.mjs' , ( createRunner , test ) => {
27- test ( 'allows to configure onSpanCreated' , async ( ) => {
80+ test ( 'allows to configure onSpanCreated and onSpanEnd ' , async ( ) => {
2881 const runner = createRunner ( )
2982 . expect ( {
30- span : {
31- items : expect . arrayContaining ( [
83+ span : container => {
84+ const segmentSpan = container . items . find ( span => span . is_segment ) ;
85+ expect ( segmentSpan ) . toEqual (
3286 expect . objectContaining ( {
33- is_segment : true ,
3487 span_id : expect . stringMatching ( / [ a - f \d ] { 16 } / ) ,
3588 trace_id : expect . stringMatching ( / [ a - f \d ] { 32 } / ) ,
3689 status : 'ok' ,
37- attributes : expect . objectContaining ( {
38- [ URL_FULL ] : { type : 'string' , value : expect . stringMatching ( / \/ t e s t $ / ) } ,
39- [ HTTP_RESPONSE_STATUS_CODE ] : { type : 'integer' , value : 200 } ,
40- [ SENTRY_OP ] : { type : 'string' , value : HTTP_SERVER } ,
41- onSpanCreated : { type : 'string' , value : 'yes' } ,
42- 'onSpanCreated.reqUrl' : { type : 'string' , value : expect . stringMatching ( / \/ t e s t $ / ) } ,
43- 'onSpanCreated.reqMethod' : { type : 'string' , value : 'GET' } ,
44- 'onSpanCreated.resUrl' : { type : 'string' , value : expect . stringMatching ( / \/ t e s t $ / ) } ,
45- 'onSpanCreated.resMethod' : { type : 'string' , value : 'GET' } ,
46- } ) ,
4790 } ) ,
48- ] ) ,
91+ ) ;
92+
93+ // Every other attribute is listed, so the test fails when the span gets a new one.
94+ const { hostContext, rest } = splitHostContextAttributes ( segmentSpan ! . attributes ) ;
95+ expect ( rest ) . toEqual ( {
96+ onSpanCreated : { type : 'string' , value : 'yes' } ,
97+ 'onSpanCreated.reqUrl' : { type : 'string' , value : '/test' } ,
98+ 'onSpanCreated.reqMethod' : { type : 'string' , value : 'GET' } ,
99+ 'onSpanCreated.resUrl' : { type : 'string' , value : '/test' } ,
100+ 'onSpanCreated.resMethod' : { type : 'string' , value : 'GET' } ,
101+ 'onSpanEnd.reqUrl' : { type : 'string' , value : '/test' } ,
102+ 'onSpanEnd.resStatusCode' : { type : 'integer' , value : 200 } ,
103+ [ SENTRY_OP ] : { type : 'string' , value : HTTP_SERVER } ,
104+ [ SENTRY_ORIGIN ] : { type : 'string' , value : 'auto.http.http_server' } ,
105+ [ SENTRY_KIND ] : { type : 'string' , value : 'server' } ,
106+ [ SENTRY_SEGMENT_ID ] : { type : 'string' , value : segmentSpan ! . span_id } ,
107+ [ SENTRY_SEGMENT_NAME ] : { type : 'string' , value : 'GET /test' } ,
108+ [ SENTRY_SEGMENT_NAME_SOURCE ] : { type : 'string' , value : 'route' } ,
109+ [ SENTRY_TRACE_LIFECYCLE ] : { type : 'string' , value : 'stream' } ,
110+ [ SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE ] : { type : 'integer' , value : 1 } ,
111+ [ SENTRY_IS_LOCALHOST ] : { type : 'boolean' , value : true } ,
112+ [ SENTRY_ENVIRONMENT ] : { type : 'string' , value : 'production' } ,
113+ [ SENTRY_RELEASE ] : { type : 'string' , value : '1.0' } ,
114+ [ SENTRY_SDK_NAME ] : { type : 'string' , value : EXPECTED_SDK_NAME } ,
115+ [ SENTRY_SDK_VERSION ] : { type : 'string' , value : SDK_VERSION } ,
116+ [ SENTRY_SDK_INTEGRATIONS ] : { type : 'array' , value : expect . arrayContaining ( [ 'Http' ] ) } ,
117+ [ HTTP_REQUEST_METHOD ] : { type : 'string' , value : 'GET' } ,
118+ [ HTTP_ROUTE ] : { type : 'string' , value : '/test' } ,
119+ [ HTTP_RESPONSE_STATUS_CODE ] : { type : 'integer' , value : 200 } ,
120+ [ HTTP_RESPONSE_STATUS_TEXT ] : { type : 'string' , value : 'OK' } ,
121+ [ URL_FULL ] : { type : 'string' , value : expect . stringMatching ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ t e s t $ / ) } ,
122+ [ URL_PATH ] : { type : 'string' , value : '/test' } ,
123+ [ URL_SCHEME ] : { type : 'string' , value : 'http' } ,
124+ [ SERVER_ADDRESS ] : { type : 'string' , value : 'localhost' } ,
125+ [ SERVER_PORT ] : { type : 'integer' , value : expect . any ( Number ) } ,
126+ [ CLIENT_ADDRESS ] : { type : 'string' , value : expect . stringMatching ( LOOPBACK_ADDRESS ) } ,
127+ [ CLIENT_PORT ] : { type : 'integer' , value : expect . any ( Number ) } ,
128+ [ USER_IP_ADDRESS ] : { type : 'string' , value : expect . stringMatching ( LOOPBACK_ADDRESS ) } ,
129+ [ NETWORK_LOCAL_ADDRESS ] : { type : 'string' , value : expect . stringMatching ( LOOPBACK_ADDRESS ) } ,
130+ [ NETWORK_LOCAL_PORT ] : { type : 'integer' , value : expect . any ( Number ) } ,
131+ [ NETWORK_PEER_ADDRESS ] : { type : 'string' , value : expect . stringMatching ( LOOPBACK_ADDRESS ) } ,
132+ [ NETWORK_PEER_PORT ] : { type : 'integer' , value : expect . any ( Number ) } ,
133+ [ NETWORK_PROTOCOL_NAME ] : { type : 'string' , value : 'http' } ,
134+ [ NETWORK_PROTOCOL_VERSION ] : { type : 'string' , value : '1.1' } ,
135+ [ NETWORK_TRANSPORT ] : { type : 'string' , value : 'tcp' } ,
136+ [ USER_AGENT_ORIGINAL ] : { type : 'string' , value : 'node' } ,
137+ 'http.request.header.accept' : { type : 'array' , value : [ '*/*' ] } ,
138+ 'http.request.header.accept-encoding' : { type : 'array' , value : [ 'gzip, deflate' ] } ,
139+ 'http.request.header.accept-language' : { type : 'array' , value : [ '*' ] } ,
140+ 'http.request.header.connection' : { type : 'array' , value : [ 'keep-alive' ] } ,
141+ 'http.request.header.host' : { type : 'array' , value : [ expect . stringMatching ( / ^ l o c a l h o s t : \d + $ / ) ] } ,
142+ 'http.request.header.sec-fetch-mode' : { type : 'array' , value : [ 'cors' ] } ,
143+ 'http.request.header.user-agent' : { type : 'array' , value : [ 'node' ] } ,
144+ } ) ;
145+ for ( const attribute of Object . values ( hostContext ) ) {
146+ expect ( attribute ) . toEqual ( { type : expect . any ( String ) , value : expect . anything ( ) } ) ;
147+ }
49148 } ,
50149 } )
51150 . start ( ) ;
@@ -57,7 +156,7 @@ describe('httpIntegration', () => {
57156
58157 describe ( 'user callbacks that throw' , ( ) => {
59158 createEsmAndCjsTests ( __dirname , 'server.mjs' , 'instrument-throwing-callbacks.mjs' , ( createRunner , test ) => {
60- test ( 'still handles requests and sends the span when `onSpanCreated` and `ignoreIncomingRequests` throw' , async ( ) => {
159+ test ( 'still handles requests and sends the span when `onSpanCreated`, `onSpanEnd` and `ignoreIncomingRequests` throw' , async ( ) => {
61160 const runner = createRunner ( )
62161 . expect ( {
63162 span : {
0 commit comments