-
Notifications
You must be signed in to change notification settings - Fork 3
/
feature.go
84 lines (68 loc) · 2.21 KB
/
feature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package feature
import (
"context"
"strings"
"github.com/alexfalkowski/go-service/env"
"github.com/alexfalkowski/go-service/errors"
"github.com/alexfalkowski/go-service/transport/grpc"
"github.com/google/uuid"
flipt "github.com/open-feature/go-sdk-contrib/providers/flipt/pkg/provider"
"github.com/open-feature/go-sdk-contrib/providers/flipt/pkg/service/transport"
"github.com/open-feature/go-sdk/openfeature"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
)
// RegisterParams for feature.
type ClientParams struct {
fx.In
Config *Config
Logger *zap.Logger
Tracer trace.Tracer
Meter metric.Meter
Name env.Name
UserAgent env.UserAgent
}
// NewFeatureProvider for feature.
func NewFeatureProvider(params ClientParams) openfeature.FeatureProvider {
if !IsEnabled(params.Config) {
return openfeature.NoopProvider{}
}
if params.Config.IsFlipt() {
is := grpc.UnaryClientInterceptors(grpc.WithClientLogger(params.Logger),
grpc.WithClientTracer(params.Tracer),
grpc.WithClientMetrics(params.Meter),
grpc.WithClientRetry(params.Config.Retry),
grpc.WithClientUserAgent(params.UserAgent),
grpc.WithClientTimeout(params.Config.Timeout))
svc := transport.New(transport.WithAddress(params.Config.Host), transport.WithUnaryClientInterceptor(is...))
return flipt.NewProvider(flipt.WithAddress(params.Config.Host), flipt.WithService(svc))
}
return openfeature.NoopProvider{}
}
// NewClient for feature.
func NewClient(lc fx.Lifecycle, name env.Name, provider openfeature.FeatureProvider) *openfeature.Client {
openfeature.SetProvider(provider)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
openfeature.Shutdown()
return nil
},
})
return openfeature.NewClient(string(name))
}
// IsNotFoundError for feature.
func IsNotFoundError(err error) bool {
return strings.Contains(err.Error(), string(openfeature.FlagNotFoundCode))
}
// Ping for feature.
func Ping(ctx context.Context, client *openfeature.Client) error {
id := uuid.New().String()
e := openfeature.NewEvaluationContext(id, nil)
_, err := client.BooleanValue(ctx, id, false, e)
if IsNotFoundError(err) {
return nil
}
return errors.Prefix("ping", err)
}