forked from gouthamve/redbull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redbull.go
177 lines (144 loc) · 4.22 KB
/
redbull.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package redbull
import (
"context"
"flag"
"path/filepath"
"time"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/spanstore"
"github.com/spf13/viper"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
)
var (
logger *zap.SugaredLogger
)
const (
sizeOfTraceID = 16
retentionFlag = "redbull.retention"
dataDirFlag = "redbull.data-dir"
binPathFlag = "redbull.sybil-path"
)
// Factory is the redbull factory that implements storage.Factory.
type Factory struct {
cfg Config
*redbull
}
// NewFactory returns a new factory.
func NewFactory() *Factory {
return &Factory{}
}
// AddFlags implements plugin.Configurable.
func (f *Factory) AddFlags(flagset *flag.FlagSet) {
flagset.Duration(retentionFlag, 24*time.Hour, "The retention period for redbull.")
flagset.String(dataDirFlag, "/data/", "The data directory for redbull.")
flagset.String(binPathFlag, "sybil", "The path to the sybil binary.")
}
// InitFromViper implements plugin.Configurable.
func (f *Factory) InitFromViper(v *viper.Viper) {
f.cfg.retention = v.GetDuration(retentionFlag)
f.cfg.dataDir = v.GetString(dataDirFlag)
f.cfg.sybilBinpath = v.GetString(binPathFlag)
}
// Initialize implements storage.Factory.
func (f *Factory) Initialize(metricsFactory metrics.Factory, zapLogger *zap.Logger) error {
logger = zapLogger.Sugar()
var err error
f.redbull, err = NewRedBull(f.cfg)
return err
}
// CreateSpanReader implements storage.Factory.
func (f *Factory) CreateSpanReader() (spanstore.Reader, error) {
return f.redbull.SpanReader(), nil
}
// CreateSpanWriter implements storage.Factory.
func (f *Factory) CreateSpanWriter() (spanstore.Writer, error) {
return f.redbull.SpanWriter(), nil
}
// CreateDependencyReader implements storage.Factory.
func (f *Factory) CreateDependencyReader() (dependencystore.Reader, error) {
return f.redbull.DependencyReader(), nil
}
type redbull struct {
cfg Config
sybil sybil
kv *kvstore
}
// Config holds the configuration for redbull.
type Config struct {
retention time.Duration
dataDir string
sybilBinpath string
}
// NewRedBull creates a redbull instance.
func NewRedBull(cfg Config) (*redbull, error) {
kv, err := newKVStore(filepath.Join(cfg.dataDir, "badger-db"), cfg.retention)
if err != nil {
return nil, err
}
rb := &redbull{
cfg: cfg,
sybil: newSybil(sybilConfig{
BinPath: cfg.sybilBinpath,
DBPath: filepath.Join(cfg.dataDir, "sybil-db"),
Retention: cfg.retention,
}),
kv: kv,
}
rb.sybil.start()
rb.kv.start()
return rb, nil
}
func (rb *redbull) Close() error {
rb.sybil.stop()
return rb.kv.stop()
}
func (rb *redbull) DependencyReader() dependencystore.Reader {
return rb
}
func (rb *redbull) SpanReader() spanstore.Reader {
return rb
}
func (rb *redbull) SpanWriter() spanstore.Writer {
return rb
}
func (rb *redbull) GetDependencies(endTs time.Time, lookback time.Duration) ([]model.DependencyLink, error) {
return nil, nil
}
func (rb *redbull) GetTrace(ctx context.Context, traceID model.TraceID) (*model.Trace, error) {
return rb.kv.getTrace(traceID)
}
func (rb *redbull) GetServices(ctx context.Context) ([]string, error) {
return rb.sybil.getServices(ctx)
}
func (rb *redbull) GetOperations(ctx context.Context, query spanstore.OperationQueryParameters) ([]spanstore.Operation, error) {
operations, err := rb.sybil.getOperations(ctx, query.ServiceName)
if err != nil {
return nil, err
}
ops := make([]spanstore.Operation, 0, len(operations))
for _, op := range operations {
ops = append(ops, spanstore.Operation{Name: op})
}
return ops, nil
}
func (rb *redbull) FindTraces(ctx context.Context, query *spanstore.TraceQueryParameters) ([]*model.Trace, error) {
traceIDs, err := rb.sybil.findTraceIDs(ctx, query)
if err != nil {
return nil, err
}
return rb.kv.getTraces(traceIDs)
}
func (rb *redbull) FindTraceIDs(ctx context.Context, query *spanstore.TraceQueryParameters) ([]model.TraceID, error) {
return rb.sybil.findTraceIDs(ctx, query)
}
func (rb *redbull) WriteSpan(span *model.Span) error {
// TODO: Reject old writes.
// Write to KV Store.
if err := rb.kv.addSpan(span); err != nil {
return err
}
// Write to columnar index store.
return rb.sybil.writeSpan(span)
}