Skip to content

Commit

Permalink
Refactor metrics hook class and use constants for metric names and de…
Browse files Browse the repository at this point in the history
…scriptions

Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt committed Dec 14, 2023
1 parent 5010a93 commit 83da161
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
23 changes: 23 additions & 0 deletions src/OpenFeature.Contrib.Hooks.Otel/MetricsConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace OpenFeature.Contrib.Hooks.Otel
{
internal static class MetricsConstants
{
internal const string UNIT = "double";

internal const string ACTIVE_COUNT_NAME = "feature_flag.evaluation_active_count";
internal const string REQUESTS_TOTAL_NAME = "feature_flag.evaluation_requests_total";
internal const string SUCCESS_TOTAL_NAME = "feature_flag.evaluation_success_total";
internal const string ERROR_TOTAL_NAME = "feature_flag.evaluation_error_total";

internal const string ACTIVE_DESCRIPTION = "active flag evaluations counter";
internal const string REQUESTS_DESCRIPTION = "feature flag evaluation request counter";
internal const string SUCCESS_DESCRIPTION = "feature flag evaluation success counter";
internal const string ERROR_DESCRIPTION = "feature flag evaluation error counter";

internal const string KEY_ATTR = "feature_flag.key";
internal const string PROVIDER_NAME_ATTR = "feature_flag.provider_name";
internal const string VARIANT_ATTR = "feature_flag.variant";
internal const string REASON_ATTR = "feature_flag.reason";
internal const string EXCEPTION_ATTR = "exception";
}
}
48 changes: 15 additions & 33 deletions src/OpenFeature.Contrib.Hooks.Otel/MetricsHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@ public class MetricsHook : Hook
private static readonly string InstrumentationName = AssemblyName.Name;
private static readonly string InstrumentationVersion = AssemblyName.Version.ToString();

private const string EXCEPTION_ATTR = "exception";
private const string UNIT = "double";

private const string ACTIVE_COUNT_NAME = "feature_flag.evaluation_active_count";
private const string REQUESTS_TOTAL_NAME = "feature_flag.evaluation_requests_total";
private const string SUCCESS_TOTAL_NAME = "feature_flag.evaluation_success_total";
private const string ERROR_TOTAL_NAME = "feature_flag.evaluation_error_total";

private const string ACTIVE_DESCRIPTION = "active flag evaluations counter";
private const string REQUESTS_DESCRIPTION = "feature flag evaluation request counter";
private const string SUCCESS_DESCRIPTION = "feature flag evaluation success counter";
private const string ERROR_DESCRIPTION = "feature flag evaluation error counter";

private const string KEY_ATTR = "feature_flag.key";
private const string PROVIDER_NAME_ATTR = "feature_flag.provider_name";
private const string VARIANT_ATTR = "feature_flag.variant";
private const string REASON_ATTR = "feature_flag.reason";

private readonly UpDownCounter<double> _evaluationActiveUpDownCounter;
private readonly Counter<double> _evaluationRequestCounter;
private readonly Counter<double> _evaluationSuccessCounter;
Expand All @@ -41,18 +23,18 @@ public MetricsHook()
{
var meter = new Meter(InstrumentationName, InstrumentationVersion);

_evaluationActiveUpDownCounter = meter.CreateUpDownCounter<double>(ACTIVE_COUNT_NAME, UNIT, ACTIVE_DESCRIPTION);
_evaluationRequestCounter = meter.CreateCounter<double>(REQUESTS_TOTAL_NAME, UNIT, REQUESTS_DESCRIPTION);
_evaluationSuccessCounter = meter.CreateCounter<double>(SUCCESS_TOTAL_NAME, UNIT, SUCCESS_DESCRIPTION);
_evaluationErrorCounter = meter.CreateCounter<double>(ERROR_TOTAL_NAME, UNIT, ERROR_DESCRIPTION);
_evaluationActiveUpDownCounter = meter.CreateUpDownCounter<double>(MetricsConstants.ACTIVE_COUNT_NAME, MetricsConstants.UNIT, MetricsConstants.ACTIVE_DESCRIPTION);
_evaluationRequestCounter = meter.CreateCounter<double>(MetricsConstants.REQUESTS_TOTAL_NAME, MetricsConstants.UNIT, MetricsConstants.REQUESTS_DESCRIPTION);
_evaluationSuccessCounter = meter.CreateCounter<double>(MetricsConstants.SUCCESS_TOTAL_NAME, MetricsConstants.UNIT, MetricsConstants.SUCCESS_DESCRIPTION);
_evaluationErrorCounter = meter.CreateCounter<double>(MetricsConstants.ERROR_TOTAL_NAME, MetricsConstants.UNIT, MetricsConstants.ERROR_DESCRIPTION);
}

public override Task<EvaluationContext> Before<T>(HookContext<T> context, IReadOnlyDictionary<string, object> hints = null)
{
var tagList = new TagList
{
{ KEY_ATTR, context.FlagKey },
{ PROVIDER_NAME_ATTR, context.ProviderMetadata.Name }
{ MetricsConstants.KEY_ATTR, context.FlagKey },
{ MetricsConstants.PROVIDER_NAME_ATTR, context.ProviderMetadata.Name }
};

_evaluationActiveUpDownCounter.Add(1, tagList);
Expand All @@ -65,10 +47,10 @@ public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> d
{
var tagList = new TagList
{
{ KEY_ATTR, context.FlagKey },
{ PROVIDER_NAME_ATTR, context.ProviderMetadata.Name },
{ VARIANT_ATTR, details.Variant ?? details.Value?.ToString() },
{ REASON_ATTR, details.Reason ?? "UNKNOWN" }
{ MetricsConstants.KEY_ATTR, context.FlagKey },
{ MetricsConstants.PROVIDER_NAME_ATTR, context.ProviderMetadata.Name },
{ MetricsConstants.VARIANT_ATTR, details.Variant ?? details.Value?.ToString() },
{ MetricsConstants.REASON_ATTR, details.Reason ?? "UNKNOWN" }
};

_evaluationSuccessCounter.Add(1, tagList);
Expand All @@ -80,9 +62,9 @@ public override Task Error<T>(HookContext<T> context, Exception error, IReadOnly
{
var tagList = new TagList
{
{ KEY_ATTR, context.FlagKey },
{ PROVIDER_NAME_ATTR, context.ProviderMetadata.Name },
{ EXCEPTION_ATTR, error?.Message ?? "Unknown error" }
{ MetricsConstants.KEY_ATTR, context.FlagKey },
{ MetricsConstants.PROVIDER_NAME_ATTR, context.ProviderMetadata.Name },
{ MetricsConstants.EXCEPTION_ATTR, error?.Message ?? "Unknown error" }
};

_evaluationErrorCounter.Add(1, tagList);
Expand All @@ -94,8 +76,8 @@ public override Task Finally<T>(HookContext<T> context, IReadOnlyDictionary<stri
{
var tagList = new TagList
{
{ KEY_ATTR, context.FlagKey },
{ PROVIDER_NAME_ATTR, context.ProviderMetadata.Name }
{ MetricsConstants.KEY_ATTR, context.FlagKey },
{ MetricsConstants.PROVIDER_NAME_ATTR, context.ProviderMetadata.Name }
};

_evaluationActiveUpDownCounter.Add(-1, tagList);
Expand Down

0 comments on commit 83da161

Please sign in to comment.