@@ -46,12 +46,33 @@ interface CLIOutput {
4646 success : boolean
4747 error ?: string
4848 sentBatches : number
49+ eventResults : EventResult [ ]
50+ httpLog: HttpLogEntry [ ]
51+ }
52+
53+ interface EventResult {
54+ event: string
55+ type: string
56+ delivered: boolean
57+ failureReason ? : string
58+ }
59+
60+ interface HttpLogEntry {
61+ timestamp: string
62+ status: number
63+ url: string
64+ retryCount ? : number
65+ bodyPreview: string
4966}
5067
5168function sleep ( ms : number ) : Promise < void > {
5269 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
5370}
5471
72+ function elapsed ( start : number ) : string {
73+ return `${ ( ( Date . now ( ) - start ) / 1000 ) . toFixed ( 1 ) } s`
74+ }
75+
5576function sendEvent ( analytics : Analytics , event : AnalyticsEvent ) : void {
5677 // SDK requires either userId or anonymousId
5778 const identity = event . userId
@@ -117,7 +138,13 @@ function sendEvent(analytics: Analytics, event: AnalyticsEvent): void {
117138}
118139
119140async function main ( ) : Promise < void > {
120- const output : CLIOutput = { success : false , sentBatches : 0 }
141+ const startTime = Date . now ( )
142+ const output : CLIOutput = {
143+ success : false ,
144+ sentBatches : 0 ,
145+ eventResults : [ ] ,
146+ httpLog : [ ] ,
147+ }
121148
122149 try {
123150 // Parse --input argument
@@ -147,36 +174,82 @@ async function main(): Promise<void> {
147174 const msg =
148175 reason instanceof Error ? reason . message : String ( reason ?? err . code )
149176 deliveryErrors . push ( msg )
177+ process . stderr . write ( `[${ elapsed ( startTime ) } ] ERROR: ${ msg } \n` )
178+ } )
179+
180+ analytics . on ( 'http_request' , ( req ) => {
181+ const body =
182+ typeof req . body === 'string' ? JSON . parse ( req . body ) : req . body
183+ const events = body . batch ?. map ( ( e : any ) => e . event ?? e . type ) ?? [ ]
184+ const retryHeader = req . headers ?. [ 'X-Retry-Count' ]
185+ process . stderr . write (
186+ `[${ elapsed ( startTime ) } ] >> ${ req . method } events=[${ events . join ( ',' ) } ]${
187+ retryHeader ? ` retry=${ retryHeader } ` : ''
188+ } \n`
189+ )
190+ } )
191+
192+ analytics . on ( 'http_response' , ( res ) => {
193+ const body =
194+ typeof res . body === 'string' ? JSON . parse ( res . body ) : res . body
195+ const events = body . batch ?. map ( ( e : any ) => e . event ?? e . type ) ?? [ ]
196+ const retryAfter = res . headers ?. [ 'retry-after' ]
197+ process . stderr . write (
198+ `[${ elapsed ( startTime ) } ] << ${ res . status } events=[${ events . join ( ',' ) } ]${
199+ retryAfter ? ` retry-after=${ retryAfter } ` : ''
200+ } \n`
201+ )
150202 } )
151203
152204 // Process event sequences
205+ let totalEvents = 0
153206 for ( const seq of sequences ) {
154207 if ( seq . delayMs > 0 ) {
208+ process . stderr . write (
209+ `[${ elapsed ( startTime ) } ] sleeping ${ seq . delayMs } ms...\n`
210+ )
155211 await sleep ( seq . delayMs )
156212 }
157213
158214 for ( const event of seq . events ) {
215+ totalEvents ++
216+ const label = event . event ?? event . type
217+ process . stderr . write (
218+ `[${ elapsed ( startTime ) } ] enqueue #${ totalEvents } : ${ label } \n`
219+ )
159220 sendEvent ( analytics , event )
160221 }
161222 }
162223
163- // Flush and close — use a generous timeout so retries with exponential
164- // backoff have time to complete (default is flushInterval * 1.25)
165- const timeoutMs = ( config . timeout ?? 60 ) * 1000
224+ process . stderr . write (
225+ `[${ elapsed (
226+ startTime
227+ ) } ] all ${ totalEvents } events enqueued, calling closeAndFlush...\n`
228+ )
229+
230+ // Flush and close
231+ const timeoutMs = ( config . timeout ?? 75 ) * 1000
166232 await analytics . closeAndFlush ( { timeout : timeoutMs } )
167233
234+ process . stderr . write (
235+ `[${ elapsed ( startTime ) } ] closeAndFlush resolved. errors=${
236+ deliveryErrors . length
237+ } \n`
238+ )
239+
168240 if ( deliveryErrors . length > 0 ) {
169241 output . success = false
170242 output . error = deliveryErrors [ 0 ]
171243 } else {
172244 output . success = true
173- output . sentBatches = 1
245+ output . sentBatches = output . httpLog . length
174246 }
175247 } catch ( err ) {
176248 output . error = err instanceof Error ? err . message : String ( err )
249+ process . stderr . write ( `[${ elapsed ( startTime ) } ] FATAL: ${ output . error } \n` )
177250 }
178251
179- console.log(JSON.stringify(output))
252+ console . log ( JSON . stringify ( output , null , 2 ) )
180253 process . exit ( output . success ? 0 : 1 )
181254}
182255
0 commit comments