Skip to content

Commit

Permalink
test: Updated tests that used the context manager directly and instea…
Browse files Browse the repository at this point in the history
…d use the tracer to access the segment context (newrelic#2643)
  • Loading branch information
bizob2828 authored Oct 9, 2024
1 parent 1a80ad5 commit b917b3e
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 228 deletions.
5 changes: 5 additions & 0 deletions lib/transaction/tracer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Tracer(agent, contextManager) {

Tracer.prototype.getTransaction = getTransaction
Tracer.prototype.getSegment = getSegment
Tracer.prototype.setSegment = setSegment
Tracer.prototype.getSpanContext = getSpanContext
Tracer.prototype.createSegment = createSegment
Tracer.prototype.addSegment = addSegment
Expand Down Expand Up @@ -57,6 +58,10 @@ function getSegment() {
return this._contextManager.getContext()
}

function setSegment(segment) {
this._contextManager.setContext(segment)
}

// TODO: Remove/replace external uses to tracer.getSpanContext()
function getSpanContext() {
const currentSegment = this.getSegment()
Expand Down
6 changes: 3 additions & 3 deletions test/benchmark/shim/record.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Shim = require('../../../lib/shim/shim')
const { RecorderSpec } = require('../../../lib/shim/specs')

const agent = helper.loadMockedAgent()
const contextManager = helper.getContextManager()
const tracer = helper.getTracer()

const shim = new Shim(agent, 'test-module', './')
const suite = benchmark.createBenchmark({ name: 'Shim#record' })
Expand Down Expand Up @@ -41,15 +41,15 @@ const wrapped = shim.record(getTest(), 'func', function () {
suite.add({
name: 'wrapper - no transaction',
fn: function () {
contextManager.setContext(null)
tracer.setSegment(null)
wrapped.func(noop)
}
})

suite.add({
name: 'wrapper - in transaction',
fn: function () {
contextManager.setContext(transaction.trace.root)
tracer.setSegment(transaction.trace.root)
wrapped.func(noop)
}
})
Expand Down
5 changes: 2 additions & 3 deletions test/benchmark/tracer/bindFunction.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ const shared = require('./shared')

const s = shared.makeSuite()
const suite = s.suite
const tracer = s.agent.tracer
const contextManager = helper.getContextManager()
const tracer = helper.getTracer()
const tx = helper.runInTransaction(s.agent, function (_tx) {
return _tx
})
contextManager.setContext(tx.root)
tracer.setSegment(tx.root)

preOptBind()
const bound = tracer.bindFunction(shared.getTest().func, tx.root, true)
Expand Down
5 changes: 2 additions & 3 deletions test/benchmark/tracer/segments.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ const shared = require('./shared')

const s = shared.makeSuite('Tracer segments')
const suite = s.suite
const tracer = s.agent.tracer
const contextManager = helper.getContextManager()
const tracer = helper.getTracer()

const tx = helper.runInTransaction(s.agent, function (_tx) {
return _tx
})

contextManager.setContext(tx.root)
tracer.setSegment(tx.root)

suite.add({
name: 'tracer.getSegment',
Expand Down
6 changes: 2 additions & 4 deletions test/benchmark/tracer/transaction.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ const shared = require('./shared')

const s = shared.makeSuite('Tracer transactions')
const suite = s.suite
const tracer = s.agent.tracer

const contextManager = helper.getContextManager()
const tracer = helper.getTracer()
const tx = helper.runInTransaction(s.agent, function (_tx) {
return _tx
})

contextManager.setContext(tx.root)
tracer.setSegment(tx.root)

suite.add({
name: 'tracer.getTransaction',
Expand Down
5 changes: 2 additions & 3 deletions test/benchmark/tracer/utilities.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ const helper = require('../../lib/agent_helper')

const s = shared.makeSuite('Tracer utilities')
const suite = s.suite
const tracer = s.agent.tracer
const contextManager = helper.getContextManager()
const tracer = helper.getTracer()

const tx = helper.runInTransaction(s.agent, function (_tx) {
return _tx
})

contextManager.setContext(tx.root)
tracer.setSegment(tx.root)

suite.add({
name: 'tracer.slice',
Expand Down
6 changes: 3 additions & 3 deletions test/benchmark/webframework-shim/recordMiddleware.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const WebFrameworkShim = require('../../../lib/shim/webframework-shim')
const symbols = require('../../../lib/symbols')

const agent = helper.loadMockedAgent()
const contextManager = helper.getContextManager()
const tracer = helper.getTracer()
const shim = new WebFrameworkShim(agent, 'test-module', './')
const { MiddlewareSpec } = require('../../../lib/shim/specs')
const suite = benchmark.createBenchmark({ name: 'recordMiddleware' })
Expand Down Expand Up @@ -59,15 +59,15 @@ function addTests(name, speccer) {
suite.add({
name: name + ' - wrapper (no tx) ',
fn: function () {
contextManager.setContext(null)
tracer.setSegment(null)
middleware(getReqd(), {}, noop)
}
})

suite.add({
name: name + ' - wrapper (tx) ',
fn: function () {
contextManager.setContext(transaction.trace.root)
tracer.setSegment(transaction.trace.root)
middleware(getReqd(), {}, noop)
}
})
Expand Down
34 changes: 15 additions & 19 deletions test/integration/core/native-promises/native-promises.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ function createPromiseTests(t, config) {
})

t.test('restores context in inactive transactions', function (t) {
const { agent, contextManager } = setupAgent(t, config)
const { agent, tracer } = setupAgent(t, config)

helper.runInTransaction(agent, function (txn) {
const res = new TestResource(1)
const root = contextManager.getContext()
const root = tracer.getSegment()
txn.end()
res.doStuff(function () {
t.equal(
contextManager.getContext(),
tracer.getSegment(),
root,
'should restore a segment when its transaction has been ended'
)
Expand All @@ -285,52 +285,48 @@ function createPromiseTests(t, config) {
})

t.test('handles multi-entry callbacks correctly', function (t) {
const { agent, contextManager } = setupAgent(t, config)
const { agent, tracer } = setupAgent(t, config)

helper.runInTransaction(agent, function () {
const root = contextManager.getContext()
const root = tracer.getSegment()

const aSeg = agent.tracer.createSegment('A')
contextManager.setContext(aSeg)
tracer.setSegment(aSeg)

const resA = new TestResource(1)

const bSeg = agent.tracer.createSegment('B')
contextManager.setContext(bSeg)
tracer.setSegment(bSeg)
const resB = new TestResource(2)

contextManager.setContext(root)
tracer.setSegment(root)

resA.doStuff(() => {
t.equal(
contextManager.getContext().name,
tracer.getSegment().name,
aSeg.name,
'runInAsyncScope should restore the segment active when a resource was made'
)

resB.doStuff(() => {
t.equal(
contextManager.getContext().name,
tracer.getSegment().name,
bSeg.name,
'runInAsyncScope should restore the segment active when a resource was made'
)

t.end()
})
t.equal(
contextManager.getContext().name,
tracer.getSegment().name,
aSeg.name,
'runInAsyncScope should restore the segment active when a callback was called'
)
})
t.equal(
contextManager.getContext().name,
root.name,
'root should be restored after we are finished'
)
t.equal(tracer.getSegment().name, root.name, 'root should be restored after we are finished')
resA.doStuff(() => {
t.equal(
contextManager.getContext().name,
tracer.getSegment().name,
aSeg.name,
'runInAsyncScope should restore the segment active when a resource was made'
)
Expand Down Expand Up @@ -609,11 +605,11 @@ function setupAgent(t, config) {
helper.unloadAgent(agent)
})

const contextManager = helper.getContextManager()
const tracer = helper.getTracer()

return {
agent,
contextManager
tracer
}
}

Expand Down
14 changes: 7 additions & 7 deletions test/integration/core/net.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ function id(tx) {
}

test('createServer', function createServerTest(t) {
const { agent, contextManager } = setupAgent(t)
const { agent, tracer } = setupAgent(t)

helper.runInTransaction(agent, function transactionWrapper(transaction) {
const server = net.createServer(handler)

server.listen(4123, function listening() {
// leave transaction
contextManager.setContext(null)
tracer.setSegment(null)
const socket = net.connect({ port: 4123 })
socket.write('test123')
socket.end()
Expand All @@ -31,7 +31,7 @@ test('createServer', function createServerTest(t) {
t.equal(id(agent.getTransaction()), id(transaction), 'should maintain tx')
socket.end('test')
t.equal(
contextManager.getContext().name,
tracer.getSegment().name,
'net.Server.onconnection',
'child segment should have correct name'
)
Expand Down Expand Up @@ -135,7 +135,7 @@ test('connect', function connectTest(t) {
})

test('createServer and connect', function createServerTest(t) {
const { agent, contextManager } = setupAgent(t)
const { agent, tracer } = setupAgent(t)

helper.runInTransaction(agent, function transactionWrapper(transaction) {
const server = net.createServer(handler)
Expand All @@ -150,7 +150,7 @@ test('createServer and connect', function createServerTest(t) {
t.equal(id(agent.getTransaction()), id(transaction), 'should maintain tx')
socket.end('test')
t.equal(
contextManager.getContext().name,
tracer.getSegment().name,
'net.Server.onconnection',
'child segment should have correct name'
)
Expand Down Expand Up @@ -199,14 +199,14 @@ test('createServer and connect', function createServerTest(t) {

function setupAgent(t) {
const agent = helper.instrumentMockedAgent()
const contextManager = helper.getContextManager()
const tracer = helper.getTracer()

t.teardown(function tearDown() {
helper.unloadAgent(agent)
})

return {
agent,
contextManager
tracer
}
}
3 changes: 1 addition & 2 deletions test/integration/core/timers.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,10 @@ tap.test('clearTimeout should not ignore parent segment when internal', (t) => {

function setupAgent(t) {
const agent = helper.instrumentMockedAgent()
const contextManager = helper.getContextManager()

t.teardown(function tearDown() {
helper.unloadAgent(agent)
})

return { agent, contextManager }
return { agent }
}
Loading

0 comments on commit b917b3e

Please sign in to comment.