-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: André Silva <[email protected]>
- Loading branch information
Showing
6 changed files
with
386 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dotnet.defaultSolution": "DotnetSdkContrib.sln" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace OpenFeature.Contrib.Hooks.Otel | ||
{ | ||
internal static class MetricsConstants | ||
{ | ||
internal const string ActiveCountName = "feature_flag.evaluation_active_count"; | ||
internal const string RequestsTotalName = "feature_flag.evaluation_requests_total"; | ||
internal const string SuccessTotalName = "feature_flag.evaluation_success_total"; | ||
internal const string ErrorTotalName = "feature_flag.evaluation_error_total"; | ||
|
||
internal const string ActiveDescription = "active flag evaluations counter"; | ||
internal const string RequestsDescription = "feature flag evaluation request counter"; | ||
internal const string SuccessDescription = "feature flag evaluation success counter"; | ||
internal const string ErrorDescription = "feature flag evaluation error counter"; | ||
|
||
internal const string KeyAttr = "key"; | ||
internal const string ProviderNameAttr = "provider_name"; | ||
internal const string VariantAttr = "variant"; | ||
internal const string ReasonAttr = "reason"; | ||
internal const string ExceptionAttr = "exception"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using OpenFeature.Model; | ||
|
||
namespace OpenFeature.Contrib.Hooks.Otel | ||
{ | ||
/// <summary> | ||
/// Represents a hook for capturing metrics related to flag evaluations. | ||
/// The meter name is "OpenFeature.Contrib.Hooks.Otel". | ||
/// </summary> | ||
public class MetricsHook : Hook | ||
{ | ||
private static readonly AssemblyName AssemblyName = typeof(MetricsHook).Assembly.GetName(); | ||
private static readonly string InstrumentationName = AssemblyName.Name; | ||
private static readonly string InstrumentationVersion = AssemblyName.Version?.ToString(); | ||
|
||
private readonly UpDownCounter<long> _evaluationActiveUpDownCounter; | ||
private readonly Counter<long> _evaluationRequestCounter; | ||
private readonly Counter<long> _evaluationSuccessCounter; | ||
private readonly Counter<long> _evaluationErrorCounter; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MetricsHook"/> class. | ||
/// </summary> | ||
public MetricsHook() | ||
{ | ||
var meter = new Meter(InstrumentationName, InstrumentationVersion); | ||
|
||
_evaluationActiveUpDownCounter = meter.CreateUpDownCounter<long>(MetricsConstants.ActiveCountName, description: MetricsConstants.ActiveDescription); | ||
_evaluationRequestCounter = meter.CreateCounter<long>(MetricsConstants.RequestsTotalName, "{request}", MetricsConstants.RequestsDescription); | ||
_evaluationSuccessCounter = meter.CreateCounter<long>(MetricsConstants.SuccessTotalName, "{impression}", MetricsConstants.SuccessDescription); | ||
_evaluationErrorCounter = meter.CreateCounter<long>(MetricsConstants.ErrorTotalName, description: MetricsConstants.ErrorDescription); | ||
} | ||
|
||
/// <summary> | ||
/// Executes before the flag evaluation and captures metrics related to the evaluation. | ||
/// The metrics are captured in the following order: | ||
/// 1. The active count is incremented. (feature_flag.evaluation_active_count) | ||
/// 2. The request count is incremented. (feature_flag.evaluation_requests_total) | ||
/// </summary> | ||
/// <typeparam name="T">The type of the flag value.</typeparam> | ||
/// <param name="context">The hook context.</param> | ||
/// <param name="hints">The optional hints.</param> | ||
/// <returns>The evaluation context.</returns> | ||
public override Task<EvaluationContext> Before<T>(HookContext<T> context, IReadOnlyDictionary<string, object> hints = null) | ||
{ | ||
var tagList = new TagList | ||
{ | ||
{ MetricsConstants.KeyAttr, context.FlagKey }, | ||
{ MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name } | ||
}; | ||
|
||
_evaluationActiveUpDownCounter.Add(1, tagList); | ||
_evaluationRequestCounter.Add(1, tagList); | ||
|
||
return base.Before(context, hints); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Executes after the flag evaluation and captures metrics related to the evaluation. | ||
/// The metrics are captured in the following order: | ||
/// 1. The success count is incremented. (feature_flag.evaluation_success_total) | ||
/// </summary> | ||
/// <typeparam name="T">The type of the flag value.</typeparam> | ||
/// <param name="context">The hook context.</param> | ||
/// <param name="details">The flag evaluation details.</param> | ||
/// <param name="hints">The optional hints.</param> | ||
/// <returns>The evaluation context.</returns> | ||
public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> details, IReadOnlyDictionary<string, object> hints = null) | ||
{ | ||
var tagList = new TagList | ||
{ | ||
{ MetricsConstants.KeyAttr, context.FlagKey }, | ||
{ MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name }, | ||
{ MetricsConstants.VariantAttr, details.Variant ?? details.Value?.ToString() }, | ||
{ MetricsConstants.ReasonAttr, details.Reason ?? "UNKNOWN" } | ||
}; | ||
|
||
_evaluationSuccessCounter.Add(1, tagList); | ||
|
||
return base.After(context, details, hints); | ||
} | ||
|
||
/// <summary> | ||
/// Executes when an error occurs during flag evaluation and captures metrics related to the error. | ||
/// The metrics are captured in the following order: | ||
/// 1. The error count is incremented. (feature_flag.evaluation_error_total) | ||
/// </summary> | ||
/// <typeparam name="T">The type of the flag value.</typeparam> | ||
/// <param name="context">The hook context.</param> | ||
/// <param name="error">The exception that occurred.</param> | ||
/// <param name="hints">The optional hints.</param> | ||
/// <returns>The evaluation context.</returns> | ||
public override Task Error<T>(HookContext<T> context, Exception error, IReadOnlyDictionary<string, object> hints = null) | ||
{ | ||
var tagList = new TagList | ||
{ | ||
{ MetricsConstants.KeyAttr, context.FlagKey }, | ||
{ MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name }, | ||
{ MetricsConstants.ExceptionAttr, error?.Message ?? "Unknown error" } | ||
}; | ||
|
||
_evaluationErrorCounter.Add(1, tagList); | ||
|
||
return base.Error(context, error, hints); | ||
} | ||
|
||
/// <summary> | ||
/// Executes after the flag evaluation is complete and captures metrics related to the evaluation. | ||
/// The active count is decremented. (feature_flag.evaluation_active_count) | ||
/// </summary> | ||
/// <typeparam name="T">The type of the flag value.</typeparam> | ||
/// <param name="context">The hook context.</param> | ||
/// <param name="hints">The optional hints.</param> | ||
/// <returns>The evaluation context.</returns> | ||
public override Task Finally<T>(HookContext<T> context, IReadOnlyDictionary<string, object> hints = null) | ||
{ | ||
var tagList = new TagList | ||
{ | ||
{ MetricsConstants.KeyAttr, context.FlagKey }, | ||
{ MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name } | ||
}; | ||
|
||
_evaluationActiveUpDownCounter.Add(-1, tagList); | ||
|
||
return base.Finally(context, hints); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.