-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add sampling handler and sampling strategy store #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
a5b3e79
c34fa8b
78a3c63
699baad
afb6146
f5268ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 sampling | ||
|
|
||
| import ( | ||
| "github.com/uber/tchannel-go/thrift" | ||
|
|
||
| "github.com/jaegertracing/jaeger/pkg/sampling/strategystore" | ||
| "github.com/jaegertracing/jaeger/thrift-gen/sampling" | ||
| ) | ||
|
|
||
| // Handler returns sampling strategies for specified services | ||
| type Handler interface { | ||
| // GetSamplingStrategy returns recommended sampling strategy for a given service name. | ||
| GetSamplingStrategy(ctx thrift.Context, serviceName string) (*sampling.SamplingStrategyResponse, error) | ||
| } | ||
|
|
||
| type handler struct { | ||
| store strategystore.StrategyStore | ||
| } | ||
|
|
||
| // NewHandler creates a handler that controls sampling strategies for services. | ||
| func NewHandler(store strategystore.StrategyStore) Handler { | ||
| return &handler{ | ||
| store: store, | ||
| } | ||
| } | ||
|
|
||
| // GetSamplingStrategy returns allowed sampling strategy for a given service name. | ||
| func (h *handler) GetSamplingStrategy(ctx thrift.Context, serviceName string) (*sampling.SamplingStrategyResponse, error) { | ||
| return h.store.GetSamplingStrategy(serviceName) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 sampling | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/jaegertracing/jaeger/thrift-gen/sampling" | ||
| ) | ||
|
|
||
| func TestHandler(t *testing.T) { | ||
| handler := NewHandler(mockStore{}) | ||
| _, err := handler.GetSamplingStrategy(nil, "") | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| type mockStore struct{} | ||
|
|
||
| func (s mockStore) GetSamplingStrategy(serviceName string) (*sampling.SamplingStrategyResponse, error) { | ||
| return nil, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 strategystore | ||
|
|
||
| import ( | ||
| "github.com/jaegertracing/jaeger/thrift-gen/sampling" | ||
| ) | ||
|
|
||
| const ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this needed in the "interface" package? Could it not be placed in the static impl? |
||
| // SamplerTypeProbabilistic is the type of sampler that samples traces | ||
| // with a certain fixed probability. | ||
| SamplerTypeProbabilistic = "probabilistic" | ||
|
|
||
| // SamplerTypeRateLimiting is the type of sampler that samples | ||
| // only up to a fixed number of traces per second. | ||
| SamplerTypeRateLimiting = "ratelimiting" | ||
|
|
||
| // DefaultSamplingProbability is the default sampling probability the | ||
| // Strategy Store will use if none is provided. | ||
| DefaultSamplingProbability = 0.001 | ||
| ) | ||
|
|
||
| var ( | ||
| // DefaultStrategy is the default sampling strategy the Strategy Store will return | ||
| // if none is provided. | ||
| DefaultStrategy = sampling.SamplingStrategyResponse{ | ||
| StrategyType: sampling.SamplingStrategyType_PROBABILISTIC, | ||
| ProbabilisticSampling: &sampling.ProbabilisticSamplingStrategy{ | ||
| SamplingRate: DefaultSamplingProbability, | ||
| }, | ||
| } | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 strategystore | ||
|
|
||
| import ( | ||
| "github.com/uber/jaeger-lib/metrics" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // Factory defines an interface for a factory that can create implementations of different strategy storage components. | ||
| // Implementations are also encouraged to implement plugin.Configurable interface. | ||
| // | ||
| // See also | ||
| // | ||
| // plugin.Configurable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Configurable also has AddFlags(flagSet *flag.FlagSet). Do you actually need to require Configurable methods in the Factory interface?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't have to be but it makes life easier. |
||
| type Factory interface { | ||
| // Initialize performs internal initialization of the factory. | ||
| Initialize(metricsFactory metrics.Factory, logger *zap.Logger) error | ||
|
|
||
| // CreateStrategyStore initializes the StrategyStore and returns it. | ||
| CreateStrategyStore() (StrategyStore, error) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 strategystore | ||
|
|
||
| import ( | ||
| "github.com/jaegertracing/jaeger/thrift-gen/sampling" | ||
| ) | ||
|
|
||
| // StrategyStore keeps track of service specific sampling strategies. | ||
| type StrategyStore interface { | ||
| // GetSamplingStrategy retrieves the sampling strategy for the specified service. | ||
| GetSamplingStrategy(serviceName string) (*sampling.SamplingStrategyResponse, error) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 strategystore | ||
|
|
||
| // Strategy defines a sampling strategy. Type can be "probabilistic" or "ratelimiting" | ||
| // and Param will represent "sampling probability" and "max traces per second" respectively. | ||
| type Strategy struct { | ||
|
||
| Type string `json:"type"` | ||
| Param float64 `json:"param"` | ||
| } | ||
|
|
||
| // ServiceStrategy defines a service specific sampling strategy. | ||
| type ServiceStrategy struct { | ||
| Service string `json:"service"` | ||
| Strategy | ||
| } | ||
|
|
||
| // Strategies holds a default sampling strategy and service specific sampling strategies. | ||
| type Strategies struct { | ||
| DefaultStrategy *Strategy `json:"default_strategy"` | ||
| ServiceStrategies []*ServiceStrategy `json:"service_strategies"` | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 strategystore | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/viper" | ||
| "github.com/uber/jaeger-lib/metrics" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/jaegertracing/jaeger/pkg/sampling/strategystore" | ||
| "github.com/jaegertracing/jaeger/plugin" | ||
| "github.com/jaegertracing/jaeger/plugin/sampling/strategystore/static" | ||
| ) | ||
|
|
||
| const ( | ||
| staticStrategyStoreType = "static" | ||
| adaptiveStrategyStoreType = "adaptive" | ||
| ) | ||
|
|
||
| var allSamplingTypes = []string{staticStrategyStoreType} // TODO support adaptive | ||
|
|
||
| // Factory implements strategystore.Factory interface as a meta-factory for strategy storage components. | ||
| type Factory struct { | ||
| FactoryConfig | ||
|
|
||
| factories map[string]strategystore.Factory | ||
| } | ||
|
|
||
| // NewFactory creates the meta-factory. | ||
| func NewFactory(config FactoryConfig) (*Factory, error) { | ||
| f := &Factory{FactoryConfig: config} | ||
| uniqueTypes := map[string]struct{}{ | ||
| f.StrategyStoreType: {}, | ||
| } | ||
| f.factories = make(map[string]strategystore.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) (strategystore.Factory, error) { | ||
| switch factoryType { | ||
| case staticStrategyStoreType: | ||
| return static.NewFactory(), nil | ||
| default: | ||
| return nil, fmt.Errorf("Unknown sampling strategy store type %s. Valid types are %v", factoryType, allSamplingTypes) | ||
| } | ||
| } | ||
|
|
||
| // 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) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // InitFromViper implements plugin.Configurable | ||
| func (f *Factory) InitFromViper(v *viper.Viper) { | ||
| for _, factory := range f.factories { | ||
| if conf, ok := factory.(plugin.Configurable); ok { | ||
| conf.InitFromViper(v) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Initialize implements strategystore.Factory | ||
| func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) error { | ||
| for _, factory := range f.factories { | ||
| if err := factory.Initialize(metricsFactory, logger); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // CreateStrategyStore implements strategystore.Factory | ||
| func (f *Factory) CreateStrategyStore() (strategystore.StrategyStore, error) { | ||
| factory, ok := f.factories[f.StrategyStoreType] | ||
| if !ok { | ||
| return nil, fmt.Errorf("No %s strategy store registered", f.StrategyStoreType) | ||
| } | ||
| return factory.CreateStrategyStore() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) 2018 The Jaeger Authors. | ||
| // | ||
| // 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 strategystore | ||
|
|
||
| import ( | ||
| "os" | ||
| ) | ||
|
|
||
| const ( | ||
| // SamplingTypeEnvVar is the name of the env var that defines the type of sampling strategy store used. | ||
| SamplingTypeEnvVar = "SAMPLING_TYPE" | ||
| ) | ||
|
|
||
| // FactoryConfig tells the Factory what sampling type it needs to create. | ||
| type FactoryConfig struct { | ||
| StrategyStoreType string | ||
| } | ||
|
|
||
| // FactoryConfigFromEnv reads the desired sampling type from the SAMPLING_TYPE environment variable. Allowed values: | ||
| // * `static` - built-in | ||
| // * `adaptive` - built-in // TODO | ||
| func FactoryConfigFromEnv() FactoryConfig { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you planning to add changes to collector.main() here? Right now this function is not used.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to do that in the next PR, this one for API |
||
| strategyStoreType := os.Getenv(SamplingTypeEnvVar) | ||
| if strategyStoreType == "" { | ||
| strategyStoreType = staticStrategyStoreType | ||
| } | ||
| return FactoryConfig{ | ||
| StrategyStoreType: strategyStoreType, | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the
pkgis rather suspicious for this. It is meant for things not directly related to Jaeger. The simplest approach would be to define the storage interface incmd/collector/sampling/strategystore/interface.goThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, should I have the static/adaptive implementations in plugin or should I move the whole thing over to cmd/collector/app?