Skip to content

Commit

Permalink
Minor comment solving on the code and fixing perofrmance.now
Browse files Browse the repository at this point in the history
  • Loading branch information
arafatkatze committed Nov 20, 2024
1 parent c497269 commit 0de24aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion vscode/src/services/open-telemetry/trace-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const TraceSender = {
* Sends trace data to the server using the provided span data as a json string
* that comes from the webview. It retrieves the current resolved configuration to obtain
* authentication details and constructs the trace URL. It sends a POST
* request with the span data as the body.
* request with the span data as the body.
*/
async function doSendTraceData(spanData: any): Promise<void> {
const { auth } = await currentResolvedConfig()
Expand Down
59 changes: 34 additions & 25 deletions vscode/webviews/chat/Transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => {
editorValue: SerializedPromptEditorValue,
intentFromSubmit?: ChatMessage['intent']
) => {
performance.mark('startSubmit')
// Start the span as soon as the user clicks the submit button
const spanManager = new SpanManager('cody-webview')
const span = spanManager.startSpan('chat-interaction', {
Expand Down Expand Up @@ -306,7 +307,6 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => {
} else {
submitHumanMessage({
...commonProps,
setActiveChatContext,
})
}

Expand All @@ -317,14 +317,14 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => {
(editorValue: SerializedPromptEditorValue, intentFromSubmit?: ChatMessage['intent']): void => {
startSpanAndSubmit('edit', editorValue, intentFromSubmit)
},
[humanMessage.index, intentResults]
[startSpanAndSubmit]
)

const onFollowupSubmit = useCallback(
(editorValue: SerializedPromptEditorValue, intentFromSubmit?: ChatMessage['intent']): void => {
startSpanAndSubmit('submit', editorValue, intentFromSubmit)
},
[intentResults, setActiveChatContext]
[startSpanAndSubmit]
)

const extensionAPI = useExtensionAPI()
Expand Down Expand Up @@ -391,13 +391,13 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => {
if (assistantMessage.isLoading && !renderSpan.current && activeChatContext) {
// Create render span as child of active chat context
context.with(activeChatContext, () => {
renderStartTime.current = Date.now()
performance.mark('startRender')
renderSpan.current = spanManager.startSpan('assistant-message-render', {
attributes: {
sampled: true,
'render.state': 'started',
'message.index': assistantMessage.index,
'render.start_time': renderStartTime.current,
'render.start_time': performance.now(),
'parent.span.id': trace.getSpan(activeChatContext)?.spanContext().spanId,
},
context: activeChatContext,
Expand All @@ -413,26 +413,33 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => {
})
} else if (!isLoading && renderSpan.current) {
// Complete the render span
renderSpan.current.setAttributes({
'render.state': 'completed',
'render.success': !assistantMessage?.error,
'message.length': assistantMessage?.text?.length ?? 0,
'render.total_time': Date.now() - (renderStartTime.current ?? Date.now()),
})
renderSpan.current.end()
performance.mark('endRender')
performance.measure('renderDuration', 'startRender', 'endRender')
const measure = performance.getEntriesByName('renderDuration')[0]
if (measure.duration > 0) {
// Ensure non-zero duration
renderSpan.current.setAttributes({
'render.state': 'completed',
'render.success': !assistantMessage?.error,
'message.length': assistantMessage?.text?.length ?? 0,
'render.total_time': measure.duration,
})
renderSpan.current.end()
}
renderSpan.current = undefined
timeToFirstTokenSpan.current?.end()
timeToFirstTokenSpan.current = undefined
renderStartTime.current = undefined
hasRecordedFirstToken.current = false

// Only end the chat context if this is truly the last message
if (activeChatContext) {
const rootSpan = trace.getSpan(activeChatContext)
if (rootSpan) {
const chatTotalTime =
performance.now() - performance.getEntriesByName('startSubmit')[0].startTime
rootSpan.setAttributes({
'chat.completed': true,
'chat.total_time': Date.now() - (renderStartTime.current ?? Date.now()),
'chat.total_time': chatTotalTime,
})
rootSpan.end()
}
Expand All @@ -441,20 +448,24 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => {
} else if (
assistantMessage.text &&
!hasRecordedFirstToken.current &&
timeToFirstTokenSpan.current &&
renderStartTime.current
timeToFirstTokenSpan.current
) {
// End the time-to-first-token span when first token appears
const timeToFirstToken = Date.now() - renderStartTime.current
timeToFirstTokenSpan.current.setAttributes({
'time.to.first.token': timeToFirstToken,
})
timeToFirstTokenSpan.current.end()
performance.mark('firstToken')
performance.measure('timeToFirstToken', 'startRender', 'firstToken')
const firstTokenMeasure = performance.getEntriesByName('timeToFirstToken')[0]

if (firstTokenMeasure.duration > 0) {
// Ensure non-zero duration
timeToFirstTokenSpan.current.setAttributes({
'time.to.first.token': firstTokenMeasure.duration,
})
timeToFirstTokenSpan.current.end()
}
timeToFirstTokenSpan.current = undefined
hasRecordedFirstToken.current = true

// Also set on parent span for backwards compatibility
renderSpan.current?.setAttribute('time.to.first.token', timeToFirstToken)
renderSpan.current?.setAttribute('time.to.first.token', firstTokenMeasure.duration)
}
}
}, [assistantMessage, activeChatContext, setActiveChatContext, spanManager, isLoading])
Expand Down Expand Up @@ -704,13 +715,11 @@ function submitHumanMessage({
intent,
intentScores,
manuallySelectedIntent,
setActiveChatContext,
}: {
editorValue: SerializedPromptEditorValue
intent?: ChatMessage['intent']
intentScores?: { intent: string; score: number }[]
manuallySelectedIntent?: boolean
setActiveChatContext: (context: Context | undefined) => void
}): void {
getVSCodeAPI().postMessage({
command: 'submit',
Expand Down
3 changes: 3 additions & 0 deletions vscode/webviews/utils/webviewOpenTelemetryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export class WebviewOpenTelemetryService {
}

public dispose(): void {
if (WebviewOpenTelemetryService.instance !== this) {
return
}
this.reset()
WebviewOpenTelemetryService.instance = null
}
Expand Down

0 comments on commit 0de24aa

Please sign in to comment.