|
| 1 | +// Copyright (c) 2018 The Jaeger Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package strategystore |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/spf13/viper" |
| 22 | + "github.com/uber/jaeger-lib/metrics" |
| 23 | + "go.uber.org/zap" |
| 24 | + |
| 25 | + "github.com/jaegertracing/jaeger/cmd/collector/app/sampling/strategystore" |
| 26 | + "github.com/jaegertracing/jaeger/plugin" |
| 27 | + "github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + staticStrategyStoreType = "static" |
| 32 | + adaptiveStrategyStoreType = "adaptive" |
| 33 | +) |
| 34 | + |
| 35 | +var allSamplingTypes = []string{staticStrategyStoreType} // TODO support adaptive |
| 36 | + |
| 37 | +// Factory implements strategystore.Factory interface as a meta-factory for strategy storage components. |
| 38 | +type Factory struct { |
| 39 | + FactoryConfig |
| 40 | + |
| 41 | + factories map[string]strategystore.Factory |
| 42 | +} |
| 43 | + |
| 44 | +// NewFactory creates the meta-factory. |
| 45 | +func NewFactory(config FactoryConfig) (*Factory, error) { |
| 46 | + f := &Factory{FactoryConfig: config} |
| 47 | + uniqueTypes := map[string]struct{}{ |
| 48 | + f.StrategyStoreType: {}, |
| 49 | + } |
| 50 | + f.factories = make(map[string]strategystore.Factory) |
| 51 | + for t := range uniqueTypes { |
| 52 | + ff, err := f.getFactoryOfType(t) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + f.factories[t] = ff |
| 57 | + } |
| 58 | + return f, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (f *Factory) getFactoryOfType(factoryType string) (strategystore.Factory, error) { |
| 62 | + switch factoryType { |
| 63 | + case staticStrategyStoreType: |
| 64 | + return static.NewFactory(), nil |
| 65 | + default: |
| 66 | + return nil, fmt.Errorf("Unknown sampling strategy store type %s. Valid types are %v", factoryType, allSamplingTypes) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// AddFlags implements plugin.Configurable |
| 71 | +func (f *Factory) AddFlags(flagSet *flag.FlagSet) { |
| 72 | + for _, factory := range f.factories { |
| 73 | + if conf, ok := factory.(plugin.Configurable); ok { |
| 74 | + conf.AddFlags(flagSet) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// InitFromViper implements plugin.Configurable |
| 80 | +func (f *Factory) InitFromViper(v *viper.Viper) { |
| 81 | + for _, factory := range f.factories { |
| 82 | + if conf, ok := factory.(plugin.Configurable); ok { |
| 83 | + conf.InitFromViper(v) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Initialize implements strategystore.Factory |
| 89 | +func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) error { |
| 90 | + for _, factory := range f.factories { |
| 91 | + if err := factory.Initialize(metricsFactory, logger); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// CreateStrategyStore implements strategystore.Factory |
| 99 | +func (f *Factory) CreateStrategyStore() (strategystore.StrategyStore, error) { |
| 100 | + factory, ok := f.factories[f.StrategyStoreType] |
| 101 | + if !ok { |
| 102 | + return nil, fmt.Errorf("No %s strategy store registered", f.StrategyStoreType) |
| 103 | + } |
| 104 | + return factory.CreateStrategyStore() |
| 105 | +} |
0 commit comments