-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.go
73 lines (61 loc) · 1.6 KB
/
plugin.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
package memcached
import (
"github.com/roadrunner-server/api/v4/plugins/v1/kv"
"github.com/roadrunner-server/endure/v2/dep"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/memcached/v5/memcachedkv"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
)
const (
PluginName string = "memcached"
RootPluginName string = "kv"
)
type Configurer interface {
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if config section exists.
Has(name string) bool
}
type Logger interface {
NamedLogger(name string) *zap.Logger
}
// Tracer is a plugin (OTEL) interface that provides tracer
type Tracer interface {
Tracer() *sdktrace.TracerProvider
}
type Plugin struct {
// logger
log *zap.Logger
// config plugin
cfgPlugin Configurer
tracer *sdktrace.TracerProvider
}
func (p *Plugin) Init(log Logger, cfg Configurer) error {
if !cfg.Has(RootPluginName) {
return errors.E(errors.Disabled)
}
p.cfgPlugin = cfg
p.log = log.NamedLogger(PluginName)
p.tracer = sdktrace.NewTracerProvider()
return nil
}
// Name returns plugin user-friendly name
func (p *Plugin) Name() string {
return PluginName
}
func (p *Plugin) Collects() []*dep.In {
return []*dep.In{
dep.Fits(func(pp any) {
p.tracer = pp.(Tracer).Tracer()
}, (*Tracer)(nil)),
}
}
func (p *Plugin) KvFromConfig(key string) (kv.Storage, error) {
const op = errors.Op("memcachedkv_plugin_provide")
st, err := memcachedkv.NewMemcachedDriver(p.log, key, p.cfgPlugin, p.tracer)
if err != nil {
return nil, errors.E(op, err)
}
return st, nil
}