Skip to content

Commit 10345d4

Browse files
authored
test(node): Add tests for Apollo / GraphQL OpenTelemetry auto-instrumentation. (#10320)
1 parent b146d4c commit 10345d4

File tree

2 files changed

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

2 files changed

+90
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { conditionalTest } from '../../../utils';
2+
import { createRunner } from '../../../utils/runner';
3+
4+
conditionalTest({ min: 14 })('GraphQL/Apollo Tests', () => {
5+
const EXPECTED_TRANSACTION = {
6+
transaction: 'Test Transaction',
7+
spans: expect.arrayContaining([
8+
expect.objectContaining({
9+
data: {
10+
'graphql.operation.type': 'query',
11+
'graphql.source': '{hello}',
12+
'otel.kind': 'INTERNAL',
13+
'sentry.origin': 'auto.graphql.otel.graphql',
14+
},
15+
description: 'query',
16+
status: 'ok',
17+
origin: 'auto.graphql.otel.graphql',
18+
}),
19+
expect.objectContaining({
20+
data: {
21+
'graphql.field.name': 'hello',
22+
'graphql.field.path': 'hello',
23+
'graphql.field.type': 'String',
24+
'graphql.source': 'hello',
25+
'otel.kind': 'INTERNAL',
26+
'sentry.origin': 'manual',
27+
},
28+
description: 'graphql.resolve',
29+
status: 'ok',
30+
origin: 'manual',
31+
}),
32+
]),
33+
};
34+
35+
test('CJS - should instrument GraphQL queries used from Apollo Server.', done => {
36+
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
37+
});
38+
});

0 commit comments

Comments
 (0)