-
Notifications
You must be signed in to change notification settings - Fork 21
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 3 commits
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
*/ | ||
import type { LDContext, LDEvaluationDetail, LDInspection } from '@launchdarkly/js-client-sdk'; | ||
|
||
import { LDClientTracking } from './api'; | ||
import { LDClientTracking, MinLogger } from './api'; | ||
import { Breadcrumb, FeatureManagementBreadcrumb } from './api/Breadcrumb'; | ||
import { BrowserTelemetry } from './api/BrowserTelemetry'; | ||
import { Collector } from './api/Collector'; | ||
|
@@ -18,6 +18,7 @@ import FetchCollector from './collectors/http/fetch'; | |
import XhrCollector from './collectors/http/xhr'; | ||
import defaultUrlFilter from './filters/defaultUrlFilter'; | ||
import makeInspectors from './inspectors'; | ||
import { fallbackLogger, prefixLog } from './logging'; | ||
import { ParsedOptions, ParsedStackOptions } from './options'; | ||
import randomUuidV4 from './randomUuidV4'; | ||
import parse from './stack/StackParser'; | ||
|
@@ -80,6 +81,11 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry { | |
private _collectors: Collector[] = []; | ||
private _sessionId: string = randomUuidV4(); | ||
|
||
private _logger: MinLogger; | ||
|
||
// Used to ensure we only log the event dropped message once. | ||
private _eventsDropped: boolean = false; | ||
|
||
constructor(private _options: ParsedOptions) { | ||
configureTraceKit(_options.stack); | ||
|
||
|
@@ -127,16 +133,28 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry { | |
const inspectors: LDInspection[] = []; | ||
makeInspectors(_options, inspectors, impl); | ||
this._inspectorInstances.push(...inspectors); | ||
|
||
// Set the initial logger, it may be replaced when the client is registered. | ||
// For typescript purposes, we need the logger to be directly set in the constructor. | ||
this._logger = this._options.logger ?? fallbackLogger; | ||
} | ||
|
||
register(client: LDClientTracking): void { | ||
this._client = client; | ||
// When the client is registered, we need to set the logger again, because we may be able to use the client's | ||
// logger. | ||
this._setLogger(); | ||
this._pendingEvents.forEach((event) => { | ||
this._client?.track(event.type, event.data); | ||
}); | ||
this._pendingEvents = []; | ||
} | ||
|
||
private _setLogger() { | ||
this._logger = | ||
this._options.logger ?? ((this._client as any)?.logger as MinLogger) ?? fallbackLogger; | ||
} | ||
|
||
inspectors(): LDInspection[] { | ||
return this._inspectorInstances; | ||
} | ||
|
@@ -153,7 +171,14 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry { | |
if (this._client === undefined) { | ||
this._pendingEvents.push({ type, data: event }); | ||
if (this._pendingEvents.length > this._maxPendingEvents) { | ||
// TODO: Log when pending events must be dropped. (SDK-915) | ||
if (!this._eventsDropped) { | ||
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. Generally whenever a max is hit it will be hit many times. This will just log once to avoid creating log spam. This situation will generally represent misconfiguration where the client is not being registered. Or something terribly wrong in the users application. |
||
this._eventsDropped = true; | ||
this._logger.warn( | ||
prefixLog( | ||
`Maximim pending events reached. Old events will be dropped until the SDK client is registered.`, | ||
kinyoklion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
); | ||
} | ||
this._pendingEvents.shift(); | ||
} | ||
} | ||
|
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.'), | ||
); | ||
} | ||
}, | ||
}; | ||
} |
Oops, something went wrong.
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.