forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mysql2 responsehook (open-telemetry#915)
* supprt responsehook config * add specs * response hook receives an info object
- Loading branch information
Showing
4 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ import { | |
SimpleSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
import * as assert from 'assert'; | ||
import { MySQL2Instrumentation } from '../src'; | ||
import { MySQL2Instrumentation, MySQL2InstrumentationConfig } from '../src'; | ||
|
||
const LIB_VERSION = testUtils.getPackageVersion('mysql2'); | ||
const port = Number(process.env.MYSQL_PORT) || 33306; | ||
|
@@ -647,6 +647,126 @@ describe('[email protected]', () => { | |
); | ||
}); | ||
}); | ||
|
||
describe('#responseHook', () => { | ||
const queryResultAttribute = 'query_result'; | ||
|
||
after(() => { | ||
instrumentation.setConfig({}); | ||
}); | ||
|
||
describe('invalid repsonse hook', () => { | ||
before(() => { | ||
instrumentation.disable(); | ||
instrumentation.setTracerProvider(provider); | ||
const config: MySQL2InstrumentationConfig = { | ||
responseHook: (span, responseHookInfo) => { | ||
throw new Error('random failure!'); | ||
}, | ||
}; | ||
instrumentation.setConfig(config); | ||
instrumentation.enable(); | ||
}); | ||
|
||
it('should not affect the behavior of the query', done => { | ||
const span = provider.getTracer('default').startSpan('test span'); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
const sql = 'SELECT 1+1 as solution'; | ||
connection.query(sql, (err, res: mysqlTypes.RowDataPacket[]) => { | ||
assert.ifError(err); | ||
assert.ok(res); | ||
assert.strictEqual(res[0].solution, 2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('valid response hook', () => { | ||
before(() => { | ||
instrumentation.disable(); | ||
instrumentation.setTracerProvider(provider); | ||
const config: MySQL2InstrumentationConfig = { | ||
responseHook: (span, responseHookInfo) => { | ||
span.setAttribute( | ||
queryResultAttribute, | ||
JSON.stringify(responseHookInfo.queryResults) | ||
); | ||
}, | ||
}; | ||
instrumentation.setConfig(config); | ||
instrumentation.enable(); | ||
}); | ||
|
||
it('should extract data from responseHook - connection', done => { | ||
const span = provider.getTracer('default').startSpan('test span'); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
const sql = 'SELECT 1+1 as solution'; | ||
connection.query(sql, (err, res: mysqlTypes.RowDataPacket[]) => { | ||
assert.ifError(err); | ||
assert.ok(res); | ||
assert.strictEqual(res[0].solution, 2); | ||
const spans = memoryExporter.getFinishedSpans(); | ||
assert.strictEqual(spans.length, 1); | ||
assertSpan(spans[0], sql); | ||
assert.strictEqual( | ||
spans[0].attributes[queryResultAttribute], | ||
JSON.stringify(res) | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should extract data from responseHook - pool', done => { | ||
const span = provider.getTracer('default').startSpan('test span'); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
const sql = 'SELECT 1+1 as solution'; | ||
pool.getConnection((err, conn) => { | ||
conn.query(sql, (err, res: mysqlTypes.RowDataPacket[]) => { | ||
assert.ifError(err); | ||
assert.ok(res); | ||
assert.strictEqual(res[0].solution, 2); | ||
const spans = memoryExporter.getFinishedSpans(); | ||
assert.strictEqual(spans.length, 1); | ||
assertSpan(spans[0], sql); | ||
assert.strictEqual( | ||
spans[0].attributes[queryResultAttribute], | ||
JSON.stringify(res) | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should extract data from responseHook - poolCluster', done => { | ||
poolCluster.getConnection((err, poolClusterConnection) => { | ||
assert.ifError(err); | ||
const span = provider.getTracer('default').startSpan('test span'); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
const sql = 'SELECT 1+1 as solution'; | ||
poolClusterConnection.query( | ||
sql, | ||
(err, res: mysqlTypes.RowDataPacket[]) => { | ||
assert.ifError(err); | ||
assert.ok(res); | ||
assert.strictEqual(res[0].solution, 2); | ||
const spans = memoryExporter.getFinishedSpans(); | ||
assert.strictEqual(spans.length, 1); | ||
assertSpan(spans[0], sql); | ||
assert.strictEqual( | ||
spans[0].attributes[queryResultAttribute], | ||
JSON.stringify(res) | ||
); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function assertSpan( | ||
|