|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and other contributors where applicable. |
| 3 | + * Licensed under the BSD 2-Clause License; you may not use this file except in |
| 4 | + * compliance with the BSD 2-Clause License. |
| 5 | + */ |
| 6 | + |
| 7 | +'use strict' |
| 8 | + |
| 9 | +// Test that instrumentation of core modules works when required with the |
| 10 | +// 'node:' prefix. |
| 11 | + |
| 12 | +const { CapturingTransport } = require('../../../_capturing_transport') |
| 13 | + |
| 14 | +const apm = require('../../../..').start({ |
| 15 | + serviceName: 'test-node-prefixed-http-require', |
| 16 | + captureExceptions: false, |
| 17 | + metricsInterval: 0, |
| 18 | + centralConfig: false, |
| 19 | + cloudProvider: 'none', |
| 20 | + transport () { |
| 21 | + return new CapturingTransport() |
| 22 | + } |
| 23 | +}) |
| 24 | + |
| 25 | +try { |
| 26 | + var http = require('node:http') |
| 27 | +} catch (_requireErr) { |
| 28 | + console.log(`# SKIP node ${process.version} doesn't support require('node:...')`) |
| 29 | + process.exit() |
| 30 | +} |
| 31 | + |
| 32 | +const test = require('tape') |
| 33 | +const { findObjInArray } = require('../../../_utils') |
| 34 | + |
| 35 | +test('node:http instrumentation works', function (t) { |
| 36 | + var server = http.createServer(function (req, res) { |
| 37 | + res.end('pong') |
| 38 | + }) |
| 39 | + |
| 40 | + server.listen(function onListen () { |
| 41 | + const aTrans = apm.startTransaction('aTrans', 'manual') |
| 42 | + const port = server.address().port |
| 43 | + const url = 'http://localhost:' + port |
| 44 | + http.get(url, function (res) { |
| 45 | + res.resume() |
| 46 | + res.on('end', function () { |
| 47 | + aTrans.end() |
| 48 | + server.close() |
| 49 | + apm.flush(() => { |
| 50 | + const aTrans_ = findObjInArray(apm._transport.transactions, 'name', 'aTrans') |
| 51 | + const httpClientSpan = findObjInArray(apm._transport.spans, 'name', `GET localhost:${port}`) |
| 52 | + const httpServerTrans = findObjInArray(apm._transport.transactions, 'type', 'request') |
| 53 | + t.ok(aTrans_ && httpClientSpan && httpServerTrans, 'received the expected trace objs') |
| 54 | + t.equal(httpClientSpan.parent_id, aTrans_.id, 'http client span is a child of the manual trans') |
| 55 | + t.equal(httpServerTrans.parent_id, httpClientSpan.id, 'http server trans is a child of the http client span') |
| 56 | + t.end() |
| 57 | + }) |
| 58 | + }) |
| 59 | + }) |
| 60 | + }) |
| 61 | +}) |
0 commit comments