-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mv3): Tracking URL resolved/observed count. (#1245)
* fix(mv3): Reverting Telemetry Changes To Use PatchedCountlySDK Signed-off-by: Nishant Arora <[email protected]> * fix(mv3): metrics build Signed-off-by: Nishant Arora <[email protected]> * fix: more reverts + fixing patch Signed-off-by: Nishant Arora <[email protected]> * feat: adding request view. Signed-off-by: Nishant Arora <[email protected]> * Reverting to mainline rc patch * Reverting to mainline rc add-on/src/lib/ipfs-companion.js * feat(telemetry): Signed-off-by: Nishant Arora <[email protected]> * feat(telemetry): Implementing RequestTracker Event Handler Signed-off-by: Nishant Arora <[email protected]> * feat(telemetry): hooking up events. Signed-off-by: Nishant Arora <[email protected]> * fix(types): annotations * fix(telemetry): 🗑️ returning to previous state Signed-off-by: Nishant Arora <[email protected]> * fix(telemetry): ♻️ Refactor Request Tracker Signed-off-by: Nishant Arora <[email protected]> * fix(telemetry): hooking up requests Signed-off-by: Nishant Arora <[email protected]> * fix(telemetry): better types Signed-off-by: Nishant Arora <[email protected]> * fix(countly): 🗑️ more stuff goes, because test need to pass. Signed-off-by: Nishant Arora <[email protected]> * fix(lint): fixed * feat(test): test tracker. * fix: remove only Signed-off-by: Nishant Arora <[email protected]> * fix: 💄 line break Signed-off-by: Nishant Arora <[email protected]> --------- Signed-off-by: Nishant Arora <[email protected]>
- Loading branch information
Showing
8 changed files
with
274 additions
and
56 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
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,49 @@ | ||
import debug from 'debug' | ||
import type browser from 'webextension-polyfill' | ||
import { trackEvent } from '../telemetry.js' | ||
|
||
export class RequestTracker { | ||
private readonly eventKey: 'url-observed' | 'url-resolved' | ||
private readonly flushInterval: number | ||
private readonly log: debug.Debugger & { error?: debug.Debugger } | ||
private lastSync: number = Date.now() | ||
private requestTypeStore: { [key in browser.WebRequest.ResourceType]?: number } = {} | ||
|
||
constructor (eventKey: 'url-observed' | 'url-resolved', flushInterval = 1000 * 60 * 5) { | ||
this.eventKey = eventKey | ||
this.log = debug(`ipfs-companion:request-tracker:${eventKey}`) | ||
this.log.error = debug(`ipfs-companion:request-tracker:${eventKey}:error`) | ||
this.flushInterval = flushInterval | ||
this.setupFlushScheduler() | ||
} | ||
|
||
track ({ type }: browser.WebRequest.OnBeforeRequestDetailsType): void { | ||
this.log(`track ${type}`, JSON.stringify(this.requestTypeStore)) | ||
this.requestTypeStore[type] = (this.requestTypeStore[type] ?? 0) + 1 | ||
} | ||
|
||
private flushStore (): void { | ||
this.log('flushing') | ||
const count = Object.values(this.requestTypeStore).reduce((a, b): number => a + b, 0) | ||
if (count === 0) { | ||
this.log('nothing to flush') | ||
return | ||
} | ||
trackEvent({ | ||
key: this.eventKey, | ||
count, | ||
dur: Date.now() - this.lastSync, | ||
segmentation: Object.assign({}, this.requestTypeStore) as unknown as Record<string, string> | ||
}) | ||
// reset | ||
this.lastSync = Date.now() | ||
this.requestTypeStore = {} | ||
} | ||
|
||
private setupFlushScheduler (): void { | ||
setTimeout(() => { | ||
this.flushStore() | ||
this.setupFlushScheduler() | ||
}, this.flushInterval) | ||
} | ||
} |
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,9 @@ | ||
declare module 'countly-web-sdk' { | ||
export interface CountlyEvent { | ||
key: string | ||
count?: number | ||
sum?: number | ||
dur?: number | ||
segmentation?: Record<string, string> | ||
} | ||
} |
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.