Skip to content

Commit 5fec3be

Browse files
authored
.Net: Add Kernel Function Arguments and Result to execute tool span (#13241)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> SK .Net is missing two attributes to the `execute_tool` span: `gen_ai.tool.call.arguments` and `gen_ai.tool.call.result`. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Add those two attributes to the span if sensitive data is turned on. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent caa6e11 commit 5fec3be

5 files changed

Lines changed: 145 additions & 32 deletions

File tree

‎dotnet/src/InternalUtilities/src/Diagnostics/ActivityExtensions.cs‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ namespace Microsoft.SemanticKernel.Diagnostics;
1313
[ExcludeFromCodeCoverage]
1414
internal static class ActivityExtensions
1515
{
16-
/// <summary>
17-
/// Starts an activity with the appropriate tags for a kernel function execution.
18-
/// </summary>
19-
public static Activity? StartFunctionActivity(this ActivitySource source, string functionName, string functionDescription)
20-
{
21-
const string OperationName = "execute_tool";
22-
23-
return source.StartActivityWithTags($"{OperationName} {functionName}", [
24-
new KeyValuePair<string, object?>("gen_ai.operation.name", OperationName),
25-
new KeyValuePair<string, object?>("gen_ai.tool.name", functionName),
26-
new KeyValuePair<string, object?>("gen_ai.tool.description", functionDescription)
27-
], ActivityKind.Internal);
28-
}
29-
3016
/// <summary>
3117
/// Starts an activity with the specified name and tags.
3218
/// </summary>
@@ -42,7 +28,6 @@ public static Activity SetTags(this Activity activity, ReadOnlySpan<KeyValuePair
4228
{
4329
activity.SetTag(tag.Key, tag.Value);
4430
}
45-
;
4631

4732
return activity;
4833
}

‎dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Linq;
78
using System.Text.Json;
89
using Microsoft.SemanticKernel.ChatCompletion;
@@ -38,6 +39,8 @@ internal static class ModelDiagnostics
3839
/// Start a text completion activity for a given model.
3940
/// The activity will be tagged with the a set of attributes specified by the semantic conventions.
4041
/// </summary>
42+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
43+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
4144
internal static Activity? StartCompletionActivity<TPromptExecutionSettings>(
4245
Uri? endpoint,
4346
string modelName,
@@ -88,6 +91,8 @@ internal static class ModelDiagnostics
8891
/// Start a chat completion activity for a given model.
8992
/// The activity will be tagged with the a set of attributes specified by the semantic conventions.
9093
/// </summary>
94+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
95+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
9196
internal static Activity? StartCompletionActivity<TPromptExecutionSettings>(
9297
Uri? endpoint,
9398
string modelName,
@@ -141,6 +146,8 @@ internal static class ModelDiagnostics
141146
/// <summary>
142147
/// Start an agent invocation activity and return the activity.
143148
/// </summary>
149+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
150+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
144151
internal static Activity? StartAgentInvocationActivity(
145152
string agentId,
146153
string agentName,
@@ -189,6 +196,8 @@ internal static class ModelDiagnostics
189196
/// <summary>
190197
/// Set the agent response for a given activity.
191198
/// </summary>
199+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
200+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
192201
internal static void SetAgentResponse(this Activity activity, IEnumerable<ChatMessageContent>? responses)
193202
{
194203
if (!IsModelDiagnosticsEnabled() || responses is null)
@@ -207,6 +216,8 @@ internal static void SetAgentResponse(this Activity activity, IEnumerable<ChatMe
207216
/// <summary>
208217
/// End the agent streaming response for a given activity.
209218
/// </summary>
219+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
220+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
210221
internal static void EndAgentStreamingResponse(
211222
this Activity activity,
212223
IEnumerable<StreamingChatMessageContent>? contents)
@@ -244,19 +255,25 @@ internal static void EndAgentStreamingResponse(
244255
/// Set the text completion response for a given activity.
245256
/// The activity will be enriched with the response attributes specified by the semantic conventions.
246257
/// </summary>
258+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
259+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
247260
internal static void SetCompletionResponse(this Activity activity, IEnumerable<TextContent> completions, int? promptTokens = null, int? completionTokens = null)
248261
=> SetCompletionResponse(activity, completions, promptTokens, completionTokens, ToGenAIConventionsChoiceFormat);
249262

250263
/// <summary>
251264
/// Set the chat completion response for a given activity.
252265
/// The activity will be enriched with the response attributes specified by the semantic conventions.
253266
/// </summary>
267+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
268+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
254269
internal static void SetCompletionResponse(this Activity activity, IEnumerable<ChatMessageContent> completions, int? promptTokens = null, int? completionTokens = null)
255270
=> SetCompletionResponse(activity, completions, promptTokens, completionTokens, ToGenAIConventionsChoiceFormat);
256271

257272
/// <summary>
258273
/// Notify the end of streaming for a given activity.
259274
/// </summary>
275+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
276+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
260277
internal static void EndStreaming(
261278
this Activity activity,
262279
IEnumerable<StreamingKernelContent>? contents,
@@ -313,6 +330,8 @@ internal static bool IsModelDiagnosticsEnabled()
313330
internal static bool HasListeners() => s_activitySource.HasListeners();
314331

315332
#region Private
333+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
334+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
316335
private static void AddOptionalTags<TPromptExecutionSettings>(Activity? activity, TPromptExecutionSettings? executionSettings)
317336
where TPromptExecutionSettings : PromptExecutionSettings
318337
{
@@ -409,6 +428,8 @@ private static object ToGenAIConventionsFormat(KernelFunctionMetadata metadata)
409428
/// <summary>
410429
/// Convert a chat model response to a JSON string based on the OTel GenAI Semantic Conventions format
411430
/// </summary>
431+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
432+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
412433
private static string ToGenAIConventionsChoiceFormat(ChatMessageContent chatMessage, int index)
413434
{
414435
var jsonObject = new
@@ -425,6 +446,8 @@ private static string ToGenAIConventionsChoiceFormat(ChatMessageContent chatMess
425446
/// <summary>
426447
/// Convert a text model response to a JSON string based on the OTel GenAI Semantic Conventions format
427448
/// </summary>
449+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
450+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
428451
private static string ToGenAIConventionsChoiceFormat(TextContent textContent, int index)
429452
{
430453
var jsonObject = new
@@ -494,6 +517,8 @@ private static void SetCompletionResponse<T>(
494517
/// <summary>
495518
/// Set the streaming completion response for a given activity.
496519
/// </summary>
520+
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
521+
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
497522
private static void SetCompletionResponse(
498523
Activity activity,
499524
Dictionary<int, List<StreamingKernelContent>> choices,

‎dotnet/src/Plugins/Plugins.StructuredData.EntityFramework/Plugins.StructuredData.EntityFramework.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Schema/*.cs" Link="%(RecursiveDir)/InternalUtilities/Schema/%(Filename)%(Extension)" />
2626
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*.cs" Link="%(RecursiveDir)/InternalUtilities/Diagnostics/%(Filename)%(Extension)" />
2727
</ItemGroup>
28-
28+
2929
<ItemGroup>
3030
<Compile Remove="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs" Link="%(RecursiveDir)/InternalUtilities/Diagnostics/%(Filename)%(Extension)" />
3131
</ItemGroup>

0 commit comments

Comments
 (0)