-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Metrics Hook #114
Changes from all commits
402b394
9d56d1b
06ba9d4
231ab4d
c50235f
53d9b0b
922fcec
98f69eb
5010a93
83da161
3654da8
c19866b
5a0ca25
f3932c2
e96b2a7
3a1edef
64e9e61
fcaea28
a813239
67eb243
4cb73a5
f8e52e6
24d1682
a46278d
ea63383
87a59e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dotnet.defaultSolution": "DotnetSdkContrib.sln" | ||
} |
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should look into why this pkg doesn't provide the attributes for FF: https://github.com/open-telemetry/opentelemetry-dotnet/tree/af83eb1043a34f82ba3bfe20c8b674ac295cfb37/src/OpenTelemetry.SemanticConventions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if they expect PRs for this or not, but we could open one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems the library has not "updated" since 2022: https://www.nuget.org/packages/OpenTelemetry.SemanticConventions/1.0.0-rc9.9 Might need some help to reopen engagement there |
||
internal const string ProviderNameAttr = "provider_name"; | ||
internal const string VariantAttr = "variant"; | ||
internal const string ReasonAttr = "reason"; | ||
internal const string ExceptionAttr = "exception"; | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@askpt I hope you don't mind I've added you here. This will add you to PRs for this component automatically, once you're an org member.