| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Repository} from '../Repository'; |
| b69ab31 | | | 9 | import type {Logger} from '../logger'; |
| b69ab31 | | | 10 | import type {ServerPlatform} from '../serverPlatform'; |
| b69ab31 | | | 11 | import type {ApplicationInfo, FullTrackData, TrackDataWithEventName} from './types'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {Internal} from '../Internal'; |
| b69ab31 | | | 14 | import {generateAnalyticsInfo} from './environment'; |
| b69ab31 | | | 15 | import {Tracker} from './tracker'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | export type ServerSideTracker = Tracker<ServerSideContext>; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | class ServerSideContext { |
| b69ab31 | | | 20 | constructor( |
| b69ab31 | | | 21 | public logger: Logger, |
| b69ab31 | | | 22 | public data: ApplicationInfo, |
| b69ab31 | | | 23 | ) {} |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | public setRepo(repo: Repository | undefined): void { |
| b69ab31 | | | 26 | this.data.repo = repo?.codeReviewProvider?.getSummaryName(); |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | const noOp = (_data: FullTrackData, _logger: Logger) => { |
| b69ab31 | | | 31 | /* In open source builds, analytics tracking is completely disabled/removed. */ |
| b69ab31 | | | 32 | }; |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | /** |
| b69ab31 | | | 35 | * Creates a Tracker which includes server-side-only cached application data like platform, username, etc, |
| b69ab31 | | | 36 | * and sends data to the underlying analytics engine outside of ISL. |
| b69ab31 | | | 37 | * This can not be global since two client connections may have different cached data. |
| b69ab31 | | | 38 | */ |
| b69ab31 | | | 39 | export function makeServerSideTracker( |
| b69ab31 | | | 40 | logger: Logger, |
| b69ab31 | | | 41 | platform: ServerPlatform, |
| b69ab31 | | | 42 | version: string, |
| b69ab31 | | | 43 | // prettier-ignore |
| b69ab31 | | | 44 | writeToServer = |
| b69ab31 | | | 45 | Internal.trackToScribe ?? |
| b69ab31 | | | 46 | noOp, |
| b69ab31 | | | 47 | ): ServerSideTracker { |
| b69ab31 | | | 48 | const analyticsInfo = generateAnalyticsInfo(platform.platformName, version, platform.sessionId); |
| b69ab31 | | | 49 | logger.info('Setup analytics, session: ', analyticsInfo.sessionId); |
| b69ab31 | | | 50 | return new Tracker( |
| b69ab31 | | | 51 | (data: TrackDataWithEventName, context: ServerSideContext) => { |
| b69ab31 | | | 52 | const {logger} = context; |
| b69ab31 | | | 53 | // log track event, since tracking events can be used as datapoints when reviewing logs |
| b69ab31 | | | 54 | logger.log( |
| b69ab31 | | | 55 | '[track]', |
| b69ab31 | | | 56 | data.eventName, |
| b69ab31 | | | 57 | data.errorName ?? '', |
| b69ab31 | | | 58 | data.extras != null ? JSON.stringify(data.extras) : '', |
| b69ab31 | | | 59 | ); |
| b69ab31 | | | 60 | writeToServer({...data, ...context.data}, logger); |
| b69ab31 | | | 61 | }, |
| b69ab31 | | | 62 | new ServerSideContext(logger, analyticsInfo), |
| b69ab31 | | | 63 | ); |
| b69ab31 | | | 64 | } |