| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Diagnostics.Metrics; |
| | | 3 | | using Intentum.Core.Behavior; |
| | | 4 | | using Intentum.Core.Intents; |
| | | 5 | | using Intentum.Runtime.Policy; |
| | | 6 | | |
| | | 7 | | namespace Intentum.Observability; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// OpenTelemetry metrics for Intentum operations. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class IntentumMetrics |
| | | 13 | | { |
| | 1 | 14 | | private static readonly Meter Meter = new("Intentum", "1.0.0"); |
| | | 15 | | |
| | 1 | 16 | | private static readonly Counter<long> IntentInferenceCount = Meter.CreateCounter<long>( |
| | 1 | 17 | | "intentum.intent.inference.count", |
| | 1 | 18 | | description: "Number of intent inferences performed"); |
| | | 19 | | |
| | 1 | 20 | | private static readonly Histogram<double> IntentInferenceDuration = Meter.CreateHistogram<double>( |
| | 1 | 21 | | "intentum.intent.inference.duration", |
| | 1 | 22 | | unit: "ms", |
| | 1 | 23 | | description: "Duration of intent inference operations"); |
| | | 24 | | |
| | 1 | 25 | | private static readonly Histogram<double> IntentConfidenceScore = Meter.CreateHistogram<double>( |
| | 1 | 26 | | "intentum.intent.confidence.score", |
| | 1 | 27 | | description: "Confidence score of inferred intents"); |
| | | 28 | | |
| | 1 | 29 | | private static readonly Counter<long> PolicyDecisionCount = Meter.CreateCounter<long>( |
| | 1 | 30 | | "intentum.policy.decision.count", |
| | 1 | 31 | | description: "Number of policy decisions made"); |
| | | 32 | | |
| | 1 | 33 | | private static readonly Histogram<int> BehaviorSpaceSize = Meter.CreateHistogram<int>( |
| | 1 | 34 | | "intentum.behavior.space.size", |
| | 1 | 35 | | description: "Size of behavior spaces"); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Records an intent inference operation. |
| | | 39 | | /// </summary> |
| | | 40 | | public static void RecordIntentInference(Intent intent, TimeSpan duration) |
| | 7 | 41 | | { |
| | 7 | 42 | | var tags = new TagList |
| | 7 | 43 | | { |
| | 7 | 44 | | { "confidence.level", intent.Confidence.Level }, |
| | 7 | 45 | | { "signal.count", intent.Signals.Count } |
| | 7 | 46 | | }; |
| | | 47 | | |
| | 7 | 48 | | IntentInferenceCount.Add(1, tags); |
| | 7 | 49 | | IntentInferenceDuration.Record(duration.TotalMilliseconds, tags); |
| | 7 | 50 | | IntentConfidenceScore.Record(intent.Confidence.Score, tags); |
| | 7 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Records a policy decision. |
| | | 55 | | /// </summary> |
| | | 56 | | public static void RecordPolicyDecision(PolicyDecision decision) |
| | 11 | 57 | | { |
| | 11 | 58 | | var tags = new TagList |
| | 11 | 59 | | { |
| | 11 | 60 | | { "decision", decision.ToString() } |
| | 11 | 61 | | }; |
| | | 62 | | |
| | 11 | 63 | | PolicyDecisionCount.Add(1, tags); |
| | 11 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Records behavior space size. |
| | | 68 | | /// </summary> |
| | | 69 | | public static void RecordBehaviorSpaceSize(BehaviorSpace space) |
| | 7 | 70 | | { |
| | 7 | 71 | | var tags = new TagList |
| | 7 | 72 | | { |
| | 7 | 73 | | { "event.count", space.Events.Count } |
| | 7 | 74 | | }; |
| | | 75 | | |
| | 7 | 76 | | BehaviorSpaceSize.Record(space.Events.Count, tags); |
| | 7 | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | private static readonly Counter<long> EmbeddingCallCount = Meter.CreateCounter<long>( |
| | 1 | 80 | | "intentum.embedding.call.count", |
| | 1 | 81 | | description: "Number of embedding API calls"); |
| | | 82 | | |
| | 1 | 83 | | private static readonly Histogram<double> EmbeddingCallDuration = Meter.CreateHistogram<double>( |
| | 1 | 84 | | "intentum.embedding.call.duration", |
| | 1 | 85 | | unit: "ms", |
| | 1 | 86 | | description: "Duration of embedding API calls"); |
| | | 87 | | |
| | 1 | 88 | | private static readonly Counter<long> EmbeddingCacheHitCount = Meter.CreateCounter<long>( |
| | 1 | 89 | | "intentum.embedding.cache.hit", |
| | 1 | 90 | | description: "Number of embedding cache hits"); |
| | | 91 | | |
| | 1 | 92 | | private static readonly Counter<long> EmbeddingCacheMissCount = Meter.CreateCounter<long>( |
| | 1 | 93 | | "intentum.embedding.cache.miss", |
| | 1 | 94 | | description: "Number of embedding cache misses"); |
| | | 95 | | |
| | 1 | 96 | | private static readonly Counter<long> LlmClassificationCount = Meter.CreateCounter<long>( |
| | 1 | 97 | | "intentum.llm.classification.count", |
| | 1 | 98 | | description: "Number of LLM classification calls"); |
| | | 99 | | |
| | 1 | 100 | | private static readonly Histogram<double> LlmClassificationDuration = Meter.CreateHistogram<double>( |
| | 1 | 101 | | "intentum.llm.classification.duration", |
| | 1 | 102 | | unit: "ms", |
| | 1 | 103 | | description: "Duration of LLM classification calls"); |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Records an embedding API call. |
| | | 107 | | /// </summary> |
| | | 108 | | public static void RecordEmbeddingCall(string provider, TimeSpan duration, bool success) |
| | 0 | 109 | | { |
| | 0 | 110 | | var tags = new TagList { { "provider", provider }, { "success", success } }; |
| | 0 | 111 | | EmbeddingCallCount.Add(1, tags); |
| | 0 | 112 | | EmbeddingCallDuration.Record(duration.TotalMilliseconds, tags); |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Records an embedding cache hit or miss. |
| | | 117 | | /// </summary> |
| | | 118 | | public static void RecordEmbeddingCacheAccess(bool hit) |
| | 0 | 119 | | { |
| | 0 | 120 | | if (hit) EmbeddingCacheHitCount.Add(1); |
| | 0 | 121 | | else EmbeddingCacheMissCount.Add(1); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Records an LLM classification call. |
| | | 126 | | /// </summary> |
| | | 127 | | public static void RecordLlmClassification(string model, TimeSpan duration, bool success) |
| | 0 | 128 | | { |
| | 0 | 129 | | var tags = new TagList { { "model", model }, { "success", success } }; |
| | 0 | 130 | | LlmClassificationCount.Add(1, tags); |
| | 0 | 131 | | LlmClassificationDuration.Record(duration.TotalMilliseconds, tags); |
| | 0 | 132 | | } |
| | | 133 | | } |