|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const fs = require('node:fs') |
| 5 | +const path = require('node:path') |
| 6 | + |
| 7 | +const { unstable_dev: unstableDev } = require('wrangler') |
| 8 | + |
| 9 | +const { FakeAgent } = require('../helpers') |
| 10 | + |
| 11 | +const FIXTURES_DIR = path.join(__dirname, 'fixtures') |
| 12 | +const CONFIG_TEMPLATE_PATH = path.join(FIXTURES_DIR, 'wrangler.jsonc') |
| 13 | +const GENERATED_CONFIG_PATH = path.join(FIXTURES_DIR, 'wrangler.generated.json') |
| 14 | +const WORKER_PATH = path.join(FIXTURES_DIR, 'worker.mjs') |
| 15 | + |
| 16 | +/** |
| 17 | + * Resolves once the FakeAgent receives an OTLP traces POST, or rejects on timeout. |
| 18 | + * The Worker's export is fire-and-forget from workerd's perspective (see worker.mjs), |
| 19 | + * so an HTTP 200 from `worker.fetch()` proves nothing by itself — only this event proves |
| 20 | + * the span actually left the isolate over OTLP. |
| 21 | + * |
| 22 | + * @param {import('../helpers/fake-agent')} agent |
| 23 | + * @param {number} timeout |
| 24 | + * @returns {Promise<{ headers: Record<string, string>, payload: object }>} |
| 25 | + */ |
| 26 | +function waitForOtlpTraces (agent, timeout) { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + const timer = setTimeout(() => reject(new Error('Timeout waiting for OTLP traces')), timeout) |
| 29 | + agent.once('otlp-traces', (msg) => { |
| 30 | + clearTimeout(timer) |
| 31 | + resolve(msg) |
| 32 | + }) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +describe('Cloudflare Workers (workerd) acceptance test', function () { |
| 37 | + this.timeout(60_000) |
| 38 | + |
| 39 | + let agent |
| 40 | + let worker |
| 41 | + |
| 42 | + before(async () => { |
| 43 | + agent = await new FakeAgent().start() |
| 44 | + |
| 45 | + // wrangler.jsonc's "vars" are static, but the FakeAgent's port is only known at |
| 46 | + // runtime, so template the endpoint into a generated config used just for this run. |
| 47 | + const template = fs.readFileSync(CONFIG_TEMPLATE_PATH, 'utf8') |
| 48 | + const endpoint = `http://127.0.0.1:${agent.port}/v1/traces` |
| 49 | + fs.writeFileSync(GENERATED_CONFIG_PATH, template.replaceAll('__OTLP_ENDPOINT__', endpoint)) |
| 50 | + |
| 51 | + worker = await unstableDev(WORKER_PATH, { |
| 52 | + config: GENERATED_CONFIG_PATH, |
| 53 | + experimental: { disableExperimentalWarning: true }, |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + after(async () => { |
| 58 | + await worker?.stop() |
| 59 | + await agent?.stop() |
| 60 | + fs.rmSync(GENERATED_CONFIG_PATH, { force: true }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('loads, initializes, and exports a span over OTLP from inside real workerd', async () => { |
| 64 | + const tracesPromise = waitForOtlpTraces(agent, 15_000) |
| 65 | + |
| 66 | + const response = await worker.fetch('/') |
| 67 | + assert.strictEqual(response.status, 200) |
| 68 | + |
| 69 | + const { payload } = await tracesPromise |
| 70 | + |
| 71 | + const resourceSpan = payload.resourceSpans[0] |
| 72 | + const serviceNameAttribute = resourceSpan.resource.attributes.find( |
| 73 | + (attribute) => attribute.key === 'service.name' |
| 74 | + ) |
| 75 | + assert.deepStrictEqual(serviceNameAttribute.value, { stringValue: 'cf-workers-ci' }) |
| 76 | + |
| 77 | + const span = resourceSpan.scopeSpans[0].spans.find((candidate) => candidate.name === 'cf.worker.test') |
| 78 | + assert.ok(span, 'expected an OTLP span named "cf.worker.test"') |
| 79 | + }) |
| 80 | +}) |
0 commit comments