From 0dbc535b454b9bb9a6ffb1e8769f5f0c90b3860e Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Wed, 24 Jan 2024 14:13:39 +0000 Subject: [PATCH 1/3] test(node): Add OTEL tests for Apollo / GraphQL auto-instrumentation. --- .../apollo-graphql/scenario.js | 52 +++++++++++++++++++ .../apollo-graphql/test.ts | 42 +++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/scenario.js create mode 100644 dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts diff --git a/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/scenario.js b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/scenario.js new file mode 100644 index 000000000000..42bb8edacb8f --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/scenario.js @@ -0,0 +1,52 @@ +const Sentry = require('@sentry/node-experimental'); +const { loggingTransport } = require('@sentry-internal/node-integration-tests'); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + release: '1.0', + tracesSampleRate: 1.0, + transport: loggingTransport, +}); + +// Stop the process from exiting before the transaction is sent +setInterval(() => {}, 1000); + +async function run() { + const { ApolloServer, gql } = require('apollo-server'); + + await Sentry.startSpan( + { + name: 'Test Transaction', + op: 'transaction', + }, + async span => { + const typeDefs = gql`type Query { hello: String }`; + + const resolvers = { + Query: { + hello: () => { + return 'Hello world!'; + }, + }, + }; + + const server = new ApolloServer({ + typeDefs, + resolvers, + }); + + // Ref: https://www.apollographql.com/docs/apollo-server/testing/testing/#testing-using-executeoperation + await server.executeOperation({ + query: '{hello}', + }); + + setTimeout(() => { + span.end(); + server.stop(); + }, 500); + }, + ); +} + +// eslint-disable-next-line @typescript-eslint/no-floating-promises +run(); diff --git a/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts new file mode 100644 index 000000000000..cb22a4334ae8 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts @@ -0,0 +1,42 @@ +import { conditionalTest } from '../../../utils'; +import { createRunner } from '../../../utils/runner'; + +jest.setTimeout(10000); + +// Node 10 is not supported by `graphql-js` +// Ref: https://github.com/graphql/graphql-js/blob/main/package.json +conditionalTest({ min: 12 })('GraphQL/Apollo Tests', () => { + const EXPECTED_TRANSACTION = { + transaction: 'Test Transaction', + spans: expect.arrayContaining([ + expect.objectContaining({ + data: { + 'graphql.operation.type': 'query', + 'graphql.source': '{hello}', + 'otel.kind': 'INTERNAL', + 'sentry.origin': 'auto.graphql.otel.graphql', + }, + description: 'query', + status: 'ok', + origin: 'auto.graphql.otel.graphql', + }), + expect.objectContaining({ + data: { + 'graphql.field.name': 'hello', + 'graphql.field.path': 'hello', + 'graphql.field.type': 'String', + 'graphql.source': 'hello', + 'otel.kind': 'INTERNAL', + 'sentry.origin': 'manual', + }, + description: 'graphql.resolve', + status: 'ok', + origin: 'manual', + }), + ]), + }; + + test('CJS - should instrument GraphQL queries used from Apollo Server.', done => { + createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done); + }); +}); From 11d1eb816d8524f1b8c0a24b34212f92df3b957e Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Wed, 24 Jan 2024 14:38:52 +0000 Subject: [PATCH 2/3] Use default jest timeout --- .../suites/tracing-experimental/apollo-graphql/test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts index cb22a4334ae8..48a1478287cf 100644 --- a/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts @@ -1,8 +1,6 @@ import { conditionalTest } from '../../../utils'; import { createRunner } from '../../../utils/runner'; -jest.setTimeout(10000); - // Node 10 is not supported by `graphql-js` // Ref: https://github.com/graphql/graphql-js/blob/main/package.json conditionalTest({ min: 12 })('GraphQL/Apollo Tests', () => { From 45495019bad240b5b34ec213f8bfb6da6ba3fe8c Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Wed, 24 Jan 2024 14:53:04 +0000 Subject: [PATCH 3/3] Set minimum Node version to 14 --- .../suites/tracing-experimental/apollo-graphql/test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts index 48a1478287cf..dc7c304484f9 100644 --- a/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts @@ -1,9 +1,7 @@ import { conditionalTest } from '../../../utils'; import { createRunner } from '../../../utils/runner'; -// Node 10 is not supported by `graphql-js` -// Ref: https://github.com/graphql/graphql-js/blob/main/package.json -conditionalTest({ min: 12 })('GraphQL/Apollo Tests', () => { +conditionalTest({ min: 14 })('GraphQL/Apollo Tests', () => { const EXPECTED_TRANSACTION = { transaction: 'Test Transaction', spans: expect.arrayContaining([