| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Diagnostics.Metrics; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Default <see cref="IGenAiTokenMetrics"/> implementation. Owns a dedicated |
| | | 8 | | /// <see cref="Meter"/> whose name is configurable via |
| | | 9 | | /// <see cref="AgentFrameworkMetricsOptions.GenAiMeterName"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// The histogram is created with the same name (<c>gen_ai.client.token.usage</c>), |
| | | 14 | | /// type (<see cref="Histogram{T}"/> of <see cref="int"/>), unit (<c>{token}</c>), |
| | | 15 | | /// description, and explicit bucket boundaries that MEAI's |
| | | 16 | | /// <see cref="Microsoft.Extensions.AI.OpenTelemetryChatClient"/> uses (per |
| | | 17 | | /// <c>OpenTelemetryConsts.GenAI.Client.TokenUsage</c> in |
| | | 18 | | /// <see href="https://github.com/dotnet/extensions/blob/v10.5.0/src/Libraries/Microsoft.Extensions.AI/OpenTelemetryCons |
| | | 19 | | /// This shape parity is required for the OpenTelemetry SDK's <c>MetricStreamIdentity</c> |
| | | 20 | | /// to consider Needlr's measurements and MEAI's measurements part of the same metric |
| | | 21 | | /// stream rather than colliding into duplicate-instrument warnings. |
| | | 22 | | /// </para> |
| | | 23 | | /// <para> |
| | | 24 | | /// Token counts are passed as <see cref="long"/> at the API surface (matching the type |
| | | 25 | | /// of <see cref="Microsoft.Extensions.AI.UsageDetails.InputTokenCount"/> et al.) but |
| | | 26 | | /// recorded as <see cref="int"/> after a saturating clamp at <see cref="int.MaxValue"/>. |
| | | 27 | | /// At single-call granularity values exceeding 2.1 billion tokens are not realistic. |
| | | 28 | | /// </para> |
| | | 29 | | /// </remarks> |
| | | 30 | | [DoNotAutoRegister] |
| | | 31 | | internal sealed class GenAiTokenMetrics : IGenAiTokenMetrics, IDisposable |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// The exact instrument name MEAI's |
| | | 35 | | /// <see cref="Microsoft.Extensions.AI.OpenTelemetryChatClient"/> uses for the token |
| | | 36 | | /// usage histogram. Sourced verbatim from MEAI's <c>OpenTelemetryConsts.GenAI.Client.TokenUsage.Name</c>. |
| | | 37 | | /// </summary> |
| | | 38 | | internal const string InstrumentName = "gen_ai.client.token.usage"; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The exact instrument unit MEAI uses. Sourced verbatim from MEAI's |
| | | 42 | | /// <c>OpenTelemetryConsts.TokensUnit</c>. |
| | | 43 | | /// </summary> |
| | | 44 | | internal const string InstrumentUnit = "{token}"; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The exact instrument description MEAI uses. Sourced verbatim from MEAI's |
| | | 48 | | /// <c>OpenTelemetryConsts.GenAI.Client.TokenUsage.Description</c>. |
| | | 49 | | /// </summary> |
| | | 50 | | internal const string InstrumentDescription = "Measures number of input and output tokens used"; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The exact explicit bucket boundaries MEAI uses. Sourced verbatim from MEAI's |
| | | 54 | | /// <c>OpenTelemetryConsts.GenAI.Client.TokenUsage.ExplicitBucketBoundaries</c>. |
| | | 55 | | /// </summary> |
| | 1 | 56 | | internal static readonly int[] InstrumentBucketBoundaries = |
| | 1 | 57 | | [ |
| | 1 | 58 | | 1, 4, 16, 64, 256, 1_024, 4_096, 16_384, |
| | 1 | 59 | | 65_536, 262_144, 1_048_576, 4_194_304, 16_777_216, 67_108_864, |
| | 1 | 60 | | ]; |
| | | 61 | | |
| | | 62 | | private readonly Meter _meter; |
| | | 63 | | private readonly Histogram<int> _tokenUsage; |
| | | 64 | | |
| | 4 | 65 | | public GenAiTokenMetrics() : this(new AgentFrameworkMetricsOptions()) { } |
| | | 66 | | |
| | 89 | 67 | | public GenAiTokenMetrics(AgentFrameworkMetricsOptions options) |
| | | 68 | | { |
| | 89 | 69 | | ArgumentNullException.ThrowIfNull(options); |
| | | 70 | | |
| | 89 | 71 | | _meter = new Meter(options.GenAiMeterName); |
| | 89 | 72 | | _tokenUsage = _meter.CreateHistogram<int>( |
| | 89 | 73 | | name: InstrumentName, |
| | 89 | 74 | | unit: InstrumentUnit, |
| | 89 | 75 | | description: InstrumentDescription, |
| | 89 | 76 | | advice: new InstrumentAdvice<int> { HistogramBucketBoundaries = InstrumentBucketBoundaries }); |
| | 89 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public void RecordTokenUsage(string tokenType, long tokenCount, GenAiTokenUsageTags tags) |
| | | 81 | | { |
| | 38 | 82 | | ArgumentException.ThrowIfNullOrEmpty(tokenType); |
| | | 83 | | |
| | 36 | 84 | | if (tokenCount <= 0) |
| | 2 | 85 | | return; |
| | | 86 | | |
| | 34 | 87 | | var sample = (int)Math.Min(tokenCount, int.MaxValue); |
| | | 88 | | |
| | 34 | 89 | | var tagList = new TagList |
| | 34 | 90 | | { |
| | 34 | 91 | | { "gen_ai.token.type", tokenType }, |
| | 34 | 92 | | { "gen_ai.operation.name", string.IsNullOrEmpty(tags.OperationName) ? "chat" : tags.OperationName }, |
| | 34 | 93 | | }; |
| | | 94 | | |
| | 34 | 95 | | if (tags.RequestModel is not null) |
| | 4 | 96 | | tagList.Add("gen_ai.request.model", tags.RequestModel); |
| | | 97 | | |
| | | 98 | | // gen_ai.provider.name is added UNCONDITIONALLY (even when null) to match MEAI's |
| | | 99 | | // OpenTelemetryChatClient.AddMetricTags exactly. Label-set parity is required for |
| | | 100 | | // the OpenTelemetry SDK MetricStreamIdentity to consider Needlr's measurements and |
| | | 101 | | // MEAI's measurements part of the same stream rather than splitting into two. |
| | 34 | 102 | | tagList.Add("gen_ai.provider.name", tags.ProviderName); |
| | | 103 | | |
| | 34 | 104 | | if (tags.ServerAddress is not null) |
| | | 105 | | { |
| | 6 | 106 | | tagList.Add("server.address", tags.ServerAddress); |
| | 6 | 107 | | if (tags.ServerPort is int port) |
| | 5 | 108 | | tagList.Add("server.port", port); |
| | | 109 | | } |
| | | 110 | | |
| | 34 | 111 | | if (tags.ResponseModel is not null) |
| | 19 | 112 | | tagList.Add("gen_ai.response.model", tags.ResponseModel); |
| | | 113 | | |
| | 34 | 114 | | _tokenUsage.Record(sample, tagList); |
| | 34 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary>Disposes the underlying <see cref="Meter"/>.</summary> |
| | 41 | 118 | | public void Dispose() => _meter.Dispose(); |
| | | 119 | | } |