@@ -35,6 +35,7 @@ import {
3535import * as assert from 'assert' ;
3636import { MySQL2Instrumentation , MySQL2InstrumentationConfig } from '../src' ;
3737
38+ const LIB_VERSION = testUtils . getPackageVersion ( 'mysql2' ) ;
3839const port = Number ( process . env . MYSQL_PORT ) || 33306 ;
3940const database = process . env . MYSQL_DATABASE || 'test_db' ;
4041const host = process . env . MYSQL_HOST || '127.0.0.1' ;
@@ -47,6 +48,7 @@ instrumentation.enable();
4748instrumentation . disable ( ) ;
4849
4950import * as mysqlTypes from 'mysql2/promise' ;
51+ import * as semver from 'semver' ;
5052
5153interface GeneralLogResult extends mysqlTypes . RowDataPacket {
5254 argument : string | Buffer ;
@@ -134,16 +136,18 @@ describe('mysql2/promise', () => {
134136 password,
135137 database,
136138 } ) ;
137- poolCluster = mysqlTypes . createPoolCluster ( ) ;
138- // the implementation actually accepts ConnectionConfig as well,
139- // but the types do not reflect that
140- poolCluster . add ( 'name' , {
141- port,
142- user,
143- host,
144- password,
145- database,
146- } ) ;
139+ if ( isPoolClusterSupportPromise ( ) ) {
140+ poolCluster = mysqlTypes . createPoolCluster ( ) ;
141+ // the implementation actually accepts ConnectionConfig as well,
142+ // but the types do not reflect that
143+ poolCluster . add ( 'name' , {
144+ port,
145+ user,
146+ host,
147+ password,
148+ database,
149+ } ) ;
150+ }
147151 } ) ;
148152
149153 afterEach ( async ( ) => {
@@ -153,7 +157,9 @@ describe('mysql2/promise', () => {
153157 instrumentation . disable ( ) ;
154158 await connection . end ( ) ;
155159 await pool . end ( ) ;
156- await poolCluster . end ( ) ;
160+ if ( isPoolClusterSupportPromise ( ) ) {
161+ await poolCluster . end ( ) ;
162+ }
157163 } ) ;
158164
159165 describe ( 'when the query is a string' , ( ) => {
@@ -726,6 +732,12 @@ describe('mysql2/promise', () => {
726732 } ) ;
727733
728734 describe ( '#PoolCluster' , ( ) => {
735+ before ( function ( ) {
736+ if ( ! isPoolClusterSupportPromise ( ) ) {
737+ this . skip ( ) ;
738+ }
739+ } ) ;
740+
729741 it ( 'should intercept poolClusterConnection.query(text: string)' , async ( ) => {
730742 const poolClusterConnection = await poolCluster . getConnection ( ) ;
731743 const span = provider . getTracer ( 'default' ) . startSpan ( 'test span' ) ;
@@ -921,7 +933,11 @@ describe('mysql2/promise', () => {
921933 } ) ;
922934 } ) ;
923935
924- it ( 'should extract data from responseHook - poolCluster' , async ( ) => {
936+ it ( 'should extract data from responseHook - poolCluster' , async function ( ) {
937+ if ( ! isPoolClusterSupportPromise ( ) ) {
938+ this . skip ( ) ;
939+ }
940+
925941 const poolClusterConnection = await poolCluster . getConnection ( ) ;
926942 const span = provider . getTracer ( 'default' ) . startSpan ( 'test span' ) ;
927943 await context . with ( trace . setSpan ( context . active ( ) , span ) , async ( ) => {
@@ -963,3 +979,9 @@ function assertSpan(
963979 assert . strictEqual ( span . status . code , SpanStatusCode . ERROR ) ;
964980 }
965981}
982+
983+ function isPoolClusterSupportPromise ( ) {
984+ // Since v2.3.0, mysql2 supports promise for PoolCluster
985+ // https://github.com/sidorares/node-mysql2/commit/2cfecd9a5d48987ba98ce7a8de26d26399cda7f6
986+ return semver . gte ( LIB_VERSION , '2.3.0' ) ;
987+ }
0 commit comments