< Summary

Information
Class: Intentum.Observability.ObservablePolicyEngine
Assembly: Intentum.Observability
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Observability/ObservablePolicyEngine.cs
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 83
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
DecideWithMetrics(...)100%11100%
DecideWithExecutionLog(...)90%1010100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Observability/ObservablePolicyEngine.cs

#LineLine coverage
 1using System.Diagnostics;
 2using Intentum.Core.Intents;
 3using Intentum.Runtime.Engine;
 4using Intentum.Runtime.Policy;
 5
 6namespace Intentum.Observability;
 7
 8/// <summary>
 9/// Extension methods for adding observability to policy decisions (metrics, OpenTelemetry spans, execution log).
 10/// </summary>
 11public 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)
 219    {
 220        var (decision, _) = DecideWithExecutionLog(intent, policy);
 221        return decision;
 222    }
 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)
 1330    {
 1331        var sw = Stopwatch.StartNew();
 32        PolicyDecision decision;
 1333        PolicyRule? matchedRule = null;
 1334        var success = true;
 1335        string? exceptionMessage = null;
 1336        using var activity = IntentumActivitySource.Source.StartActivity();
 37
 38        try
 1339        {
 1340            (decision, matchedRule) = IntentPolicyEngine.EvaluateWithRule(intent, policy);
 41
 1142            if (activity != null)
 743            {
 744                activity.DisplayName = IntentumActivitySource.PolicyEvaluateSpanName;
 745                activity.SetTag("intentum.policy.decision", decision.ToString());
 746                activity.SetTag("intentum.intent.name", intent.Name);
 747                activity.SetTag("intentum.intent.confidence.level", intent.Confidence.Level);
 748                if (matchedRule != null)
 649                    activity.SetTag("intentum.policy.matched_rule", matchedRule.Name);
 750            }
 51
 1152            IntentumMetrics.RecordPolicyDecision(decision);
 1153        }
 254        catch (Exception ex)
 255        {
 256            activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
 257            success = false;
 258            exceptionMessage = ex.Message;
 259            decision = PolicyDecision.Observe;
 260            sw.Stop();
 261            var record = new PolicyExecutionRecord(
 262                intent.Name,
 263                matchedRule?.Name,
 264                decision,
 265                sw.Elapsed.TotalMilliseconds,
 266                success,
 267                exceptionMessage,
 268                ExceptionTrace: ex.StackTrace);
 269            return (decision, record);
 70        }
 71
 1172        sw.Stop();
 1173        var successRecord = new PolicyExecutionRecord(
 1174            intent.Name,
 1175            matchedRule?.Name,
 1176            decision,
 1177            sw.Elapsed.TotalMilliseconds,
 1178            success,
 1179            exceptionMessage);
 80
 1181        return (decision, successRecord);
 1382    }
 83}