Skip to content

Commit

Permalink
test: Migrated test/integration/api tests to node:test (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Nov 25, 2024
1 parent eebf06b commit 03b01a3
Show file tree
Hide file tree
Showing 8 changed files with 585 additions and 589 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,78 @@

'use strict'

const tap = require('tap')

const test = require('node:test')
const assert = require('node:assert')
const API = require('../../../api')
const AttributeFilter = require('../../../lib/config/attribute-filter')
const helper = require('../../lib/agent_helper')

const DESTINATIONS = AttributeFilter.DESTINATIONS

tap.test('#addAttribute', (t) => {
t.autoend()

let agent = null
let api = null

t.beforeEach(() => {
agent = helper.loadMockedAgent({
test('#addAttribute', async (t) => {
t.beforeEach((ctx) => {
const agent = helper.loadMockedAgent({
distributed_tracing: {
enabled: true
}
})
api = new API(agent)
const api = new API(agent)
ctx.nr = {
agent,
api
}
})

t.afterEach(() => {
helper.unloadAgent(agent)
t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

t.test('should add attribute to current span', (t) => {
await t.test('should add attribute to current span', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
api.addCustomAttribute('key1', 'value1')

const customSpanAttributes = getCustomSpanAttributes(agent)
t.equal(customSpanAttributes.key1, 'value1')
assert.equal(customSpanAttributes.key1, 'value1')

t.end()
end()
})
})
})

t.test('should overwrite for same key added via addCustomAttribute', (t) => {
await t.test('should overwrite for same key added via addCustomAttribute', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
api.addCustomAttribute('key1', 'value1')
api.addCustomAttribute('key1', 'last-wins')

const customSpanAttributes = getCustomSpanAttributes(agent)
t.equal(customSpanAttributes.key1, 'last-wins')
assert.equal(customSpanAttributes.key1, 'last-wins')

t.end()
end()
})
})
})

t.test('should not overwrite for same key added via addCustomSpanAttribute', (t) => {
await t.test('should not overwrite for same key added via addCustomSpanAttribute', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
api.addCustomSpanAttribute('key1', 'custom-span-wins')
api.addCustomAttribute('key1', 'does-not-overwrite')

const customSpanAttributes = getCustomSpanAttributes(agent)
t.equal(customSpanAttributes.key1, 'custom-span-wins')
assert.equal(customSpanAttributes.key1, 'custom-span-wins')

t.end()
end()
})
})
})

t.test('should not add attribute when over the limit', (t) => {
await t.test('should not add attribute when over the limit', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
// the cap is 64
Expand All @@ -86,34 +89,34 @@ tap.test('#addAttribute', (t) => {
const customSpanAttributes = getCustomSpanAttributes(agent)
const hasAttribute = Object.hasOwnProperty.bind(customSpanAttributes)

t.notOk(hasAttribute(unexpectedAttributeName))
assert.ok(!hasAttribute(unexpectedAttributeName))

t.end()
end()
})
})
})
})

tap.test('#addCustomSpanAttribute', (t) => {
t.autoend()

let agent = null
let api = null

t.beforeEach(() => {
agent = helper.loadMockedAgent({
test('#addCustomSpanAttribute', async (t) => {
t.beforeEach((ctx) => {
const agent = helper.loadMockedAgent({
distributed_tracing: {
enabled: true
}
})
api = new API(agent)
const api = new API(agent)
ctx.nr = {
agent,
api
}
})

t.afterEach(() => {
helper.unloadAgent(agent)
t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

t.test('should not add attribute to transaction', (t) => {
await t.test('should not add attribute to transaction', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, (transaction) => {
api.startSegment('segment1', true, () => {
const unexpectedAttributeName = 'should-not-exist'
Expand All @@ -122,82 +125,91 @@ tap.test('#addCustomSpanAttribute', (t) => {
const customTransactionAttributes = getCustomTransactionAttributes(transaction)
const hasAttribute = Object.hasOwnProperty.bind(customTransactionAttributes)

t.notOk(hasAttribute(unexpectedAttributeName))
assert.ok(!hasAttribute(unexpectedAttributeName))

t.end()
end()
})
})
})

t.test('should overwrite for same key added via addCustomAttribute', (t) => {
await t.test('should overwrite for same key added via addCustomAttribute', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
api.addCustomAttribute('key1', 'value1')
api.addCustomSpanAttribute('key1', 'custom-span-wins')

const customSpanAttributes = getCustomSpanAttributes(agent)

t.equal(customSpanAttributes.key1, 'custom-span-wins')
assert.equal(customSpanAttributes.key1, 'custom-span-wins')

t.end()
end()
})
})
})

t.test('should overwrite for same key added via addCustomSpanAttribute', (t) => {
await t.test('should overwrite for same key added via addCustomSpanAttribute', (t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
api.addCustomSpanAttribute('key1', 'value1')
api.addCustomSpanAttribute('key1', 'last-wins')

const customSpanAttributes = getCustomSpanAttributes(agent)

t.equal(customSpanAttributes.key1, 'last-wins')
assert.equal(customSpanAttributes.key1, 'last-wins')

t.end()
end()
})
})
})

t.test('should replace newest added via addCustomAttribute when over the limit', (t) => {
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
// the cap is 64
const lastAddedName = batchAddCustomAttributes(api, 32)
batchAddCustomSpanAttributes(api, 32)
await t.test(
'should replace newest added via addCustomAttribute when over the limit',
(t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
// the cap is 64
const lastAddedName = batchAddCustomAttributes(api, 32)
batchAddCustomSpanAttributes(api, 32)

api.addCustomSpanAttribute('should-replace-add-custom', 'replaced')
api.addCustomSpanAttribute('should-replace-add-custom', 'replaced')

const customSpanAttributes = getCustomSpanAttributes(agent)
const hasAttribute = Object.hasOwnProperty.bind(customSpanAttributes)
const customSpanAttributes = getCustomSpanAttributes(agent)
const hasAttribute = Object.hasOwnProperty.bind(customSpanAttributes)

t.notOk(hasAttribute(lastAddedName), 'should drop last added via addCustomAttribute')
assert.ok(!hasAttribute(lastAddedName), 'should drop last added via addCustomAttribute')

t.equal(customSpanAttributes['should-replace-add-custom'], 'replaced')
assert.equal(customSpanAttributes['should-replace-add-custom'], 'replaced')

t.end()
end()
})
})
})
})

t.test('should not replace any added via addCustomSpanAttribute when over the limit', (t) => {
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
// the cap is 64
batchAddCustomSpanAttributes(api, 64)

const unexpectedAttributeName = 'should-not-replace-add-custom-span'
api.addCustomSpanAttribute(unexpectedAttributeName, 'does-not-exist')

const customSpanAttributes = getCustomSpanAttributes(agent)
const hasAttribute = Object.hasOwnProperty.bind(customSpanAttributes)

t.notOk(hasAttribute(unexpectedAttributeName))

t.end()
}
)

await t.test(
'should not replace any added via addCustomSpanAttribute when over the limit',
(t, end) => {
const { agent, api } = t.nr
helper.runInTransaction(agent, () => {
api.startSegment('segment1', true, () => {
// the cap is 64
batchAddCustomSpanAttributes(api, 64)

const unexpectedAttributeName = 'should-not-replace-add-custom-span'
api.addCustomSpanAttribute(unexpectedAttributeName, 'does-not-exist')

const customSpanAttributes = getCustomSpanAttributes(agent)
const hasAttribute = Object.hasOwnProperty.bind(customSpanAttributes)

assert.ok(!hasAttribute(unexpectedAttributeName))
end()
})
})
})
})
}
)
})

function getCustomSpanAttributes(agent) {
Expand Down
91 changes: 0 additions & 91 deletions test/integration/api/instrument-loaded.tap.js

This file was deleted.

Loading

0 comments on commit 03b01a3

Please sign in to comment.