| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Diagnostics.Metrics; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Default <see cref="IAgentMetrics"/> implementation using <see cref="Meter"/> |
| | | 8 | | /// for counters/histograms and <see cref="System.Diagnostics.ActivitySource"/> for |
| | | 9 | | /// distributed tracing spans. Compatible with OpenTelemetry — both metrics and traces |
| | | 10 | | /// are exported when listeners are registered. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Source names default to <c>"NexusLabs.Needlr.AgentFramework"</c> but can be |
| | | 14 | | /// overridden via <see cref="AgentFrameworkMetricsOptions.MeterName"/> and |
| | | 15 | | /// <see cref="AgentFrameworkMetricsOptions.ActivitySourceName"/> to match consumers' |
| | | 16 | | /// existing dashboard queries. |
| | | 17 | | /// </remarks> |
| | | 18 | | [DoNotAutoRegister] |
| | | 19 | | internal sealed class AgentMetrics : IAgentMetrics, IDisposable |
| | | 20 | | { |
| | | 21 | | private readonly Meter _meter; |
| | | 22 | | private readonly ActivitySource _activitySource; |
| | | 23 | | private readonly Counter<long> _runsStarted; |
| | | 24 | | private readonly Counter<long> _runsCompleted; |
| | | 25 | | private readonly Histogram<double> _runDuration; |
| | | 26 | | private readonly Counter<long> _tokensUsed; |
| | | 27 | | private readonly Counter<long> _toolCallsCompleted; |
| | | 28 | | private readonly Histogram<double> _toolCallDuration; |
| | | 29 | | private readonly Histogram<double> _chatCompletionDuration; |
| | | 30 | | |
| | 50 | 31 | | public AgentMetrics() : this(new AgentFrameworkMetricsOptions()) { } |
| | | 32 | | |
| | 87 | 33 | | public AgentMetrics(AgentFrameworkMetricsOptions options) |
| | | 34 | | { |
| | 87 | 35 | | ArgumentNullException.ThrowIfNull(options); |
| | | 36 | | |
| | 87 | 37 | | _meter = new Meter(options.MeterName); |
| | 87 | 38 | | _activitySource = new ActivitySource(options.ResolvedActivitySourceName); |
| | | 39 | | |
| | 87 | 40 | | _runsStarted = _meter.CreateCounter<long>( |
| | 87 | 41 | | "agent.run.started", |
| | 87 | 42 | | description: "Agent runs started"); |
| | | 43 | | |
| | 87 | 44 | | _runsCompleted = _meter.CreateCounter<long>( |
| | 87 | 45 | | "agent.run.completed", |
| | 87 | 46 | | description: "Agent runs completed"); |
| | | 47 | | |
| | 87 | 48 | | _runDuration = _meter.CreateHistogram<double>( |
| | 87 | 49 | | "agent.run.duration", |
| | 87 | 50 | | unit: "s", |
| | 87 | 51 | | description: "Agent run execution duration"); |
| | | 52 | | |
| | 87 | 53 | | _tokensUsed = _meter.CreateCounter<long>( |
| | 87 | 54 | | "agent.tokens.used", |
| | 87 | 55 | | description: "Tokens consumed by agent runs"); |
| | | 56 | | |
| | 87 | 57 | | _toolCallsCompleted = _meter.CreateCounter<long>( |
| | 87 | 58 | | "agent.tool.completed", |
| | 87 | 59 | | description: "Agent tool calls completed"); |
| | | 60 | | |
| | 87 | 61 | | _toolCallDuration = _meter.CreateHistogram<double>( |
| | 87 | 62 | | "agent.tool.duration", |
| | 87 | 63 | | unit: "s", |
| | 87 | 64 | | description: "Agent tool call execution duration"); |
| | | 65 | | |
| | 87 | 66 | | _chatCompletionDuration = _meter.CreateHistogram<double>( |
| | 87 | 67 | | "agent.chat.duration", |
| | 87 | 68 | | unit: "s", |
| | 87 | 69 | | description: "Agent chat completion duration"); |
| | 87 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | 138 | 73 | | public ActivitySource ActivitySource => _activitySource; |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public void RecordRunStarted(string agentName) => |
| | 60 | 77 | | _runsStarted.Add(1, new KeyValuePair<string, object?>("agent_name", agentName)); |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public void RecordRunCompleted(IAgentRunDiagnostics diagnostics) |
| | | 81 | | { |
| | 59 | 82 | | var status = diagnostics.Succeeded ? "success" : "failed"; |
| | 59 | 83 | | KeyValuePair<string, object?> agentTag = new("agent_name", diagnostics.AgentName); |
| | 59 | 84 | | KeyValuePair<string, object?> statusTag = new("status", status); |
| | | 85 | | |
| | 59 | 86 | | _runsCompleted.Add(1, agentTag, statusTag); |
| | 59 | 87 | | _runDuration.Record(diagnostics.TotalDuration.TotalSeconds, agentTag, statusTag); |
| | | 88 | | |
| | 59 | 89 | | if (diagnostics.AggregateTokenUsage.InputTokens > 0) |
| | | 90 | | { |
| | 29 | 91 | | _tokensUsed.Add( |
| | 29 | 92 | | diagnostics.AggregateTokenUsage.InputTokens, |
| | 29 | 93 | | agentTag, new KeyValuePair<string, object?>("direction", "input")); |
| | | 94 | | } |
| | | 95 | | |
| | 59 | 96 | | if (diagnostics.AggregateTokenUsage.OutputTokens > 0) |
| | | 97 | | { |
| | 29 | 98 | | _tokensUsed.Add( |
| | 29 | 99 | | diagnostics.AggregateTokenUsage.OutputTokens, |
| | 29 | 100 | | agentTag, new KeyValuePair<string, object?>("direction", "output")); |
| | | 101 | | } |
| | 59 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | | 105 | | public void RecordToolCall(string toolName, TimeSpan duration, bool succeeded, string? agentName = null) |
| | | 106 | | { |
| | 16 | 107 | | var status = succeeded ? "success" : "failed"; |
| | 16 | 108 | | KeyValuePair<string, object?> toolTag = new("tool_name", toolName); |
| | 16 | 109 | | KeyValuePair<string, object?> statusTag = new("status", status); |
| | | 110 | | |
| | 16 | 111 | | if (agentName is not null) |
| | | 112 | | { |
| | 12 | 113 | | KeyValuePair<string, object?> agentTag = new("agent_name", agentName); |
| | 12 | 114 | | _toolCallsCompleted.Add(1, toolTag, statusTag, agentTag); |
| | 12 | 115 | | _toolCallDuration.Record(duration.TotalSeconds, toolTag, statusTag, agentTag); |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | 4 | 119 | | _toolCallsCompleted.Add(1, toolTag, statusTag); |
| | 4 | 120 | | _toolCallDuration.Record(duration.TotalSeconds, toolTag, statusTag); |
| | | 121 | | } |
| | 4 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <inheritdoc /> |
| | | 125 | | public void RecordChatCompletion(string model, TimeSpan duration, bool succeeded, string? agentName = null) |
| | | 126 | | { |
| | 78 | 127 | | var status = succeeded ? "success" : "failed"; |
| | 78 | 128 | | KeyValuePair<string, object?> modelTag = new("model", model); |
| | 78 | 129 | | KeyValuePair<string, object?> statusTag = new("status", status); |
| | | 130 | | |
| | 78 | 131 | | if (agentName is not null) |
| | | 132 | | { |
| | 61 | 133 | | KeyValuePair<string, object?> agentTag = new("agent_name", agentName); |
| | 61 | 134 | | _chatCompletionDuration.Record(duration.TotalSeconds, modelTag, statusTag, agentTag); |
| | | 135 | | } |
| | | 136 | | else |
| | | 137 | | { |
| | 17 | 138 | | _chatCompletionDuration.Record(duration.TotalSeconds, modelTag, statusTag); |
| | | 139 | | } |
| | 17 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary>Disposes the underlying <see cref="Meter"/> and <see cref="System.Diagnostics.ActivitySource"/>.</summa |
| | | 143 | | public void Dispose() |
| | | 144 | | { |
| | 21 | 145 | | _meter.Dispose(); |
| | 21 | 146 | | _activitySource.Dispose(); |
| | 21 | 147 | | } |
| | | 148 | | } |