Skip to content

Commit 2ac08ff

Browse files
authored
test: stop using the obsolete AsyncHooksContextManager in tests (#2864)
Since support for Node.js <14.8.0 was dropped, the modern AsyncLocalStorageContextManager can always be used. This was done for runtime code in open-telemetry/opentelemetry-js#4341 This PR does this for tests that were explicitly setting a context manager.
1 parent 6f65523 commit 2ac08ff

File tree

36 files changed

+167
-115
lines changed

36 files changed

+167
-115
lines changed

plugins/node/instrumentation-cucumber/test/cucumber.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { context, SpanStatusCode } from '@opentelemetry/api';
1818
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
19-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
19+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2020
import {
2121
InMemorySpanExporter,
2222
SimpleSpanProcessor,
@@ -61,7 +61,7 @@ describe('CucumberInstrumentation', () => {
6161
}),
6262
spanProcessors: [spanProcessor],
6363
});
64-
const contextManager = new AsyncHooksContextManager().enable();
64+
const contextManager = new AsyncLocalStorageContextManager().enable();
6565

6666
before(() => {
6767
instrumentation.setTracerProvider(provider);

plugins/node/instrumentation-dataloader/test/dataloader.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from '@opentelemetry/sdk-trace-base';
2121
import { context, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api';
2222
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
23-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
23+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2424

2525
import { DataloaderInstrumentation } from '../src';
2626
const instrumentation = new DataloaderInstrumentation();
@@ -34,7 +34,7 @@ import * as Dataloader from 'dataloader';
3434

3535
describe('DataloaderInstrumentation', () => {
3636
let dataloader: Dataloader<string, number>;
37-
let contextManager: AsyncHooksContextManager;
37+
let contextManager: AsyncLocalStorageContextManager;
3838

3939
const memoryExporter = new InMemorySpanExporter();
4040
const provider = new NodeTracerProvider({
@@ -47,7 +47,7 @@ describe('DataloaderInstrumentation', () => {
4747

4848
beforeEach(async () => {
4949
instrumentation.enable();
50-
contextManager = new AsyncHooksContextManager();
50+
contextManager = new AsyncLocalStorageContextManager();
5151
context.setGlobalContextManager(contextManager.enable());
5252
dataloader = new Dataloader(async keys => keys.map((_, idx) => idx), {
5353
cache: false,

plugins/node/instrumentation-fs/test/fs.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { context, trace } from '@opentelemetry/api';
17-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
17+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
1818
import {
1919
BasicTracerProvider,
2020
InMemorySpanExporter,
@@ -65,12 +65,12 @@ const provider = new BasicTracerProvider({
6565
const tracer = provider.getTracer('default');
6666

6767
describe('fs instrumentation', () => {
68-
let contextManager: AsyncHooksContextManager;
68+
let contextManager: AsyncLocalStorageContextManager;
6969
let fs: typeof FSType;
7070
let plugin: FsInstrumentation;
7171

7272
beforeEach(async () => {
73-
contextManager = new AsyncHooksContextManager();
73+
contextManager = new AsyncLocalStorageContextManager();
7474
context.setGlobalContextManager(contextManager.enable());
7575
plugin = new FsInstrumentation(pluginConfig);
7676
plugin.setTracerProvider(provider);

plugins/node/instrumentation-fs/test/fsPromises.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { context, trace } from '@opentelemetry/api';
17-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
17+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
1818
import {
1919
BasicTracerProvider,
2020
InMemorySpanExporter,
@@ -44,12 +44,12 @@ const provider = new BasicTracerProvider({
4444
const tracer = provider.getTracer('default');
4545

4646
describe('fs/promises instrumentation', () => {
47-
let contextManager: AsyncHooksContextManager;
47+
let contextManager: AsyncLocalStorageContextManager;
4848
let fsPromises: typeof FSPromisesType;
4949
let plugin: FsInstrumentation;
5050

5151
beforeEach(async () => {
52-
contextManager = new AsyncHooksContextManager();
52+
contextManager = new AsyncLocalStorageContextManager();
5353
context.setGlobalContextManager(contextManager.enable());
5454
plugin = new FsInstrumentation(pluginConfig);
5555
plugin.setTracerProvider(provider);

plugins/node/instrumentation-fs/test/parent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as assert from 'assert';
2323
import type * as FSType from 'fs';
2424
import type { FsInstrumentationConfig } from '../src/types';
2525
import * as api from '@opentelemetry/api';
26-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
26+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2727

2828
const memoryExporter = new InMemorySpanExporter();
2929
const provider = new BasicTracerProvider({
@@ -49,7 +49,7 @@ describe('fs instrumentation: requireParentSpan', () => {
4949
};
5050

5151
beforeEach(() => {
52-
const contextManager = new AsyncHooksContextManager();
52+
const contextManager = new AsyncLocalStorageContextManager();
5353
api.context.setGlobalContextManager(contextManager.enable());
5454
});
5555

plugins/node/instrumentation-tedious/test/instrumentation.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api';
18-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
18+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
1919
import {
2020
DBSYSTEMVALUES_MSSQL,
2121
SEMATTRS_DB_NAME,
@@ -87,7 +87,7 @@ const incompatVersions =
8787

8888
describe('tedious', () => {
8989
let tedious: any;
90-
let contextManager: AsyncHooksContextManager;
90+
let contextManager: AsyncLocalStorageContextManager;
9191
let connection: Connection;
9292
const memoryExporter = new InMemorySpanExporter();
9393
const provider = new BasicTracerProvider({
@@ -124,7 +124,7 @@ describe('tedious', () => {
124124
// connecting often takes more time even if the DB is running locally
125125
this.timeout(10000);
126126
instrumentation.disable();
127-
contextManager = new AsyncHooksContextManager().enable();
127+
contextManager = new AsyncLocalStorageContextManager().enable();
128128
context.setGlobalContextManager(contextManager);
129129
instrumentation.setTracerProvider(provider);
130130
instrumentation.enable();

plugins/node/instrumentation-undici/test/fetch.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
propagation,
2323
trace,
2424
} from '@opentelemetry/api';
25-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
25+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2626
import {
2727
InMemorySpanExporter,
2828
SimpleSpanProcessor,
@@ -59,7 +59,9 @@ describe('UndiciInstrumentation `fetch` tests', function () {
5959
instrumentation.setTracerProvider(provider);
6060

6161
propagation.setGlobalPropagator(new MockPropagation());
62-
context.setGlobalContextManager(new AsyncHooksContextManager().enable());
62+
context.setGlobalContextManager(
63+
new AsyncLocalStorageContextManager().enable()
64+
);
6365
mockServer.start(done);
6466
mockServer.mockListener((req, res) => {
6567
// There are some situations where there is no way to access headers

plugins/node/instrumentation-undici/test/metrics.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import * as assert from 'assert';
1717

1818
import { context, propagation } from '@opentelemetry/api';
19-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
19+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2020
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2121
import {
2222
AggregationTemporality,
@@ -58,7 +58,9 @@ describe('UndiciInstrumentation metrics tests', function () {
5858
instrumentation.setTracerProvider(provider);
5959
instrumentation.setMeterProvider(meterProvider);
6060

61-
context.setGlobalContextManager(new AsyncHooksContextManager().enable());
61+
context.setGlobalContextManager(
62+
new AsyncLocalStorageContextManager().enable()
63+
);
6264
mockServer.start(done);
6365
mockServer.mockListener((req, res) => {
6466
// Return a valid response always

plugins/node/instrumentation-undici/test/undici.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
propagation,
2525
trace,
2626
} from '@opentelemetry/api';
27-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
27+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2828
import {
2929
InMemorySpanExporter,
3030
SimpleSpanProcessor,
@@ -85,7 +85,9 @@ describe('UndiciInstrumentation `undici` tests', function () {
8585
instrumentation.setTracerProvider(provider);
8686

8787
propagation.setGlobalPropagator(new MockPropagation());
88-
context.setGlobalContextManager(new AsyncHooksContextManager().enable());
88+
context.setGlobalContextManager(
89+
new AsyncLocalStorageContextManager().enable()
90+
);
8991
mockServer.start(done);
9092
mockServer.mockListener((req, res) => {
9193
// There are some situations where there is no way to access headers

plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
SpanStatusCode,
2929
} from '@opentelemetry/api';
3030
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
31-
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
31+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
3232
import {
3333
DBSYSTEMVALUES_CASSANDRA,
3434
SEMATTRS_DB_STATEMENT,
@@ -37,6 +37,8 @@ import {
3737
SEMATTRS_EXCEPTION_MESSAGE,
3838
SEMATTRS_EXCEPTION_STACKTRACE,
3939
SEMATTRS_EXCEPTION_TYPE,
40+
SEMATTRS_NET_PEER_NAME,
41+
SEMATTRS_NET_PEER_PORT,
4042
} from '@opentelemetry/semantic-conventions';
4143
import * as assert from 'assert';
4244
import * as testUtils from '@opentelemetry/contrib-test-utils';
@@ -51,12 +53,16 @@ const memoryExporter = new InMemorySpanExporter();
5153
const provider = new NodeTracerProvider({
5254
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
5355
});
54-
context.setGlobalContextManager(new AsyncHooksContextManager());
56+
context.setGlobalContextManager(new AsyncLocalStorageContextManager());
5557

5658
const testCassandra = process.env.RUN_CASSANDRA_TESTS;
5759
const testCassandraLocally = process.env.RUN_CASSANDRA_TESTS_LOCAL;
5860
const shouldTest = testCassandra || testCassandraLocally;
5961
const cassandraTimeoutMs = 60000;
62+
const cassandraContactPoint =
63+
process.env.CASSANDRA_HOST ?? testCassandraLocally
64+
? '127.0.0.1'
65+
: 'cassandra';
6066

6167
function assertSpan(
6268
span: ReadableSpan,
@@ -80,11 +86,16 @@ function assertSpan(
8086
testUtils.assertSpan(span, SpanKind.CLIENT, attributes, [], spanStatus);
8187
}
8288

83-
function assertSingleSpan(name: string, query?: string, status?: SpanStatus) {
89+
function assertSingleSpan(
90+
name: string,
91+
query?: string,
92+
status?: SpanStatus,
93+
customAttributes?: Attributes
94+
) {
8495
const spans = memoryExporter.getFinishedSpans();
8596
assert.strictEqual(spans.length, 1);
8697
const [span] = spans;
87-
assertSpan(span, name, query, status);
98+
assertSpan(span, name, query, status, customAttributes);
8899
}
89100

90101
function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
@@ -97,7 +108,8 @@ function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
97108
function assertErrorSpan(
98109
name: string,
99110
error: Error & { code?: number },
100-
query?: string
111+
query?: string,
112+
customAttributes?: Attributes
101113
) {
102114
const spans = memoryExporter.getFinishedSpans();
103115
assert.strictEqual(spans.length, 1);
@@ -106,6 +118,7 @@ function assertErrorSpan(
106118
const attributes: Attributes = {
107119
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
108120
[SEMATTRS_DB_USER]: 'cassandra',
121+
...customAttributes,
109122
};
110123

111124
if (query !== undefined) {
@@ -154,12 +167,8 @@ describe('CassandraDriverInstrumentation', () => {
154167
instrumentation.setTracerProvider(provider);
155168

156169
const cassandra = require('cassandra-driver');
157-
const endpoint =
158-
process.env.CASSANDRA_HOST ?? testCassandraLocally
159-
? '127.0.0.1'
160-
: 'cassandra';
161170
client = new cassandra.Client({
162-
contactPoints: [endpoint],
171+
contactPoints: [cassandraContactPoint],
163172
localDataCenter: 'datacenter1',
164173
credentials: {
165174
username: 'cassandra',
@@ -198,12 +207,18 @@ describe('CassandraDriverInstrumentation', () => {
198207

199208
it('creates a span for promise based execute', async () => {
200209
await client.execute('select * from ot.test');
201-
assertSingleSpan('cassandra-driver.execute');
210+
assertSingleSpan('cassandra-driver.execute', undefined, undefined, {
211+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
212+
[SEMATTRS_NET_PEER_PORT]: 9042,
213+
});
202214
});
203215

204216
it('creates a span for callback based execute', done => {
205217
client.execute('select * from ot.test', () => {
206-
assertSingleSpan('cassandra-driver.execute');
218+
assertSingleSpan('cassandra-driver.execute', undefined, undefined, {
219+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
220+
[SEMATTRS_NET_PEER_PORT]: 9042,
221+
});
207222
done();
208223
});
209224
});
@@ -212,7 +227,10 @@ describe('CassandraDriverInstrumentation', () => {
212227
try {
213228
await client.execute('selec * from');
214229
} catch (e: any) {
215-
assertErrorSpan('cassandra-driver.execute', e);
230+
assertErrorSpan('cassandra-driver.execute', e, undefined, {
231+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
232+
[SEMATTRS_NET_PEER_PORT]: 9042,
233+
});
216234
return;
217235
}
218236

@@ -239,13 +257,31 @@ describe('CassandraDriverInstrumentation', () => {
239257
it('retains statements', async () => {
240258
const query = 'select * from ot.test';
241259
await client.execute(query);
242-
assertSingleSpan('cassandra-driver.execute', query);
260+
assertSingleSpan('cassandra-driver.execute', query, undefined, {
261+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
262+
[SEMATTRS_NET_PEER_PORT]: 9042,
263+
});
243264
});
244265

245266
it('truncates long queries', async () => {
246267
const query = 'select userid, count from ot.test';
247268
await client.execute(query);
248-
assertSingleSpan('cassandra-driver.execute', query.substring(0, 25));
269+
const customAttributes = {
270+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
271+
[SEMATTRS_NET_PEER_PORT]: 9042,
272+
};
273+
assertSingleSpan(
274+
'cassandra-driver.execute',
275+
query.substring(0, 25),
276+
undefined,
277+
customAttributes
278+
);
279+
assertSingleSpan(
280+
'cassandra-driver.execute',
281+
query.substring(0, 25),
282+
undefined,
283+
customAttributes
284+
);
249285
});
250286
});
251287

@@ -278,6 +314,8 @@ describe('CassandraDriverInstrumentation', () => {
278314
assertAttributeInSingleSpan('cassandra-driver.execute', {
279315
[customAttributeName]: customAttributeValue,
280316
[responseAttributeName]: 2,
317+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
318+
[SEMATTRS_NET_PEER_PORT]: 9042,
281319
});
282320
});
283321

@@ -299,6 +337,8 @@ describe('CassandraDriverInstrumentation', () => {
299337

300338
assertAttributeInSingleSpan('cassandra-driver.execute', {
301339
[hookAttributeName]: hookAttributeValue,
340+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
341+
[SEMATTRS_NET_PEER_PORT]: 9042,
302342
});
303343
});
304344
});
@@ -320,7 +360,10 @@ describe('CassandraDriverInstrumentation', () => {
320360

321361
it('creates a span for callback based batch', done => {
322362
client.batch([q1, q2], () => {
323-
assertSingleSpan('cassandra-driver.batch');
363+
assertSingleSpan('cassandra-driver.batch', undefined, undefined, {
364+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
365+
[SEMATTRS_NET_PEER_PORT]: 9042,
366+
});
324367
done();
325368
});
326369
});
@@ -370,7 +413,10 @@ describe('CassandraDriverInstrumentation', () => {
370413
const spans = memoryExporter.getFinishedSpans();
371414
// stream internally uses execute
372415
assert.strictEqual(spans.length, 2);
373-
assertSpan(spans[0], 'cassandra-driver.execute');
416+
assertSpan(spans[0], 'cassandra-driver.execute', undefined, undefined, {
417+
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
418+
[SEMATTRS_NET_PEER_PORT]: 9042,
419+
});
374420
assertSpan(spans[1], 'cassandra-driver.stream');
375421
}
376422

0 commit comments

Comments
 (0)