|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { initTracing, reset, getCurrent } from './node-tracing'; |
| 6 | +import { TRACER_NAME } from './node-tracing'; |
| 7 | +import { TracingOpts } from './config'; |
| 8 | +import http from 'http'; |
| 9 | + |
| 10 | +describe('#integration - OTLP exporter integration test', () => { |
| 11 | + let receivedSpans: any[] = []; |
| 12 | + |
| 13 | + let server: http.Server; |
| 14 | + let port: number; |
| 15 | + |
| 16 | + beforeAll((done) => { |
| 17 | + server = http.createServer((req, res) => { |
| 18 | + let body = ''; |
| 19 | + req.on('data', (chunk) => (body += chunk)); |
| 20 | + req.on('end', () => { |
| 21 | + try { |
| 22 | + // parse incoming payload and add it to receivedSpans |
| 23 | + // mock server just accepts any payload |
| 24 | + const parsed = JSON.parse(body); |
| 25 | + receivedSpans.push(parsed); |
| 26 | + res.writeHead(200); |
| 27 | + res.end(); |
| 28 | + } catch (err) { |
| 29 | + res.writeHead(500); |
| 30 | + res.end(); |
| 31 | + } |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + server.listen(0, () => { |
| 36 | + port = (server.address() as any).port; |
| 37 | + done(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + afterAll(() => { |
| 42 | + server.close(); |
| 43 | + }); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + receivedSpans = []; |
| 47 | + reset(); // reset global state |
| 48 | + }); |
| 49 | + |
| 50 | + it('sends a span to the OTLP collector', async () => { |
| 51 | + const logger = console; // or mock logger |
| 52 | + const opts: TracingOpts = { |
| 53 | + otel: { |
| 54 | + enabled: true, |
| 55 | + url: `http://0.0.0.0:${port}/v1/traces`, |
| 56 | + concurrencyLimit: 1, |
| 57 | + }, |
| 58 | + gcp: { enabled: false }, |
| 59 | + console: { enabled: false }, |
| 60 | + batchProcessor: false, // use SimpleSpanProcessor to flush immediately |
| 61 | + clientName: 'test', |
| 62 | + corsUrls: '', |
| 63 | + filterPii: false, |
| 64 | + sampleRate: 1, |
| 65 | + serviceName: 'test-service', |
| 66 | + }; |
| 67 | + |
| 68 | + initTracing(opts, logger); |
| 69 | + const tracer = getCurrent()?.getProvider().getTracer(TRACER_NAME); |
| 70 | + |
| 71 | + tracer?.startActiveSpan('test-span', (span) => { |
| 72 | + span.setAttribute('test-attribute', 'test-value'); |
| 73 | + span.addEvent('test-event', { key: 'value' }); |
| 74 | + span.end(); |
| 75 | + }); |
| 76 | + |
| 77 | + // Poll for receivedSpans to have a length > 0, up to 1 second |
| 78 | + const start = Date.now(); |
| 79 | + while (receivedSpans.length === 0 && Date.now() - start < 1000) { |
| 80 | + await new Promise((r) => setTimeout(r, 50)); |
| 81 | + } |
| 82 | + |
| 83 | + expect(receivedSpans.length).toBeGreaterThan(0); |
| 84 | + const request = receivedSpans[0]; |
| 85 | + |
| 86 | + expect(JSON.stringify(request)).toContain('test-span'); |
| 87 | + expect(JSON.stringify(request)).toContain('test-service'); |
| 88 | + }); |
| 89 | +}); |
0 commit comments