File
Blob: src/workerd/api/tests/tail-worker-test-receiver.js
| 1 | // Copyright (c) 2017-2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import * as assert from 'node:assert'; |
| 5 | |
| 6 | let resposeMap = new Map(); |
| 7 | |
| 8 | export default { |
| 9 | // https://developers.cloudflare.com/workers/observability/logs/tail-workers/ |
| 10 | tailStream(event, env, ctx) { |
| 11 | // Onset event, must be singleton |
| 12 | |
| 13 | // Set inner spanId to zero to have deterministic output. |
| 14 | event.event.spanId = '0000000000000000'; |
| 15 | |
| 16 | resposeMap.set(event.spanContext.traceId, JSON.stringify(event.event)); |
| 17 | return (event) => { |
| 18 | let cons = resposeMap.get(event.spanContext.traceId); |
| 19 | resposeMap.set( |
| 20 | event.spanContext.traceId, |
| 21 | cons + JSON.stringify(event.event) |
| 22 | ); |
| 23 | }; |
| 24 | }, |
| 25 | }; |
| 26 | |
| 27 | export const test = { |
| 28 | async test() { |
| 29 | // HACK: The prior tests terminates once the scheduled() invocation has returned a response, but |
| 30 | // propagating the outcome of the invocation may take longer. Wait briefly so this can go ahead. |
| 31 | await scheduler.wait(50); |
| 32 | |
| 33 | // The shared tail worker we configured only produces onset and outcome events, so every trace is identical here. |
| 34 | // Number of traces based on how often main tail worker is invoked from previous tests |
| 35 | let numTraces = 33; |
| 36 | let basicTrace = |
| 37 | '{"type":"onset","executionModel":"stateless","spanId":"0000000000000000","scriptTags":[],"info":{"type":"trace","traces":[]}}{"type":"return"}{"type":"outcome","outcome":"ok","cpuTime":0,"wallTime":0}'; |
| 38 | assert.deepStrictEqual( |
| 39 | Array.from(resposeMap.values()), |
| 40 | Array.from({ length: numTraces }, () => basicTrace) |
| 41 | ); |
| 42 | }, |
| 43 | }; |