11// <reference lib="deno.ns" />
22
3- import { denoServeIntegration , init } from '@sentry/deno' ;
3+ import { denoServeIntegration , flush , init } from '@sentry/deno' ;
44import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts' ;
5- import { resetGlobals , transactionSink , withTimeout } from '../../src/index.ts' ;
5+ import { getSpanOp , resetGlobals , spanSink , withTimeout } from '../../src/index.ts' ;
66
77Deno . test ( 'Deno.serve captures incoming request bodies by default' , async ( ) => {
88 resetGlobals ( ) ;
9- const sink = transactionSink ( ) ;
9+ const sink = spanSink ( ) ;
1010
1111 init ( {
12- traceLifecycle : 'static' ,
1312 dsn : 'https://username@domain/123' ,
1413 tracesSampleRate : 1 ,
15- beforeSendTransaction : sink . beforeSendTransaction ,
14+ transport : sink . transport ,
1615 } ) ;
1716
1817 const requestBody = 'captured-by-default' ;
@@ -25,39 +24,45 @@ Deno.test('Deno.serve captures incoming request bodies by default', async () =>
2524 } ) ;
2625 await listening ;
2726
28- const transactionPromise = withTimeout (
29- sink . waitFor ( event => event . request ?. url ?. endsWith ( '/default' ) === true ) ,
30- 5_000 ,
31- 'transaction for /default' ,
32- ) ;
33- const response = await fetch ( `http://localhost:${ server . addr . port } /default` , {
34- method : 'POST' ,
35- headers : { 'content-type' : 'text/plain' } ,
36- body : requestBody ,
37- } ) ;
38- assertEquals ( await response . text ( ) , 'OK' ) ;
39-
40- const transaction = await transactionPromise ;
41- assertEquals ( transaction . request ?. data , requestBody ) ;
42-
43- abortController . abort ( ) ;
44- await server . finished ;
27+ try {
28+ const spanPromise = withTimeout (
29+ sink . waitFor (
30+ span =>
31+ span . is_segment && getSpanOp ( span ) === 'http.server' && span . attributes [ 'url.path' ] ?. value === '/default' ,
32+ ) ,
33+ 5_000 ,
34+ 'segment span for /default' ,
35+ ) ;
36+ const response = await fetch ( `http://localhost:${ server . addr . port } /default` , {
37+ method : 'POST' ,
38+ headers : { 'content-type' : 'text/plain' } ,
39+ body : requestBody ,
40+ } ) ;
41+ assertEquals ( await response . text ( ) , 'OK' ) ;
42+
43+ await flush ( ) ;
44+ const span = await spanPromise ;
45+ assertEquals ( span . name , 'POST' ) ;
46+ assertEquals ( span . attributes [ 'http.request.body.data' ] ?. value , requestBody ) ;
47+ } finally {
48+ abortController . abort ( ) ;
49+ await server . finished ;
50+ }
4551} ) ;
4652
4753Deno . test ( 'Deno.serve explicit small overrides disabled incoming request body collection' , async ( ) => {
4854 resetGlobals ( ) ;
49- const sink = transactionSink ( ) ;
55+ const sink = spanSink ( ) ;
5056
5157 init ( {
52- traceLifecycle : 'static' ,
5358 dsn : 'https://username@domain/123' ,
5459 tracesSampleRate : 1 ,
5560 dataCollection : { httpBodies : [ ] } ,
5661 integrations : integrations => [
5762 ...integrations . filter ( integration => integration . name !== 'DenoServe' ) ,
5863 denoServeIntegration ( { maxRequestBodySize : 'small' } ) ,
5964 ] ,
60- beforeSendTransaction : sink . beforeSendTransaction ,
65+ transport : sink . transport ,
6166 } ) ;
6267
6368 const requestBody = 'a' . repeat ( 1_001 ) ;
@@ -71,39 +76,47 @@ Deno.test('Deno.serve explicit small overrides disabled incoming request body co
7176 } ) ;
7277 await listening ;
7378
74- const transactionPromise = withTimeout (
75- sink . waitFor ( event => event . request ?. url ?. endsWith ( '/explicit-small' ) === true ) ,
76- 5_000 ,
77- 'transaction for /explicit-small' ,
78- ) ;
79- const response = await fetch ( `http://localhost:${ server . addr . port } /explicit-small` , {
80- method : 'POST' ,
81- headers : { 'content-type' : 'text/plain' } ,
82- body : requestBody ,
83- } ) ;
84- assertEquals ( await response . text ( ) , 'OK' ) ;
85-
86- const transaction = await transactionPromise ;
87- assertEquals ( transaction . request ?. data , expectedBody ) ;
88-
89- abortController . abort ( ) ;
90- await server . finished ;
79+ try {
80+ const spanPromise = withTimeout (
81+ sink . waitFor (
82+ span =>
83+ span . is_segment &&
84+ getSpanOp ( span ) === 'http.server' &&
85+ span . attributes [ 'url.path' ] ?. value === '/explicit-small' ,
86+ ) ,
87+ 5_000 ,
88+ 'segment span for /explicit-small' ,
89+ ) ;
90+ const response = await fetch ( `http://localhost:${ server . addr . port } /explicit-small` , {
91+ method : 'POST' ,
92+ headers : { 'content-type' : 'text/plain' } ,
93+ body : requestBody ,
94+ } ) ;
95+ assertEquals ( await response . text ( ) , 'OK' ) ;
96+
97+ await flush ( ) ;
98+ const span = await spanPromise ;
99+ assertEquals ( span . name , 'POST' ) ;
100+ assertEquals ( span . attributes [ 'http.request.body.data' ] ?. value , expectedBody ) ;
101+ } finally {
102+ abortController . abort ( ) ;
103+ await server . finished ;
104+ }
91105} ) ;
92106
93107Deno . test ( 'Deno.serve explicit none overrides enabled incoming request body collection' , async ( ) => {
94108 resetGlobals ( ) ;
95- const sink = transactionSink ( ) ;
109+ const sink = spanSink ( ) ;
96110
97111 init ( {
98- traceLifecycle : 'static' ,
99112 dsn : 'https://username@domain/123' ,
100113 tracesSampleRate : 1 ,
101114 dataCollection : { httpBodies : [ 'incomingRequest' ] } ,
102115 integrations : integrations => [
103116 ...integrations . filter ( integration => integration . name !== 'DenoServe' ) ,
104117 denoServeIntegration ( { maxRequestBodySize : 'none' } ) ,
105118 ] ,
106- beforeSendTransaction : sink . beforeSendTransaction ,
119+ transport : sink . transport ,
107120 } ) ;
108121
109122 const abortController = new AbortController ( ) ;
@@ -115,21 +128,30 @@ Deno.test('Deno.serve explicit none overrides enabled incoming request body coll
115128 } ) ;
116129 await listening ;
117130
118- const transactionPromise = withTimeout (
119- sink . waitFor ( event => event . request ?. url ?. endsWith ( '/explicit-none' ) === true ) ,
120- 5_000 ,
121- 'transaction for /explicit-none' ,
122- ) ;
123- const response = await fetch ( `http://localhost:${ server . addr . port } /explicit-none` , {
124- method : 'POST' ,
125- headers : { 'content-type' : 'text/plain' } ,
126- body : 'do-not-capture' ,
127- } ) ;
128- assertEquals ( await response . text ( ) , 'OK' ) ;
129-
130- const transaction = await transactionPromise ;
131- assertEquals ( transaction . request ?. data , undefined ) ;
132-
133- abortController . abort ( ) ;
134- await server . finished ;
131+ try {
132+ const spanPromise = withTimeout (
133+ sink . waitFor (
134+ span =>
135+ span . is_segment &&
136+ getSpanOp ( span ) === 'http.server' &&
137+ span . attributes [ 'url.path' ] ?. value === '/explicit-none' ,
138+ ) ,
139+ 5_000 ,
140+ 'segment span for /explicit-none' ,
141+ ) ;
142+ const response = await fetch ( `http://localhost:${ server . addr . port } /explicit-none` , {
143+ method : 'POST' ,
144+ headers : { 'content-type' : 'text/plain' } ,
145+ body : 'do-not-capture' ,
146+ } ) ;
147+ assertEquals ( await response . text ( ) , 'OK' ) ;
148+
149+ await flush ( ) ;
150+ const span = await spanPromise ;
151+ assertEquals ( span . name , 'POST' ) ;
152+ assertEquals ( span . attributes [ 'http.request.body.data' ] ?. value , undefined ) ;
153+ } finally {
154+ abortController . abort ( ) ;
155+ await server . finished ;
156+ }
135157} ) ;
0 commit comments