Skip to content

test: stop using the obsolete AsyncHooksContextManager in tests #2864

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-cucumber/test/cucumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { context, SpanStatusCode } from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('CucumberInstrumentation', () => {
}),
spanProcessors: [spanProcessor],
});
const contextManager = new AsyncHooksContextManager().enable();
const contextManager = new AsyncLocalStorageContextManager().enable();

before(() => {
instrumentation.setTracerProvider(provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '@opentelemetry/sdk-trace-base';
import { context, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';

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

describe('DataloaderInstrumentation', () => {
let dataloader: Dataloader<string, number>;
let contextManager: AsyncHooksContextManager;
let contextManager: AsyncLocalStorageContextManager;

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

beforeEach(async () => {
instrumentation.enable();
contextManager = new AsyncHooksContextManager();
contextManager = new AsyncLocalStorageContextManager();
context.setGlobalContextManager(contextManager.enable());
dataloader = new Dataloader(async keys => keys.map((_, idx) => idx), {
cache: false,
Expand Down
6 changes: 3 additions & 3 deletions plugins/node/instrumentation-fs/test/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { context, trace } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
BasicTracerProvider,
InMemorySpanExporter,
Expand Down Expand Up @@ -65,12 +65,12 @@ const provider = new BasicTracerProvider({
const tracer = provider.getTracer('default');

describe('fs instrumentation', () => {
let contextManager: AsyncHooksContextManager;
let contextManager: AsyncLocalStorageContextManager;
let fs: typeof FSType;
let plugin: FsInstrumentation;

beforeEach(async () => {
contextManager = new AsyncHooksContextManager();
contextManager = new AsyncLocalStorageContextManager();
context.setGlobalContextManager(contextManager.enable());
plugin = new FsInstrumentation(pluginConfig);
plugin.setTracerProvider(provider);
Expand Down
6 changes: 3 additions & 3 deletions plugins/node/instrumentation-fs/test/fsPromises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { context, trace } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
BasicTracerProvider,
InMemorySpanExporter,
Expand Down Expand Up @@ -44,12 +44,12 @@ const provider = new BasicTracerProvider({
const tracer = provider.getTracer('default');

describe('fs/promises instrumentation', () => {
let contextManager: AsyncHooksContextManager;
let contextManager: AsyncLocalStorageContextManager;
let fsPromises: typeof FSPromisesType;
let plugin: FsInstrumentation;

beforeEach(async () => {
contextManager = new AsyncHooksContextManager();
contextManager = new AsyncLocalStorageContextManager();
context.setGlobalContextManager(contextManager.enable());
plugin = new FsInstrumentation(pluginConfig);
plugin.setTracerProvider(provider);
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-fs/test/parent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as assert from 'assert';
import type * as FSType from 'fs';
import type { FsInstrumentationConfig } from '../src/types';
import * as api from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';

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

beforeEach(() => {
const contextManager = new AsyncHooksContextManager();
const contextManager = new AsyncLocalStorageContextManager();
api.context.setGlobalContextManager(contextManager.enable());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
DBSYSTEMVALUES_MSSQL,
SEMATTRS_DB_NAME,
Expand Down Expand Up @@ -87,7 +87,7 @@ const incompatVersions =

describe('tedious', () => {
let tedious: any;
let contextManager: AsyncHooksContextManager;
let contextManager: AsyncLocalStorageContextManager;
let connection: Connection;
const memoryExporter = new InMemorySpanExporter();
const provider = new BasicTracerProvider({
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('tedious', () => {
// connecting often takes more time even if the DB is running locally
this.timeout(10000);
instrumentation.disable();
contextManager = new AsyncHooksContextManager().enable();
contextManager = new AsyncLocalStorageContextManager().enable();
context.setGlobalContextManager(contextManager);
instrumentation.setTracerProvider(provider);
instrumentation.enable();
Expand Down
6 changes: 4 additions & 2 deletions plugins/node/instrumentation-undici/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
propagation,
trace,
} from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
Expand Down Expand Up @@ -59,7 +59,9 @@ describe('UndiciInstrumentation `fetch` tests', function () {
instrumentation.setTracerProvider(provider);

propagation.setGlobalPropagator(new MockPropagation());
context.setGlobalContextManager(new AsyncHooksContextManager().enable());
context.setGlobalContextManager(
new AsyncLocalStorageContextManager().enable()
);
mockServer.start(done);
mockServer.mockListener((req, res) => {
// There are some situations where there is no way to access headers
Expand Down
6 changes: 4 additions & 2 deletions plugins/node/instrumentation-undici/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import * as assert from 'assert';

import { context, propagation } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
AggregationTemporality,
Expand Down Expand Up @@ -58,7 +58,9 @@ describe('UndiciInstrumentation metrics tests', function () {
instrumentation.setTracerProvider(provider);
instrumentation.setMeterProvider(meterProvider);

context.setGlobalContextManager(new AsyncHooksContextManager().enable());
context.setGlobalContextManager(
new AsyncLocalStorageContextManager().enable()
);
mockServer.start(done);
mockServer.mockListener((req, res) => {
// Return a valid response always
Expand Down
6 changes: 4 additions & 2 deletions plugins/node/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
propagation,
trace,
} from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
Expand Down Expand Up @@ -85,7 +85,9 @@ describe('UndiciInstrumentation `undici` tests', function () {
instrumentation.setTracerProvider(provider);

propagation.setGlobalPropagator(new MockPropagation());
context.setGlobalContextManager(new AsyncHooksContextManager().enable());
context.setGlobalContextManager(
new AsyncLocalStorageContextManager().enable()
);
mockServer.start(done);
mockServer.mockListener((req, res) => {
// There are some situations where there is no way to access headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
SpanStatusCode,
} from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import {
DBSYSTEMVALUES_CASSANDRA,
SEMATTRS_DB_STATEMENT,
Expand All @@ -37,6 +37,8 @@ import {
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_EXCEPTION_STACKTRACE,
SEMATTRS_EXCEPTION_TYPE,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';
import * as assert from 'assert';
import * as testUtils from '@opentelemetry/contrib-test-utils';
Expand All @@ -51,12 +53,16 @@ const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
context.setGlobalContextManager(new AsyncHooksContextManager());
context.setGlobalContextManager(new AsyncLocalStorageContextManager());

const testCassandra = process.env.RUN_CASSANDRA_TESTS;
const testCassandraLocally = process.env.RUN_CASSANDRA_TESTS_LOCAL;
const shouldTest = testCassandra || testCassandraLocally;
const cassandraTimeoutMs = 60000;
const cassandraContactPoint =
process.env.CASSANDRA_HOST ?? testCassandraLocally
? '127.0.0.1'
: 'cassandra';

function assertSpan(
span: ReadableSpan,
Expand All @@ -80,11 +86,16 @@ function assertSpan(
testUtils.assertSpan(span, SpanKind.CLIENT, attributes, [], spanStatus);
}

function assertSingleSpan(name: string, query?: string, status?: SpanStatus) {
function assertSingleSpan(
name: string,
query?: string,
status?: SpanStatus,
customAttributes?: Attributes
) {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1);
const [span] = spans;
assertSpan(span, name, query, status);
assertSpan(span, name, query, status, customAttributes);
}

function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
Expand All @@ -97,7 +108,8 @@ function assertAttributeInSingleSpan(name: string, attributes?: Attributes) {
function assertErrorSpan(
name: string,
error: Error & { code?: number },
query?: string
query?: string,
customAttributes?: Attributes
) {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1);
Expand All @@ -106,6 +118,7 @@ function assertErrorSpan(
const attributes: Attributes = {
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
[SEMATTRS_DB_USER]: 'cassandra',
...customAttributes,
};

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

const cassandra = require('cassandra-driver');
const endpoint =
process.env.CASSANDRA_HOST ?? testCassandraLocally
? '127.0.0.1'
: 'cassandra';
client = new cassandra.Client({
contactPoints: [endpoint],
contactPoints: [cassandraContactPoint],
localDataCenter: 'datacenter1',
credentials: {
username: 'cassandra',
Expand Down Expand Up @@ -198,12 +207,18 @@ describe('CassandraDriverInstrumentation', () => {

it('creates a span for promise based execute', async () => {
await client.execute('select * from ot.test');
assertSingleSpan('cassandra-driver.execute');
assertSingleSpan('cassandra-driver.execute', undefined, undefined, {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review note: the switch to AsyncLocalStorage for context management results in some spans getting net.peer.{name,port} being applied where it was not before. Presumably this is because AsyncLocalStorage is doing a better job following the async-context and this code block now gets triggered.

});

it('creates a span for callback based execute', done => {
client.execute('select * from ot.test', () => {
assertSingleSpan('cassandra-driver.execute');
assertSingleSpan('cassandra-driver.execute', undefined, undefined, {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
done();
});
});
Expand All @@ -212,7 +227,10 @@ describe('CassandraDriverInstrumentation', () => {
try {
await client.execute('selec * from');
} catch (e: any) {
assertErrorSpan('cassandra-driver.execute', e);
assertErrorSpan('cassandra-driver.execute', e, undefined, {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
return;
}

Expand All @@ -239,13 +257,31 @@ describe('CassandraDriverInstrumentation', () => {
it('retains statements', async () => {
const query = 'select * from ot.test';
await client.execute(query);
assertSingleSpan('cassandra-driver.execute', query);
assertSingleSpan('cassandra-driver.execute', query, undefined, {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
});

it('truncates long queries', async () => {
const query = 'select userid, count from ot.test';
await client.execute(query);
assertSingleSpan('cassandra-driver.execute', query.substring(0, 25));
const customAttributes = {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
};
assertSingleSpan(
'cassandra-driver.execute',
query.substring(0, 25),
undefined,
customAttributes
);
assertSingleSpan(
'cassandra-driver.execute',
query.substring(0, 25),
undefined,
customAttributes
);
});
});

Expand Down Expand Up @@ -278,6 +314,8 @@ describe('CassandraDriverInstrumentation', () => {
assertAttributeInSingleSpan('cassandra-driver.execute', {
[customAttributeName]: customAttributeValue,
[responseAttributeName]: 2,
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
});

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

assertAttributeInSingleSpan('cassandra-driver.execute', {
[hookAttributeName]: hookAttributeValue,
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
});
});
Expand All @@ -320,7 +360,10 @@ describe('CassandraDriverInstrumentation', () => {

it('creates a span for callback based batch', done => {
client.batch([q1, q2], () => {
assertSingleSpan('cassandra-driver.batch');
assertSingleSpan('cassandra-driver.batch', undefined, undefined, {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
done();
});
});
Expand Down Expand Up @@ -370,7 +413,10 @@ describe('CassandraDriverInstrumentation', () => {
const spans = memoryExporter.getFinishedSpans();
// stream internally uses execute
assert.strictEqual(spans.length, 2);
assertSpan(spans[0], 'cassandra-driver.execute');
assertSpan(spans[0], 'cassandra-driver.execute', undefined, undefined, {
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
[SEMATTRS_NET_PEER_PORT]: 9042,
});
assertSpan(spans[1], 'cassandra-driver.stream');
}

Expand Down
Loading