Skip to content

Commit

Permalink
spread detail
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-messiah committed Jan 16, 2025
1 parent ce7402e commit 1ee45a5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/common/config/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const model = () => {
set capture_marks (val) { hiddenState.experimental.marks = val },
get capture_measures () { return hiddenState.feature_flags.includes(FEATURE_FLAGS.MEASURES) || hiddenState.experimental.measures },
set capture_measures (val) { hiddenState.experimental.measures = val },
capture_detail: true,
resources: {
// whether to run this subfeature or not in the generic_events feature. false by default through experimental phase, but flipped to true once GA'd
get enabled () { return hiddenState.feature_flags.includes(FEATURE_FLAGS.RESOURCES) || hiddenState.experimental.resources },
Expand Down
18 changes: 16 additions & 2 deletions src/features/generic_events/aggregate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,28 @@ export class Aggregate extends AggregateBase {
list.getEntries().forEach(entry => {
try {
handle(SUPPORTABILITY_METRIC_CHANNEL, ['Generic/Performance/' + type + '/Seen'])
const detailObj = agentRef.init.performance.capture_detail ? createDetailAttrs(entry.detail) : {}
this.addEvent({
...detailObj,
eventType: 'BrowserPerformance',
timestamp: this.toEpoch(entry.startTime),
entryName: cleanURL(entry.name),
entryDuration: entry.duration,
entryType: type,
...(entry.detail && { entryDetail: entry.detail })
entryType: type
})

function createDetailAttrs (obj, rootKey = '', output = {}) {
if (obj === null || obj === undefined) return output
// if (typeof obj === 'string') output.entryDetail = obj
else if (typeof obj !== 'object') output.entryDetail = obj
else {
Object.entries(obj).forEach(([key, value]) => {
if (typeof value === 'object') createDetailAttrs(value, rootKey + key + '.', output)
else output['entryDetail.' + rootKey + key] = value
})
}
return output
}
} catch (err) {
}
})
Expand Down
25 changes: 25 additions & 0 deletions tests/assets/marks-and-measures-detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!--
Copyright 2020 New Relic Corporation.
PDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>RUM Unit Test</title>
<script>performance.mark('before-agent')</script>
{init} {config} {loader}
<script>performance.mark('after-agent')</script>
</head>
<body>Instrumented
<script>
performance.measure('simple-object', {detail: {foo:'bar'}, start: 'before-agent', end: 'after-agent'} )
performance.measure('nested-object', {detail: {nested1:{nested2:{nested3:{nested4: {foo: 'bar'}}}}, notNested: 'hi'}, start: 'before-agent', end: 'after-agent'} )
performance.measure('string', {detail: 'hi', start: 'before-agent', end: 'after-agent'} )
performance.measure('falsy-string', {detail: '', start: 'before-agent', end: 'after-agent'} )
performance.measure('number', {detail: 1, start: 'before-agent', end: 'after-agent'} )
performance.measure('falsy-number', {detail: 0, start: 'before-agent', end: 'after-agent'} )
performance.measure('boolean', {detail: true, start: 'before-agent', end: 'after-agent'} )
performance.measure('falsy-boolean', {detail: false, start: 'before-agent', end: 'after-agent'} )
</script>
</body>
</html>
7 changes: 3 additions & 4 deletions tests/components/generic_events/aggregate/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ describe('sub-features', () => {
genericEventsAggregate.ee.emit('ua', [{ timeStamp: 234567, type: 'blur', target: window }])

const harvest = genericEventsAggregate.makeHarvestPayload() // force it to put the aggregation into the event buffer
console.log(harvest)
expect(harvest[0].payload.body.ins[0]).toMatchObject({
eventType: 'UserAction',
timestamp: expect.any(Number),
Expand Down Expand Up @@ -286,7 +285,7 @@ describe('sub-features', () => {
})

test('should record measures when enabled', async () => {
mainAgent.init.performance = { capture_measures: true, resources: { enabled: false, asset_types: [], first_party_domains: [], ignore_newrelic: true } }
mainAgent.init.performance = { capture_measures: true, capture_detail: true, resources: { enabled: false, asset_types: [], first_party_domains: [], ignore_newrelic: true } }
getInfo(mainAgent.agentIdentifier).jsAttributes = { globalFoo: 'globalBar' }
const mockPerformanceObserver = jest.fn(cb => ({
observe: () => {
Expand All @@ -295,7 +294,7 @@ describe('sub-features', () => {
cb({getEntries: () => [{
name: 'test',
duration: 10,
detail: JSON.stringify({ foo: 'bar' }),
detail: { foo: 'bar' },
startTime: performance.now()
}]
})
Expand All @@ -321,7 +320,7 @@ describe('sub-features', () => {
entryName: 'test',
entryDuration: 10,
entryType: 'measure',
entryDetail: JSON.stringify({ foo: 'bar' })
'entryDetail.foo': 'bar'
})
})
})
29 changes: 28 additions & 1 deletion tests/specs/ins/harvesting.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('ins harvesting', () => {

expect(insHarvest.length).toEqual(1) // this page sets one measure
expect(insHarvest[0]).toMatchObject({
entryDetail: '{"foo":"bar"}',
'entryDetail.foo': 'bar',
entryDuration: expect.any(Number),
eventType: 'BrowserPerformance',
entryName: 'agent-load',
Expand All @@ -235,6 +235,33 @@ describe('ins harvesting', () => {
})
})

it('should spread detail', async () => {
const testUrl = await browser.testHandle.assetURL('marks-and-measures-detail.html', getInsInit({ performance: { capture_measures: true } }))
await browser.url(testUrl).then(() => browser.waitForAgentLoad())

const [[{ request: { body: { ins: insHarvest } } }]] = await Promise.all([
insightsCapture.waitForResult({ totalCount: 1 })
])

expect(insHarvest.length).toEqual(8) // this page sets seven measures
// detail: {foo:'bar'}
expect(insHarvest[0]['entryDetail.foo']).toEqual('bar')
// detail: {nested1:{nested2:{nested3:{nested4: {foo: 'bar'}}}}
expect(insHarvest[1]['entryDetail.nested1.nested2.nested3.nested4.foo']).toEqual('bar')
// detail: 'hi'
expect(insHarvest[2].entryDetail).toEqual('hi')
// detail: ''
expect(insHarvest[3].entryDetail).toEqual('')
// detail: 1
expect(insHarvest[4].entryDetail).toEqual(1)
// detail: 0
expect(insHarvest[5].entryDetail).toEqual(0)
// detail: true
expect(insHarvest[6].entryDetail).toEqual(true)
// detail: false
expect(insHarvest[7].entryDetail).toEqual(false)
})

;[
[getInsInit({ performance: { resources: { enabled: true, ignore_newrelic: false } } }), 'enabled'],
[getInsInit({ performance: { resources: { enabled: false, ignore_newrelic: false } }, feature_flags: [FEATURE_FLAGS.RESOURCES] }), 'feature flag']
Expand Down
1 change: 1 addition & 0 deletions tests/unit/common/config/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ test('init props exist and return expected defaults', () => {
expect(config.performance).toEqual({
capture_marks: false,
capture_measures: false,
capture_detail: true,
resources: {
enabled: false,
asset_types: [],
Expand Down

0 comments on commit 1ee45a5

Please sign in to comment.