/
factory.go
339 lines (307 loc) · 10.8 KB
/
factory.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package storage
import (
"errors"
"flag"
"fmt"
"io"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/plugin"
"github.com/jaegertracing/jaeger/plugin/storage/badger"
"github.com/jaegertracing/jaeger/plugin/storage/cassandra"
"github.com/jaegertracing/jaeger/plugin/storage/es"
"github.com/jaegertracing/jaeger/plugin/storage/grpc"
"github.com/jaegertracing/jaeger/plugin/storage/kafka"
"github.com/jaegertracing/jaeger/plugin/storage/memory"
"github.com/jaegertracing/jaeger/storage"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
const (
cassandraStorageType = "cassandra"
opensearchStorageType = "opensearch"
elasticsearchStorageType = "elasticsearch"
memoryStorageType = "memory"
kafkaStorageType = "kafka"
grpcPluginStorageType = "grpc-plugin"
badgerStorageType = "badger"
downsamplingRatio = "downsampling.ratio"
downsamplingHashSalt = "downsampling.hashsalt"
spanStorageType = "span-storage-type"
// defaultDownsamplingRatio is the default downsampling ratio.
defaultDownsamplingRatio = 1.0
// defaultDownsamplingHashSalt is the default downsampling hashsalt.
defaultDownsamplingHashSalt = ""
)
// AllStorageTypes defines all available storage backends
var AllStorageTypes = []string{
cassandraStorageType,
opensearchStorageType,
elasticsearchStorageType,
memoryStorageType,
kafkaStorageType,
badgerStorageType,
grpcPluginStorageType,
}
// AllSamplingStorageTypes returns all storage backends that implement adaptive sampling
func AllSamplingStorageTypes() []string {
f := &Factory{}
var backends []string
for _, st := range AllStorageTypes {
f, _ := f.getFactoryOfType(st) // no errors since we're looping through supported types
if _, ok := f.(storage.SamplingStoreFactory); ok {
backends = append(backends, st)
}
}
return backends
}
var (
_ io.Closer = (*Factory)(nil)
_ plugin.Configurable = (*Factory)(nil)
)
// Factory implements storage.Factory interface as a meta-factory for storage components.
type Factory struct {
FactoryConfig
metricsFactory metrics.Factory
factories map[string]storage.Factory
downsamplingFlagsAdded bool
}
// NewFactory creates the meta-factory.
func NewFactory(config FactoryConfig) (*Factory, error) {
f := &Factory{FactoryConfig: config}
uniqueTypes := map[string]struct{}{
f.SpanReaderType: {},
f.DependenciesStorageType: {},
}
for _, storageType := range f.SpanWriterTypes {
uniqueTypes[storageType] = struct{}{}
}
// skip SamplingStorageType if it is empty. See CreateSamplingStoreFactory for details
if f.SamplingStorageType != "" {
uniqueTypes[f.SamplingStorageType] = struct{}{}
}
f.factories = make(map[string]storage.Factory)
for t := range uniqueTypes {
ff, err := f.getFactoryOfType(t)
if err != nil {
return nil, err
}
f.factories[t] = ff
}
return f, nil
}
func (f *Factory) getFactoryOfType(factoryType string) (storage.Factory, error) {
switch factoryType {
case cassandraStorageType:
return cassandra.NewFactory(), nil
case elasticsearchStorageType, opensearchStorageType:
return es.NewFactory(), nil
case memoryStorageType:
return memory.NewFactory(), nil
case kafkaStorageType:
return kafka.NewFactory(), nil
case badgerStorageType:
return badger.NewFactory(), nil
case grpcPluginStorageType:
return grpc.NewFactory(), nil
default:
return nil, fmt.Errorf("unknown storage type %s. Valid types are %v", factoryType, AllStorageTypes)
}
}
// Initialize implements storage.Factory.
func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) error {
f.metricsFactory = metricsFactory
for _, factory := range f.factories {
if err := factory.Initialize(metricsFactory, logger); err != nil {
return err
}
}
f.publishOpts()
return nil
}
// CreateSpanReader implements storage.Factory.
func (f *Factory) CreateSpanReader() (spanstore.Reader, error) {
factory, ok := f.factories[f.SpanReaderType]
if !ok {
return nil, fmt.Errorf("no %s backend registered for span store", f.SpanReaderType)
}
return factory.CreateSpanReader()
}
// CreateSpanWriter implements storage.Factory.
func (f *Factory) CreateSpanWriter() (spanstore.Writer, error) {
var writers []spanstore.Writer
for _, storageType := range f.SpanWriterTypes {
factory, ok := f.factories[storageType]
if !ok {
return nil, fmt.Errorf("no %s backend registered for span store", storageType)
}
writer, err := factory.CreateSpanWriter()
if err != nil {
return nil, err
}
writers = append(writers, writer)
}
var spanWriter spanstore.Writer
if len(f.SpanWriterTypes) == 1 {
spanWriter = writers[0]
} else {
spanWriter = spanstore.NewCompositeWriter(writers...)
}
// Turn off DownsamplingWriter entirely if ratio == defaultDownsamplingRatio.
if f.DownsamplingRatio == defaultDownsamplingRatio {
return spanWriter, nil
}
return spanstore.NewDownsamplingWriter(spanWriter, spanstore.DownsamplingOptions{
Ratio: f.DownsamplingRatio,
HashSalt: f.DownsamplingHashSalt,
MetricsFactory: f.metricsFactory.Namespace(metrics.NSOptions{Name: "downsampling_writer"}),
}), nil
}
// CreateSamplingStoreFactory creates a distributedlock.Lock and samplingstore.Store for use with adaptive sampling
func (f *Factory) CreateSamplingStoreFactory() (storage.SamplingStoreFactory, error) {
// if a sampling storage type was specified then use it, otherwise search all factories
// for compatibility
if f.SamplingStorageType != "" {
factory, ok := f.factories[f.SamplingStorageType]
if !ok {
return nil, fmt.Errorf("no %s backend registered for sampling store", f.SamplingStorageType)
}
ss, ok := factory.(storage.SamplingStoreFactory)
if !ok {
return nil, fmt.Errorf("storage factory of type %s does not support sampling store", f.SamplingStorageType)
}
return ss, nil
}
for _, factory := range f.factories {
ss, ok := factory.(storage.SamplingStoreFactory)
if ok {
return ss, nil
}
}
// returning nothing is valid here. it's quite possible that the user has no backend that can support adaptive sampling
// this is fine as long as adaptive sampling is also not configured
return nil, nil
}
// CreateDependencyReader implements storage.Factory
func (f *Factory) CreateDependencyReader() (dependencystore.Reader, error) {
factory, ok := f.factories[f.DependenciesStorageType]
if !ok {
return nil, fmt.Errorf("no %s backend registered for span store", f.DependenciesStorageType)
}
return factory.CreateDependencyReader()
}
// AddFlags implements plugin.Configurable
func (f *Factory) AddFlags(flagSet *flag.FlagSet) {
for _, factory := range f.factories {
if conf, ok := factory.(plugin.Configurable); ok {
conf.AddFlags(flagSet)
}
}
}
// AddPipelineFlags adds all the standard flags as well as the downsampling
// flags. This is intended to be used in Jaeger pipeline services such as
// the collector or ingester.
func (f *Factory) AddPipelineFlags(flagSet *flag.FlagSet) {
f.AddFlags(flagSet)
f.addDownsamplingFlags(flagSet)
}
// addDownsamplingFlags add flags for Downsampling params
func (f *Factory) addDownsamplingFlags(flagSet *flag.FlagSet) {
f.downsamplingFlagsAdded = true
flagSet.Float64(
downsamplingRatio,
defaultDownsamplingRatio,
"Ratio of spans passed to storage after downsampling (between 0 and 1), e.g ratio = 0.3 means we are keeping 30% of spans and dropping 70% of spans; ratio = 1.0 disables downsampling.",
)
flagSet.String(
downsamplingHashSalt,
defaultDownsamplingHashSalt,
"Salt used when hashing trace id for downsampling.",
)
}
// InitFromViper implements plugin.Configurable
func (f *Factory) InitFromViper(v *viper.Viper, logger *zap.Logger) {
for _, factory := range f.factories {
if conf, ok := factory.(plugin.Configurable); ok {
conf.InitFromViper(v, logger)
}
}
f.initDownsamplingFromViper(v)
}
func (f *Factory) initDownsamplingFromViper(v *viper.Viper) {
// if the downsampling flag isn't set then this component used the standard "AddFlags" method
// and has no use for downsampling. the default settings effectively disable downsampling
if !f.downsamplingFlagsAdded {
f.FactoryConfig.DownsamplingRatio = defaultDownsamplingRatio
f.FactoryConfig.DownsamplingHashSalt = defaultDownsamplingHashSalt
return
}
f.FactoryConfig.DownsamplingRatio = v.GetFloat64(downsamplingRatio)
if f.FactoryConfig.DownsamplingRatio < 0 || f.FactoryConfig.DownsamplingRatio > 1 {
// Values not in the range of 0 ~ 1.0 will be set to default.
f.FactoryConfig.DownsamplingRatio = 1.0
}
f.FactoryConfig.DownsamplingHashSalt = v.GetString(downsamplingHashSalt)
}
// CreateArchiveSpanReader implements storage.ArchiveFactory
func (f *Factory) CreateArchiveSpanReader() (spanstore.Reader, error) {
factory, ok := f.factories[f.SpanReaderType]
if !ok {
return nil, fmt.Errorf("no %s backend registered for span store", f.SpanReaderType)
}
archive, ok := factory.(storage.ArchiveFactory)
if !ok {
return nil, storage.ErrArchiveStorageNotSupported
}
return archive.CreateArchiveSpanReader()
}
// CreateArchiveSpanWriter implements storage.ArchiveFactory
func (f *Factory) CreateArchiveSpanWriter() (spanstore.Writer, error) {
factory, ok := f.factories[f.SpanWriterTypes[0]]
if !ok {
return nil, fmt.Errorf("no %s backend registered for span store", f.SpanWriterTypes[0])
}
archive, ok := factory.(storage.ArchiveFactory)
if !ok {
return nil, storage.ErrArchiveStorageNotSupported
}
return archive.CreateArchiveSpanWriter()
}
var _ io.Closer = (*Factory)(nil)
// Close closes the resources held by the factory
func (f *Factory) Close() error {
var errs []error
for _, storageType := range f.SpanWriterTypes {
if factory, ok := f.factories[storageType]; ok {
if closer, ok := factory.(io.Closer); ok {
err := closer.Close()
if err != nil {
errs = append(errs, err)
}
}
}
}
return errors.Join(errs...)
}
func (f *Factory) publishOpts() {
internalFactory := f.metricsFactory.Namespace(metrics.NSOptions{Name: "internal"})
internalFactory.Gauge(metrics.Options{Name: downsamplingRatio}).
Update(int64(f.FactoryConfig.DownsamplingRatio))
internalFactory.Gauge(metrics.Options{Name: spanStorageType + "-" + f.SpanReaderType}).
Update(1)
}