|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Test that http/https are not *double*-instrumented if http is loaded by |
| 18 | +// both `require` and `import`. |
| 19 | +// |
| 20 | +// This is a separate .cjs file to (a) more clearly use `require` and `import` |
| 21 | +// without .ts transpilation muddying the issue and (b) to separate this |
| 22 | +// file from being run with other `*.test.ts` test files in this package, |
| 23 | +// because this needs to be run in a separate process to avoid crosstalk. |
| 24 | + |
| 25 | +const assert = require('assert'); |
| 26 | +const path = require('path'); |
| 27 | +const fs = require('fs'); |
| 28 | + |
| 29 | +const { SpanKind } = require('@opentelemetry/api'); |
| 30 | +const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); |
| 31 | +const { |
| 32 | + InMemorySpanExporter, |
| 33 | + SimpleSpanProcessor, |
| 34 | +} = require('@opentelemetry/sdk-trace-base'); |
| 35 | + |
| 36 | +const { HttpInstrumentation } = require('../../build/src/index.js'); |
| 37 | + |
| 38 | +const memoryExporter = new InMemorySpanExporter(); |
| 39 | +const provider = new NodeTracerProvider({ |
| 40 | + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], |
| 41 | +}); |
| 42 | +const instrumentation = new HttpInstrumentation(); |
| 43 | +instrumentation.setTracerProvider(provider); |
| 44 | + |
| 45 | +/** |
| 46 | + * Each of the test cases expects two spans: the http(s) SERVER span and the |
| 47 | + * single http(s) CLIENT span. |
| 48 | + */ |
| 49 | +function assertTwoSpans(spans, opts) { |
| 50 | + assert.strictEqual(spans.length, 2); |
| 51 | + for (const span of spans) { |
| 52 | + assert.strictEqual( |
| 53 | + span.instrumentationScope.name, |
| 54 | + '@opentelemetry/instrumentation-http' |
| 55 | + ); |
| 56 | + if (opts.pathname) { |
| 57 | + assert.strictEqual(span.attributes['http.target'], opts.pathname); |
| 58 | + } |
| 59 | + } |
| 60 | + assert.strictEqual(spans[0].kind, SpanKind.SERVER); |
| 61 | + assert.strictEqual(spans[1].kind, SpanKind.CLIENT); |
| 62 | +} |
| 63 | + |
| 64 | +describe('HttpInstrumentation double-instr tests', function () { |
| 65 | + let rhttp; |
| 66 | + let ihttp; |
| 67 | + let rhttps; |
| 68 | + let ihttps; |
| 69 | + |
| 70 | + before(async () => { |
| 71 | + // arrange |
| 72 | + // This is the main thing being tested: does instr-http work when 'http' |
| 73 | + // has been loaded by both `require` and `import`. |
| 74 | + rhttp = require('http'); |
| 75 | + ihttp = await import('http'); |
| 76 | + rhttps = require('https'); |
| 77 | + ihttps = await import('https'); |
| 78 | + }); |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + memoryExporter.reset(); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('http', function () { |
| 85 | + let port; |
| 86 | + let server; |
| 87 | + |
| 88 | + before(done => { |
| 89 | + server = rhttp.createServer((req, res) => { |
| 90 | + req.resume(); |
| 91 | + req.on('end', () => { |
| 92 | + res.writeHead(200); |
| 93 | + res.end('pong'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + server.listen(0, '127.0.0.1', () => { |
| 98 | + port = server.address().port; |
| 99 | + assert.ok(Number.isInteger(port)); |
| 100 | + done(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + after(done => { |
| 105 | + server.close(done); |
| 106 | + }); |
| 107 | + |
| 108 | + it('one span for one http.request (require)', async function () { |
| 109 | + // act |
| 110 | + await new Promise(resolve => { |
| 111 | + const clientReq = rhttp.request( |
| 112 | + `http://127.0.0.1:${port}/rhttp.request`, |
| 113 | + clientRes => { |
| 114 | + clientRes.resume(); |
| 115 | + clientRes.on('end', resolve); |
| 116 | + } |
| 117 | + ); |
| 118 | + clientReq.end(); |
| 119 | + }); |
| 120 | + |
| 121 | + // assert |
| 122 | + const spans = memoryExporter.getFinishedSpans(); |
| 123 | + assertTwoSpans(spans, { pathname: '/rhttp.request' }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('one span for one http.request (import)', async function () { |
| 127 | + await new Promise(resolve => { |
| 128 | + const clientReq = ihttp.request( |
| 129 | + `http://127.0.0.1:${port}/ihttp.request`, |
| 130 | + clientRes => { |
| 131 | + clientRes.resume(); |
| 132 | + clientRes.on('end', resolve); |
| 133 | + } |
| 134 | + ); |
| 135 | + clientReq.end(); |
| 136 | + }); |
| 137 | + |
| 138 | + const spans = memoryExporter.getFinishedSpans(); |
| 139 | + assertTwoSpans(spans, { pathname: '/ihttp.request' }); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('https', function () { |
| 144 | + let port; |
| 145 | + let server; |
| 146 | + |
| 147 | + before(done => { |
| 148 | + server = rhttps.createServer( |
| 149 | + { |
| 150 | + key: fs.readFileSync( |
| 151 | + path.resolve(__dirname, '../fixtures/server-key.pem') |
| 152 | + ), |
| 153 | + cert: fs.readFileSync( |
| 154 | + path.resolve(__dirname, '../fixtures/server-cert.pem') |
| 155 | + ), |
| 156 | + }, |
| 157 | + (req, res) => { |
| 158 | + req.resume(); |
| 159 | + req.on('end', () => { |
| 160 | + res.writeHead(200); |
| 161 | + res.end('pong'); |
| 162 | + }); |
| 163 | + } |
| 164 | + ); |
| 165 | + |
| 166 | + server.listen(0, '127.0.0.1', () => { |
| 167 | + port = server.address().port; |
| 168 | + assert.ok(Number.isInteger(port)); |
| 169 | + done(); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + after(done => { |
| 174 | + server.close(done); |
| 175 | + }); |
| 176 | + |
| 177 | + it('one span for one https.request (require)', async function () { |
| 178 | + await new Promise(resolve => { |
| 179 | + const clientReq = rhttps.request( |
| 180 | + `https://127.0.0.1:${port}/rhttps.request`, |
| 181 | + { |
| 182 | + rejectUnauthorized: false, |
| 183 | + }, |
| 184 | + clientRes => { |
| 185 | + clientRes.resume(); |
| 186 | + clientRes.on('end', resolve); |
| 187 | + } |
| 188 | + ); |
| 189 | + clientReq.end(); |
| 190 | + }); |
| 191 | + |
| 192 | + const spans = memoryExporter.getFinishedSpans(); |
| 193 | + assertTwoSpans(spans, { pathname: '/rhttps.request' }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('one span for one https.request (import)', async function () { |
| 197 | + await new Promise(resolve => { |
| 198 | + const clientReq = ihttps.request( |
| 199 | + `https://127.0.0.1:${port}/ihttps.request`, |
| 200 | + { |
| 201 | + rejectUnauthorized: false, |
| 202 | + }, |
| 203 | + clientRes => { |
| 204 | + clientRes.resume(); |
| 205 | + clientRes.on('end', resolve); |
| 206 | + } |
| 207 | + ); |
| 208 | + clientReq.end(); |
| 209 | + }); |
| 210 | + |
| 211 | + const spans = memoryExporter.getFinishedSpans(); |
| 212 | + assertTwoSpans(spans, { pathname: '/ihttps.request' }); |
| 213 | + }); |
| 214 | + }); |
| 215 | +}); |
0 commit comments