@@ -6,57 +6,55 @@ import (
66 "github.com/open-feature/go-sdk/openfeature"
77 "go.opentelemetry.io/otel"
88 "go.opentelemetry.io/otel/attribute"
9- api "go.opentelemetry.io/otel/metric"
10- semconv "go.opentelemetry.io/otel/semconv/v1.18 .0"
9+ "go.opentelemetry.io/otel/metric"
10+ semconv "go.opentelemetry.io/otel/semconv/v1.34 .0"
1111)
1212
1313const (
14- meterName = "go.openfeature.dev"
15-
1614 evaluationActive = "feature_flag.evaluation_active_count"
1715 evaluationRequests = "feature_flag.evaluation_requests_total"
1816 evaluationSuccess = "feature_flag.evaluation_success_total"
1917 evaluationErrors = "feature_flag.evaluation_error_total"
2018)
2119
2220type MetricsHook struct {
23- activeCounter api .Int64UpDownCounter
24- requestCounter api .Int64Counter
25- successCounter api .Int64Counter
26- errorCounter api .Int64Counter
21+ activeCounter metric .Int64UpDownCounter
22+ requestCounter metric .Int64Counter
23+ successCounter metric .Int64Counter
24+ errorCounter metric .Int64Counter
2725
2826 flagEvalMetadataDimensions []DimensionDescription
2927 attributeMapperCallback func (openfeature.FlagMetadata ) []attribute.KeyValue
3028}
3129
3230var _ openfeature.Hook = & MetricsHook {}
3331
34- // NewMetricsHook builds a metric hook backed by a globally set metric.MeterProvider.
35- // Use otel.SetMeterProvider to set the global provider or use NewMetricsHookForProvider.
32+ // NewMetricsHook builds a metric hook backed by a globally set [ metric.MeterProvider] .
33+ // Use [ otel.SetMeterProvider] to set the global provider or use [ NewMetricsHookForProvider] .
3634func NewMetricsHook (opts ... MetricOptions ) (* MetricsHook , error ) {
3735 return NewMetricsHookForProvider (otel .GetMeterProvider (), opts ... )
3836}
3937
40- // NewMetricsHookForProvider builds a metric hook backed by metric.MeterProvider.
41- func NewMetricsHookForProvider (provider api .MeterProvider , opts ... MetricOptions ) (* MetricsHook , error ) {
42- meter := provider .Meter (meterName )
38+ // NewMetricsHookForProvider builds a metric hook backed by [ metric.MeterProvider] .
39+ func NewMetricsHookForProvider (provider metric .MeterProvider , opts ... MetricOptions ) (* MetricsHook , error ) {
40+ meter := provider .Meter (ScopeName )
4341
44- activeCounter , err := meter .Int64UpDownCounter (evaluationActive , api .WithDescription ("active flag evaluations counter" ))
42+ activeCounter , err := meter .Int64UpDownCounter (evaluationActive , metric .WithDescription ("active flag evaluations counter" ))
4543 if err != nil {
4644 return nil , err
4745 }
4846
49- evalCounter , err := meter .Int64Counter (evaluationRequests , api .WithDescription ("feature flag evaluation request counter" ))
47+ evalCounter , err := meter .Int64Counter (evaluationRequests , metric .WithDescription ("feature flag evaluation request counter" ))
5048 if err != nil {
5149 return nil , err
5250 }
5351
54- successCounter , err := meter .Int64Counter (evaluationSuccess , api .WithDescription ("feature flag evaluation success counter" ))
52+ successCounter , err := meter .Int64Counter (evaluationSuccess , metric .WithDescription ("feature flag evaluation success counter" ))
5553 if err != nil {
5654 return nil , err
5755 }
5856
59- errorCounter , err := meter .Int64Counter (evaluationErrors , api .WithDescription ("feature flag evaluation error counter" ))
57+ errorCounter , err := meter .Int64Counter (evaluationErrors , metric .WithDescription ("feature flag evaluation error counter" ))
6058 if err != nil {
6159 return nil , err
6260 }
@@ -78,10 +76,10 @@ func NewMetricsHookForProvider(provider api.MeterProvider, opts ...MetricOptions
7876func (h * MetricsHook ) Before (ctx context.Context , hCtx openfeature.HookContext ,
7977 hint openfeature.HookHints ,
8078) (* openfeature.EvaluationContext , error ) {
81- h .activeCounter .Add (ctx , + 1 , api .WithAttributes (semconv .FeatureFlagKey (hCtx .FlagKey ())))
79+ h .activeCounter .Add (ctx , 1 , metric .WithAttributes (semconv .FeatureFlagKey (hCtx .FlagKey ())))
8280
8381 h .requestCounter .Add (ctx , 1 ,
84- api .WithAttributes (
82+ metric .WithAttributes (
8583 semconv .FeatureFlagKey (hCtx .FlagKey ()),
8684 semconv .FeatureFlagProviderName (hCtx .ProviderMetadata ().Name )))
8785
@@ -97,7 +95,7 @@ func (h *MetricsHook) After(ctx context.Context, hCtx openfeature.HookContext,
9795 }
9896
9997 if details .Variant != "" {
100- attribs = append (attribs , semconv .FeatureFlagVariant (details .Variant ))
98+ attribs = append (attribs , semconv .FeatureFlagResultVariant (details .Variant ))
10199 }
102100
103101 if details .Reason != "" {
@@ -110,21 +108,23 @@ func (h *MetricsHook) After(ctx context.Context, hCtx openfeature.HookContext,
110108 attribs = append (attribs , h .attributeMapperCallback (details .FlagMetadata )... )
111109 }
112110
113- h .successCounter .Add (ctx , 1 , api .WithAttributes (attribs ... ))
111+ h .successCounter .Add (ctx , 1 , metric .WithAttributes (attribs ... ))
114112
115113 return nil
116114}
117115
118116func (h * MetricsHook ) Error (ctx context.Context , hCtx openfeature.HookContext , err error , hint openfeature.HookHints ) {
119117 h .errorCounter .Add (ctx , 1 ,
120- api .WithAttributes (
118+ metric .WithAttributes (
121119 semconv .FeatureFlagKey (hCtx .FlagKey ()),
122120 semconv .FeatureFlagProviderName (hCtx .ProviderMetadata ().Name ),
123- attribute .String (semconv .ExceptionEventName , err .Error ())))
121+ semconv .ErrorMessage (err .Error ()),
122+ ),
123+ )
124124}
125125
126126func (h * MetricsHook ) Finally (ctx context.Context , hCtx openfeature.HookContext , flagEvaluationDetails openfeature.InterfaceEvaluationDetails , hint openfeature.HookHints ) {
127- h .activeCounter .Add (ctx , - 1 , api .WithAttributes (semconv .FeatureFlagKey (hCtx .FlagKey ())))
127+ h .activeCounter .Add (ctx , - 1 , metric .WithAttributes (semconv .FeatureFlagKey (hCtx .FlagKey ())))
128128}
129129
130130// Extra options for metrics hook
@@ -150,23 +150,23 @@ type DimensionDescription struct {
150150}
151151
152152// WithFlagMetadataDimensions allows configuring extra dimensions for feature_flag.evaluation_success_total metric.
153- // If provided, dimensions will be extracted from openfeature.FlagMetadata & added to the metric with the same key
153+ // If provided, dimensions will be extracted from [ openfeature.FlagMetadata] & added to the metric with the same key.
154154func WithFlagMetadataDimensions (descriptions ... DimensionDescription ) MetricOptions {
155155 return func (metricsHook * MetricsHook ) {
156156 metricsHook .flagEvalMetadataDimensions = descriptions
157157 }
158158}
159159
160- // WithMetricsAttributeSetter allows to set a extractionCallback which accept openfeature.FlagMetadata and returns
160+ // WithMetricsAttributeSetter allows to set a extractionCallback which accepts a [ openfeature.FlagMetadata] and returns
161161// []attribute.KeyValue derived from those metadata.
162162func WithMetricsAttributeSetter (callback func (openfeature.FlagMetadata ) []attribute.KeyValue ) MetricOptions {
163163 return func (metricsHook * MetricsHook ) {
164164 metricsHook .attributeMapperCallback = callback
165165 }
166166}
167167
168- // descriptionsToAttributes is a helper to extract dimensions from openfeature.FlagMetadata. Missing metadata
169- // dimensions are ignore .
168+ // descriptionsToAttributes is a helper to extract dimensions from [ openfeature.FlagMetadata] . Missing metadata
169+ // dimensions are ignored .
170170func descriptionsToAttributes (metadata openfeature.FlagMetadata , descriptions []DimensionDescription ) []attribute.KeyValue {
171171 attribs := []attribute.KeyValue {}
172172 for _ , dimension := range descriptions {
0 commit comments