|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | + |
| 5 | +const dc = require('dc-polyfill') |
| 6 | +const { afterEach, beforeEach, describe, it } = require('mocha') |
| 7 | + |
| 8 | +const { storage } = require('../../datadog-core') |
| 9 | + |
| 10 | +require('../src/aerospike') |
| 11 | + |
| 12 | +const HOOK = globalThis[Symbol.for('_ddtrace_instrumentations')].aerospike |
| 13 | + .find(entry => entry.file === 'lib/commands/command.js') |
| 14 | + .hook |
| 15 | + |
| 16 | +const commandStorage = storage('aerospike-command-test') |
| 17 | +const commandChannel = dc.tracingChannel('apm:aerospike:command') |
| 18 | + |
| 19 | +function wrapCommandFactory (commandFactory) { |
| 20 | + return HOOK(commandFactory)() |
| 21 | +} |
| 22 | + |
| 23 | +describe('packages/datadog-instrumentations/src/aerospike.js', () => { |
| 24 | + let starts |
| 25 | + let asyncStarts |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + starts = 0 |
| 29 | + asyncStarts = 0 |
| 30 | + |
| 31 | + commandChannel.start.bindStore(commandStorage, ctx => { |
| 32 | + starts++ |
| 33 | + const parentStore = commandStorage.getStore() |
| 34 | + ctx.parentStore = parentStore |
| 35 | + ctx.currentStore = { ...parentStore, span: { name: 'aerospike-command' } } |
| 36 | + return ctx.currentStore |
| 37 | + }) |
| 38 | + |
| 39 | + commandChannel.asyncStart.bindStore(commandStorage, ctx => { |
| 40 | + asyncStarts++ |
| 41 | + return ctx.parentStore |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + commandChannel.start.unbindStore(commandStorage) |
| 47 | + commandChannel.asyncStart.unbindStore(commandStorage) |
| 48 | + }) |
| 49 | + |
| 50 | + it('runs callbacks in the parent context after Aerospike defers a synchronous result', async () => { |
| 51 | + const Command = wrapCommandFactory(() => class FakeCommand { |
| 52 | + constructor () { |
| 53 | + this.args = ['arg'] |
| 54 | + this.client = { config: { hosts: '127.0.0.1:3000' } } |
| 55 | + } |
| 56 | + |
| 57 | + process (callback) { |
| 58 | + callback(null, 'ok') |
| 59 | + } |
| 60 | + |
| 61 | + executeWithCallback (callback) { |
| 62 | + let sync = true |
| 63 | + this.process((error, result) => { |
| 64 | + if (sync) { |
| 65 | + process.nextTick(callback, error, result) |
| 66 | + } else { |
| 67 | + callback(error, result) |
| 68 | + } |
| 69 | + }) |
| 70 | + sync = false |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + const parentSpan = { name: 'parent' } |
| 75 | + const command = new Command() |
| 76 | + |
| 77 | + const result = await new Promise((resolve, reject) => { |
| 78 | + commandStorage.run({ span: parentSpan }, () => { |
| 79 | + command.executeWithCallback((error, value) => { |
| 80 | + try { |
| 81 | + assert.ifError(error) |
| 82 | + assert.equal(commandStorage.getStore()?.span, parentSpan) |
| 83 | + resolve(value) |
| 84 | + } catch (err) { |
| 85 | + reject(err) |
| 86 | + } |
| 87 | + }) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + assert.equal(result, 'ok') |
| 92 | + assert.equal(starts, 1) |
| 93 | + assert.equal(asyncStarts, 1) |
| 94 | + }) |
| 95 | + |
| 96 | + it('still traces commands through process when no callback helper exists', async () => { |
| 97 | + const Command = wrapCommandFactory(() => class FakeCommand { |
| 98 | + constructor () { |
| 99 | + this.args = ['arg'] |
| 100 | + this.client = { config: { hosts: '127.0.0.1:3000' } } |
| 101 | + } |
| 102 | + |
| 103 | + process (callback) { |
| 104 | + process.nextTick(callback, null, 'ok') |
| 105 | + } |
| 106 | + |
| 107 | + executeAndReturnPromise () { |
| 108 | + return new Promise((resolve, reject) => { |
| 109 | + this.process((error, result) => { |
| 110 | + if (error) { |
| 111 | + reject(error) |
| 112 | + } else { |
| 113 | + resolve(result) |
| 114 | + } |
| 115 | + }) |
| 116 | + }) |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + const parentSpan = { name: 'parent' } |
| 121 | + const command = new Command() |
| 122 | + |
| 123 | + const result = await commandStorage.run({ span: parentSpan }, () => command.executeAndReturnPromise()) |
| 124 | + |
| 125 | + assert.equal(result, 'ok') |
| 126 | + assert.equal(starts, 1) |
| 127 | + assert.equal(asyncStarts, 1) |
| 128 | + }) |
| 129 | +}) |
0 commit comments