Skip to content

Commit

Permalink
test(instrumentation-fastify): check for similarity with ROOT_CONTEXT…
Browse files Browse the repository at this point in the history
…, not reference equality

In the CI there will be 3 instances of the API installed(relative to the repo root):

- `./plugins/node/opentelemetry-instrumentation-fastify/node_modules/@opentelemetry/api`,
- `./node_modules/@opentelemetry/api`,
- `./plugins/node/opentelemetry-instrumentation-fastify/node_modules/@opentelemetry/instrumentation-http/node_modules/@opentelemetry/api`.
  • Loading branch information
rauno56 committed Sep 9, 2022
1 parent 84def58 commit 1e6bddf
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as assert from 'assert';
import { context, ROOT_CONTEXT, SpanStatusCode } from '@opentelemetry/api';
import { context, SpanStatusCode } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
Expand Down Expand Up @@ -72,6 +72,14 @@ import { FastifyInstance } from 'fastify/types/instance';

const Fastify = require('fastify');

const assertRootContextActive = () => {
// Asserting the context.active() to strictly equal ROOT_CONTEXT doesn't
// always work because of the linking and dep resolution.
// Specially in our CI environment there can be multiple instances to
// different @opentelemetry/api and thus ROOT_CONTEXTs in the tree.
assert.strictEqual((context.active() as any)['_currentContext'].size, 0);
};

function getSpans(): ReadableSpan[] {
const spans = memoryExporter.getFinishedSpans().filter(s => {
return (
Expand Down Expand Up @@ -367,9 +375,14 @@ describe('fastify', () => {
});

describe('application hooks', () => {
afterEach(() => {
const spans = getSpans();
assert.strictEqual(spans.length, 0);
});

it('onRoute not instrumented', async () => {
app.addHook('onRoute', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
assertRootContextActive();
});
// add a route to trigger the 'onRoute' hook
app.get('/test', (_req: FastifyRequest, reply: FastifyReply) => {
Expand All @@ -381,7 +394,7 @@ describe('fastify', () => {

it('onRegister is not instrumented', async () => {
app.addHook('onRegister', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
assertRootContextActive();
});
// register a plugin to trigger 'onRegister' hook
app.register((fastify, options, done) => {
Expand All @@ -393,19 +406,18 @@ describe('fastify', () => {

it('onReady is not instrumented', async () => {
app.addHook('onReady', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
assertRootContextActive();
});

await startServer();
});

it('onClose is not instrumented', async () => {
app.addHook('onClose', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
assertRootContextActive();
});

await startServer();
await app.close();
});
});
});
Expand Down

0 comments on commit 1e6bddf

Please sign in to comment.