-
Notifications
You must be signed in to change notification settings - Fork 20
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: Add basic logging support for browser-telemetry. #736
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b6852d2
feat: Add basic logging support for browser-telemetry.
kinyoklion 3b7030b
More robust max pending event handling.
kinyoklion 5064806
Remove unused options from test.
kinyoklion da9f668
Remove robustness change included in another PR.
kinyoklion 9ed9920
Merge branch 'main' into rlamb/emsr-6/browser-telemetry-logging
kinyoklion 7d6c5ab
Add tests for breadcrumb warning message.
kinyoklion 0106f1b
Refactoring and additional tests.
kinyoklion 58961ee
Add extra comment.
kinyoklion 63ea0b9
More comments
kinyoklion bf24176
Fix typo.
kinyoklion 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
52 changes: 52 additions & 0 deletions
52
packages/telemetry/browser-telemetry/__tests__/logging.test.ts
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,52 @@ | ||
import { MinLogger } from '../src/api'; | ||
import { fallbackLogger, prefixLog, safeMinLogger } from '../src/logging'; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('prefixes the message with the telemetry prefix', () => { | ||
const message = 'test message'; | ||
const prefixed = prefixLog(message); | ||
expect(prefixed).toBe('LaunchDarkly - Browser Telemetry: test message'); | ||
}); | ||
|
||
it('uses fallback logger when no logger provided', () => { | ||
const spy = jest.spyOn(fallbackLogger, 'warn'); | ||
const logger = safeMinLogger(undefined); | ||
|
||
logger.warn('test message'); | ||
|
||
expect(spy).toHaveBeenCalledWith('test message'); | ||
spy.mockRestore(); | ||
}); | ||
|
||
it('uses provided logger when it works correctly', () => { | ||
const mockWarn = jest.fn(); | ||
const testLogger: MinLogger = { | ||
warn: mockWarn, | ||
}; | ||
|
||
const logger = safeMinLogger(testLogger); | ||
logger.warn('test message'); | ||
|
||
expect(mockWarn).toHaveBeenCalledWith('test message'); | ||
}); | ||
|
||
it('falls back to fallback logger when provided logger throws', () => { | ||
const spy = jest.spyOn(fallbackLogger, 'warn'); | ||
const testLogger: MinLogger = { | ||
warn: () => { | ||
throw new Error('logger error'); | ||
}, | ||
}; | ||
|
||
const logger = safeMinLogger(testLogger); | ||
logger.warn('test message'); | ||
|
||
expect(spy).toHaveBeenCalledWith('test message'); | ||
expect(spy).toHaveBeenCalledWith( | ||
'LaunchDarkly - Browser Telemetry: The provided logger threw an exception, using fallback logger.', | ||
); | ||
spy.mockRestore(); | ||
}); |
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
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { BrowserTelemetry } from './api/BrowserTelemetry'; | ||
import { Options } from './api/Options'; | ||
import BrowserTelemetryImpl from './BrowserTelemetryImpl'; | ||
import { safeMinLogger } from './logging'; | ||
import parse from './options'; | ||
|
||
export * from './api'; | ||
|
||
export function initializeTelemetry(options?: Options): BrowserTelemetry { | ||
const parsedOptions = parse(options || {}); | ||
const parsedOptions = parse(options || {}, safeMinLogger(options?.logger)); | ||
return new BrowserTelemetryImpl(parsedOptions); | ||
} |
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,33 @@ | ||
import { MinLogger } from './api'; | ||
|
||
export const fallbackLogger: MinLogger = { | ||
// Intentionally using console.warn as a fallback logger. | ||
// eslint-disable-next-line no-console | ||
warn: console.warn, | ||
}; | ||
|
||
const loggingPrefix = 'LaunchDarkly - Browser Telemetry:'; | ||
|
||
export function prefixLog(message: string) { | ||
return `${loggingPrefix} ${message}`; | ||
} | ||
|
||
export function safeMinLogger(logger: MinLogger | undefined): MinLogger { | ||
return { | ||
warn: (...args: any[]) => { | ||
if (!logger) { | ||
fallbackLogger.warn(...args); | ||
return; | ||
} | ||
|
||
try { | ||
logger.warn(...args); | ||
} catch { | ||
fallbackLogger.warn(...args); | ||
fallbackLogger.warn( | ||
prefixLog('The provided logger threw an exception, using fallback logger.'), | ||
); | ||
} | ||
}, | ||
}; | ||
} |
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.
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.
So, basically in the 4.x SDK there is a
logger
property, but in 3.x there is not.Additionally error monitoring should always be initialized as early as possible, which is before the SDK (there are also some inter-dependencies that require this order). As a result we need some intermediate logging.