|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Diagnostics.Metrics; |
| 5 | +using System.Reflection; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using OpenFeature.Model; |
| 8 | + |
| 9 | +namespace OpenFeature.Contrib.Hooks.Otel |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Represents a hook for capturing metrics related to flag evaluations. |
| 13 | + /// The meter name is "OpenFeature.Contrib.Hooks.Otel". |
| 14 | + /// </summary> |
| 15 | + public class MetricsHook : Hook |
| 16 | + { |
| 17 | + private static readonly AssemblyName AssemblyName = typeof(MetricsHook).Assembly.GetName(); |
| 18 | + private static readonly string InstrumentationName = AssemblyName.Name; |
| 19 | + private static readonly string InstrumentationVersion = AssemblyName.Version?.ToString(); |
| 20 | + |
| 21 | + private readonly UpDownCounter<long> _evaluationActiveUpDownCounter; |
| 22 | + private readonly Counter<long> _evaluationRequestCounter; |
| 23 | + private readonly Counter<long> _evaluationSuccessCounter; |
| 24 | + private readonly Counter<long> _evaluationErrorCounter; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="MetricsHook"/> class. |
| 28 | + /// </summary> |
| 29 | + public MetricsHook() |
| 30 | + { |
| 31 | + var meter = new Meter(InstrumentationName, InstrumentationVersion); |
| 32 | + |
| 33 | + _evaluationActiveUpDownCounter = meter.CreateUpDownCounter<long>(MetricsConstants.ActiveCountName, description: MetricsConstants.ActiveDescription); |
| 34 | + _evaluationRequestCounter = meter.CreateCounter<long>(MetricsConstants.RequestsTotalName, "{request}", MetricsConstants.RequestsDescription); |
| 35 | + _evaluationSuccessCounter = meter.CreateCounter<long>(MetricsConstants.SuccessTotalName, "{impression}", MetricsConstants.SuccessDescription); |
| 36 | + _evaluationErrorCounter = meter.CreateCounter<long>(MetricsConstants.ErrorTotalName, description: MetricsConstants.ErrorDescription); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Executes before the flag evaluation and captures metrics related to the evaluation. |
| 41 | + /// The metrics are captured in the following order: |
| 42 | + /// 1. The active count is incremented. (feature_flag.evaluation_active_count) |
| 43 | + /// 2. The request count is incremented. (feature_flag.evaluation_requests_total) |
| 44 | + /// </summary> |
| 45 | + /// <typeparam name="T">The type of the flag value.</typeparam> |
| 46 | + /// <param name="context">The hook context.</param> |
| 47 | + /// <param name="hints">The optional hints.</param> |
| 48 | + /// <returns>The evaluation context.</returns> |
| 49 | + public override Task<EvaluationContext> Before<T>(HookContext<T> context, IReadOnlyDictionary<string, object> hints = null) |
| 50 | + { |
| 51 | + var tagList = new TagList |
| 52 | + { |
| 53 | + { MetricsConstants.KeyAttr, context.FlagKey }, |
| 54 | + { MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name } |
| 55 | + }; |
| 56 | + |
| 57 | + _evaluationActiveUpDownCounter.Add(1, tagList); |
| 58 | + _evaluationRequestCounter.Add(1, tagList); |
| 59 | + |
| 60 | + return base.Before(context, hints); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Executes after the flag evaluation and captures metrics related to the evaluation. |
| 66 | + /// The metrics are captured in the following order: |
| 67 | + /// 1. The success count is incremented. (feature_flag.evaluation_success_total) |
| 68 | + /// </summary> |
| 69 | + /// <typeparam name="T">The type of the flag value.</typeparam> |
| 70 | + /// <param name="context">The hook context.</param> |
| 71 | + /// <param name="details">The flag evaluation details.</param> |
| 72 | + /// <param name="hints">The optional hints.</param> |
| 73 | + /// <returns>The evaluation context.</returns> |
| 74 | + public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> details, IReadOnlyDictionary<string, object> hints = null) |
| 75 | + { |
| 76 | + var tagList = new TagList |
| 77 | + { |
| 78 | + { MetricsConstants.KeyAttr, context.FlagKey }, |
| 79 | + { MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name }, |
| 80 | + { MetricsConstants.VariantAttr, details.Variant ?? details.Value?.ToString() }, |
| 81 | + { MetricsConstants.ReasonAttr, details.Reason ?? "UNKNOWN" } |
| 82 | + }; |
| 83 | + |
| 84 | + _evaluationSuccessCounter.Add(1, tagList); |
| 85 | + |
| 86 | + return base.After(context, details, hints); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Executes when an error occurs during flag evaluation and captures metrics related to the error. |
| 91 | + /// The metrics are captured in the following order: |
| 92 | + /// 1. The error count is incremented. (feature_flag.evaluation_error_total) |
| 93 | + /// </summary> |
| 94 | + /// <typeparam name="T">The type of the flag value.</typeparam> |
| 95 | + /// <param name="context">The hook context.</param> |
| 96 | + /// <param name="error">The exception that occurred.</param> |
| 97 | + /// <param name="hints">The optional hints.</param> |
| 98 | + /// <returns>The evaluation context.</returns> |
| 99 | + public override Task Error<T>(HookContext<T> context, Exception error, IReadOnlyDictionary<string, object> hints = null) |
| 100 | + { |
| 101 | + var tagList = new TagList |
| 102 | + { |
| 103 | + { MetricsConstants.KeyAttr, context.FlagKey }, |
| 104 | + { MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name }, |
| 105 | + { MetricsConstants.ExceptionAttr, error?.Message ?? "Unknown error" } |
| 106 | + }; |
| 107 | + |
| 108 | + _evaluationErrorCounter.Add(1, tagList); |
| 109 | + |
| 110 | + return base.Error(context, error, hints); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Executes after the flag evaluation is complete and captures metrics related to the evaluation. |
| 115 | + /// The active count is decremented. (feature_flag.evaluation_active_count) |
| 116 | + /// </summary> |
| 117 | + /// <typeparam name="T">The type of the flag value.</typeparam> |
| 118 | + /// <param name="context">The hook context.</param> |
| 119 | + /// <param name="hints">The optional hints.</param> |
| 120 | + /// <returns>The evaluation context.</returns> |
| 121 | + public override Task Finally<T>(HookContext<T> context, IReadOnlyDictionary<string, object> hints = null) |
| 122 | + { |
| 123 | + var tagList = new TagList |
| 124 | + { |
| 125 | + { MetricsConstants.KeyAttr, context.FlagKey }, |
| 126 | + { MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name } |
| 127 | + }; |
| 128 | + |
| 129 | + _evaluationActiveUpDownCounter.Add(-1, tagList); |
| 130 | + |
| 131 | + return base.Finally(context, hints); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments