-
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
402b394
Adding inital Metrics Hook.
askpt 9d56d1b
Add metrics tracking to MetricsHook class
askpt 06ba9d4
Add metrics instrumentation to MetricsHook class
askpt 231ab4d
Add descriptions to metric counters
askpt c50235f
Update MetricsHook.cs with new constant UNIT
askpt 53d9b0b
Add evaluationSuccessCounter metric tracking
askpt 922fcec
Add error tracking for evaluation errors
askpt 98f69eb
Add tags to evaluationActiveUpDownCounter in Finally method
askpt 5010a93
Remove empty line in MetricsHook.cs
askpt 83da161
Refactor metrics hook class and use constants for metric names and de…
askpt 3654da8
Add documentation.
askpt c19866b
Merge branch 'main' into askpt/113-add-metrics
askpt 5a0ca25
Refactor metrics constants and attributes
askpt f3932c2
Update metrics constants names
askpt e96b2a7
Add meter name to MetricsHook class
askpt 3a1edef
Add MetricsHookTest class for OpenFeature.Contrib.Hooks.Otel.Test
askpt 64e9e61
Add Finally_Test to MetricsHookTest.cs
askpt fcaea28
Refactor metrics hook tests
askpt a813239
Refactor metrics assertions in MetricsHookTest.cs
askpt 67eb243
Update README.md with usage examples for Traces and Metrics
askpt 4cb73a5
Update OpenTelemetry hook example to send metrics to the console
askpt f8e52e6
Update MetricsConstants and MetricsHook to use long instead of double
askpt 24d1682
Remove unused constant and update metric counter parameters
askpt a46278d
Update metrics documentation in README.md
askpt ea63383
fixup: add askpt to component_owners
toddbaert 87a59e8
Merge branch 'main' into askpt/113-add-metrics
askpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,23 @@ | ||
namespace OpenFeature.Contrib.Hooks.Otel | ||
{ | ||
internal static class MetricsConstants | ||
{ | ||
internal const string Unit = "double"; | ||
|
||
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<double> _evaluationActiveUpDownCounter; | ||
askpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private readonly Counter<double> _evaluationRequestCounter; | ||
private readonly Counter<double> _evaluationSuccessCounter; | ||
private readonly Counter<double> _evaluationErrorCounter; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MetricsHook"/> class. | ||
/// </summary> | ||
public MetricsHook() | ||
{ | ||
var meter = new Meter(InstrumentationName, InstrumentationVersion); | ||
|
||
_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> | ||
/// 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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 comment
The 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