Skip to content

Commit

Permalink
[PLAT-12224] Add setTraceCorrelation method to event (#2159)
Browse files Browse the repository at this point in the history
* feat: ✨ add setTraceCorrelation method to event

* v7.25.0-alpha.0

* refactor: 🦺 update span and trace id validation in setTraceCorrelation
  • Loading branch information
gingerbenw authored Jun 19, 2024
1 parent 04160d9 commit 76f50d9
Show file tree
Hide file tree
Showing 152 changed files with 365 additions and 316 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- (core) Add new `setTraceCorrelation` method to events [#2159](https://github.com/bugsnag/bugsnag-js/pull/2159)

### Fixed

- (react-native) Use synchronous native module calls when New Architecture is enabled [#2152](https://github.com/bugsnag/bugsnag-js/pull/2152)
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"packages": [
"packages/*"
],
"version": "7.24.0"
"version": "7.25.0-alpha.0"
}
2 changes: 1 addition & 1 deletion packages/browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/browser",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "dist/bugsnag.js",
"types": "types/bugsnag.d.ts",
"description": "Bugsnag error reporter for browser JavaScript",
Expand Down Expand Up @@ -31,25 +31,25 @@
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.0.1",
"@bugsnag/delivery-x-domain-request": "^7.22.7",
"@bugsnag/delivery-xml-http-request": "^7.22.7",
"@bugsnag/plugin-app-duration": "^7.22.7",
"@bugsnag/plugin-browser-context": "^7.22.7",
"@bugsnag/plugin-browser-device": "^7.22.7",
"@bugsnag/plugin-browser-request": "^7.22.7",
"@bugsnag/plugin-browser-session": "^7.22.7",
"@bugsnag/plugin-client-ip": "^7.22.7",
"@bugsnag/plugin-console-breadcrumbs": "^7.22.7",
"@bugsnag/plugin-inline-script-content": "^7.22.7",
"@bugsnag/plugin-interaction-breadcrumbs": "^7.22.7",
"@bugsnag/plugin-navigation-breadcrumbs": "^7.22.7",
"@bugsnag/plugin-network-breadcrumbs": "^7.22.7",
"@bugsnag/plugin-simple-throttle": "^7.22.7",
"@bugsnag/plugin-strip-query-string": "^7.22.7",
"@bugsnag/plugin-window-onerror": "^7.22.7",
"@bugsnag/plugin-window-unhandled-rejection": "^7.22.7"
"@bugsnag/delivery-x-domain-request": "^7.25.0-alpha.0",
"@bugsnag/delivery-xml-http-request": "^7.25.0-alpha.0",
"@bugsnag/plugin-app-duration": "^7.25.0-alpha.0",
"@bugsnag/plugin-browser-context": "^7.25.0-alpha.0",
"@bugsnag/plugin-browser-device": "^7.25.0-alpha.0",
"@bugsnag/plugin-browser-request": "^7.25.0-alpha.0",
"@bugsnag/plugin-browser-session": "^7.25.0-alpha.0",
"@bugsnag/plugin-client-ip": "^7.25.0-alpha.0",
"@bugsnag/plugin-console-breadcrumbs": "^7.25.0-alpha.0",
"@bugsnag/plugin-inline-script-content": "^7.25.0-alpha.0",
"@bugsnag/plugin-interaction-breadcrumbs": "^7.25.0-alpha.0",
"@bugsnag/plugin-navigation-breadcrumbs": "^7.25.0-alpha.0",
"@bugsnag/plugin-network-breadcrumbs": "^7.25.0-alpha.0",
"@bugsnag/plugin-simple-throttle": "^7.25.0-alpha.0",
"@bugsnag/plugin-strip-query-string": "^7.25.0-alpha.0",
"@bugsnag/plugin-window-onerror": "^7.25.0-alpha.0",
"@bugsnag/plugin-window-unhandled-rejection": "^7.25.0-alpha.0"
},
"dependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
}
}
2 changes: 2 additions & 0 deletions packages/core/event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class EventWithInternals extends Event {
_featuresIndex: { [key: string]: number }
_user: User
_handledState: HandledState
_correlation?: { spanId: string, traceId: string }
_session?: Session
toJSON(): {
payloadVersion: '4'
Expand All @@ -40,6 +41,7 @@ export default class EventWithInternals extends Event {
request: Request
breadcrumbs: Breadcrumb[]
context: string | undefined
correlation: { spanId: string, traceId: string } | undefined
groupingHash: string | undefined
metaData: { [key: string]: any }
user: User
Expand Down
17 changes: 16 additions & 1 deletion packages/core/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Event {
this._featuresIndex = {}
this._user = {}
this._session = undefined
this._correlation = undefined

this.errors = [
createBugsnagError(errorClass, errorMessage, Event.__type, stacktrace)
Expand All @@ -48,6 +49,19 @@ class Event {
return metadataDelegate.add(this._metadata, section, keyOrObj, maybeVal)
}

/**
* Associate this event with a specific trace. This is usually done automatically when
* using bugsnag-js-performance, but can also be set manually if required.
*
* @param traceId the ID of the trace the event occurred within
* @param spanId the ID of the span that the event occurred within
*/
setTraceCorrelation (traceId, spanId) {
if (typeof traceId === 'string') {
this._correlation = { traceId, ...typeof spanId === 'string' ? { spanId } : { } }
}
}

getMetadata (section, key) {
return metadataDelegate.get(this._metadata, section, key)
}
Expand Down Expand Up @@ -101,7 +115,8 @@ class Event {
metaData: this._metadata,
user: this._user,
session: this._session,
featureFlags: this.getFeatureFlags()
featureFlags: this.getFeatureFlags(),
correlation: this._correlation
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@bugsnag/core",
"main": "index.js",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"types": "types/index.d.ts",
"description": "Core classes and utilities for Bugsnag notifiers",
"homepage": "https://www.bugsnag.com/",
Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,29 @@ describe('@bugsnag/core/event', () => {
}))
})
})

describe('event.setTraceCorrelation()', () => {
it('allows setting the span and trace id', () => {
const event = new Event('Err', 'bad', [])
event.setTraceCorrelation('test-trace-id', 'test-span-id')
const serialized = event.toJSON()
expect(serialized.correlation).toEqual({ traceId: 'test-trace-id', spanId: 'test-span-id' })
})

it.each([undefined, null])('sets the trace id if span id is %s', (type) => {
const event = new Event('Err', 'bad', [])
// @ts-expect-error spanId unexpected type
event.setTraceCorrelation('test-trace-id', type)
const serialized = event.toJSON()
expect(serialized.correlation).toEqual({ traceId: 'test-trace-id' })
})

it.each([undefined, null])('does not set trace or span id if trace id is %s', (type) => {
const event = new Event('Err', 'bad', [])
// @ts-expect-error traceId unexpected type
event.setTraceCorrelation(type, 'test-span-id')
const serialized = event.toJSON()
expect(serialized.correlation).toBeUndefined()
})
})
})
3 changes: 3 additions & 0 deletions packages/core/types/event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ declare class Event {
public addFeatureFlags(featureFlags: FeatureFlag[]): void
public clearFeatureFlag(name: string): void
public clearFeatureFlags(): void

// trace correlation
public setTraceCorrelation(traceId: string, spanId?: string): void
}

interface HandledState {
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/delivery-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/delivery-electron",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "delivery.js",
"description": "@bugsnag/electron delivery mechanism to send events, sessions, and crash dumps from Electron apps",
"homepage": "https://www.bugsnag.com/",
Expand All @@ -17,9 +17,9 @@
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.22.7",
"@bugsnag/electron-network-status": "^7.22.7",
"@bugsnag/plugin-electron-client-state-manager": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0",
"@bugsnag/electron-network-status": "^7.25.0-alpha.0",
"@bugsnag/plugin-electron-client-state-manager": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.9.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-fetch/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/delivery-fetch/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@bugsnag/delivery-fetch",
"author": "Bugsnag",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "delivery.js",
"description": "@bugsnag/js delivery mechanism using the fetch API",
"homepage": "https://www.bugsnag.com/",
Expand All @@ -14,7 +14,7 @@
},
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/delivery-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/delivery-node",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "delivery.js",
"description": "@bugsnag/node delivery mechanism",
"homepage": "https://www.bugsnag.com/",
Expand All @@ -17,7 +17,7 @@
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-react-native/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/delivery-react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/delivery-react-native",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "delivery.js",
"description": "@bugsnag/js delivery mechanism for React Native",
"homepage": "https://www.bugsnag.com/",
Expand All @@ -17,7 +17,7 @@
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-x-domain-request/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/delivery-x-domain-request/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/delivery-x-domain-request",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "delivery.js",
"description": "@bugsnag/js delivery mechanism for IE 8, 9 and 10",
"homepage": "https://www.bugsnag.com/",
Expand All @@ -17,7 +17,7 @@
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-xml-http-request/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/delivery-xml-http-request/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/delivery-xml-http-request",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "delivery.js",
"description": "@bugsnag/js delivery mechanism for most browsers",
"homepage": "https://www.bugsnag.com/",
Expand All @@ -17,7 +17,7 @@
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.0.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/electron-network-status/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@bugsnag/electron-network-status",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "network-status.js",
"description": "@bugsnag/electron package for watching network status",
"author": "Bugsnag",
"license": "MIT",
"homepage": "https://www.bugsnag.com/",
"devDependencies": {
"@bugsnag/core": "^7.22.7",
"@bugsnag/plugin-electron-client-state-manager": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0",
"@bugsnag/plugin-electron-client-state-manager": "^7.25.0-alpha.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.10.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-test-helpers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/electron-test-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugsnag/electron-test-helpers",
"version": "7.22.7",
"version": "7.25.0-alpha.0",
"main": "helpers.ts",
"description": "Test helpers for @bugsnag/electron",
"repository": {
Expand All @@ -15,7 +15,7 @@
"helpers.ts"
],
"devDependencies": {
"@bugsnag/core": "^7.22.7"
"@bugsnag/core": "^7.25.0-alpha.0"
},
"author": "Bugsnag",
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion packages/electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 76f50d9

Please sign in to comment.