Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 46b5e9d

Browse files
author
Amir Blum
authored
test: use mocha plugin for otel setup (#123)
1 parent 0ffb67e commit 46b5e9d

File tree

32 files changed

+522
-481
lines changed

32 files changed

+522
-481
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ yarn.lock
1919

2020
# typescript
2121
*.tsbuildinfo
22-
*.DS_STORE*
22+
*.DS_STORE*
23+
24+
# aspecto
25+
aspecto.json
26+
aspecto.log

packages/instrumentation-amqplib/package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
"scripts": {
2525
"build": "tsc",
2626
"prepare": "yarn run build",
27-
"test": "mocha",
27+
"test": "mocha --require opentelemetry-instrumentation-testing-utils",
28+
"test:jaeger": "OTEL_EXPORTER_JAEGER_AGENT_HOST=localhost mocha --require opentelemetry-instrumentation-testing-utils",
2829
"watch": "tsc -w",
2930
"version:update": "node ../../scripts/version-update.js",
30-
"version": "yarn run version:update",
3131
"test-all-versions": "tav",
3232
"test:ci": "yarn test-all-versions",
33+
"version": "yarn run version:update",
3334
"test:docker:run": "docker run -d --hostname demo-amqplib-rabbit --name amqplib-unittests -p 22221:5672 rabbitmq:3"
3435
},
3536
"bugs": {
@@ -42,8 +43,6 @@
4243
},
4344
"devDependencies": {
4445
"@opentelemetry/core": "^0.19.0",
45-
"@opentelemetry/node": "^0.19.0",
46-
"@opentelemetry/tracing": "^0.19.0",
4746
"@types/amqplib": "^0.5.17",
4847
"@types/lodash": "^4.14.168",
4948
"@types/mocha": "^8.2.2",
@@ -52,6 +51,8 @@
5251
"expect": "^26.6.2",
5352
"lodash": "^4.17.21",
5453
"mocha": "^8.4.0",
54+
"opentelemetry-instrumentation-mocha": "^0.0.1-rc.0",
55+
"opentelemetry-instrumentation-testing-utils": "^0.4.0",
5556
"sinon": "^9.2.4",
5657
"test-all-versions": "^5.0.1",
5758
"ts-node": "^9.1.1",
@@ -62,6 +63,9 @@
6263
"ts"
6364
],
6465
"spec": "test/**/*.spec.ts",
65-
"require": "ts-node/register"
66+
"require": [
67+
"ts-node/register",
68+
"opentelemetry-instrumentation-mocha"
69+
]
6670
}
6771
}

packages/instrumentation-amqplib/test/amqplib-callbacks.spec.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
import 'mocha';
22
import expect from 'expect';
3-
import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
4-
import { NodeTracerProvider } from '@opentelemetry/node';
5-
import { HttpTraceContext } from '@opentelemetry/core';
63
import { AmqplibInstrumentation } from '../src';
74

85
const instrumentation = new AmqplibInstrumentation();
96
instrumentation.enable();
107
import amqpCallback from 'amqplib/callback_api';
118
import { MessagingDestinationKindValues, SemanticAttributes } from '@opentelemetry/semantic-conventions';
12-
import { propagation, SpanKind } from '@opentelemetry/api';
9+
import { SpanKind, context } from '@opentelemetry/api';
1310
import { asyncConsume } from './utils';
11+
import { TEST_RABBITMQ_HOST, TEST_RABBITMQ_PORT } from './config';
12+
import { getTestSpans } from 'opentelemetry-instrumentation-testing-utils';
1413

1514
const msgPayload = 'payload from test';
1615
const queueName = 'queue-name-from-unittest';
1716

1817
describe('amqplib instrumentation callback model', function () {
19-
const provider = new NodeTracerProvider();
20-
const memoryExporter = new InMemorySpanExporter();
21-
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
22-
propagation.setGlobalPropagator(new HttpTraceContext());
23-
provider.addSpanProcessor(spanProcessor);
24-
instrumentation.setTracerProvider(provider);
25-
26-
const url = 'amqp://localhost:22221';
18+
const url = `amqp://${TEST_RABBITMQ_HOST}:${TEST_RABBITMQ_PORT}`;
2719
let conn: amqpCallback.Connection;
2820
before((done) => {
2921
amqpCallback.connect(url, (err, connection) => {
@@ -38,19 +30,27 @@ describe('amqplib instrumentation callback model', function () {
3830

3931
let channel: amqpCallback.Channel;
4032
beforeEach((done) => {
41-
memoryExporter.reset();
4233
instrumentation.enable();
43-
conn.createChannel((err, c) => {
44-
channel = c;
45-
// install an error handler, otherwise when we have tests that create error on the channel,
46-
// it throws and crash process
47-
channel.on('error', () => {});
48-
channel.assertQueue(queueName, { durable: false }, (err, ok) => {
49-
channel.purgeQueue(queueName, (err, ok) => {
50-
done();
51-
});
52-
});
53-
});
34+
conn.createChannel(
35+
context.bind((err, c) => {
36+
channel = c;
37+
// install an error handler, otherwise when we have tests that create error on the channel,
38+
// it throws and crash process
39+
channel.on('error', () => {});
40+
channel.assertQueue(
41+
queueName,
42+
{ durable: false },
43+
context.bind((err, ok) => {
44+
channel.purgeQueue(
45+
queueName,
46+
context.bind((err, ok) => {
47+
done();
48+
})
49+
);
50+
})
51+
);
52+
})
53+
);
5454
});
5555

5656
afterEach((done) => {
@@ -69,7 +69,7 @@ describe('amqplib instrumentation callback model', function () {
6969
asyncConsume(channel, queueName, [(msg) => expect(msg.content.toString()).toEqual(msgPayload)], {
7070
noAck: true,
7171
}).then(() => {
72-
const [publishSpan, consumeSpan] = memoryExporter.getFinishedSpans();
72+
const [publishSpan, consumeSpan] = getTestSpans();
7373

7474
// assert publish span
7575
expect(publishSpan.kind).toEqual(SpanKind.PRODUCER);
@@ -82,8 +82,8 @@ describe('amqplib instrumentation callback model', function () {
8282
expect(publishSpan.attributes[SemanticAttributes.MESSAGING_PROTOCOL]).toEqual('AMQP');
8383
expect(publishSpan.attributes[SemanticAttributes.MESSAGING_PROTOCOL_VERSION]).toEqual('0.9.1');
8484
expect(publishSpan.attributes[SemanticAttributes.MESSAGING_URL]).toEqual(url);
85-
expect(publishSpan.attributes[SemanticAttributes.NET_PEER_NAME]).toEqual('localhost');
86-
expect(publishSpan.attributes[SemanticAttributes.NET_PEER_PORT]).toEqual(22221);
85+
expect(publishSpan.attributes[SemanticAttributes.NET_PEER_NAME]).toEqual(TEST_RABBITMQ_HOST);
86+
expect(publishSpan.attributes[SemanticAttributes.NET_PEER_PORT]).toEqual(TEST_RABBITMQ_PORT);
8787

8888
// assert consume span
8989
expect(consumeSpan.kind).toEqual(SpanKind.CONSUMER);
@@ -96,8 +96,8 @@ describe('amqplib instrumentation callback model', function () {
9696
expect(consumeSpan.attributes[SemanticAttributes.MESSAGING_PROTOCOL]).toEqual('AMQP');
9797
expect(consumeSpan.attributes[SemanticAttributes.MESSAGING_PROTOCOL_VERSION]).toEqual('0.9.1');
9898
expect(consumeSpan.attributes[SemanticAttributes.MESSAGING_URL]).toEqual(url);
99-
expect(consumeSpan.attributes[SemanticAttributes.NET_PEER_NAME]).toEqual('localhost');
100-
expect(consumeSpan.attributes[SemanticAttributes.NET_PEER_PORT]).toEqual(22221);
99+
expect(consumeSpan.attributes[SemanticAttributes.NET_PEER_NAME]).toEqual(TEST_RABBITMQ_HOST);
100+
expect(consumeSpan.attributes[SemanticAttributes.NET_PEER_PORT]).toEqual(TEST_RABBITMQ_PORT);
101101

102102
// assert context propagation
103103
expect(consumeSpan.spanContext.traceId).toEqual(publishSpan.spanContext.traceId);
@@ -112,7 +112,7 @@ describe('amqplib instrumentation callback model', function () {
112112

113113
asyncConsume(channel, queueName, [(msg) => channel.ack(msg)]).then(() => {
114114
// assert consumed message span has ended
115-
expect(memoryExporter.getFinishedSpans().length).toBe(2);
115+
expect(getTestSpans().length).toBe(2);
116116
done();
117117
});
118118
});
@@ -124,7 +124,7 @@ describe('amqplib instrumentation callback model', function () {
124124
(msg) =>
125125
setTimeout(() => {
126126
channel.ack(msg);
127-
expect(memoryExporter.getFinishedSpans().length).toBe(2);
127+
expect(getTestSpans().length).toBe(2);
128128
done();
129129
}, 1),
130130
]);

0 commit comments

Comments
 (0)