Skip to content

test(node): Add mysql2 auto instrumentation test for @sentry/node-experimental #10259

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 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build:dev": "yarn build",
"build:transpile": "rollup -c rollup.npm.config.mjs",
"build:types": "tsc -p tsconfig.types.json",
"clean": "rimraf -g **/node_modules",
"clean": "rimraf -g **/node_modules && run-p clean:docker:*",
"clean:docker:mysql2": "cd suites/tracing-experimental/mysql2 && docker-compose down --volumes",
"prisma:init": "(cd suites/tracing/prisma-orm && ts-node ./setup.ts)",
"prisma:init:new": "(cd suites/tracing-new/prisma-orm && ts-node ./setup.ts)",
"lint": "eslint . --format stylish",
Expand Down Expand Up @@ -44,6 +45,7 @@
"mongodb-memory-server-global": "^7.6.3",
"mongoose": "^5.13.22",
"mysql": "^2.18.1",
"mysql2": "^3.7.1",
"nock": "^13.1.0",
"pg": "^8.7.3",
"proxy": "^2.1.1",
Expand Down
6 changes: 5 additions & 1 deletion dev-packages/node-integration-tests/suites/proxy/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createRunner } from '../../utils/runner';
import { cleanupChildProcesses, createRunner } from '../../utils/runner';

afterAll(() => {
cleanupChildProcesses();
});

test('proxies sentry requests', done => {
createRunner(__dirname, 'basic.js')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { conditionalTest } from '../../../utils';
import { createRunner } from '../../../utils/runner';
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

conditionalTest({ min: 14 })('mysql auto instrumentation', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('should auto-instrument `mysql` package when using connection.connect()', done => {
const EXPECTED_TRANSACTION = {
transaction: 'Test Transaction',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
db:
image: mysql:8
restart: always
container_name: integration-tests-mysql
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: password
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/node-experimental');

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
});

// Stop the process from exiting before the transaction is sent
setInterval(() => {}, 1000);

const mysql = require('mysql2/promise');

mysql
.createConnection({
user: 'root',

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "root" is used as [user name](1).
password: 'password',
host: 'localhost',
port: 3306,
})
.then(connection => {
return Sentry.startSpan(
{
op: 'transaction',
name: 'Test Transaction',
},
async _ => {
await connection.query('SELECT 1 + 1 AS solution');
await connection.query('SELECT NOW()', ['1', '2']);
},
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { conditionalTest } from '../../../utils';
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

conditionalTest({ min: 14 })('mysql2 auto instrumentation', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('should auto-instrument `mysql` package without connection.connect()', done => {
const EXPECTED_TRANSACTION = {
transaction: 'Test Transaction',
spans: expect.arrayContaining([
expect.objectContaining({
description: 'SELECT 1 + 1 AS solution',
op: 'db',
data: expect.objectContaining({
'db.system': 'mysql',
'net.peer.name': 'localhost',
'net.peer.port': 3306,
'db.user': 'root',
}),
}),
expect.objectContaining({
description: 'SELECT NOW()',
op: 'db',
data: expect.objectContaining({
'db.system': 'mysql',
'net.peer.name': 'localhost',
'net.peer.port': 3306,
'db.user': 'root',
}),
}),
]),
};

createRunner(__dirname, 'scenario.js')
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port: 3306'] })
.expect({ transaction: EXPECTED_TRANSACTION })
.start(done);
});
});
201 changes: 137 additions & 64 deletions dev-packages/node-integration-tests/utils/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ChildProcess } from 'child_process';
import { spawn } from 'child_process';
import { spawn, spawnSync } from 'child_process';
import { join } from 'path';
import type { Envelope, EnvelopeItemType, Event, SerializedSession } from '@sentry/types';
import axios from 'axios';
Expand Down Expand Up @@ -30,14 +29,17 @@ export function assertSentryTransaction(actual: Event, expected: Partial<Event>)
});
}

const CHILD_PROCESSES = new Set<ChildProcess>();
const CLEANUP_STEPS = new Set<VoidFunction>();

export function cleanupChildProcesses(): void {
for (const child of CHILD_PROCESSES) {
child.kill();
for (const step of CLEANUP_STEPS) {
step();
}
CLEANUP_STEPS.clear();
}

process.on('exit', cleanupChildProcesses);

/** Promise only resolves when fn returns true */
async function waitFor(fn: () => boolean, timeout = 10_000): Promise<void> {
let remaining = timeout;
Expand All @@ -50,6 +52,58 @@ async function waitFor(fn: () => boolean, timeout = 10_000): Promise<void> {
}
}

type VoidFunction = () => void;

interface DockerOptions {
/**
* The working directory to run docker compose in
*/
workingDirectory: string[];
/**
* The strings to look for in the output to know that the docker compose is ready for the test to be run
*/
readyMatches: string[];
}

/**
* Runs docker compose up and waits for the readyMatches to appear in the output
*
* Returns a function that can be called to docker compose down
*/
async function runDockerCompose(options: DockerOptions): Promise<VoidFunction> {
return new Promise((resolve, reject) => {
const cwd = join(...options.workingDirectory);
const close = (): void => {
spawnSync('docker', ['compose', 'down', '--volumes'], { cwd });
};

// ensure we're starting fresh
close();

const child = spawn('docker', ['compose', 'up'], { cwd });

const timeout = setTimeout(() => {
close();
reject(new Error('Timed out waiting for docker-compose'));
}, 60_000);

function newData(data: Buffer): void {
const text = data.toString('utf8');

for (const match of options.readyMatches) {
if (text.includes(match)) {
child.stdout.removeAllListeners();
clearTimeout(timeout);
resolve(close);
}
}
}

child.stdout.on('data', newData);
child.stderr.on('data', newData);
});
}

type Expected =
| {
event: Partial<Event> | ((event: Event) => void);
Expand All @@ -70,6 +124,7 @@ export function createRunner(...paths: string[]) {
const flags: string[] = [];
const ignored: EnvelopeItemType[] = [];
let withSentryServer = false;
let dockerOptions: DockerOptions | undefined;
let ensureNoErrorOutput = false;

if (testPath.endsWith('.ts')) {
Expand All @@ -93,6 +148,10 @@ export function createRunner(...paths: string[]) {
ignored.push(...types);
return this;
},
withDockerCompose: function (options: DockerOptions) {
dockerOptions = options;
return this;
},
ensureNoErrorOutput: function () {
ensureNoErrorOutput = true;
return this;
Expand Down Expand Up @@ -182,80 +241,94 @@ export function createRunner(...paths: string[]) {
? createBasicSentryServer(newEnvelope)
: Promise.resolve(undefined);

const dockerStartup: Promise<VoidFunction | undefined> = dockerOptions
? runDockerCompose(dockerOptions)
: Promise.resolve(undefined);

const startup = Promise.all([dockerStartup, serverStartup]);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
serverStartup.then(mockServerPort => {
const env = mockServerPort
? { ...process.env, SENTRY_DSN: `http://public@localhost:${mockServerPort}/1337` }
: process.env;
startup
.then(([dockerChild, mockServerPort]) => {
if (dockerChild) {
CLEANUP_STEPS.add(dockerChild);
}

// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('starting scenario', testPath, flags, env.SENTRY_DSN);
const env = mockServerPort
? { ...process.env, SENTRY_DSN: `http://public@localhost:${mockServerPort}/1337` }
: process.env;

child = spawn('node', [...flags, testPath], { env });
// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('starting scenario', testPath, flags, env.SENTRY_DSN);

CHILD_PROCESSES.add(child);
child = spawn('node', [...flags, testPath], { env });

if (ensureNoErrorOutput) {
child.stderr.on('data', (data: Buffer) => {
const output = data.toString();
complete(new Error(`Expected no error output but got: '${output}'`));
CLEANUP_STEPS.add(() => {
child?.kill();
});
}

child.on('close', () => {
hasExited = true;

if (ensureNoErrorOutput) {
complete();
child.stderr.on('data', (data: Buffer) => {
const output = data.toString();
complete(new Error(`Expected no error output but got: '${output}'`));
});
}
});

// Pass error to done to end the test quickly
child.on('error', e => {
// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('scenario error', e);
complete(e);
});

function tryParseEnvelopeFromStdoutLine(line: string): void {
// Lines can have leading '[something] [{' which we need to remove
const cleanedLine = line.replace(/^.*?] \[{"/, '[{"');

// See if we have a port message
if (cleanedLine.startsWith('{"port":')) {
const { port } = JSON.parse(cleanedLine) as { port: number };
scenarioServerPort = port;
return;
}
child.on('close', () => {
hasExited = true;

// Skip any lines that don't start with envelope JSON
if (!cleanedLine.startsWith('[{')) {
return;
}
if (ensureNoErrorOutput) {
complete();
}
});

try {
const envelope = JSON.parse(cleanedLine) as Envelope;
newEnvelope(envelope);
} catch (_) {
//
}
}
// Pass error to done to end the test quickly
child.on('error', e => {
// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('scenario error', e);
complete(e);
});

let buffer = Buffer.alloc(0);
child.stdout.on('data', (data: Buffer) => {
// This is horribly memory inefficient but it's only for tests
buffer = Buffer.concat([buffer, data]);
function tryParseEnvelopeFromStdoutLine(line: string): void {
// Lines can have leading '[something] [{' which we need to remove
const cleanedLine = line.replace(/^.*?] \[{"/, '[{"');

let splitIndex = -1;
while ((splitIndex = buffer.indexOf(0xa)) >= 0) {
const line = buffer.subarray(0, splitIndex).toString();
buffer = Buffer.from(buffer.subarray(splitIndex + 1));
// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('line', line);
tryParseEnvelopeFromStdoutLine(line);
// See if we have a port message
if (cleanedLine.startsWith('{"port":')) {
const { port } = JSON.parse(cleanedLine) as { port: number };
scenarioServerPort = port;
return;
}

// Skip any lines that don't start with envelope JSON
if (!cleanedLine.startsWith('[{')) {
return;
}

try {
const envelope = JSON.parse(cleanedLine) as Envelope;
newEnvelope(envelope);
} catch (_) {
//
}
}
});
});

let buffer = Buffer.alloc(0);
child.stdout.on('data', (data: Buffer) => {
// This is horribly memory inefficient but it's only for tests
buffer = Buffer.concat([buffer, data]);

let splitIndex = -1;
while ((splitIndex = buffer.indexOf(0xa)) >= 0) {
const line = buffer.subarray(0, splitIndex).toString();
buffer = Buffer.from(buffer.subarray(splitIndex + 1));
// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('line', line);
tryParseEnvelopeFromStdoutLine(line);
}
});
})
.catch(e => complete(e));

return {
childHasExited: function (): boolean {
Expand Down
Loading