-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAgentFrameworkSpanProcessor.cs
More file actions
82 lines (72 loc) · 3 KB
/
Copy pathAgentFrameworkSpanProcessor.cs
File metadata and controls
82 lines (72 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Agents.A365.Observability.Extensions.AgentFramework.Utils;
using Microsoft.Agents.A365.Observability.Runtime.Tracing.Scopes;
using OpenTelemetry;
using System.Diagnostics;
namespace Microsoft.Agents.A365.Observability.Extensions.AgentFramework
{
internal class AgentFrameworkSpanProcessor : BaseProcessor<Activity>
{
private const string InvokeAgentOperation = "invoke_agent";
private const string ChatOperation = "chat";
private const string ExecuteToolOperation = "execute_tool";
private const string ToolCallResultTag = "gen_ai.tool.call.result";
private const string EventContentTag = "gen_ai.event.content";
private readonly string[] _additionalSources;
public AgentFrameworkSpanProcessor(params string[] additionalSources)
{
_additionalSources = additionalSources ?? [];
}
public override void OnStart(Activity activity)
{
}
public override void OnEnd(Activity activity)
{
if (activity == null)
return;
if (IsTrackedSource(activity.Source.Name))
{
var operationName = activity.GetTagItem(OpenTelemetryConstants.GenAiOperationNameKey);
if (operationName is string opName)
{
switch (opName)
{
case InvokeAgentOperation:
case ChatOperation:
var inputMessages = AgentFrameworkMessageMapper.MapInputMessages(activity);
if (inputMessages != null)
{
activity.SetTag(OpenTelemetryConstants.GenAiInputMessagesKey, inputMessages);
}
var outputMessages = AgentFrameworkMessageMapper.MapOutputMessages(activity);
if (outputMessages != null)
{
activity.SetTag(OpenTelemetryConstants.GenAiOutputMessagesKey, outputMessages);
}
break;
case ExecuteToolOperation:
var toolCallResult = activity.GetTagItem(ToolCallResultTag);
activity.SetTag(EventContentTag, toolCallResult);
break;
}
}
}
}
private bool IsTrackedSource(string sourceName)
{
if (sourceName.StartsWith(BuilderExtensions.AgentFrameworkSource))
{
return true;
}
foreach (var source in _additionalSources)
{
if (!string.IsNullOrWhiteSpace(source) && sourceName.StartsWith(source))
{
return true;
}
}
return false;
}
}
}