-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for composite sampling policy to the tailsampler (#4958)
Is your feature request related to a problem? Please describe. #1410 Design Doc - Added support for composite policy in tailsampling processor. This would help in grouping sampling policies and rate limiting them. https://docs.google.com/document/d/10wpIv3TtXgOik05smHm3nYeBX48Bj76TCMxPy8e1NZw/edit#heading=h.ecy5l2puwtp4 This is a split. Refer PR open-telemetry/opentelemetry-collector#1894 (comment) Due to EasyCLA issue opening a new PR: #4396 Link to tracking Issue: 1306
- Loading branch information
Showing
11 changed files
with
819 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright The OpenTelemetry 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 tailsamplingprocessor | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/sampling" | ||
) | ||
|
||
func getNewCompositePolicy(logger *zap.Logger, config CompositeCfg) (sampling.PolicyEvaluator, error) { | ||
var subPolicyEvalParams []sampling.SubPolicyEvalParams | ||
rateAllocationsMap := getRateAllocationMap(config) | ||
for i := range config.SubPolicyCfg { | ||
policyCfg := config.SubPolicyCfg[i] | ||
policy, _ := getSubPolicyEvaluator(logger, &policyCfg) | ||
|
||
evalParams := sampling.SubPolicyEvalParams{ | ||
Evaluator: policy, | ||
MaxSpansPerSecond: int64(rateAllocationsMap[policyCfg.Name]), | ||
} | ||
subPolicyEvalParams = append(subPolicyEvalParams, evalParams) | ||
} | ||
return sampling.NewComposite(logger, config.MaxTotalSpansPerSecond, subPolicyEvalParams, sampling.MonotonicClock{}), nil | ||
} | ||
|
||
// Apply rate allocations to the sub-policies | ||
func getRateAllocationMap(config CompositeCfg) map[string]float64 { | ||
rateAllocationsMap := make(map[string]float64) | ||
maxTotalSPS := float64(config.MaxTotalSpansPerSecond) | ||
// Default SPS determined by equally diving number of sub policies | ||
defaultSPS := maxTotalSPS / float64(len(config.SubPolicyCfg)) | ||
for _, rAlloc := range config.RateAllocation { | ||
if rAlloc.Percent > 0 { | ||
rateAllocationsMap[rAlloc.Policy] = (float64(rAlloc.Percent) / 100) * maxTotalSPS | ||
} else { | ||
rateAllocationsMap[rAlloc.Policy] = defaultSPS | ||
} | ||
} | ||
return rateAllocationsMap | ||
} | ||
|
||
// Return instance of composite sub-policy | ||
func getSubPolicyEvaluator(logger *zap.Logger, cfg *SubPolicyCfg) (sampling.PolicyEvaluator, error) { | ||
switch cfg.Type { | ||
case AlwaysSample: | ||
return sampling.NewAlwaysSample(logger), nil | ||
case NumericAttribute: | ||
nafCfg := cfg.NumericAttributeCfg | ||
return sampling.NewNumericAttributeFilter(logger, nafCfg.Key, nafCfg.MinValue, nafCfg.MaxValue), nil | ||
case StringAttribute: | ||
safCfg := cfg.StringAttributeCfg | ||
return sampling.NewStringAttributeFilter(logger, safCfg.Key, safCfg.Values, safCfg.EnabledRegexMatching, safCfg.CacheMaxSize, safCfg.InvertMatch), nil | ||
case RateLimiting: | ||
rlfCfg := cfg.RateLimitingCfg | ||
return sampling.NewRateLimiting(logger, rlfCfg.SpansPerSecond), nil | ||
default: | ||
return nil, fmt.Errorf("unknown sampling policy type %s", cfg.Type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright The OpenTelemetry 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 tailsamplingprocessor | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/config" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestCompositeHelper(t *testing.T) { | ||
cfg := &Config{ | ||
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)), | ||
DecisionWait: 10 * time.Second, | ||
NumTraces: 100, | ||
ExpectedNewTracesPerSec: 10, | ||
PolicyCfgs: []PolicyCfg{ | ||
{ | ||
Name: "composite-policy-1", | ||
Type: Composite, | ||
CompositeCfg: CompositeCfg{ | ||
MaxTotalSpansPerSecond: 1000, | ||
PolicyOrder: []string{"test-composite-policy-1", "test-composite-policy-2", "test-composite-policy-3", "test-composite-policy-4", "test-composite-policy-5"}, | ||
SubPolicyCfg: []SubPolicyCfg{ | ||
{ | ||
Name: "test-composite-policy-1", | ||
Type: NumericAttribute, | ||
NumericAttributeCfg: NumericAttributeCfg{Key: "key1", MinValue: 50, MaxValue: 100}, | ||
}, | ||
{ | ||
Name: "test-composite-policy-2", | ||
Type: StringAttribute, | ||
StringAttributeCfg: StringAttributeCfg{Key: "key2", Values: []string{"value1", "value2"}}, | ||
}, | ||
{ | ||
Name: "test-composite-policy-3", | ||
Type: RateLimiting, | ||
RateLimitingCfg: RateLimitingCfg{SpansPerSecond: 10}, | ||
}, | ||
{ | ||
Name: "test-composite-policy-4", | ||
Type: AlwaysSample, | ||
}, | ||
{ | ||
Name: "test-composite-policy-5", | ||
}, | ||
}, | ||
RateAllocation: []RateAllocationCfg{ | ||
{ | ||
Policy: "test-composite-policy-1", | ||
Percent: 50, | ||
}, | ||
{ | ||
Policy: "test-composite-policy-2", | ||
Percent: 25, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
rlfCfg := cfg.PolicyCfgs[0].CompositeCfg | ||
composite, e := getNewCompositePolicy(zap.NewNop(), rlfCfg) | ||
require.NotNil(t, composite) | ||
require.NotNil(t, cfg.ProcessorSettings) | ||
require.Equal(t, 10*time.Second, cfg.DecisionWait) | ||
require.Equal(t, uint64(100), cfg.NumTraces) | ||
require.Equal(t, uint64(10), cfg.ExpectedNewTracesPerSec) | ||
require.NoError(t, e) | ||
// TBD add more assertions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.