Skip to content

Commit 25143ab

Browse files
feat: add pageLoadParentId configuration (#1521)
* feat(rum): expose pageLoadParentId configuration * chore: unblock ci * chore: update configuration doc * chore: unblock ci * Review feedback * Make sure right variable is referenced * Review feedback --------- Co-authored-by: Alberto Delgado <[email protected]>
1 parent ad4a71b commit 25143ab

File tree

8 files changed

+69
-10
lines changed

8 files changed

+69
-10
lines changed

docs/configuration.asciidoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ NOTE: After each flush of the queue, the next flush isn't scheduled until an ite
185185

186186
This option overrides the page load transactions trace ID.
187187

188+
[float]
189+
[[page-load-parent-id]]
190+
=== `pageLoadParentId`
191+
192+
* *Type:* String
193+
194+
This option allows the creation of the page load transaction as child of an existing one. By default,
195+
the agent treats it as the root transaction.
196+
188197

189198
[float]
190199
[[page-load-sampled]]

packages/rum-core/src/common/compress.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export function compressTransaction(transaction) {
232232

233233
const tr = {
234234
id: transaction.id,
235+
pid: transaction.parent_id,
235236
tid: transaction.trace_id,
236237
n: transaction.name,
237238
t: transaction.type,

packages/rum-core/src/common/config-service.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Config {
8585
pageLoadTraceId: '',
8686
pageLoadSpanId: '',
8787
pageLoadSampled: false,
88+
pageLoadParentId: '',
8889
pageLoadTransactionName: '',
8990
propagateTracestate: false,
9091
transactionSampleRate: 1.0,

packages/rum-core/src/performance-monitoring/navigation/capture-navigation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ function captureNavigation(transaction) {
9595
}
9696
)
9797

98+
99+
/**
100+
* Set the parent id of the transaction to the page load parent ID.
101+
* This means that the backend transaction will be the parent of
102+
* the page load transaction, which is useful for e.g Synthetics.
103+
*/
104+
105+
if (transaction.options.pageLoadParentId) {
106+
transaction.parentId = transaction.options.pageLoadParentId
107+
}
108+
98109
/**
99110
* Page load marks that are gathered from NavigationTiming API
100111
*/

packages/rum-core/src/performance-monitoring/performance-monitoring.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ export default class PerformanceMonitoring {
398398

399399
const transactionData = {
400400
id: transaction.id,
401+
parent_id: transaction.parentId,
401402
trace_id: transaction.traceId,
402403
session: transaction.session,
403404
name: transaction.name,

packages/rum-core/src/performance-monitoring/transaction-service.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class TransactionService {
109109
pageLoadTraceId: config.pageLoadTraceId,
110110
pageLoadSampled: config.pageLoadSampled,
111111
pageLoadSpanId: config.pageLoadSpanId,
112-
pageLoadTransactionName: config.pageLoadTransactionName
112+
pageLoadTransactionName: config.pageLoadTransactionName,
113+
pageLoadParentId: config.pageLoadParentId
113114
},
114115
perfOptions
115116
)
@@ -283,6 +284,7 @@ class TransactionService {
283284
if (name === NAME_UNKNOWN && pageLoadTransactionName) {
284285
tr.name = pageLoadTransactionName
285286
}
287+
286288
/**
287289
* Capture the TBT as span after observing for all long task entries
288290
* and once performance observer is disconnected

packages/rum-core/test/common/apm-server.spec.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ describe('ApmServer', function () {
301301
clock.install()
302302
apmServer.init()
303303
spyOn(apmServer, '_postJson')
304-
const trs = generateTransaction(2).map(tr =>
305-
performanceMonitoring.createTransactionDataModel(tr)
306-
)
304+
const trs = generateTransaction(2).map((tr, i) => {
305+
tr.parentId = 'parent-transaction-id-' + i
306+
return performanceMonitoring.createTransactionDataModel(tr)
307+
})
307308
const errors = generateErrors(2).map(err => ({
308309
name: err.name,
309310
message: err.message
@@ -321,9 +322,9 @@ describe('ApmServer', function () {
321322
'{"metadata":{"service":{"name":"test","agent":{"name":"rum-js","version":"N/A"},"language":{"name":"javascript"}}}}',
322323
'{"error":{"name":"Error","message":"error #0"}}',
323324
'{"error":{"name":"Error","message":"error #1"}}',
324-
'{"transaction":{"id":"transaction-id-0","trace_id":"trace-id-0","name":"transaction #0","type":"transaction","duration":990,"span_count":{"started":1},"sampled":false}}',
325+
'{"transaction":{"id":"transaction-id-0","parent_id":"parent-transaction-id-0","trace_id":"trace-id-0","name":"transaction #0","type":"transaction","duration":990,"span_count":{"started":1},"sampled":false}}',
325326
'{"span":{"id":"span-id-0-1","transaction_id":"transaction-id-0","parent_id":"transaction-id-0","trace_id":"trace-id-0","name":"name","type":"type","subtype":"subtype","sync":false,"start":10,"duration":10}}',
326-
'{"transaction":{"id":"transaction-id-1","trace_id":"trace-id-1","name":"transaction #1","type":"transaction","duration":990,"span_count":{"started":1},"sampled":false}}',
327+
'{"transaction":{"id":"transaction-id-1","parent_id":"parent-transaction-id-1","trace_id":"trace-id-1","name":"transaction #1","type":"transaction","duration":990,"span_count":{"started":1},"sampled":false}}',
327328
'{"span":{"id":"span-id-1-1","transaction_id":"transaction-id-1","parent_id":"transaction-id-1","trace_id":"trace-id-1","name":"name","type":"type","subtype":"subtype","sync":false,"start":10,"duration":10}}'
328329
]
329330

@@ -337,9 +338,10 @@ describe('ApmServer', function () {
337338
configService.setConfig({ apiVersion: 3 })
338339
apmServer.init()
339340
spyOn(apmServer, '_postJson')
340-
const trs = generateTransaction(1, true).map(tr =>
341-
performanceMonitoring.createTransactionDataModel(tr)
342-
)
341+
const trs = generateTransaction(1, true).map((tr, i) => {
342+
tr.parentId = 'parent-transaction-id-' + i
343+
return performanceMonitoring.createTransactionDataModel(tr)
344+
})
343345
const errors = generateErrors(1).map((err, i) => {
344346
let model = errorLogging.createErrorDataModel(err)
345347
model.id = 'error-id-' + i
@@ -357,7 +359,7 @@ describe('ApmServer', function () {
357359
const expected = [
358360
'{"m":{"se":{"n":"test","a":{"n":"rum-js","ve":"N/A"},"la":{"n":"javascript"}}}}',
359361
'{"e":{"id":"error-id-0","cl":"(inline script)","ex":{"mg":"error #0","st":[]},"c":null}}',
360-
'{"x":{"id":"transaction-id-0","tid":"trace-id-0","n":"transaction #0","t":"transaction","d":990,"c":null,"k":null,"me":[{"y":{"t":"app"},"sa":{"ysc":{"v":1},"yss":{"v":980000}}},{"y":{"t":"type"},"sa":{"ysc":{"v":1},"yss":{"v":10000}}}],"y":[{"id":"span-id-0-1","n":"name","t":"type","s":10,"d":10,"c":null,"sr":0.1,"su":"subtype"}],"yc":{"sd":1},"sm":true,"sr":0.1}}'
362+
'{"x":{"id":"transaction-id-0","pid":"parent-transaction-id-0","tid":"trace-id-0","n":"transaction #0","t":"transaction","d":990,"c":null,"k":null,"me":[{"y":{"t":"app"},"sa":{"ysc":{"v":1},"yss":{"v":980000}}},{"y":{"t":"type"},"sa":{"ysc":{"v":1},"yss":{"v":10000}}}],"y":[{"id":"span-id-0-1","n":"name","t":"type","s":10,"d":10,"c":null,"sr":0.1,"su":"subtype"}],"yc":{"sd":1},"sm":true,"sr":0.1}}'
361363
]
362364
expect(payload.split('\n').filter(a => a)).toEqual(expected)
363365
clock.uninstall()

packages/rum-core/test/performance-monitoring/transaction-service.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@ describe('TransactionService', function () {
261261
})
262262
})
263263

264+
it('should set page load parentId before ending the transaction', function (done) {
265+
config.setConfig({
266+
pageLoadParentId: 'test-page-load-parent-id'
267+
})
268+
transactionService = new TransactionService(logger, config)
269+
270+
const tr = transactionService.startTransaction(undefined, 'page-load', {
271+
managed: true
272+
})
273+
274+
tr.detectFinish()
275+
276+
/**
277+
* For page load transaction we set the transaction parentId using
278+
* transaction.onEnd
279+
*/
280+
config.events.observe(TRANSACTION_END, tr => {
281+
expect(tr.parentId).toBe('test-page-load-parent-id')
282+
done()
283+
})
284+
})
285+
264286
it('should capture resources from navigation timing', function (done) {
265287
const unMock = mockGetEntriesByType()
266288

@@ -362,6 +384,16 @@ describe('TransactionService', function () {
362384
})
363385
})
364386

387+
it('should consider page load parentId', function () {
388+
config.setConfig({
389+
pageLoadParentId: 'test-page-load-parent-id'
390+
})
391+
392+
transactionService = new TransactionService(logger, config)
393+
const tr = sendPageLoadMetrics()
394+
expect(tr.options.pageLoadParentId).toBe('test-page-load-parent-id')
395+
})
396+
365397
it('should call createCurrentTransaction once per startTransaction', function () {
366398
spyOn(transactionService, 'createCurrentTransaction').and.callThrough()
367399
transactionService.startTransaction('test-name', 'test-type', {

0 commit comments

Comments
 (0)