-
Notifications
You must be signed in to change notification settings - Fork 531
Initial front-end metrics implementation #51171
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
Merged
sanchitmalhotra126
merged 3 commits into
staging
from
sanchit/metrics/initial-front-end
Apr 14, 2023
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** | ||
| * Interface for interacting with a metrics service API. | ||
| */ | ||
| export default interface MetricsApi { | ||
| sendLogs: (logs: object[]) => Promise<Response>; | ||
| } | ||
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,116 @@ | ||
| import MetricsApi from './MetricsApi'; | ||
|
|
||
| const isDevelopmentEnvironment = | ||
| require('../../utils').isDevelopmentEnvironment; | ||
|
|
||
| /** | ||
| * If we receive an unauthorized response from the server, this may | ||
| * indicate that browser event reporting has been temporarily disabled. | ||
| * We will wait for a specific time interval (defined below) before making | ||
| * a request again, so as to not flood the server with requests. | ||
| */ | ||
| const CHECK_CAN_REPORT_INTERVAL_MINUTES = 30; | ||
| const CHECK_CAN_REPORT_INTERVAL_MS = | ||
| CHECK_CAN_REPORT_INTERVAL_MINUTES * 60 * 1000; | ||
| const LOCAL_STORAGE_KEY_NAME = 'cdo-metrics-reporter-last-check-time'; | ||
|
|
||
| type LogLevel = 'INFO' | 'WARNING' | 'SEVERE'; | ||
|
|
||
| /** | ||
| * Reports logs and metrics, intended primarily for developer-facing | ||
| * error reporting, metric reporting, and logging. | ||
| * | ||
| * For tracking user interactions and behaviors (product-facing), | ||
| * see {@link AnalyticsReporter} which reports to Amplitude. | ||
| * | ||
| * For legacy client-side reporting see {@link firehose} for AWS | ||
| * Firehose reporting and {@link logToCloud} for New Relic reporting. | ||
| */ | ||
| class MetricsReporter { | ||
| private lastCheckCanReportTime: number; | ||
|
|
||
| constructor(private readonly metricsApi: MetricsApi) { | ||
| this.metricsApi = metricsApi; | ||
| this.lastCheckCanReportTime = | ||
| parseInt(localStorage.getItem(LOCAL_STORAGE_KEY_NAME) || '0') || 0; | ||
| } | ||
|
|
||
| logInfo(message: string | object) { | ||
| this.log('INFO', message); | ||
| if (isDevelopmentEnvironment()) { | ||
| console.log('[MetricsReporter] ' + JSON.stringify(message)); | ||
| } | ||
| } | ||
|
|
||
| logWarning(message: string | object) { | ||
| this.log('WARNING', message); | ||
| if (isDevelopmentEnvironment()) { | ||
| console.warn('[MetricsReporter] ' + JSON.stringify(message)); | ||
| } | ||
| } | ||
|
|
||
| logError(message: string | object) { | ||
| this.log('SEVERE', message); | ||
| if (isDevelopmentEnvironment()) { | ||
| console.error('[MetricsReporter] ' + JSON.stringify(message)); | ||
| } | ||
| } | ||
|
|
||
| private log(level: LogLevel, message: string | object) { | ||
| const payload = { | ||
| level, | ||
| message, | ||
| deviceInfo: this.getDeviceInfo() | ||
| }; | ||
|
|
||
| if (!this.isReportingEnabled()) { | ||
| this.fallbackLog(payload); | ||
| return; | ||
| } | ||
|
|
||
| this.metricsApi.sendLogs([payload]).then(response => { | ||
| if (!response.ok) { | ||
| this.fallbackLog(payload); | ||
| } | ||
|
|
||
| if (response.status === 401) { | ||
| // Unauthorized response from server; client logging is likely disabled. | ||
| // We will check again after a time period of CHECK_CAN_REPORT_INTERVAL | ||
| this.setReportingDisabled(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private getDeviceInfo(): object { | ||
| return { | ||
| user_agent: window.navigator.userAgent, | ||
| window_width: window.innerWidth, | ||
| window_height: window.innerHeight, | ||
| hostname: window.location.hostname, | ||
| full_path: window.location.href | ||
| }; | ||
| } | ||
|
|
||
| private fallbackLog(payload: object) { | ||
| if (isDevelopmentEnvironment()) { | ||
| console.log( | ||
| 'Client-side reporting disabled. Attempted to report: ' + | ||
| JSON.stringify(payload) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private isReportingEnabled(): boolean { | ||
| return ( | ||
| Date.now() - this.lastCheckCanReportTime > CHECK_CAN_REPORT_INTERVAL_MS | ||
| ); | ||
| } | ||
|
|
||
| private setReportingDisabled() { | ||
| this.lastCheckCanReportTime = Date.now(); | ||
| localStorage.setItem( | ||
| LOCAL_STORAGE_KEY_NAME, | ||
| this.lastCheckCanReportTime.toString() | ||
| ); | ||
| } | ||
| } |
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.
should we define the log type more than just an
object?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.
I was thinking this could just be a generic JSON object payload; but I guess if we know about some required parameters that the metrics reporter adds (such as device info), this could be defined in a little more detail?
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.
I opted to just keep this as an
objectfor now for flexibility and to match the backend method signature (which just takes a list of JSON objects to log).