|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { spawnSync } from 'child_process'; |
| 17 | +import { execFile, PromiseWithChild } from 'child_process'; |
18 | 18 | import * as assert from 'assert';
|
| 19 | +import { promisify } from 'util'; |
| 20 | +import { Readable } from 'stream'; |
19 | 21 |
|
20 |
| -describe('Register', function () { |
21 |
| - it('can load auto instrumentation from command line', () => { |
22 |
| - const proc = spawnSync( |
23 |
| - process.execPath, |
24 |
| - ['--require', '../build/src/register.js', './test-app/app.js'], |
25 |
| - { |
26 |
| - cwd: __dirname, |
27 |
| - timeout: 5000, |
28 |
| - killSignal: 'SIGKILL', // SIGTERM is not sufficient to terminate some hangs |
29 |
| - env: Object.assign({}, process.env, { |
30 |
| - OTEL_NODE_RESOURCE_DETECTORS: 'none', |
31 |
| - OTEL_TRACES_EXPORTER: 'console', |
32 |
| - // nx (used by lerna run) defaults `FORCE_COLOR=true`, which in |
33 |
| - // node v18.17.0, v20.3.0 and later results in ANSI color escapes |
34 |
| - // in the ConsoleSpanExporter output that is checked below. |
35 |
| - FORCE_COLOR: '0', |
36 |
| - }), |
| 22 | +const execFilePromise = promisify(execFile); |
| 23 | + |
| 24 | +function runWithRegister(path: string): PromiseWithChild<{ |
| 25 | + stdout: string; |
| 26 | + stderr: string; |
| 27 | +}> { |
| 28 | + return execFilePromise( |
| 29 | + process.execPath, |
| 30 | + ['--require', '../build/src/register.js', path], |
| 31 | + { |
| 32 | + cwd: __dirname, |
| 33 | + timeout: 1500, |
| 34 | + killSignal: 'SIGKILL', // SIGTERM is not sufficient to terminate some hangs |
| 35 | + env: Object.assign({}, process.env, { |
| 36 | + OTEL_NODE_RESOURCE_DETECTORS: 'none', |
| 37 | + OTEL_TRACES_EXPORTER: 'console', |
| 38 | + OTEL_LOG_LEVEL: 'debug', |
| 39 | + // nx (used by lerna run) defaults `FORCE_COLOR=true`, which in |
| 40 | + // node v18.17.0, v20.3.0 and later results in ANSI color escapes |
| 41 | + // in the ConsoleSpanExporter output that is checked below. |
| 42 | + FORCE_COLOR: '0', |
| 43 | + }), |
| 44 | + } |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +function waitForString(stream: Readable, str: string): Promise<void> { |
| 49 | + return new Promise((resolve, reject) => { |
| 50 | + function check(chunk: Buffer): void { |
| 51 | + if (chunk.includes(str)) { |
| 52 | + resolve(); |
| 53 | + stream.off('data', check); |
37 | 54 | }
|
| 55 | + } |
| 56 | + stream.on('data', check); |
| 57 | + stream.on('close', () => |
| 58 | + reject(`Stream closed without ever seeing "${str}"`) |
| 59 | + ); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +describe('Register', function () { |
| 64 | + it('can load auto instrumentation from command line', async () => { |
| 65 | + const runPromise = runWithRegister('./test-app/app.js'); |
| 66 | + const { child } = runPromise; |
| 67 | + const { stdout } = await runPromise; |
| 68 | + assert.equal(child.exitCode, 0, `child.exitCode (${child.exitCode})`); |
| 69 | + assert.equal( |
| 70 | + child.signalCode, |
| 71 | + null, |
| 72 | + `child.signalCode (${child.signalCode})` |
38 | 73 | );
|
39 |
| - assert.ifError(proc.error); |
40 |
| - assert.equal(proc.status, 0, `proc.status (${proc.status})`); |
41 |
| - assert.equal(proc.signal, null, `proc.signal (${proc.signal})`); |
42 | 74 |
|
43 | 75 | assert.ok(
|
44 |
| - proc.stdout.includes( |
| 76 | + stdout.includes( |
45 | 77 | 'OpenTelemetry automatic instrumentation started successfully'
|
46 | 78 | )
|
47 | 79 | );
|
48 | 80 |
|
| 81 | + assert.ok( |
| 82 | + stdout.includes('OpenTelemetry SDK terminated'), |
| 83 | + `Process output was missing message indicating successful shutdown, got stdout:\n${stdout}` |
| 84 | + ); |
| 85 | + |
49 | 86 | // Check a span has been generated for the GET request done in app.js
|
| 87 | + assert.ok(stdout.includes("name: 'GET'"), 'console span output in stdout'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('shuts down the NodeSDK when SIGTERM is received', async () => { |
| 91 | + const runPromise = runWithRegister('./test-app/app-server.js'); |
| 92 | + const { child } = runPromise; |
| 93 | + await waitForString(child.stdout!, 'Finshed request'); |
| 94 | + child.kill('SIGTERM'); |
| 95 | + const { stdout } = await runPromise; |
| 96 | + |
50 | 97 | assert.ok(
|
51 |
| - proc.stdout.includes("name: 'GET'"), |
52 |
| - 'console span output in stdout' |
| 98 | + stdout.includes('OpenTelemetry SDK terminated'), |
| 99 | + `Process output was missing message indicating successful shutdown, got stdout:\n${stdout}` |
53 | 100 | );
|
| 101 | + |
| 102 | + // Check a span has been generated for the GET request done in app.js |
| 103 | + assert.ok(stdout.includes("name: 'GET'"), 'console span output in stdout'); |
54 | 104 | });
|
55 | 105 | });
|
0 commit comments