Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Make override fields optional (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Jun 27, 2018
1 parent ac91954 commit 89fe26f
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 14 deletions.
40 changes: 29 additions & 11 deletions rules/validator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,46 @@ func (c Configuration) NewValidator(
if err != nil {
return nil, err
}
opts := c.newValidatorOptions(nsValidator)
return NewValidator(opts), nil
}

func (c Configuration) newValidatorOptions(
nsValidator namespace.Validator,
) Options {
opts := NewOptions().
SetNamespaceValidator(nsValidator).
SetRequiredRollupTags(c.RequiredRollupTags).
SetMetricTypesFn(c.MetricTypes.NewMetricTypesFn()).
SetDefaultAllowedStoragePolicies(c.Policies.DefaultAllowed.StoragePolicies).
SetDefaultAllowedFirstLevelAggregationTypes(c.Policies.DefaultAllowed.FirstLevelAggregationTypes).
SetDefaultAllowedNonFirstLevelAggregationTypes(c.Policies.DefaultAllowed.NonFirstLevelAggregationTypes).
SetTagNameInvalidChars(toRunes(c.TagNameInvalidChars)).
SetMetricNameInvalidChars(toRunes(c.MetricNameInvalidChars))
if c.Policies.DefaultAllowed.StoragePolicies != nil {
opts = opts.SetDefaultAllowedStoragePolicies(*c.Policies.DefaultAllowed.StoragePolicies)
}
if c.Policies.DefaultAllowed.FirstLevelAggregationTypes != nil {
opts = opts.SetDefaultAllowedFirstLevelAggregationTypes(*c.Policies.DefaultAllowed.FirstLevelAggregationTypes)
}
if c.Policies.DefaultAllowed.NonFirstLevelAggregationTypes != nil {
opts = opts.SetDefaultAllowedNonFirstLevelAggregationTypes(*c.Policies.DefaultAllowed.NonFirstLevelAggregationTypes)
}
for _, override := range c.Policies.Overrides {
opts = opts.
SetAllowedStoragePoliciesFor(override.Type, override.Allowed.StoragePolicies).
SetAllowedFirstLevelAggregationTypesFor(override.Type, override.Allowed.FirstLevelAggregationTypes).
SetAllowedNonFirstLevelAggregationTypesFor(override.Type, override.Allowed.NonFirstLevelAggregationTypes)
if override.Allowed.StoragePolicies != nil {
opts = opts.SetAllowedStoragePoliciesFor(override.Type, *override.Allowed.StoragePolicies)
}
if override.Allowed.FirstLevelAggregationTypes != nil {
opts = opts.SetAllowedFirstLevelAggregationTypesFor(override.Type, *override.Allowed.FirstLevelAggregationTypes)
}
if override.Allowed.NonFirstLevelAggregationTypes != nil {
opts = opts.SetAllowedNonFirstLevelAggregationTypesFor(override.Type, *override.Allowed.NonFirstLevelAggregationTypes)
}
}
if c.MaxTransformationDerivativeOrder != nil {
opts = opts.SetMaxTransformationDerivativeOrder(*c.MaxTransformationDerivativeOrder)
}
if c.MaxRollupLevels != nil {
opts = opts.SetMaxRollupLevels(*c.MaxRollupLevels)
}
return NewValidator(opts), nil
return opts
}

type namespaceValidatorConfiguration struct {
Expand Down Expand Up @@ -152,9 +170,9 @@ type policiesOverrideConfiguration struct {

// policiesConfiguration is the configuration for storage policies and aggregation types.
type policiesConfiguration struct {
StoragePolicies []policy.StoragePolicy `yaml:"storagePolicies"`
FirstLevelAggregationTypes []aggregation.Type `yaml:"firstLevelAggregationTypes"`
NonFirstLevelAggregationTypes []aggregation.Type `yaml:"nonFirstLevelAggregationTypes"`
StoragePolicies *[]policy.StoragePolicy `yaml:"storagePolicies"`
FirstLevelAggregationTypes *[]aggregation.Type `yaml:"firstLevelAggregationTypes"`
NonFirstLevelAggregationTypes *[]aggregation.Type `yaml:"nonFirstLevelAggregationTypes"`
}

func toRunes(s string) []rune {
Expand Down
159 changes: 156 additions & 3 deletions rules/validator/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"github.com/m3db/m3cluster/client"
"github.com/m3db/m3cluster/kv"
"github.com/m3db/m3cluster/kv/mem"
"github.com/m3db/m3metrics/aggregation"
"github.com/m3db/m3metrics/filters"
"github.com/m3db/m3metrics/metric"
"github.com/m3db/m3metrics/policy"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -82,10 +84,161 @@ kv:
require.NoError(t, err)
}

func TestNamespaceValidatorConfigurationStatic(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
func TestNewValidator(t *testing.T) {
cfgStr := `
namespace:
static:
validationResult: valid
requiredRollupTags:
- tag1
- tag2
maxTransformationDerivativeOrder: 2
maxRollupLevels: 1
metricTypes:
typeTag: type
allowed:
- counter
- timer
- gauge
policies:
defaultAllowed:
storagePolicies:
- 10s:2d
- 1m:40d
nonFirstLevelAggregationTypes:
- Sum
- Last
overrides:
- type: counter
allowed:
firstLevelAggregationTypes:
- Sum
- type: timer
allowed:
storagePolicies:
- 10s:2d
firstLevelAggregationTypes:
- P50
- P9999
- type: gauge
allowed:
firstLevelAggregationTypes:
- Last
`

var cfg Configuration
require.NoError(t, yaml.Unmarshal([]byte(cfgStr), &cfg))
opts := cfg.newValidatorOptions(nil)

inputs := []struct {
metricType metric.Type
allowedStoragePolicies policy.StoragePolicies
disallowedStoragePolicies policy.StoragePolicies
allowedFirstLevelAggTypes aggregation.Types
disallowedFirstLevelAggTypes aggregation.Types
allowedNonFirstLevelAggTypes aggregation.Types
disallowedNonFirstLevelAggTypes aggregation.Types
}{
{
metricType: metric.CounterType,
allowedStoragePolicies: policy.StoragePolicies{
policy.MustParseStoragePolicy("10s:2d"),
policy.MustParseStoragePolicy("1m:40d"),
},
disallowedStoragePolicies: policy.StoragePolicies{
policy.MustParseStoragePolicy("1m:2d"),
policy.MustParseStoragePolicy("10s:40d"),
},
allowedFirstLevelAggTypes: aggregation.Types{
aggregation.Sum,
},
disallowedFirstLevelAggTypes: aggregation.Types{
aggregation.Last,
},
allowedNonFirstLevelAggTypes: aggregation.Types{
aggregation.Sum,
aggregation.Last,
},
disallowedNonFirstLevelAggTypes: aggregation.Types{
aggregation.Min,
aggregation.P99,
},
},
{
metricType: metric.TimerType,
allowedStoragePolicies: policy.StoragePolicies{
policy.MustParseStoragePolicy("10s:2d"),
},
disallowedStoragePolicies: policy.StoragePolicies{
policy.MustParseStoragePolicy("1m:2d"),
policy.MustParseStoragePolicy("1m:40d"),
},
allowedFirstLevelAggTypes: aggregation.Types{
aggregation.P50,
aggregation.P9999,
},
disallowedFirstLevelAggTypes: aggregation.Types{
aggregation.Last,
},
allowedNonFirstLevelAggTypes: aggregation.Types{
aggregation.Sum,
aggregation.Last,
},
disallowedNonFirstLevelAggTypes: aggregation.Types{
aggregation.Min,
aggregation.P99,
},
},
{
metricType: metric.GaugeType,
allowedStoragePolicies: policy.StoragePolicies{
policy.MustParseStoragePolicy("10s:2d"),
policy.MustParseStoragePolicy("1m:40d"),
},
disallowedStoragePolicies: policy.StoragePolicies{
policy.MustParseStoragePolicy("1m:2d"),
policy.MustParseStoragePolicy("10s:40d"),
},
allowedFirstLevelAggTypes: aggregation.Types{
aggregation.Last,
},
disallowedFirstLevelAggTypes: aggregation.Types{
aggregation.Sum,
},
allowedNonFirstLevelAggTypes: aggregation.Types{
aggregation.Sum,
aggregation.Last,
},
disallowedNonFirstLevelAggTypes: aggregation.Types{
aggregation.Min,
aggregation.P99,
},
},
}

for _, input := range inputs {
for _, storagePolicy := range input.allowedStoragePolicies {
require.True(t, opts.IsAllowedStoragePolicyFor(input.metricType, storagePolicy))
}
for _, storagePolicy := range input.disallowedStoragePolicies {
require.False(t, opts.IsAllowedStoragePolicyFor(input.metricType, storagePolicy))
}
for _, aggregationType := range input.allowedFirstLevelAggTypes {
require.True(t, opts.IsAllowedFirstLevelAggregationTypeFor(input.metricType, aggregationType))
}
for _, aggregationType := range input.disallowedFirstLevelAggTypes {
require.False(t, opts.IsAllowedFirstLevelAggregationTypeFor(input.metricType, aggregationType))
}
for _, aggregationType := range input.allowedNonFirstLevelAggTypes {
require.True(t, opts.IsAllowedNonFirstLevelAggregationTypeFor(input.metricType, aggregationType))
}
for _, aggregationType := range input.disallowedNonFirstLevelAggTypes {
require.False(t, opts.IsAllowedNonFirstLevelAggregationTypeFor(input.metricType, aggregationType))
}
}
}

func TestNamespaceValidatorConfigurationStatic(t *testing.T) {
cfgStr := `
static:
validationResult: valid
Expand Down

0 comments on commit 89fe26f

Please sign in to comment.