Skip to content

Commit ffc280d

Browse files
yemohyleyemohyleYevhen MohylevskyyCopilot
authored
Yemohyle/add to telemetry (#311837)
* add last asst messages logs * add last asst messages logs ... * add last asst messages logs .... * add last asst messages logs ..... * add assistant messages added logs * ... * add tokens info * add parentHeaderrequestId value to subagent telemetry * remove debug logging * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * suggested change * extra verification * revert suggested change * remove debug logs * add cashed tokens to Legasy SSE path --------- Co-authored-by: Yevhen Mohylevskyy <yevhenmohylevskyy@Yevhens-MacBook-Pro-2.local> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 47b9582 commit ffc280d

11 files changed

Lines changed: 50 additions & 2 deletions

File tree

extensions/copilot/src/extension/intents/node/toolCallingLoop.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export abstract class ToolCallingLoop<TOptions extends IToolCallingLoopOptions =
176176
private agentSpan: ISpanHandle | undefined;
177177
private chatSessionIdForTools: string | undefined;
178178
private toolsAvailableEmitted = false;
179+
private lastHeaderRequestId: string | undefined;
179180

180181
public appendAdditionalHookContext(context: string): void {
181182
if (!context) {
@@ -269,6 +270,7 @@ export abstract class ToolCallingLoop<TOptions extends IToolCallingLoopOptions =
269270
hasStopHookQuery,
270271
modeInstructions: this.options.request.modeInstructions2,
271272
additionalHookContext: this.additionalHookContext,
273+
parentHeaderRequestId: this.lastHeaderRequestId,
272274
};
273275
}
274276

@@ -1383,6 +1385,14 @@ export abstract class ToolCallingLoop<TOptions extends IToolCallingLoopOptions =
13831385
});
13841386
markChatExt(this.options.conversation.sessionId, ChatExtPerfMark.DidFetch);
13851387

1388+
// Store the headerRequestId from the fetch response for subagent telemetry linking.
1389+
// Use requestId (the client-generated UUID sent as X-Request-Id header), not serverRequestId
1390+
// (the server's response header value), because requestId is what appears as headerRequestId
1391+
// across all telemetry events.
1392+
if (fetchResult.type === ChatFetchResponseType.Success) {
1393+
this.lastHeaderRequestId = fetchResult.requestId;
1394+
}
1395+
13861396
const promptTokenDetails = await computePromptTokenDetails({
13871397
messages: effectiveBuildPromptResult.messages,
13881398
tokenizer,

extensions/copilot/src/extension/prompt/common/intents.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ export interface IBuildPromptContext {
120120
* Additional context provided by a hook.
121121
*/
122122
readonly additionalHookContext?: string;
123+
/**
124+
* The headerRequestId from the most recent parent fetch response.
125+
* Used by subagent tools to link their telemetry back to the parent's HTTP request.
126+
*/
127+
readonly parentHeaderRequestId?: string;
123128
}
124129

125130
export const IBuildPromptContext = createServiceIdentifier<IBuildPromptContext>('IBuildPromptContext');

extensions/copilot/src/extension/prompt/node/chatMLFetcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ export class ChatMLFetcherImpl extends AbstractChatMLFetcher {
217217
const countTokens = () => tokenCountPromise ??= chatEndpoint.acquireTokenizer().countMessagesTokens(messages);
218218
const copilotToken = await this._authenticationService.getCopilotToken();
219219
usernameToScrub = copilotToken.username;
220+
220221
const fetchResult = await this._fetchAndStreamChat(
221222
chatEndpoint,
222223
requestBody,

extensions/copilot/src/extension/prompt/node/executionSubagentToolCallingLoop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export interface IExecutionSubagentToolCallingLoopOptions extends IToolCallingLo
3737
subAgentInvocationId?: string;
3838
/** The tool_call_id from the parent agent's LLM response that triggered this subagent invocation. */
3939
parentToolCallId?: string;
40+
/** The headerRequestId from the parent agent's fetch response that triggered this subagent invocation. */
41+
parentHeaderRequestId?: string;
4042
}
4143

4244
export class ExecutionSubagentToolCallingLoop extends ToolCallingLoop<IExecutionSubagentToolCallingLoopOptions> {
@@ -161,6 +163,7 @@ export class ExecutionSubagentToolCallingLoop extends ToolCallingLoop<IExecution
161163
subType: 'subagent/execution',
162164
conversationId: this.options.conversation.sessionId,
163165
parentToolCallId: this.options.parentToolCallId,
166+
parentHeaderRequestId: this.options.parentHeaderRequestId,
164167
},
165168
}, token);
166169
}

extensions/copilot/src/extension/prompt/node/searchSubagentToolCallingLoop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export interface ISearchSubagentToolCallingLoopOptions extends IToolCallingLoopO
3737
subAgentInvocationId?: string;
3838
/** The tool_call_id from the parent agent's LLM response that triggered this subagent invocation. */
3939
parentToolCallId?: string;
40+
/** The headerRequestId from the parent agent's fetch response that triggered this subagent invocation. */
41+
parentHeaderRequestId?: string;
4042
/** Thoroughness level for the search, passed through to the prompt when thoroughnessEnabled config is on. */
4143
thoroughness?: 'normal' | 'deep';
4244
}
@@ -164,6 +166,7 @@ export class SearchSubagentToolCallingLoop extends ToolCallingLoop<ISearchSubage
164166
subType: 'subagent/search',
165167
conversationId: this.options.conversation.sessionId,
166168
parentToolCallId: this.options.parentToolCallId,
169+
parentHeaderRequestId: this.options.parentHeaderRequestId,
167170
},
168171
requestKindOptions: { kind: 'subagent' }
169172
}, token);

extensions/copilot/src/extension/tools/node/executionSubagentTool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ExecutionSubagentTool implements ICopilotTool<IExecutionSubagentParams> {
6868
promptText: options.input.query,
6969
subAgentInvocationId: subAgentInvocationId,
7070
parentToolCallId: options.chatStreamToolCallId,
71+
parentHeaderRequestId: this._inputContext?.parentHeaderRequestId,
7172
});
7273

7374
const stream = this._inputContext?.stream && ChatResponseStreamImpl.filter(

extensions/copilot/src/extension/tools/node/searchSubagentTool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class SearchSubagentTool implements ICopilotTool<ISearchSubagentParams> {
124124
promptText: options.input.query,
125125
subAgentInvocationId: subAgentInvocationId,
126126
parentToolCallId: options.chatStreamToolCallId,
127+
parentHeaderRequestId: this._inputContext?.parentHeaderRequestId,
127128
thoroughness: thoroughnessEnabled ? options.input.thoroughness : undefined,
128129
});
129130

extensions/copilot/src/platform/endpoint/node/messagesApi.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,13 @@ export async function processResponseFromMessagesEndpoint(
654654
telemetryDataWithUsage = telemetryData.extendedBy({}, {
655655
promptTokens: completion.usage.prompt_tokens,
656656
completionTokens: completion.usage.completion_tokens,
657-
totalTokens: completion.usage.total_tokens
657+
totalTokens: completion.usage.total_tokens,
658+
...(completion.usage.prompt_tokens_details && { cachedTokens: completion.usage.prompt_tokens_details.cached_tokens }),
659+
...(completion.usage.completion_tokens_details && {
660+
reasoningTokens: completion.usage.completion_tokens_details.reasoning_tokens,
661+
acceptedPredictionTokens: completion.usage.completion_tokens_details.accepted_prediction_tokens,
662+
rejectedPredictionTokens: completion.usage.completion_tokens_details.rejected_prediction_tokens,
663+
}),
658664
});
659665
}
660666
sendEngineMessagesTelemetry(telemetryService, [telemetryMessage], telemetryDataWithUsage, true, logService);

extensions/copilot/src/platform/endpoint/node/responsesApi.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,12 @@ export function sendCompletionOutputTelemetry(telemetryService: ITelemetryServic
832832
promptTokens: completion.usage.prompt_tokens,
833833
completionTokens: completion.usage.completion_tokens,
834834
totalTokens: completion.usage.total_tokens,
835+
...(completion.usage.prompt_tokens_details && { cachedTokens: completion.usage.prompt_tokens_details.cached_tokens }),
836+
...(completion.usage.completion_tokens_details && {
837+
reasoningTokens: completion.usage.completion_tokens_details.reasoning_tokens,
838+
acceptedPredictionTokens: completion.usage.completion_tokens_details.accepted_prediction_tokens,
839+
rejectedPredictionTokens: completion.usage.completion_tokens_details.rejected_prediction_tokens,
840+
}),
835841
});
836842
}
837843
sendEngineMessagesTelemetry(telemetryService, [telemetryMessage], telemetryDataWithUsage, true, logService);

extensions/copilot/src/platform/networking/common/networking.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ export type IChatRequestTelemetryProperties = {
231231
parentRequestId?: string;
232232
/** For a subagent: The tool_call_id from the parent agent's LLM response that triggered this subagent invocation. */
233233
parentToolCallId?: string;
234+
/** For a subagent: The headerRequestId from the parent agent's fetch response that triggered this subagent invocation. */
235+
parentHeaderRequestId?: string;
234236
};
235237

236238
export interface ICreateEndpointBodyOptions extends IMakeChatRequestOptions {

0 commit comments

Comments
 (0)