-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/tracing-add-more-network-timings
- Loading branch information
Showing
22 changed files
with
359 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { EventItem, EventProcessor, Hub, Integration } from '@sentry/types'; | ||
import { forEachEnvelopeItem } from '@sentry/utils'; | ||
|
||
import { addMetadataToStackFrames, stripMetadataFromStackFrames } from '../metadata'; | ||
|
||
/** | ||
* Adds module metadata to stack frames. | ||
* | ||
* Metadata can be injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option. | ||
* | ||
* When this integration is added, the metadata passed to the bundler plugin is added to the stack frames of all events | ||
* under the `module_metadata` property. This can be used to help in tagging or routing of events from different teams | ||
* our sources | ||
*/ | ||
export class ModuleMetadata implements Integration { | ||
/* | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'ModuleMetadata'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string = ModuleMetadata.id; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(addGlobalEventProcessor: (processor: EventProcessor) => void, getCurrentHub: () => Hub): void { | ||
const client = getCurrentHub().getClient(); | ||
|
||
if (!client || typeof client.on !== 'function') { | ||
return; | ||
} | ||
|
||
// We need to strip metadata from stack frames before sending them to Sentry since these are client side only. | ||
client.on('beforeEnvelope', envelope => { | ||
forEachEnvelopeItem(envelope, (item, type) => { | ||
if (type === 'event') { | ||
const event = Array.isArray(item) ? (item as EventItem)[1] : undefined; | ||
|
||
if (event) { | ||
stripMetadataFromStackFrames(event); | ||
item[1] = event; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
const stackParser = client.getOptions().stackParser; | ||
|
||
addGlobalEventProcessor(event => { | ||
addMetadataToStackFrames(stackParser, event); | ||
return event; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { Event } from '@sentry/types'; | ||
import { createStackParser, GLOBAL_OBJ, nodeStackLineParser, parseEnvelope } from '@sentry/utils'; | ||
import { TextDecoder, TextEncoder } from 'util'; | ||
|
||
import { createTransport, getCurrentHub, ModuleMetadata } from '../../../src'; | ||
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; | ||
|
||
const stackParser = createStackParser(nodeStackLineParser()); | ||
|
||
const stack = new Error().stack || ''; | ||
|
||
describe('ModuleMetadata integration', () => { | ||
beforeEach(() => { | ||
TestClient.sendEventCalled = undefined; | ||
TestClient.instance = undefined; | ||
|
||
GLOBAL_OBJ._sentryModuleMetadata = GLOBAL_OBJ._sentryModuleMetadata || {}; | ||
GLOBAL_OBJ._sentryModuleMetadata[stack] = { team: 'frontend' }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('Adds and removes metadata from stack frames', done => { | ||
const options = getDefaultTestClientOptions({ | ||
dsn: 'https://username@domain/123', | ||
enableSend: true, | ||
stackParser, | ||
integrations: [new ModuleMetadata()], | ||
beforeSend: (event, _hint) => { | ||
// copy the frames since reverse in in-place | ||
const lastFrame = [...(event.exception?.values?.[0].stacktrace?.frames || [])].reverse()[0]; | ||
// Ensure module_metadata is populated in beforeSend callback | ||
expect(lastFrame?.module_metadata).toEqual({ team: 'frontend' }); | ||
return event; | ||
}, | ||
transport: () => | ||
createTransport({ recordDroppedEvent: () => undefined, textEncoder: new TextEncoder() }, async req => { | ||
const [, items] = parseEnvelope(req.body, new TextEncoder(), new TextDecoder()); | ||
|
||
expect(items[0][1]).toBeDefined(); | ||
const event = items[0][1] as Event; | ||
const error = event.exception?.values?.[0]; | ||
|
||
// Ensure we're looking at the same error we threw | ||
expect(error?.value).toEqual('Some error'); | ||
|
||
const lastFrame = [...(error?.stacktrace?.frames || [])].reverse()[0]; | ||
// Ensure the last frame is in fact for this file | ||
expect(lastFrame?.filename).toEqual(__filename); | ||
|
||
// Ensure module_metadata has been stripped from the event | ||
expect(lastFrame?.module_metadata).toBeUndefined(); | ||
|
||
done(); | ||
return {}; | ||
}), | ||
}); | ||
|
||
const client = new TestClient(options); | ||
const hub = getCurrentHub(); | ||
hub.bindClient(client); | ||
hub.captureException(new Error('Some error')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { captureException, withScope } from '@sentry/core'; | ||
import { addExceptionMechanism, isNodeEnv, isString } from '@sentry/utils'; | ||
|
||
import type { ErrorResponse } from '../utils/vendor/types'; | ||
|
||
/** | ||
* Checks whether the given error is an ErrorResponse. | ||
* ErrorResponse is when users throw a response from their loader or action functions. | ||
* This is in fact a server-side error that we capture on the client. | ||
* | ||
* @param error The error to check. | ||
* @returns boolean | ||
*/ | ||
function isErrorResponse(error: unknown): error is ErrorResponse { | ||
return typeof error === 'object' && error !== null && 'status' in error && 'statusText' in error; | ||
} | ||
|
||
/** | ||
* Captures an error that is thrown inside a Remix ErrorBoundary. | ||
* | ||
* @param error The error to capture. | ||
* @returns void | ||
*/ | ||
export function captureRemixErrorBoundaryError(error: unknown): void { | ||
const isClientSideRuntimeError = !isNodeEnv() && error instanceof Error; | ||
const isRemixErrorResponse = isErrorResponse(error); | ||
// Server-side errors apart from `ErrorResponse`s also appear here without their stacktraces. | ||
// So, we only capture: | ||
// 1. `ErrorResponse`s | ||
// 2. Client-side runtime errors here, | ||
// And other server - side errors in `handleError` function where stacktraces are available. | ||
if (isRemixErrorResponse || isClientSideRuntimeError) { | ||
const eventData = isRemixErrorResponse | ||
? { | ||
function: 'ErrorResponse', | ||
...error.data, | ||
} | ||
: { | ||
function: 'ReactError', | ||
}; | ||
|
||
withScope(scope => { | ||
scope.addEventProcessor(event => { | ||
addExceptionMechanism(event, { | ||
type: 'instrument', | ||
handled: true, | ||
data: eventData, | ||
}); | ||
return event; | ||
}); | ||
|
||
if (isRemixErrorResponse) { | ||
if (isString(error.data)) { | ||
captureException(error.data); | ||
} else if (error.statusText) { | ||
captureException(error.statusText); | ||
} else { | ||
captureException(error); | ||
} | ||
} else { | ||
captureException(error); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { GLOBAL_OBJ } from '@sentry/utils'; | ||
|
||
import type { FutureConfig, ServerBuild } from './vendor/types'; | ||
|
||
export type EnhancedGlobal = typeof GLOBAL_OBJ & { | ||
__remixContext?: { | ||
future?: FutureConfig; | ||
}; | ||
}; | ||
|
||
/** | ||
* Get the future flags from the Remix browser context | ||
* | ||
* @returns The future flags | ||
*/ | ||
export function getFutureFlagsBrowser(): FutureConfig | undefined { | ||
const window = GLOBAL_OBJ as EnhancedGlobal; | ||
|
||
if (!window.__remixContext) { | ||
return; | ||
} | ||
|
||
return window.__remixContext.future; | ||
} | ||
|
||
/** | ||
* Get the future flags from the Remix server build | ||
* | ||
* @param build The Remix server build | ||
* | ||
* @returns The future flags | ||
*/ | ||
export function getFutureFlagsServer(build: ServerBuild): FutureConfig | undefined { | ||
return build.future; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.