| | | 1 | | using System.Diagnostics; |
| | | 2 | | using Intentum.Core.Intents; |
| | | 3 | | using Intentum.Runtime.Engine; |
| | | 4 | | using Intentum.Runtime.Policy; |
| | | 5 | | |
| | | 6 | | namespace Intentum.Observability; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Extension methods for adding observability to policy decisions (metrics, OpenTelemetry spans, execution log). |
| | | 10 | | /// </summary> |
| | | 11 | | public static class ObservablePolicyEngine |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Decides on an intent with policy, records metrics, and creates a policy.evaluate span. |
| | | 15 | | /// </summary> |
| | | 16 | | public static PolicyDecision DecideWithMetrics( |
| | | 17 | | this Intent intent, |
| | | 18 | | IntentPolicy policy) |
| | 2 | 19 | | { |
| | 2 | 20 | | var (decision, _) = DecideWithExecutionLog(intent, policy); |
| | 2 | 21 | | return decision; |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Decides on an intent with policy, records metrics and span, and returns an execution record for logging (matched |
| | | 26 | | /// </summary> |
| | | 27 | | public static (PolicyDecision Decision, PolicyExecutionRecord Record) DecideWithExecutionLog( |
| | | 28 | | this Intent intent, |
| | | 29 | | IntentPolicy policy) |
| | 13 | 30 | | { |
| | 13 | 31 | | var sw = Stopwatch.StartNew(); |
| | | 32 | | PolicyDecision decision; |
| | 13 | 33 | | PolicyRule? matchedRule = null; |
| | 13 | 34 | | var success = true; |
| | 13 | 35 | | string? exceptionMessage = null; |
| | 13 | 36 | | using var activity = IntentumActivitySource.Source.StartActivity(); |
| | | 37 | | |
| | | 38 | | try |
| | 13 | 39 | | { |
| | 13 | 40 | | (decision, matchedRule) = IntentPolicyEngine.EvaluateWithRule(intent, policy); |
| | | 41 | | |
| | 11 | 42 | | if (activity != null) |
| | 7 | 43 | | { |
| | 7 | 44 | | activity.DisplayName = IntentumActivitySource.PolicyEvaluateSpanName; |
| | 7 | 45 | | activity.SetTag("intentum.policy.decision", decision.ToString()); |
| | 7 | 46 | | activity.SetTag("intentum.intent.name", intent.Name); |
| | 7 | 47 | | activity.SetTag("intentum.intent.confidence.level", intent.Confidence.Level); |
| | 7 | 48 | | if (matchedRule != null) |
| | 6 | 49 | | activity.SetTag("intentum.policy.matched_rule", matchedRule.Name); |
| | 7 | 50 | | } |
| | | 51 | | |
| | 11 | 52 | | IntentumMetrics.RecordPolicyDecision(decision); |
| | 11 | 53 | | } |
| | 2 | 54 | | catch (Exception ex) |
| | 2 | 55 | | { |
| | 2 | 56 | | activity?.SetStatus(ActivityStatusCode.Error, ex.Message); |
| | 2 | 57 | | success = false; |
| | 2 | 58 | | exceptionMessage = ex.Message; |
| | 2 | 59 | | decision = PolicyDecision.Observe; |
| | 2 | 60 | | sw.Stop(); |
| | 2 | 61 | | var record = new PolicyExecutionRecord( |
| | 2 | 62 | | intent.Name, |
| | 2 | 63 | | matchedRule?.Name, |
| | 2 | 64 | | decision, |
| | 2 | 65 | | sw.Elapsed.TotalMilliseconds, |
| | 2 | 66 | | success, |
| | 2 | 67 | | exceptionMessage, |
| | 2 | 68 | | ExceptionTrace: ex.StackTrace); |
| | 2 | 69 | | return (decision, record); |
| | | 70 | | } |
| | | 71 | | |
| | 11 | 72 | | sw.Stop(); |
| | 11 | 73 | | var successRecord = new PolicyExecutionRecord( |
| | 11 | 74 | | intent.Name, |
| | 11 | 75 | | matchedRule?.Name, |
| | 11 | 76 | | decision, |
| | 11 | 77 | | sw.Elapsed.TotalMilliseconds, |
| | 11 | 78 | | success, |
| | 11 | 79 | | exceptionMessage); |
| | | 80 | | |
| | 11 | 81 | | return (decision, successRecord); |
| | 13 | 82 | | } |
| | | 83 | | } |