|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const agent = require('../../dd-trace/test/plugins/agent') |
| 4 | +const { breakThen, unbreakThen } = require('../../dd-trace/test/plugins/helpers') |
| 5 | +const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants') |
| 6 | + |
| 7 | +const { expectedSchema, rawExpectedSchema } = require('./naming') |
| 8 | + |
| 9 | +describe('Plugin', () => { |
| 10 | + let Redis |
| 11 | + let redis |
| 12 | + let tracer |
| 13 | + |
| 14 | + describe('iovalkey', () => { |
| 15 | + withVersions('iovalkey', 'iovalkey', version => { |
| 16 | + beforeEach(() => { |
| 17 | + tracer = require('../../dd-trace') |
| 18 | + Redis = require(`../../../versions/iovalkey@${version}`).get() |
| 19 | + redis = new Redis({ connectionName: 'test' }) |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + unbreakThen(Promise.prototype) |
| 24 | + redis.quit() |
| 25 | + }) |
| 26 | + |
| 27 | + describe('without configuration', () => { |
| 28 | + before(() => agent.load(['iovalkey'])) |
| 29 | + |
| 30 | + after(() => agent.close({ ritmReset: false })) |
| 31 | + |
| 32 | + it('should do automatic instrumentation when using callbacks', done => { |
| 33 | + agent.use(() => {}) // wait for initial info command |
| 34 | + agent |
| 35 | + .use(traces => { |
| 36 | + expect(traces[0][0]).to.have.property('name', expectedSchema.outbound.opName) |
| 37 | + expect(traces[0][0]).to.have.property('service', expectedSchema.outbound.serviceName) |
| 38 | + expect(traces[0][0]).to.have.property('resource', 'get') |
| 39 | + expect(traces[0][0]).to.have.property('type', 'redis') |
| 40 | + expect(traces[0][0].meta).to.have.property('component', 'iovalkey') |
| 41 | + expect(traces[0][0].meta).to.have.property('db.name', '0') |
| 42 | + expect(traces[0][0].meta).to.have.property('db.type', 'redis') |
| 43 | + expect(traces[0][0].meta).to.have.property('span.kind', 'client') |
| 44 | + expect(traces[0][0].meta).to.have.property('out.host', 'localhost') |
| 45 | + expect(traces[0][0].meta).to.have.property('redis.raw_command', 'GET foo') |
| 46 | + expect(traces[0][0].metrics).to.have.property('network.destination.port', 6379) |
| 47 | + }) |
| 48 | + .then(done) |
| 49 | + .catch(done) |
| 50 | + |
| 51 | + redis.get('foo').catch(done) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should run the callback in the parent context', () => { |
| 55 | + const span = {} |
| 56 | + |
| 57 | + return tracer.scope().activate(span, () => { |
| 58 | + return redis.get('foo') |
| 59 | + .then(() => { |
| 60 | + expect(tracer.scope().active()).to.equal(span) |
| 61 | + }) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should handle errors', done => { |
| 66 | + let error |
| 67 | + |
| 68 | + agent.use(() => {}) // wait for initial info command |
| 69 | + agent |
| 70 | + .use(traces => { |
| 71 | + expect(traces[0][0]).to.have.property('error', 1) |
| 72 | + expect(traces[0][0].meta).to.have.property(ERROR_TYPE, error.name) |
| 73 | + expect(traces[0][0].meta).to.have.property(ERROR_MESSAGE, error.message) |
| 74 | + expect(traces[0][0].meta).to.have.property(ERROR_STACK, error.stack) |
| 75 | + expect(traces[0][0].meta).to.have.property('component', 'iovalkey') |
| 76 | + }) |
| 77 | + .then(done) |
| 78 | + .catch(done) |
| 79 | + |
| 80 | + redis.set('foo', 123, 'bar') |
| 81 | + .catch(err => { |
| 82 | + error = err |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should work with userland promises', done => { |
| 87 | + agent.use(() => {}) // wait for initial info command |
| 88 | + agent |
| 89 | + .use(traces => { |
| 90 | + expect(traces[0][0]).to.have.property('name', expectedSchema.outbound.opName) |
| 91 | + expect(traces[0][0]).to.have.property('service', expectedSchema.outbound.serviceName) |
| 92 | + expect(traces[0][0]).to.have.property('resource', 'get') |
| 93 | + expect(traces[0][0]).to.have.property('type', 'redis') |
| 94 | + expect(traces[0][0].meta).to.have.property('db.name', '0') |
| 95 | + expect(traces[0][0].meta).to.have.property('db.type', 'redis') |
| 96 | + expect(traces[0][0].meta).to.have.property('span.kind', 'client') |
| 97 | + expect(traces[0][0].meta).to.have.property('out.host', 'localhost') |
| 98 | + expect(traces[0][0].meta).to.have.property('redis.raw_command', 'GET foo') |
| 99 | + expect(traces[0][0].meta).to.have.property('component', 'iovalkey') |
| 100 | + expect(traces[0][0].metrics).to.have.property('network.destination.port', 6379) |
| 101 | + }) |
| 102 | + .then(done) |
| 103 | + .catch(done) |
| 104 | + |
| 105 | + breakThen(Promise.prototype) |
| 106 | + |
| 107 | + redis.get('foo').catch(done) |
| 108 | + }) |
| 109 | + |
| 110 | + withNamingSchema( |
| 111 | + done => redis.get('foo').catch(done), |
| 112 | + rawExpectedSchema.outbound |
| 113 | + ) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('with configuration', () => { |
| 117 | + before(() => agent.load('iovalkey', { |
| 118 | + service: 'custom', |
| 119 | + splitByInstance: true, |
| 120 | + allowlist: ['get'] |
| 121 | + })) |
| 122 | + |
| 123 | + after(() => agent.close({ ritmReset: false })) |
| 124 | + |
| 125 | + it('should be configured with the correct values', done => { |
| 126 | + agent |
| 127 | + .use(traces => { |
| 128 | + expect(traces[0][0]).to.have.property('service', 'custom-test') |
| 129 | + }) |
| 130 | + .then(done) |
| 131 | + .catch(done) |
| 132 | + |
| 133 | + redis.get('foo').catch(done) |
| 134 | + }) |
| 135 | + |
| 136 | + it('should be able to filter commands', done => { |
| 137 | + agent.use(() => {}) // wait for initial command |
| 138 | + agent |
| 139 | + .use(traces => { |
| 140 | + expect(traces[0][0]).to.have.property('resource', 'get') |
| 141 | + }) |
| 142 | + .then(done) |
| 143 | + .catch(done) |
| 144 | + |
| 145 | + redis.get('foo').catch(done) |
| 146 | + }) |
| 147 | + |
| 148 | + withNamingSchema( |
| 149 | + done => redis.get('foo').catch(done), |
| 150 | + { |
| 151 | + v0: { |
| 152 | + opName: 'redis.command', |
| 153 | + serviceName: 'custom-test' |
| 154 | + }, |
| 155 | + v1: { |
| 156 | + opName: 'redis.command', |
| 157 | + serviceName: 'custom' |
| 158 | + } |
| 159 | + } |
| 160 | + ) |
| 161 | + }) |
| 162 | + |
| 163 | + describe('with legacy configuration', () => { |
| 164 | + before(() => agent.load('iovalkey', { |
| 165 | + whitelist: ['get'] |
| 166 | + })) |
| 167 | + |
| 168 | + after(() => agent.close({ ritmReset: false })) |
| 169 | + |
| 170 | + it('should be able to filter commands', done => { |
| 171 | + agent.use(() => {}) // wait for initial command |
| 172 | + agent |
| 173 | + .use(traces => { |
| 174 | + expect(traces[0][0]).to.have.property('resource', 'get') |
| 175 | + }) |
| 176 | + .then(done) |
| 177 | + .catch(done) |
| 178 | + |
| 179 | + redis.get('foo').catch(done) |
| 180 | + }) |
| 181 | + }) |
| 182 | + }) |
| 183 | + }) |
| 184 | +}) |
0 commit comments