Skip to content

Commit 5c02e62

Browse files
committed
feat: Fixed code after flatenning segment tree
1 parent 9f22953 commit 5c02e62

File tree

11 files changed

+1013
-109
lines changed

11 files changed

+1013
-109
lines changed

tests/integration/config-capture-scalars.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ tests.push({
113113
}
114114

115115
// Exact match to ensure no extra fields snuck in
116-
assertSegments(transaction.trace.root, expectedSegments, { exact: true })
116+
assertSegments(transaction.trace, transaction.trace.root, expectedSegments, { exact: true })
117117
})
118118

119119
executeQuery(serverUrl, query, (err) => {

tests/lib/custom-assertions.js

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,64 @@ function assertMetrics(
6363
}
6464

6565
/**
66+
* This function is used to verify that a tree of trace segments matches an
67+
* expected tree of segment names. For example, if the trace looks like (i.e
68+
* the `parent` parameter):
69+
*
70+
* ```js
71+
* {
72+
* name: 'root-segment',
73+
* children: [
74+
* {
75+
* name: 'child 1',
76+
* children: [
77+
* {
78+
* name: 'grandchild 1',
79+
* children: [
80+
* {
81+
* name: 'great-grandchild',
82+
* children: []
83+
* }
84+
* ]
85+
* },
86+
* {
87+
* name: 'grandchild 2',
88+
* children: []
89+
* }
90+
* ]
91+
* },
92+
* {
93+
* name: 'child 2',
94+
* children: []
95+
* }
96+
* ]
97+
* }
98+
* ```
99+
*
100+
* Then the provided `expected` parameter should look like:
101+
*
102+
* ```js
103+
* [
104+
* 'root-segment',
105+
* [
106+
* 'child 1',
107+
* [
108+
* 'grandchild 1',
109+
* ['great-grandchild],
110+
* 'grandchild 2'
111+
* ],
112+
* 'child 2'
113+
* ],
114+
* ]
115+
* ```
116+
*
117+
* Ordering of the elements in the `expected` parameter is significant when
118+
* `options.exact = true`. Regardless of the `exact` value, ordering of elements
119+
* is significant to indicate the nesting order. Any string immediately
120+
* followed by an array of strings indicates that the first string is a parent
121+
* element, and the subsequent array of strings is its child elements.
122+
*
123+
* @param {Trace} trace Transaction trace
66124
* @param {TraceSegment} parent Parent segment
67125
* @param {Array} expected Array of strings that represent segment names.
68126
* If an item in the array is another array, it
@@ -78,7 +136,13 @@ function assertMetrics(
78136
* @param {object} [deps] Injected dependencies.
79137
* @param {object} [deps.assert] Assertion library to use.
80138
*/
81-
function assertSegments(parent, expected, options, { assert = require('node:assert') } = {}) {
139+
function assertSegments(
140+
trace,
141+
parent,
142+
expected,
143+
options,
144+
{ assert = require('node:assert') } = {}
145+
) {
82146
let child
83147
let childCount = 0
84148

@@ -91,7 +155,8 @@ function assertSegments(parent, expected, options, { assert = require('node:asse
91155
}
92156

93157
function getChildren(_parent) {
94-
return _parent.children.filter(function (item) {
158+
const children = trace.getChildren(_parent.id)
159+
return children.filter(function (item) {
95160
if (exact && options && options.exclude) {
96161
return options.exclude.indexOf(item.name) === -1
97162
}
@@ -126,7 +191,7 @@ function assertSegments(parent, expected, options, { assert = require('node:asse
126191
)
127192
}
128193
} else if (typeof sequenceItem === 'object') {
129-
assertSegments(child, sequenceItem, options, { assert })
194+
assertSegments(trace, child, sequenceItem, options, { assert })
130195
}
131196
}
132197

@@ -138,14 +203,14 @@ function assertSegments(parent, expected, options, { assert = require('node:asse
138203

139204
if (typeof sequenceItem === 'string') {
140205
// find corresponding child in parent
141-
for (let j = 0; j < parent.children.length; j++) {
142-
if (parent.children[j].name.startsWith(sequenceItem) === true) {
143-
child = parent.children[j]
206+
for (let j = 0; j < children.length; j++) {
207+
if (children[j].name.startsWith(sequenceItem) === true) {
208+
child = children[j]
144209
}
145210
}
146211
assert.ok(child, 'segment "' + parent.name + '" should have child "' + sequenceItem + '"')
147212
if (typeof expected[i + 1] === 'object') {
148-
assertSegments(child, expected[i + 1], { exact }, { assert })
213+
assertSegments(trace, child, expected[i + 1], { exact }, { assert })
149214
}
150215
}
151216
}

tests/lib/find-segment.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
module.exports = function findSegmentByName(trace, root, name) {
3+
const children = trace.getChildren(root.id)
4+
if (root.name === name) {
5+
return root
6+
} else if (children.length) {
7+
for (let i = 0; i < children.length; i++) {
8+
const child = children[i]
9+
const found = findSegmentByName(trace, child, name)
10+
if (found) {
11+
return found
12+
}
13+
}
14+
}
15+
return null
16+
}

tests/unit/create-plugin.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ test('createPlugin edge cases', async (t) => {
1717
ctx.nr.operationSegment = {
1818
start: sinon.stub(),
1919
addAttribute: sinon.stub(),
20-
transaction: { nameState: { setName: sinon.stub() } },
21-
end: sinon.stub()
20+
end: sinon.stub(),
21+
start: sinon.stub()
2222
}
2323

2424
ctx.nr.instrumentationApi = {

tests/versioned/apollo-federation/federated-gateway-server-setup.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
'use strict'
77
const utils = require('@newrelic/test-utilities')
8-
98
const federatedData = require('./federated-data-definitions')
109
const { unloadModules } = require('../../lib/test-tools')
1110

tests/versioned/apollo-federation/query-obfuscation.test.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const assert = require('node:assert')
99
const { setupFederatedGateway, teardownGateway } = require('./federated-gateway-server-setup')
1010
const { executeQuery } = require('../../lib/test-client')
1111
const { checkResult } = require('../common')
12+
const findSegmentByName = require('../../lib/find-segment')
1213
const SEGMENT_DESTINATION = 0x20
1314
const ANON_PLACEHOLDER = '<anonymous>'
1415
const QUERY_ATTRIBUTE_NAME = 'graphql.operation.query'
@@ -45,7 +46,11 @@ test('apollo-federation: query obfuscation', async (t) => {
4546
executeQuery(serverUrl, query, (err, result) => {
4647
assert.ok(!err)
4748
const operationName = `${OPERATION_PREFIX}/${ANON_PLACEHOLDER}/${path}`
48-
const operationSegment = findSegmentByName(tx.trace.root, operationName)
49+
const operationSegment = findSegmentByName(
50+
tx.trace,
51+
tx.trace.root,
52+
operationName
53+
)
4954

5055
// only test one operation segment of three federated server transactions
5156
if (operationSegment) {
@@ -59,19 +64,3 @@ test('apollo-federation: query obfuscation', async (t) => {
5964
})
6065
})
6166
})
62-
63-
function findSegmentByName(root, name) {
64-
if (root.name === name) {
65-
return root
66-
} else if (root.children && root.children.length) {
67-
for (let i = 0; i < root.children.length; i++) {
68-
const child = root.children[i]
69-
const found = findSegmentByName(child, name)
70-
if (found) {
71-
return found
72-
}
73-
}
74-
}
75-
76-
return null
77-
}

tests/versioned/apollo-federation/segments.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test('apollo-federation: federated segments', async (t) => {
7272
]
7373
]
7474

75-
assertSegments(tx.trace.root, expectedSegments, { exact: false }, { assert: plan })
75+
assertSegments(tx.trace, tx.trace.root, expectedSegments, { exact: false }, { assert: plan })
7676
checkResult(plan, result, () => {})
7777
})
7878
await plan.completed
@@ -141,7 +141,7 @@ test('apollo-federation: federated segments', async (t) => {
141141
]
142142
]
143143

144-
assertSegments(tx.trace.root, expectedSegments, { exact: false }, { assert: plan })
144+
assertSegments(tx.trace, tx.trace.root, expectedSegments, { exact: false }, { assert: plan })
145145
checkResult(plan, result, () => {
146146
plan.equal(result.length, 2)
147147
})

0 commit comments

Comments
 (0)