/
pg.spec.js
77 lines (63 loc) · 1.95 KB
/
pg.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const pg = require('pg')
const knex = require('knex')
const { expect } = require('chai')
const { Tracer, Tags } = require('opentracing')
const cls = require('../cls')
const instrumentation = require('./pg')
describe('instrumentation: pg', () => {
let tracer
let mockChildSpan
let db
beforeEach(function () {
tracer = new Tracer()
mockChildSpan = {
setTag: this.sandbox.spy(),
log: this.sandbox.spy(),
finish: this.sandbox.spy()
}
this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan)
instrumentation.patch(pg, [tracer])
db = knex({
client: 'pg',
connection: process.env.PG_URI
})
})
afterEach(() => {
instrumentation.unpatch(pg)
})
describe('#patch', () => {
it('should start and finish span', async () => {
const query = 'SELECT 1 AS result'
const { rows } = await db.raw(query)
expect(rows).to.be.eql([{ result: 1 }])
expect(cls.startChildSpan).to.be.calledWith(tracer, `${instrumentation.OPERATION_NAME}_query`, {
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_CLIENT,
[Tags.DB_TYPE]: instrumentation.DB_TYPE,
[Tags.DB_STATEMENT]: query
}
})
// FIXME: only with ../instrument.js tests together
// expect(mockChildSpan.finish).to.have.callCount(1)
})
it('should flag error', async () => {
const query = 'SELECT invalid AS result'
try {
await db.raw(query)
} catch (err) {
expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true)
expect(mockChildSpan.log).to.be.calledWith({
event: 'error',
'error.object': err,
message: 'column "invalid" does not exist',
stack: err.stack
})
// FIXME: only with ../instrument.js tests together
// expect(mockChildSpan.finish).to.have.callCount(1)
return
}
throw new Error('Uncaught exception')
})
})
})