Skip to content

Commit

Permalink
fix(instrumentation-mysql2): Make Promise test of PoolCluster be ex…
Browse files Browse the repository at this point in the history
…ecuted from v2.3.0 or later
  • Loading branch information
Karibash committed Dec 11, 2024
1 parent 59b934b commit c68038c
Showing 1 changed file with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import * as assert from 'assert';
import { MySQL2Instrumentation, MySQL2InstrumentationConfig } from '../src';

const LIB_VERSION = testUtils.getPackageVersion('mysql2');
const port = Number(process.env.MYSQL_PORT) || 33306;
const database = process.env.MYSQL_DATABASE || 'test_db';
const host = process.env.MYSQL_HOST || '127.0.0.1';
Expand All @@ -47,6 +48,7 @@ instrumentation.enable();
instrumentation.disable();

import * as mysqlTypes from 'mysql2/promise';
import * as semver from 'semver';

interface GeneralLogResult extends mysqlTypes.RowDataPacket {
argument: string | Buffer;
Expand Down Expand Up @@ -134,16 +136,18 @@ describe('mysql2/promise', () => {
password,
database,
});
poolCluster = mysqlTypes.createPoolCluster();
// the implementation actually accepts ConnectionConfig as well,
// but the types do not reflect that
poolCluster.add('name', {
port,
user,
host,
password,
database,
});
if (isPoolClusterSupportPromise()) {
poolCluster = mysqlTypes.createPoolCluster();
// the implementation actually accepts ConnectionConfig as well,
// but the types do not reflect that
poolCluster.add('name', {
port,
user,
host,
password,
database,
});
}
});

afterEach(async () => {
Expand All @@ -153,7 +157,9 @@ describe('mysql2/promise', () => {
instrumentation.disable();
await connection.end();
await pool.end();
await poolCluster.end();
if (isPoolClusterSupportPromise()) {
await poolCluster.end();
}
});

describe('when the query is a string', () => {
Expand Down Expand Up @@ -726,6 +732,12 @@ describe('mysql2/promise', () => {
});

describe('#PoolCluster', () => {
before(function () {
if (!isPoolClusterSupportPromise()) {
this.skip();
}
});

it('should intercept poolClusterConnection.query(text: string)', async () => {
const poolClusterConnection = await poolCluster.getConnection();
const span = provider.getTracer('default').startSpan('test span');
Expand Down Expand Up @@ -921,7 +933,11 @@ describe('mysql2/promise', () => {
});
});

it('should extract data from responseHook - poolCluster', async () => {
it('should extract data from responseHook - poolCluster', async function () {
if (!isPoolClusterSupportPromise()) {
this.skip();
}

const poolClusterConnection = await poolCluster.getConnection();
const span = provider.getTracer('default').startSpan('test span');
await context.with(trace.setSpan(context.active(), span), async () => {
Expand Down Expand Up @@ -963,3 +979,9 @@ function assertSpan(
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
}
}

function isPoolClusterSupportPromise() {
// Since v2.3.0, mysql2 supports promise for PoolCluster
// https://github.com/sidorares/node-mysql2/commit/2cfecd9a5d48987ba98ce7a8de26d26399cda7f6
return semver.gte(LIB_VERSION, '2.3.0');
}

0 comments on commit c68038c

Please sign in to comment.