Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rum-core): add network info for all transactions #752

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dev-utils/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,21 @@ export function describeIf(description, specDefinitions, condition) {

return describeFn.apply(this, [description, specDefinitions])
}

/**
* Custom Jasmine matcher to check if given field in a object can either be
* undefined or match object deeply
*/
export function toEqualOrUndefined() {
return {
compare: (actual, expected) => {
const result = {}
if (typeof actual === 'undefined') {
result.pass = true
} else {
result.pass = jasmine.matchersUtil.equals(actual, expected)
}
return result
}
}
}
31 changes: 26 additions & 5 deletions packages/rum-core/src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,34 @@ function getTimeOrigin() {
return PERF.timing.fetchStart
}

function getPageMetadata() {
function getNetworkInformation() {
const connection = navigator && navigator.connection

if (!(connection && typeof connection === 'object')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to not negate the condition and just wrap the following return inside if.

return
}
/**
* Ignoring `type` and `downlinkMax` as they are only
* supported on Chrome OS
*/
return {
page: {
referer: document.referrer,
url: window.location.href
}
downlink: connection.downlink,
effective_type: connection.effectiveType,
rtt: connection.rtt,
save_data: !!connection.saveData
}
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
}

function getPageMetadata() {
const context = {
referer: document.referrer,
url: window.location.href
}
const networkInfo = getNetworkInformation()
if (networkInfo != null) {
context.netinfo = networkInfo
}
return { page: context }
}

function stripQueryStringFromUrl(url) {
Expand Down
26 changes: 17 additions & 9 deletions packages/rum-core/test/common/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import resourceEntries from '../fixtures/resource-entries'
import Span from '../../src/performance-monitoring/span'
import Transaction from '../../src/performance-monitoring/transaction'
import { PAGE_LOAD } from '../../src/common/constants'
import { toEqualOrUndefined } from '../../../../dev-utils/jasmine'
import { mockGetEntriesByType } from '../utils/globals-mock'

describe('Context', () => {
Expand Down Expand Up @@ -166,6 +167,7 @@ describe('Context', () => {
})

it('should enrich transaction with context info based on type', () => {
jasmine.addMatchers({ toEqualOrUndefined })
const transaction = new Transaction('test', 'custom')
const trContext = { tags: { tag1: 'tag1' } }
transaction.addContext(trContext)
Expand All @@ -182,12 +184,16 @@ describe('Context', () => {
message: 'test'
}
}
addTransactionContext(transaction, configContext)
expect(transaction.context).toEqual({
page: {
const pageContext = {
page: jasmine.objectContaining({
referer: jasmine.any(String),
url: jasmine.any(String)
},
})
}

addTransactionContext(transaction, configContext)
expect(transaction.context).toEqual({
...pageContext,
...userContext,
...trContext
})
Expand All @@ -197,10 +203,7 @@ describe('Context', () => {
pageloadTr.end()
addTransactionContext(pageloadTr, configContext)
expect(pageloadTr.context).toEqual({
page: {
referer: jasmine.any(String),
url: jasmine.any(String)
},
...pageContext,
response: {
transfer_size: 26941,
encoded_body_size: 105297,
Expand All @@ -211,7 +214,12 @@ describe('Context', () => {
},
...userContext
})

expect(pageloadTr.context.page.netinfo).toEqualOrUndefined({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, using toEqualOrUndefined is not a good idea, instead we should detect if the feature that is required for this to work exist and we should add explicit checks for the outcomes. With this matcher we don't know whether the result was undefined because of a bug or because the api is not supported.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i get it right, do you want to test it only for supported browsers? Like we do explicit check like

if (connection) {
  expect(measuredInfo).toBe(actualInfo)? 
}

I was trying to express this using the matcher, May be i missed something. We can also do explicit checks in tests if its confusing.

Copy link
Contributor

@hmdhk hmdhk Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct! also check the else condition, i.e:

if (connection) {
  expect(measuredInfo).toBe(actualInfo)
} else {
 expect(measuredInfo).toBe(undefined)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sounds good 👍

downlink: jasmine.any(Number),
effective_type: jasmine.any(String),
rtt: jasmine.any(Number),
save_data: jasmine.any(Boolean)
})
unmock()
})
})
35 changes: 20 additions & 15 deletions packages/rum-core/test/error-logging/error-logging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import { createServiceFactory, createCustomEvent } from '../'
import { ERRORS } from '../../src/common/constants'
import { getGlobalConfig } from '../../../../dev-utils/test-config'
import { toEqualOrUndefined } from '../../../../dev-utils/jasmine'

const { agentConfig } = getGlobalConfig('rum-core')

Expand Down Expand Up @@ -149,6 +150,7 @@ describe('ErrorLogging', function() {
}

it('should include context info on error', () => {
jasmine.addMatchers({ toEqualOrUndefined })
const transaction = transactionService.startTransaction('test', 'dummy', {
managed: true
})
Expand All @@ -167,21 +169,24 @@ describe('ErrorLogging', function() {
error: new Error(testErrorMessage)
}
const errorData = errorLogging.createErrorDataModel(errorEvent)
expect(errorData.context).toEqual(
jasmine.objectContaining({
page: {
referer: jasmine.any(String),
url: jasmine.any(String)
},
managed: true,
dummy: {
foo: 'bar',
bar: 20
},
user: { id: 12, username: 'test' }
})
)
transaction.end()
expect(errorData.context).toEqual({
page: jasmine.objectContaining({
referer: jasmine.any(String),
url: jasmine.any(String)
}),
managed: true,
dummy: {
foo: 'bar',
bar: 20
},
user: { id: 12, username: 'test' }
})
expect(errorData.context.page.netinfo).toEqualOrUndefined({
downlink: jasmine.any(Number),
effective_type: jasmine.any(String),
rtt: jasmine.any(Number),
save_data: jasmine.any(Boolean)
})
})

it('should support ErrorEvent', function(done) {
Expand Down