Skip to content

Commit

Permalink
Refactor metrics constants and attributes
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt committed Dec 14, 2023
1 parent c19866b commit 5a0ca25
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
28 changes: 14 additions & 14 deletions src/OpenFeature.Contrib.Hooks.Otel/MetricsConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ namespace OpenFeature.Contrib.Hooks.Otel
{
internal static class MetricsConstants
{
internal const string UNIT = "double";
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 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 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 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 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";
internal const string KeyAttr = "feature_flag.key";
internal const string ProviderNameAttr = "feature_flag.provider_name";
internal const string VariantAttr = "feature_flag.variant";
internal const string ReasonAttr = "feature_flag.reason";
internal const string ExceptionAttr = "exception";
}
}
30 changes: 15 additions & 15 deletions src/OpenFeature.Contrib.Hooks.Otel/MetricsHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public MetricsHook()
{
var meter = new Meter(InstrumentationName, InstrumentationVersion);

_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);
_evaluationActiveUpDownCounter = meter.CreateUpDownCounter<double>(MetricsConstants.ActiveCountName, MetricsConstants.Unit, MetricsConstants.ActiveDescription);
_evaluationRequestCounter = meter.CreateCounter<double>(MetricsConstants.RequestsTotalName, MetricsConstants.Unit, MetricsConstants.RequestsDescription);
_evaluationSuccessCounter = meter.CreateCounter<double>(MetricsConstants.SuccessTotalName, MetricsConstants.Unit, MetricsConstants.SuccessDescription);
_evaluationErrorCounter = meter.CreateCounter<double>(MetricsConstants.ErrorTotalName, MetricsConstants.Unit, MetricsConstants.ErrorDescription);
}

/// <summary>
Expand All @@ -49,8 +49,8 @@ public override Task<EvaluationContext> Before<T>(HookContext<T> context, IReadO
{
var tagList = new TagList
{
{ MetricsConstants.KEY_ATTR, context.FlagKey },
{ MetricsConstants.PROVIDER_NAME_ATTR, context.ProviderMetadata.Name }
{ MetricsConstants.KeyAttr, context.FlagKey },
{ MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name }
};

_evaluationActiveUpDownCounter.Add(1, tagList);
Expand All @@ -74,10 +74,10 @@ public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> d
{
var tagList = new TagList
{
{ 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" }
{ 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);
Expand All @@ -99,9 +99,9 @@ public override Task Error<T>(HookContext<T> context, Exception error, IReadOnly
{
var tagList = new TagList
{
{ MetricsConstants.KEY_ATTR, context.FlagKey },
{ MetricsConstants.PROVIDER_NAME_ATTR, context.ProviderMetadata.Name },
{ MetricsConstants.EXCEPTION_ATTR, error?.Message ?? "Unknown error" }
{ MetricsConstants.KeyAttr, context.FlagKey },
{ MetricsConstants.ProviderNameAttr, context.ProviderMetadata.Name },
{ MetricsConstants.ExceptionAttr, error?.Message ?? "Unknown error" }
};

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

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

0 comments on commit 5a0ca25

Please sign in to comment.