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

Commit

Permalink
Add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao Wang committed Jun 23, 2017
1 parent 977ac1b commit 2884ab9
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
45 changes: 45 additions & 0 deletions matcher/cache/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cache

import (
"time"

"github.com/m3db/m3metrics/matcher"
"github.com/m3db/m3x/clock"
"github.com/m3db/m3x/instrument"
)

// Configuration is config used to create a matcher.Cache.
type Configuration struct {
Capacity int `yaml:"capacity"`
FreshDuration time.Duration `yaml:"freshDuration"`
StutterDuration time.Duration `yaml:"stutterDuration"`
EvictionBatchSize int `yaml:"evictionBatchSize"`
DeletionBatchSize int `yaml:"deletionBatchSize"`
}

// NewCache creates a matcher.Cache.
func (cfg *Configuration) NewCache(
clockOpts clock.Options,
instrumentOpts instrument.Options,
) matcher.Cache {
opts := NewOptions().
SetClockOptions(clockOpts).
SetInstrumentOptions(instrumentOpts)
if cfg.Capacity != 0 {
opts = opts.SetCapacity(cfg.Capacity)
}
if cfg.FreshDuration != 0 {
opts = opts.SetFreshDuration(cfg.FreshDuration)
}
if cfg.StutterDuration != 0 {
opts = opts.SetStutterDuration(cfg.StutterDuration)
}
if cfg.EvictionBatchSize != 0 {
opts = opts.SetEvictionBatchSize(cfg.EvictionBatchSize)
}
if cfg.DeletionBatchSize != 0 {
opts = opts.SetDeletionBatchSize(cfg.DeletionBatchSize)
}

return NewCache(opts)
}
90 changes: 90 additions & 0 deletions matcher/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package matcher

import (
"fmt"
"time"

"github.com/m3db/m3cluster/client"
"github.com/m3db/m3metrics/metric/id"
"github.com/m3db/m3metrics/metric/id/m3"
"github.com/m3db/m3metrics/rules"
"github.com/m3db/m3x/clock"
"github.com/m3db/m3x/instrument"
)

// Configuration is config used to create a matcher.Matcher.
type Configuration struct {
TagsFilter m3.TagsFilterConfiguration `yaml:"tagsFilter" validate:"nonzero"`
InitWatchTimeout time.Duration `yaml:"initWatchTimeout"`
RulesKVNamespace string `yaml:"rulesKVNamespace" validate:"nonzero"`
NamespacesKey string `yaml:"namespacesKey" validate:"nonzero"`
RuleSetKeyFmt string `yaml:"ruleSetKeyFmt" validate:"nonzero"`
NamespaceTag string `yaml:"namespaceTag" validate:"nonzero"`
DefaultNamespace string `yaml:"defaultNamespace" validate:"nonzero"`
}

// NewMatcher creates a Matcher.
func (cfg *Configuration) NewMatcher(
cache Cache,
kvCluster client.Client,
iter id.SortedTagIterator,
clockOpts clock.Options,
instrumentOpts instrument.Options,
) (Matcher, error) {
isRollupIDFn := func(name []byte, tags []byte) bool {
return m3.IsRollupID(name, tags, iter)
}
// Configure ruleset options.
tagsFilterOpts := cfg.TagsFilter.NewTagsFilterOptions(instrumentOpts)
ruleSetOpts := rules.NewOptions().
SetTagsFilterOptions(tagsFilterOpts).
SetNewRollupIDFn(m3.NewRollupID).
SetIsRollupIDFn(isRollupIDFn)

// Configure rules kv store.
rulesStore, err := kvCluster.Store(cfg.RulesKVNamespace)
if err != nil {
return nil, err
}

// Configure ruleset key function.
ruleSetKeyFn := func(namespace []byte) string {
return fmt.Sprintf(cfg.RuleSetKeyFmt, namespace)
}

opts := NewOptions().
SetClockOptions(clockOpts).
SetInstrumentOptions(instrumentOpts).
SetRuleSetOptions(ruleSetOpts).
SetKVStore(rulesStore).
SetNamespacesKey(cfg.NamespacesKey).
SetRuleSetKeyFn(ruleSetKeyFn).
SetNamespaceTag([]byte(cfg.NamespaceTag)).
SetDefaultNamespace([]byte(cfg.DefaultNamespace))

if cfg.InitWatchTimeout != 0 {
opts = opts.SetInitWatchTimeout(cfg.InitWatchTimeout)
}

return NewMatcher(cache, opts)
}
30 changes: 30 additions & 0 deletions metric/id/m3/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import (
"errors"
"sort"

"github.com/m3db/m3metrics/filters"
"github.com/m3db/m3metrics/metric/id"
"github.com/m3db/m3x/instrument"
"github.com/m3db/m3x/pool"
)

const (
Expand Down Expand Up @@ -214,3 +217,30 @@ func (it *sortedTagIterator) Close() {
it.pool.Put(it)
}
}

// TagsFilterConfiguration is config used to create TagsFilterOptions.
type TagsFilterConfiguration struct {
NameTagKey string `yaml:"nameTagKey" validate:"nonzero"`
SortedTagIteratorPool pool.ObjectPoolConfiguration `yaml:"sortedTagIteratorPool"`
}

// NewTagsFilterOptions creates TagsFilterOptions.
func (cfg *TagsFilterConfiguration) NewTagsFilterOptions(instrumentOpts instrument.Options) filters.TagsFilterOptions {
scope := instrumentOpts.MetricsScope().SubScope("sorted-tag-iterator-pool")
poolOpts := cfg.SortedTagIteratorPool.NewObjectPoolOptions(instrumentOpts.SetMetricsScope(scope))
sortedTagIteratorPool := id.NewSortedTagIteratorPool(poolOpts)
sortedTagIteratorPool.Init(func() id.SortedTagIterator {
return NewPooledSortedTagIterator(nil, sortedTagIteratorPool)
})
sortedTagIteratorFn := func(tagPairs []byte) id.SortedTagIterator {
it := sortedTagIteratorPool.Get()
it.Reset(tagPairs)
return it
}
tagsFilterOptions := filters.TagsFilterOptions{
NameTagKey: []byte(cfg.NameTagKey),
NameAndTagsFn: NameAndTags,
SortedTagIteratorFn: sortedTagIteratorFn,
}
return tagsFilterOptions
}

0 comments on commit 2884ab9

Please sign in to comment.