Skip to content

Commit cf7f81c

Browse files
committed
test(node): Add OTEL tests for Apollo / GraphQL auto-instrumentation.
1 parent 6fe349f commit cf7f81c

File tree

2 files changed

+94
-0
lines changed
  • dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql

2 files changed

+94
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const Sentry = require('@sentry/node-experimental');
2+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
14+
async function run() {
15+
const { ApolloServer, gql } = require('apollo-server');
16+
17+
await Sentry.startSpan(
18+
{
19+
name: 'Test Transaction',
20+
op: 'transaction',
21+
},
22+
async span => {
23+
const typeDefs = gql`type Query { hello: String }`;
24+
25+
const resolvers = {
26+
Query: {
27+
hello: () => {
28+
return 'Hello world!';
29+
},
30+
},
31+
};
32+
33+
const server = new ApolloServer({
34+
typeDefs,
35+
resolvers,
36+
});
37+
38+
// Ref: https://www.apollographql.com/docs/apollo-server/testing/testing/#testing-using-executeoperation
39+
await server.executeOperation({
40+
query: '{hello}',
41+
});
42+
43+
setTimeout(() => {
44+
span.end();
45+
server.stop();
46+
}, 500);
47+
},
48+
);
49+
}
50+
51+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
52+
run();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { conditionalTest } from '../../../utils';
2+
import { createRunner } from '../../../utils/runner';
3+
4+
jest.setTimeout(10000);
5+
6+
// Node 10 is not supported by `graphql-js`
7+
// Ref: https://github.com/graphql/graphql-js/blob/main/package.json
8+
conditionalTest({ min: 12 })('GraphQL/Apollo Tests', () => {
9+
const EXPECTED_TRANSACTION = {
10+
transaction: 'Test Transaction',
11+
spans: expect.arrayContaining([
12+
expect.objectContaining({
13+
data: {
14+
'graphql.operation.type': 'query',
15+
'graphql.source': '{hello}',
16+
'otel.kind': 'INTERNAL',
17+
'sentry.origin': 'auto.graphql.otel.graphql',
18+
},
19+
description: 'query',
20+
status: 'ok',
21+
origin: 'auto.graphql.otel.graphql',
22+
}),
23+
expect.objectContaining({
24+
data: {
25+
'graphql.field.name': 'hello',
26+
'graphql.field.path': 'hello',
27+
'graphql.field.type': 'String',
28+
'graphql.source': 'hello',
29+
'otel.kind': 'INTERNAL',
30+
'sentry.origin': 'manual',
31+
},
32+
description: 'graphql.resolve',
33+
status: 'ok',
34+
origin: 'manual',
35+
}),
36+
]),
37+
};
38+
39+
test('CJS - should instrument GraphQL queries used from Apollo Server.', done => {
40+
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
41+
});
42+
});

0 commit comments

Comments
 (0)