github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/matcher/options.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package matcher
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/cluster/kv"
    29  	"github.com/m3db/m3/src/cluster/kv/mem"
    30  	"github.com/m3db/m3/src/metrics/matcher/namespace"
    31  	"github.com/m3db/m3/src/metrics/rules"
    32  	"github.com/m3db/m3/src/x/clock"
    33  	"github.com/m3db/m3/src/x/instrument"
    34  )
    35  
    36  const (
    37  	defaultInitWatchTimeout = 10 * time.Second
    38  	defaultNamespacesKey    = "/namespaces"
    39  	defaultRuleSetKeyFormat = "/ruleset/%s"
    40  	defaultMatchRangePast   = time.Duration(math.MaxInt64)
    41  )
    42  
    43  var (
    44  	defaultNamespaceTag     = []byte("namespace")
    45  	defaultDefaultNamespace = []byte("default")
    46  )
    47  
    48  // RuleSetKeyFn generates the ruleset key for a given namespace.
    49  type RuleSetKeyFn func(namespace []byte) string
    50  
    51  // OnNamespaceAddedFn is called when a namespace is added.
    52  type OnNamespaceAddedFn func(namespace []byte, ruleSet RuleSet)
    53  
    54  // OnNamespaceRemovedFn is called when a namespace is removed.
    55  type OnNamespaceRemovedFn func(namespace []byte)
    56  
    57  // OnRuleSetUpdatedFn is called when a ruleset is updated.
    58  type OnRuleSetUpdatedFn func(namespace []byte, ruleSet RuleSet)
    59  
    60  // Options provide a set of options for the matcher.
    61  type Options interface {
    62  	// SetClockOptions sets the clock options.
    63  	SetClockOptions(value clock.Options) Options
    64  
    65  	// ClockOptions returns the clock options.
    66  	ClockOptions() clock.Options
    67  
    68  	// SetInstrumentOptions sets the instrument options.
    69  	SetInstrumentOptions(value instrument.Options) Options
    70  
    71  	// InstrumentOptions returns the instrument options.
    72  	InstrumentOptions() instrument.Options
    73  
    74  	// SetRuleSetOptions sets the ruleset options.
    75  	SetRuleSetOptions(value rules.Options) Options
    76  
    77  	// RuleSetOptions returns the ruleset options.
    78  	RuleSetOptions() rules.Options
    79  
    80  	// SetInitWatchTimeout sets the initial watch timeout.
    81  	SetInitWatchTimeout(value time.Duration) Options
    82  
    83  	// InitWatchTimeout returns the initial watch timeout.
    84  	InitWatchTimeout() time.Duration
    85  
    86  	// SetKVStore sets the kv store.
    87  	SetKVStore(value kv.Store) Options
    88  
    89  	// KVStore returns the kv store.
    90  	KVStore() kv.Store
    91  
    92  	// SetNamespacesKey sets the key for the full list of namespaces.
    93  	SetNamespacesKey(value string) Options
    94  
    95  	// NamespacesKey returns the key for the full list of namespaces.
    96  	NamespacesKey() string
    97  
    98  	// SetRuleSetKeyFn sets the function to generate ruleset keys.
    99  	SetRuleSetKeyFn(value RuleSetKeyFn) Options
   100  
   101  	// RuleSetKeyFn returns the function to generate ruleset keys.
   102  	RuleSetKeyFn() RuleSetKeyFn
   103  
   104  	// SetNamespaceResolver sets the NamespaceResolver.
   105  	SetNamespaceResolver(value namespace.Resolver) Options
   106  
   107  	// NamespaceResolver returns the namespace Resolver.
   108  	NamespaceResolver() namespace.Resolver
   109  
   110  	// SetMatchRangePast sets the limit on the earliest time eligible for rule matching.
   111  	SetMatchRangePast(value time.Duration) Options
   112  
   113  	// MatchRangePast returns the limit on the earliest time eligible for rule matching.
   114  	MatchRangePast() time.Duration
   115  
   116  	// SetOnNamespaceAddedFn sets the function to be called when a namespace is added.
   117  	SetOnNamespaceAddedFn(value OnNamespaceAddedFn) Options
   118  
   119  	// OnNamespaceAddedFn returns the function to be called when a namespace is added.
   120  	OnNamespaceAddedFn() OnNamespaceAddedFn
   121  
   122  	// SetOnNamespaceRemovedFn sets the function to be called when a namespace is removed.
   123  	SetOnNamespaceRemovedFn(value OnNamespaceRemovedFn) Options
   124  
   125  	// OnNamespaceRemovedFn returns the function to be called when a namespace is removed.
   126  	OnNamespaceRemovedFn() OnNamespaceRemovedFn
   127  
   128  	// SetOnRuleSetUpdatedFn sets the function to be called when a ruleset is updated.
   129  	SetOnRuleSetUpdatedFn(value OnRuleSetUpdatedFn) Options
   130  
   131  	// OnRuleSetUpdatedFn returns the function to be called when a ruleset is updated.
   132  	OnRuleSetUpdatedFn() OnRuleSetUpdatedFn
   133  
   134  	// SetRequireNamespaceWatchOnInit sets the flag to ensure matcher is initialized with a loaded namespace watch.
   135  	SetRequireNamespaceWatchOnInit(value bool) Options
   136  
   137  	// RequireNamespaceWatchOnInit returns the flag to ensure matcher is initialized with a loaded namespace watch.
   138  	RequireNamespaceWatchOnInit() bool
   139  
   140  	// InterruptedCh returns the interrupted channel.
   141  	InterruptedCh() <-chan struct{}
   142  
   143  	// SetInterruptedCh sets the interrupted channel.
   144  	SetInterruptedCh(value <-chan struct{}) Options
   145  }
   146  
   147  type options struct {
   148  	clockOpts                   clock.Options
   149  	instrumentOpts              instrument.Options
   150  	ruleSetOpts                 rules.Options
   151  	initWatchTimeout            time.Duration
   152  	kvStore                     kv.Store
   153  	namespacesKey               string
   154  	ruleSetKeyFn                RuleSetKeyFn
   155  	nsResolver                  namespace.Resolver
   156  	matchRangePast              time.Duration
   157  	onNamespaceAddedFn          OnNamespaceAddedFn
   158  	onNamespaceRemovedFn        OnNamespaceRemovedFn
   159  	onRuleSetUpdatedFn          OnRuleSetUpdatedFn
   160  	requireNamespaceWatchOnInit bool
   161  	interruptedCh               <-chan struct{}
   162  }
   163  
   164  // NewOptions creates a new set of options.
   165  func NewOptions() Options {
   166  	return &options{
   167  		clockOpts:        clock.NewOptions(),
   168  		instrumentOpts:   instrument.NewOptions(),
   169  		ruleSetOpts:      rules.NewOptions(),
   170  		initWatchTimeout: defaultInitWatchTimeout,
   171  		kvStore:          mem.NewStore(),
   172  		namespacesKey:    defaultNamespacesKey,
   173  		ruleSetKeyFn:     defaultRuleSetKeyFn,
   174  		nsResolver:       namespace.Default,
   175  		matchRangePast:   defaultMatchRangePast,
   176  	}
   177  }
   178  
   179  func (o *options) SetClockOptions(value clock.Options) Options {
   180  	opts := *o
   181  	opts.clockOpts = value
   182  	return &opts
   183  }
   184  
   185  func (o *options) ClockOptions() clock.Options {
   186  	return o.clockOpts
   187  }
   188  
   189  func (o *options) SetInstrumentOptions(value instrument.Options) Options {
   190  	opts := *o
   191  	opts.instrumentOpts = value
   192  	return &opts
   193  }
   194  
   195  func (o *options) InstrumentOptions() instrument.Options {
   196  	return o.instrumentOpts
   197  }
   198  
   199  func (o *options) SetRuleSetOptions(value rules.Options) Options {
   200  	opts := *o
   201  	opts.ruleSetOpts = value
   202  	return &opts
   203  }
   204  
   205  func (o *options) RuleSetOptions() rules.Options {
   206  	return o.ruleSetOpts
   207  }
   208  
   209  func (o *options) SetInitWatchTimeout(value time.Duration) Options {
   210  	opts := *o
   211  	opts.initWatchTimeout = value
   212  	return &opts
   213  }
   214  
   215  func (o *options) InitWatchTimeout() time.Duration {
   216  	return o.initWatchTimeout
   217  }
   218  
   219  func (o *options) SetKVStore(value kv.Store) Options {
   220  	opts := *o
   221  	opts.kvStore = value
   222  	return &opts
   223  }
   224  
   225  func (o *options) KVStore() kv.Store {
   226  	return o.kvStore
   227  }
   228  
   229  func (o *options) SetNamespacesKey(value string) Options {
   230  	opts := *o
   231  	opts.namespacesKey = value
   232  	return &opts
   233  }
   234  
   235  func (o *options) NamespacesKey() string {
   236  	return o.namespacesKey
   237  }
   238  
   239  func (o *options) SetRuleSetKeyFn(value RuleSetKeyFn) Options {
   240  	opts := *o
   241  	opts.ruleSetKeyFn = value
   242  	return &opts
   243  }
   244  
   245  func (o *options) RuleSetKeyFn() RuleSetKeyFn {
   246  	return o.ruleSetKeyFn
   247  }
   248  
   249  func (o *options) SetNamespaceResolver(value namespace.Resolver) Options {
   250  	opts := *o
   251  	opts.nsResolver = value
   252  	return &opts
   253  }
   254  
   255  func (o *options) NamespaceResolver() namespace.Resolver {
   256  	return o.nsResolver
   257  }
   258  
   259  func (o *options) SetMatchRangePast(value time.Duration) Options {
   260  	opts := *o
   261  	opts.matchRangePast = value
   262  	return &opts
   263  }
   264  
   265  func (o *options) MatchRangePast() time.Duration {
   266  	return o.matchRangePast
   267  }
   268  
   269  func (o *options) SetOnNamespaceAddedFn(value OnNamespaceAddedFn) Options {
   270  	opts := *o
   271  	opts.onNamespaceAddedFn = value
   272  	return &opts
   273  }
   274  
   275  func (o *options) OnNamespaceAddedFn() OnNamespaceAddedFn {
   276  	return o.onNamespaceAddedFn
   277  }
   278  
   279  func (o *options) SetOnNamespaceRemovedFn(value OnNamespaceRemovedFn) Options {
   280  	opts := *o
   281  	opts.onNamespaceRemovedFn = value
   282  	return &opts
   283  }
   284  
   285  func (o *options) OnNamespaceRemovedFn() OnNamespaceRemovedFn {
   286  	return o.onNamespaceRemovedFn
   287  }
   288  
   289  func (o *options) SetOnRuleSetUpdatedFn(value OnRuleSetUpdatedFn) Options {
   290  	opts := *o
   291  	opts.onRuleSetUpdatedFn = value
   292  	return &opts
   293  }
   294  
   295  func (o *options) OnRuleSetUpdatedFn() OnRuleSetUpdatedFn {
   296  	return o.onRuleSetUpdatedFn
   297  }
   298  
   299  // SetRequireNamespaceWatchOnInit sets the flag to ensure matcher is initialized with a loaded namespace watch.
   300  func (o *options) SetRequireNamespaceWatchOnInit(value bool) Options {
   301  	opts := *o
   302  	opts.requireNamespaceWatchOnInit = value
   303  	return &opts
   304  }
   305  
   306  // RequireNamespaceWatchOnInit returns the flag to ensure matcher is initialized with a loaded namespace watch.
   307  func (o *options) RequireNamespaceWatchOnInit() bool {
   308  	return o.requireNamespaceWatchOnInit
   309  }
   310  
   311  func (o *options) SetInterruptedCh(ch <-chan struct{}) Options {
   312  	o.interruptedCh = ch
   313  	return o
   314  }
   315  
   316  func (o *options) InterruptedCh() <-chan struct{} {
   317  	return o.interruptedCh
   318  }
   319  
   320  func defaultRuleSetKeyFn(namespace []byte) string {
   321  	return fmt.Sprintf(defaultRuleSetKeyFormat, namespace)
   322  }