Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(instrumentation-restify): add ESM test for restify #2149

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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();
});

@@ -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();
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
},
});
});
});