-
Notifications
You must be signed in to change notification settings - Fork 8
[RUM-13448] Add source code context injection for microfrontend #255
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
Draft
amortemousque
wants to merge
1
commit into
master
Choose a base branch
from
aymeric/rum-microfrontend
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,24 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| import type { SourceCodeContextOptions } from './types'; | ||
|
|
||
| export const DEFAULT_SOURCE_CODE_CONTEXT_VARIABLE = 'DD_SOURCE_CODE_CONTEXT' as const; | ||
|
|
||
| // The source code context snippet - single injection with function definition and call | ||
| // SSR-safe: checks window before accessing, never throws | ||
| // | ||
| // Unminified version: | ||
| // (function(c, n) { | ||
| // try { | ||
| // if (typeof window === 'undefined') return; | ||
| // var w = window, | ||
| // m = w[n] = w[n] || {}, | ||
| // s = new Error().stack; | ||
| // s && (m[s] = c) | ||
| // } catch (e) {} | ||
| // })(context, variableName); | ||
| export const getSourceCodeContextSnippet = (context: SourceCodeContextOptions): string => { | ||
| return `(function(c,n){try{if(typeof window==='undefined')return;var w=window,m=w[n]=w[n]||{},s=new Error().stack;s&&(m[s]=c)}catch(e){}})(${JSON.stringify(context)},${JSON.stringify(DEFAULT_SOURCE_CODE_CONTEXT_VARIABLE)});`; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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,20 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| import type { Logger } from '@dd/core/types'; | ||
|
|
||
| import type { SourceCodeContextOptions } from './types'; | ||
|
|
||
| export const DEFAULT_SOURCE_CODE_CONTEXT_VARIABLE = 'DD_SOURCE_CODE_CONTEXT' as const; | ||
|
|
||
| // The source code context snippet - single injection with function definition and call | ||
| // SSR-safe: checks window before accessing, never throws | ||
| // Minified for minimal bundle size impact | ||
| export const getSourceCodeContextSnippet = ( | ||
| context: SourceCodeContextOptions, | ||
| log: Logger, | ||
| ): string => { | ||
| // prettier-ignore | ||
| return `(function(c,n){try{if(typeof window==='undefined')return;var w=window,m=w[n]=w[n]||{},s=new Error().stack;s&&(m[s]=c)}catch(e){}})(${JSON.stringify(context)},${JSON.stringify(DEFAULT_SOURCE_CODE_CONTEXT_VARIABLE)});`; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,12 @@ import chalk from 'chalk'; | |||||
|
|
||||||
| import { CONFIG_KEY, PLUGIN_NAME } from './constants'; | ||||||
| import type { PrivacyOptionsWithDefaults } from './privacy/types'; | ||||||
| import type { RumOptions, RumOptionsWithDefaults, SDKOptionsWithDefaults } from './types'; | ||||||
| import type { | ||||||
| RumOptions, | ||||||
| RumOptionsWithDefaults, | ||||||
| SDKOptionsWithDefaults, | ||||||
| SourceCodeContextOptions, | ||||||
| } from './types'; | ||||||
|
|
||||||
| export const validateOptions = ( | ||||||
| options: OptionsWithDefaults, | ||||||
|
|
@@ -18,9 +23,11 @@ export const validateOptions = ( | |||||
| // Validate and add defaults sub-options. | ||||||
| const sdkResults = validateSDKOptions(options); | ||||||
| const privacyResults = validatePrivacyOptions(options); | ||||||
| const sourceCodeContextResults = validateSourceCodeContextOptions(options); | ||||||
|
|
||||||
| errors.push(...sdkResults.errors); | ||||||
| errors.push(...privacyResults.errors); | ||||||
| errors.push(...sourceCodeContextResults.errors); | ||||||
|
|
||||||
| // Throw if there are any errors. | ||||||
| if (errors.length) { | ||||||
|
|
@@ -34,6 +41,7 @@ export const validateOptions = ( | |||||
| ...options[CONFIG_KEY], | ||||||
| sdk: undefined, | ||||||
| privacy: undefined, | ||||||
| sourceCodeContext: undefined, | ||||||
| }; | ||||||
|
|
||||||
| // Fill in the defaults. | ||||||
|
|
@@ -55,6 +63,10 @@ export const validateOptions = ( | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if (sourceCodeContextResults.config) { | ||||||
| toReturn.sourceCodeContext = sourceCodeContextResults.config; | ||||||
| } | ||||||
|
|
||||||
| return toReturn; | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -141,3 +153,26 @@ export const validatePrivacyOptions = (options: Options): ToReturn<PrivacyOption | |||||
|
|
||||||
| return toReturn; | ||||||
| }; | ||||||
|
|
||||||
| export const validateSourceCodeContextOptions = ( | ||||||
| options: OptionsWithDefaults, | ||||||
| ): ToReturn<SourceCodeContextOptions> => { | ||||||
| const red = chalk.bold.red; | ||||||
| const validatedOptions: RumOptions = options[CONFIG_KEY] || {}; | ||||||
| const toReturn: ToReturn<SourceCodeContextOptions> = { | ||||||
| errors: [], | ||||||
| }; | ||||||
|
|
||||||
| if (!validatedOptions.sourceCodeContext) { | ||||||
| return toReturn; | ||||||
| } | ||||||
|
|
||||||
| const cfg = validatedOptions.sourceCodeContext as SourceCodeContextOptions; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this work?
Suggested change
|
||||||
|
|
||||||
| if (!cfg?.service || typeof cfg.service !== 'string') { | ||||||
| toReturn.errors.push(`Missing ${red('"sourceCodeContext.service"')}.`); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid any confusion from user's standpoint.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| toReturn.config = cfg; | ||||||
| return toReturn; | ||||||
| }; | ||||||
19 changes: 19 additions & 0 deletions
19
packages/tests/src/e2e/sourceCodeContext/project/index.html
This file contains hidden or 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,19 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="icon" type="image/svg+xml" sizes="21x21" href="data:image/svg+xml," /> | ||
| <title>Source Code Context Test</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <h1>Source Code Context Test - {{bundler}}</h1> | ||
| <p>Testing source code context injection.</p> | ||
|
|
||
| <button id="trigger_error" role="button">Trigger Error</button> | ||
| <button id="capture_stack" role="button">Capture Stack</button> | ||
|
|
||
| <script src="./dist/{{bundler}}.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or 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,31 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| /* eslint-env browser */ | ||
|
|
||
| const $ = document.querySelector.bind(document); | ||
|
|
||
| // Function to capture and store a stack trace | ||
| function captureStackTrace() { | ||
| const error = new Error(); | ||
| const stack = error.stack; | ||
| return stack; | ||
| } | ||
|
|
||
| // Store stack trace globally for testing | ||
| $('#capture_stack').addEventListener('click', () => { | ||
| const stack = captureStackTrace(); | ||
| window.capturedStack = stack; | ||
| console.log('Stack captured:', stack); | ||
| }); | ||
|
|
||
| // Trigger an error for testing | ||
| $('#trigger_error').addEventListener('click', () => { | ||
| try { | ||
| throw new Error('Test error from source code context'); | ||
| } catch (e) { | ||
| window.caughtError = e; | ||
| console.log('Error caught:', e); | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this file accidentally duplicated?