|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { AuthUtil, getSelectedCustomization } from 'aws-core-vscode/codewhisperer' |
| 7 | +import { CodewhispererLanguage } from 'aws-core-vscode/shared' |
| 8 | +import { CodewhispererTriggerType, telemetry } from 'aws-core-vscode/telemetry' |
| 9 | +import { InlineCompletionTriggerKind } from 'vscode' |
| 10 | + |
| 11 | +export class TelemetryHelper { |
| 12 | + // Variables needed for client component latency |
| 13 | + private _invokeSuggestionStartTime = 0 |
| 14 | + private _preprocessEndTime = 0 |
| 15 | + private _sdkApiCallStartTime = 0 |
| 16 | + private _sdkApiCallEndTime = 0 |
| 17 | + private _allPaginationEndTime = 0 |
| 18 | + private _firstSuggestionShowTime = 0 |
| 19 | + private _firstResponseRequestId = '' |
| 20 | + private _sessionId = '' |
| 21 | + private _language: CodewhispererLanguage = 'java' |
| 22 | + private _triggerType: CodewhispererTriggerType = 'OnDemand' |
| 23 | + |
| 24 | + constructor() {} |
| 25 | + |
| 26 | + static #instance: TelemetryHelper |
| 27 | + |
| 28 | + public static get instance() { |
| 29 | + return (this.#instance ??= new this()) |
| 30 | + } |
| 31 | + |
| 32 | + public resetClientComponentLatencyTime() { |
| 33 | + this._invokeSuggestionStartTime = 0 |
| 34 | + this._preprocessEndTime = 0 |
| 35 | + this._sdkApiCallStartTime = 0 |
| 36 | + this._sdkApiCallEndTime = 0 |
| 37 | + this._firstSuggestionShowTime = 0 |
| 38 | + this._allPaginationEndTime = 0 |
| 39 | + this._firstResponseRequestId = '' |
| 40 | + } |
| 41 | + |
| 42 | + public setInvokeSuggestionStartTime() { |
| 43 | + this.resetClientComponentLatencyTime() |
| 44 | + this._invokeSuggestionStartTime = performance.now() |
| 45 | + } |
| 46 | + |
| 47 | + get invokeSuggestionStartTime(): number { |
| 48 | + return this._invokeSuggestionStartTime |
| 49 | + } |
| 50 | + |
| 51 | + public setPreprocessEndTime() { |
| 52 | + this._preprocessEndTime = performance.now() |
| 53 | + } |
| 54 | + |
| 55 | + get preprocessEndTime(): number { |
| 56 | + return this._preprocessEndTime |
| 57 | + } |
| 58 | + |
| 59 | + public setSdkApiCallStartTime() { |
| 60 | + if (this._sdkApiCallStartTime === 0) { |
| 61 | + this._sdkApiCallStartTime = performance.now() |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + get sdkApiCallStartTime(): number { |
| 66 | + return this._sdkApiCallStartTime |
| 67 | + } |
| 68 | + |
| 69 | + public setSdkApiCallEndTime() { |
| 70 | + if (this._sdkApiCallEndTime === 0 && this._sdkApiCallStartTime !== 0) { |
| 71 | + this._sdkApiCallEndTime = performance.now() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + get sdkApiCallEndTime(): number { |
| 76 | + return this._sdkApiCallEndTime |
| 77 | + } |
| 78 | + |
| 79 | + public setAllPaginationEndTime() { |
| 80 | + if (this._allPaginationEndTime === 0 && this._sdkApiCallEndTime !== 0) { |
| 81 | + this._allPaginationEndTime = performance.now() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + get allPaginationEndTime(): number { |
| 86 | + return this._allPaginationEndTime |
| 87 | + } |
| 88 | + |
| 89 | + public setFirstSuggestionShowTime() { |
| 90 | + if (this._firstSuggestionShowTime === 0 && this._sdkApiCallEndTime !== 0) { |
| 91 | + this._firstSuggestionShowTime = performance.now() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + get firstSuggestionShowTime(): number { |
| 96 | + return this._firstSuggestionShowTime |
| 97 | + } |
| 98 | + |
| 99 | + public setFirstResponseRequestId(requestId: string) { |
| 100 | + if (this._firstResponseRequestId === '') { |
| 101 | + this._firstResponseRequestId = requestId |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + get firstResponseRequestId(): string { |
| 106 | + return this._firstResponseRequestId |
| 107 | + } |
| 108 | + |
| 109 | + public setSessionId(sessionId: string) { |
| 110 | + if (this._sessionId === '') { |
| 111 | + this._sessionId = sessionId |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + get sessionId(): string { |
| 116 | + return this._sessionId |
| 117 | + } |
| 118 | + |
| 119 | + public setLanguage(language: CodewhispererLanguage) { |
| 120 | + this._language = language |
| 121 | + } |
| 122 | + |
| 123 | + get language(): CodewhispererLanguage { |
| 124 | + return this._language |
| 125 | + } |
| 126 | + |
| 127 | + public setTriggerType(triggerType: InlineCompletionTriggerKind) { |
| 128 | + if (triggerType === InlineCompletionTriggerKind.Invoke) { |
| 129 | + this._triggerType = 'OnDemand' |
| 130 | + } else if (triggerType === InlineCompletionTriggerKind.Automatic) { |
| 131 | + this._triggerType = 'AutoTrigger' |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + get triggerType(): string { |
| 136 | + return this._triggerType |
| 137 | + } |
| 138 | + |
| 139 | + // report client component latency after all pagination call finish |
| 140 | + // and at least one suggestion is shown to the user |
| 141 | + public tryRecordClientComponentLatency() { |
| 142 | + if (this._firstSuggestionShowTime === 0 || this._allPaginationEndTime === 0) { |
| 143 | + return |
| 144 | + } |
| 145 | + telemetry.codewhisperer_clientComponentLatency.emit({ |
| 146 | + codewhispererAllCompletionsLatency: this._allPaginationEndTime - this._sdkApiCallStartTime, |
| 147 | + codewhispererCompletionType: 'Line', |
| 148 | + codewhispererCredentialFetchingLatency: 0, // no longer relevant, because we don't re-build the sdk. Flare already has that set |
| 149 | + codewhispererCustomizationArn: getSelectedCustomization().arn, |
| 150 | + codewhispererEndToEndLatency: this._firstSuggestionShowTime - this._invokeSuggestionStartTime, |
| 151 | + codewhispererFirstCompletionLatency: this._sdkApiCallEndTime - this._sdkApiCallStartTime, |
| 152 | + codewhispererLanguage: this._language, |
| 153 | + codewhispererPostprocessingLatency: this._firstSuggestionShowTime - this._sdkApiCallEndTime, |
| 154 | + codewhispererPreprocessingLatency: this._preprocessEndTime - this._invokeSuggestionStartTime, |
| 155 | + codewhispererRequestId: this._firstResponseRequestId, |
| 156 | + codewhispererSessionId: this._sessionId, |
| 157 | + codewhispererTriggerType: this._triggerType, |
| 158 | + credentialStartUrl: AuthUtil.instance.startUrl, |
| 159 | + result: 'Succeeded', |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
0 commit comments