| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Configuration options for the agent framework's OpenTelemetry metrics and tracing. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// <para> |
| | | 8 | | /// Consumers that have existing dashboards keyed to a specific meter name (e.g., |
| | | 9 | | /// <c>"BrandGhost.Agents"</c>) can set <see cref="MeterName"/> to match, avoiding a |
| | | 10 | | /// dashboard migration when adopting Needlr's <see cref="IAgentMetrics"/>. |
| | | 11 | | /// </para> |
| | | 12 | | /// <para> |
| | | 13 | | /// Configure via the syringe: |
| | | 14 | | /// <code> |
| | | 15 | | /// .UsingAgentFramework(af => af |
| | | 16 | | /// .ConfigureMetrics(o => o.MeterName = "MyApp.Agents")) |
| | | 17 | | /// </code> |
| | | 18 | | /// </para> |
| | | 19 | | /// </remarks> |
| | | 20 | | [DoNotAutoRegister] |
| | | 21 | | public sealed class AgentFrameworkMetricsOptions |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// The name used for the <see cref="System.Diagnostics.Metrics.Meter"/> that emits |
| | | 25 | | /// counters and histograms. Defaults to <c>"NexusLabs.Needlr.AgentFramework"</c>. |
| | | 26 | | /// </summary> |
| | 502 | 27 | | public string MeterName { get; set; } = "NexusLabs.Needlr.AgentFramework"; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The name used for the <see cref="System.Diagnostics.ActivitySource"/> that emits |
| | | 31 | | /// distributed tracing spans. Defaults to <see cref="MeterName"/> when |
| | | 32 | | /// <see langword="null"/>. |
| | | 33 | | /// </summary> |
| | 114 | 34 | | public string? ActivitySourceName { get; set; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The name used for the <see cref="System.Diagnostics.Metrics.Meter"/> that owns |
| | | 38 | | /// the <c>gen_ai.client.token.usage</c> histogram on which Needlr emits |
| | | 39 | | /// <c>cache_read</c> and <c>reasoning</c> measurements via |
| | | 40 | | /// <see cref="IGenAiTokenMetrics"/>. Defaults to <c>"Experimental.Microsoft.Extensions.AI"</c>, |
| | | 41 | | /// matching MEAI 10.5.0's <c>OpenTelemetryConsts.DefaultSourceName</c>. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <remarks> |
| | | 44 | | /// <para> |
| | | 45 | | /// MEAI's <see cref="Microsoft.Extensions.AI.OpenTelemetryChatClient"/> emits the |
| | | 46 | | /// same <c>gen_ai.client.token.usage</c> histogram for <c>input</c> and <c>output</c> |
| | | 47 | | /// token types under a meter named by its <c>sourceName</c> constructor parameter |
| | | 48 | | /// (default <c>"Experimental.Microsoft.Extensions.AI"</c>). For the OpenTelemetry SDK |
| | | 49 | | /// to aggregate Needlr's <c>cache_read</c> / <c>reasoning</c> measurements into the |
| | | 50 | | /// same metric stream as MEAI's <c>input</c> / <c>output</c> measurements, the meter |
| | | 51 | | /// names MUST match. If the host configures MEAI with a custom <c>sourceName</c> |
| | | 52 | | /// (e.g. <c>UseOpenTelemetry(sourceName: "MyApp.GenAI")</c>), set this property to |
| | | 53 | | /// the same value: |
| | | 54 | | /// <code> |
| | | 55 | | /// .UsingAgentFramework(af => af |
| | | 56 | | /// .ConfigureMetrics(o => o.GenAiMeterName = "MyApp.GenAI")) |
| | | 57 | | /// </code> |
| | | 58 | | /// </para> |
| | | 59 | | /// <para> |
| | | 60 | | /// This property is independent of <see cref="MeterName"/> — that property scopes |
| | | 61 | | /// Needlr-shape agent metrics (<c>agent.run.*</c>, <c>agent.tool.*</c>, etc.) which |
| | | 62 | | /// have no upstream cohabitation requirement, while this property scopes the OTel |
| | | 63 | | /// gen_ai semantic-convention histogram that Needlr shares with MEAI. |
| | | 64 | | /// </para> |
| | | 65 | | /// </remarks> |
| | 389 | 66 | | public string GenAiMeterName { get; set; } = "Experimental.Microsoft.Extensions.AI"; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Controls how Needlr's diagnostics middleware creates |
| | | 70 | | /// <see cref="System.Diagnostics.Activity"/> spans for chat completion calls. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <remarks> |
| | | 73 | | /// <para> |
| | | 74 | | /// When MEAI's <c>UseOpenTelemetry()</c> or MAF's <c>WithOpenTelemetry()</c> is |
| | | 75 | | /// also active, both Needlr and the upstream middleware create spans for the same |
| | | 76 | | /// chat completion call. Set this to |
| | | 77 | | /// <see cref="ChatCompletionActivityMode.EnrichParent"/> to avoid duplicate spans — |
| | | 78 | | /// Needlr will add its tags (sequence number, char counts, agent name) to the |
| | | 79 | | /// existing parent <c>gen_ai.*</c> activity instead of creating a new one. |
| | | 80 | | /// </para> |
| | | 81 | | /// <para> |
| | | 82 | | /// Tool call activities (<c>agent.tool</c>) are not affected by this setting — |
| | | 83 | | /// they are always created because neither MEAI nor MAF produces per-tool-call spans. |
| | | 84 | | /// </para> |
| | | 85 | | /// </remarks> |
| | 40 | 86 | | public ChatCompletionActivityMode ChatCompletionActivityMode { get; set; } = |
| | | 87 | | ChatCompletionActivityMode.Always; |
| | | 88 | | |
| | 101 | 89 | | internal string ResolvedActivitySourceName => ActivitySourceName ?? MeterName; |
| | | 90 | | } |