Skip to content

Commit

Permalink
test: Updated unit tests that were missing constructing specs at inst…
Browse files Browse the repository at this point in the history
…rumentation source (#2252)
  • Loading branch information
bizob2828 authored Jun 6, 2024
1 parent 8b1fa5d commit 54ab238
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 140 deletions.
2 changes: 1 addition & 1 deletion lib/instrumentation/amqplib/amqplib.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function wrapModel(shim, Model, promiseMode) {
if (TEMP_RE.test(queue)) {
queue = null
}
return new MessageSubscribeSpec({
return new MessageSpec({
queue,
promise: promiseMode,
callback: setCallback(shim, promiseMode)
Expand Down
5 changes: 3 additions & 2 deletions test/unit/metric/datastore-instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const tap = require('tap')
const helper = require('../../lib/agent_helper')
const DatastoreShim = require('../../../lib/shim/datastore-shim')
const tests = require('../../lib/cross_agent_tests/datastores/datastore_instances')
const DatastoreParameters = require('../../../lib/shim/specs/params/datastore')

tap.test('Datastore instance metrics collected via the datastore shim', function (t) {
t.autoend()
Expand Down Expand Up @@ -53,10 +54,10 @@ tap.test('Datastore instance metrics collected via the datastore shim', function
port = test.unix_socket || test.database_path || test.port
}
return {
parameters: {
parameters: new DatastoreParameters({
host: dbHost,
port_path_or_id: port
}
})
}
})

Expand Down
92 changes: 57 additions & 35 deletions test/unit/shim/datastore-shim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const helper = require('../../lib/agent_helper')
const Shim = require('../../../lib/shim/shim')
const DatastoreShim = require('../../../lib/shim/datastore-shim')
const ParsedStatement = require('../../../lib/db/parsed-statement')
const { QuerySpec, OperationSpec } = require('../../../lib/shim/specs')

test('DatastoreShim', function (t) {
t.autoend()
Expand Down Expand Up @@ -371,7 +372,7 @@ test('DatastoreShim', function (t) {

t.test('should create a child segment when opaque is false', (t) => {
shim.recordOperation(wrappable, 'withNested', () => {
return { name: 'test', opaque: false }
return new OperationSpec({ name: 'test', opaque: false })
})
helper.runInTransaction(agent, (tx) => {
const startingSegment = agent.tracer.getSegment()
Expand All @@ -388,7 +389,7 @@ test('DatastoreShim', function (t) {

t.test('should not create a child segment when opaque is true', (t) => {
shim.recordOperation(wrappable, 'withNested', () => {
return { name: 'test', opaque: true }
return new OperationSpec({ name: 'test', opaque: true })
})
helper.runInTransaction(agent, (tx) => {
const startingSegment = agent.tracer.getSegment()
Expand Down Expand Up @@ -455,7 +456,7 @@ test('DatastoreShim', function (t) {
beforeEach()
localhost = getMetricHostName(agent, 'localhost')
shim.recordOperation(wrappable, 'getActiveSegment', function (s, fn, n, args) {
return { parameters: args[0] }
return new OperationSpec({ parameters: args[0] })
})
})
t.afterEach(afterEach)
Expand Down Expand Up @@ -547,14 +548,14 @@ test('DatastoreShim', function (t) {
t.beforeEach(function () {
beforeEach()
shim.recordOperation(wrappable, 'getActiveSegment', function () {
return {
return new OperationSpec({
name: 'op',
parameters: {
host: 'some_host',
port_path_or_id: 1234,
database_name: 'foobar'
}
}
})
})

return new Promise((resolve) => {
Expand Down Expand Up @@ -627,11 +628,15 @@ test('DatastoreShim', function (t) {
t.test(
'should create a datastore query segment but no metric when `record` is false',
function (t) {
shim.recordQuery(wrappable, 'getActiveSegment', {
query: shim.FIRST,
record: false,
name: 'getActiveSegment'
})
shim.recordQuery(
wrappable,
'getActiveSegment',
new QuerySpec({
query: shim.FIRST,
record: false,
name: 'getActiveSegment'
})
)

helper.runInTransaction(agent, function (tx) {
const startingSegment = agent.tracer.getSegment()
Expand All @@ -646,7 +651,11 @@ test('DatastoreShim', function (t) {
)

t.test('should create a datastore query metric when `record` is true', function (t) {
shim.recordQuery(wrappable, 'getActiveSegment', { query: shim.FIRST, record: true })
shim.recordQuery(
wrappable,
'getActiveSegment',
new QuerySpec({ query: shim.FIRST, record: true })
)

helper.runInTransaction(agent, function (tx) {
const startingSegment = agent.tracer.getSegment()
Expand All @@ -660,7 +669,7 @@ test('DatastoreShim', function (t) {
})

t.test('should create a datastore query metric when `record` is defaulted', function (t) {
shim.recordQuery(wrappable, 'getActiveSegment', { query: shim.FIRST })
shim.recordQuery(wrappable, 'getActiveSegment', new QuerySpec({ query: shim.FIRST }))

helper.runInTransaction(agent, function (tx) {
const startingSegment = agent.tracer.getSegment()
Expand Down Expand Up @@ -691,14 +700,17 @@ test('DatastoreShim', function (t) {
t.test('should allow after handlers to be specified', function (t) {
let executed = false
const toWrap = function () {}
const wrapped = shim.recordQuery(toWrap, {
query: function () {
return 'test'
},
after: function () {
executed = true
}
})
const wrapped = shim.recordQuery(
toWrap,
new QuerySpec({
query: function () {
return 'test'
},
after: function () {
executed = true
}
})
)

helper.runInTransaction(agent, function () {
t.notOk(executed)
Expand All @@ -710,10 +722,13 @@ test('DatastoreShim', function (t) {

t.test('should bind the callback if there is one', function (t) {
const cb = function () {}
const wrapped = shim.recordQuery(helper.checkWrappedCb.bind(t, shim, cb), {
query: shim.FIRST,
callback: shim.LAST
})
const wrapped = shim.recordQuery(
helper.checkWrappedCb.bind(t, shim, cb),
new QuerySpec({
query: shim.FIRST,
callback: shim.LAST
})
)

helper.runInTransaction(agent, function () {
wrapped(query, cb)
Expand All @@ -723,23 +738,30 @@ test('DatastoreShim', function (t) {
t.test('should bind the row callback if there is one', function (t) {
const cb = function () {}

const wrapped = shim.recordQuery(helper.checkWrappedCb.bind(t, shim, cb), {
query: shim.FIRST,
rowCallback: shim.LAST
})
const wrapped = shim.recordQuery(
helper.checkWrappedCb.bind(t, shim, cb),
new QuerySpec({
query: shim.FIRST,
rowCallback: shim.LAST
})
)

helper.runInTransaction(agent, function () {
wrapped(query, cb)
})
})

t.test('should execute inContext function when specified in spec', function (t) {
shim.recordQuery(wrappable, 'bar', {
query: 'select foo from bar;',
inContext(segment) {
segment.addAttribute('test-attr', 'unit-test')
}
})
shim.recordQuery(
wrappable,
'bar',
new QuerySpec({
query: 'select foo from bar;',
inContext(segment) {
segment.addAttribute('test-attr', 'unit-test')
}
})
)

helper.runInTransaction(agent, (tx) => {
wrappable.bar()
Expand Down Expand Up @@ -797,7 +819,7 @@ test('DatastoreShim', function (t) {
})

t.test('should create a datastore batch query metric', function (t) {
shim.recordBatchQuery(wrappable, 'getActiveSegment', { query: shim.FIRST })
shim.recordBatchQuery(wrappable, 'getActiveSegment', new QuerySpec({ query: shim.FIRST }))

helper.runInTransaction(agent, function (tx) {
const startingSegment = agent.tracer.getSegment()
Expand Down
Loading

0 comments on commit 54ab238

Please sign in to comment.