From 9a9cd7054f5a86e5615b2ee34f3615012036f0fb Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Thu, 25 Apr 2024 17:46:53 -0400 Subject: [PATCH 1/2] test(instrumentation-restify): add ESM test for restify --- .../test/fixtures/use-restify.mjs | 53 +++++++++++++++++++ .../test/restify.test.ts | 23 ++++++++ 2 files changed, 76 insertions(+) create mode 100644 plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.mjs diff --git a/plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.mjs b/plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.mjs new file mode 100644 index 0000000000..5285d2c3c3 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.mjs @@ -0,0 +1,53 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Use fastify from an ES module: +// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-restify.mjs + +import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; + +import { RestifyInstrumentation } from '../../build/src/index.js'; + +const sdk = createTestNodeSdk({ + serviceName: 'use-restify', + instrumentations: [ + new RestifyInstrumentation() + ] +}) +sdk.start(); + +import restify from 'restify'; +import http from 'http'; + +const app = restify.createServer(); +const PORT = 3000; + +app.get('/post/:id', (req, res, next) => { + res.send(`Post id: ${req.params.id}`); +}); +app.listen(PORT) + +await new Promise(resolve => { + http.get(`http://localhost:${PORT}/post/0`, (res) => { + res.resume(); + res.on('end', () => { + resolve(); + }); + }) +}); + +await app.close(); +await sdk.shutdown(); diff --git a/plugins/node/opentelemetry-instrumentation-restify/test/restify.test.ts b/plugins/node/opentelemetry-instrumentation-restify/test/restify.test.ts index bea8b196a0..69f8c9264f 100644 --- a/plugins/node/opentelemetry-instrumentation-restify/test/restify.test.ts +++ b/plugins/node/opentelemetry-instrumentation-restify/test/restify.test.ts @@ -23,6 +23,7 @@ import { InMemorySpanExporter, SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; +import * as testUtils from '@opentelemetry/contrib-test-utils'; import RestifyInstrumentation from '../src'; import * as types from '../src/internal-types'; @@ -580,4 +581,26 @@ describe('Restify Instrumentation', () => { ); }); }); + it('should work with ESM usage', async () => { + await testUtils.runTestFixture({ + cwd: __dirname, + argv: ['fixtures/use-restify.mjs'], + env: { + NODE_OPTIONS: + '--experimental-loader=@opentelemetry/instrumentation/hook.mjs', + NODE_NO_WARNINGS: '1', + }, + checkResult: (err, stdout, stderr) => { + assert.ifError(err); + }, + checkCollector: (collector: testUtils.TestCollector) => { + // use-restify.mjs creates a restify app with a 'GET /post/:id' endpoint, + // then makes a single 'GET /post/0' request. We expect to see a span like this: + // span 'request handler - /post/:id' + const spans = collector.sortedSpans; + assert.strictEqual(spans.length, 1); + assert.strictEqual(spans[0].name, 'request handler - /post/:id'); + }, + }); + }); }); From bdb89be9db7c1fa3f1a28b79b4e7fd8f35f40bc1 Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Thu, 25 Apr 2024 17:47:17 -0400 Subject: [PATCH 2/2] temp: show working cjs restify compared to mjs --- .../test/fixtures/use-restify.cjs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.cjs diff --git a/plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.cjs b/plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.cjs new file mode 100644 index 0000000000..f8d815a740 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-restify/test/fixtures/use-restify.cjs @@ -0,0 +1,55 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Use fastify from an ES module: +// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-restify.mjs + +const { createTestNodeSdk } = require('@opentelemetry/contrib-test-utils'); + +const { RestifyInstrumentation } = require('../../build/src/index.js'); + +const sdk = createTestNodeSdk({ + serviceName: 'use-restify', + instrumentations: [ + new RestifyInstrumentation() + ] +}) +sdk.start(); + +const restify = require('restify'); +const http = require('http'); + +const app = restify.createServer(); +const PORT = 3000; + +app.get('/post/:id', (req, res, next) => { + res.send(`Post id: ${req.params.id}`); +}); +app.listen(PORT) + +new Promise(resolve => { + http.get(`http://localhost:${PORT}/post/0`, (res) => { + res.resume(); + res.on('end', () => { + resolve(); + }); + }) +}).then(() => { + app.close(); +}).then(() => { + sdk.shutdown(); +}); +