This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
304 lines (240 loc) · 8.49 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 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"
"math"
"time"
"github.com/m3db/m3cluster/kv"
"github.com/m3db/m3cluster/kv/mem"
"github.com/m3db/m3metrics/rules"
"github.com/m3db/m3x/clock"
"github.com/m3db/m3x/instrument"
)
const (
defaultInitWatchTimeout = 10 * time.Second
defaultNamespacesKey = "/namespaces"
defaultRuleSetKeyFormat = "/ruleset/%s"
defaultMatchRangePast = time.Duration(math.MaxInt64)
)
var (
defaultNamespaceTag = []byte("namespace")
defaultDefaultNamespace = []byte("default")
)
// RuleSetKeyFn generates the ruleset key for a given namespace.
type RuleSetKeyFn func(namespace []byte) string
// OnNamespaceAddedFn is called when a namespace is added.
type OnNamespaceAddedFn func(namespace []byte, ruleSet RuleSet)
// OnNamespaceRemovedFn is called when a namespace is removed.
type OnNamespaceRemovedFn func(namespace []byte)
// OnRuleSetUpdatedFn is called when a ruleset is updated.
type OnRuleSetUpdatedFn func(namespace []byte, ruleSet RuleSet)
// Options provide a set of options for the matcher.
type Options interface {
// SetClockOptions sets the clock options.
SetClockOptions(value clock.Options) Options
// ClockOptions returns the clock options.
ClockOptions() clock.Options
// SetInstrumentOptions sets the instrument options.
SetInstrumentOptions(value instrument.Options) Options
// InstrumentOptions returns the instrument options.
InstrumentOptions() instrument.Options
// SetRuleSetOptions sets the ruleset options.
SetRuleSetOptions(value rules.Options) Options
// RuleSetOptions returns the ruleset options.
RuleSetOptions() rules.Options
// SetInitWatchTimeout sets the initial watch timeout.
SetInitWatchTimeout(value time.Duration) Options
// InitWatchTimeout returns the initial watch timeout.
InitWatchTimeout() time.Duration
// SetKVStore sets the kv store.
SetKVStore(value kv.Store) Options
// KVStore returns the kv store.
KVStore() kv.Store
// SetNamespacesKey sets the key for the full list of namespaces.
SetNamespacesKey(value string) Options
// NamespacesKey returns the key for the full list of namespaces.
NamespacesKey() string
// SetRuleSetKeyFn sets the function to generate ruleset keys.
SetRuleSetKeyFn(value RuleSetKeyFn) Options
// RuleSetKeyFn returns the function to generate ruleset keys.
RuleSetKeyFn() RuleSetKeyFn
// SetNamespaceTag sets the namespace tag.
SetNamespaceTag(value []byte) Options
// NamespaceTag returns the namespace tag.
NamespaceTag() []byte
// SetDefaultNamespace sets the default namespace for ids without a namespace.
SetDefaultNamespace(value []byte) Options
// DefaultNamespace returns the default namespace for ids without a namespace.
DefaultNamespace() []byte
// SetMatchRangePast sets the limit on the earliest time eligible for rule matching.
SetMatchRangePast(value time.Duration) Options
// MatchRangePast returns the limit on the earliest time eligible for rule matching.
MatchRangePast() time.Duration
// SetOnNamespaceAddedFn sets the function to be called when a namespace is added.
SetOnNamespaceAddedFn(value OnNamespaceAddedFn) Options
// OnNamespaceAddedFn returns the function to be called when a namespace is added.
OnNamespaceAddedFn() OnNamespaceAddedFn
// SetOnNamespaceRemovedFn sets the function to be called when a namespace is removed.
SetOnNamespaceRemovedFn(value OnNamespaceRemovedFn) Options
// OnNamespaceRemovedFn returns the function to be called when a namespace is removed.
OnNamespaceRemovedFn() OnNamespaceRemovedFn
// SetOnRuleSetUpdatedFn sets the function to be called when a ruleset is updated.
SetOnRuleSetUpdatedFn(value OnRuleSetUpdatedFn) Options
// OnRuleSetUpdatedFn returns the function to be called when a ruleset is updated.
OnRuleSetUpdatedFn() OnRuleSetUpdatedFn
}
type options struct {
clockOpts clock.Options
instrumentOpts instrument.Options
ruleSetOpts rules.Options
initWatchTimeout time.Duration
kvStore kv.Store
namespacesKey string
ruleSetKeyFn RuleSetKeyFn
namespaceTag []byte
defaultNamespace []byte
matchRangePast time.Duration
onNamespaceAddedFn OnNamespaceAddedFn
onNamespaceRemovedFn OnNamespaceRemovedFn
onRuleSetUpdatedFn OnRuleSetUpdatedFn
}
// NewOptions creates a new set of options.
func NewOptions() Options {
return &options{
clockOpts: clock.NewOptions(),
instrumentOpts: instrument.NewOptions(),
ruleSetOpts: rules.NewOptions(),
initWatchTimeout: defaultInitWatchTimeout,
kvStore: mem.NewStore(),
namespacesKey: defaultNamespacesKey,
ruleSetKeyFn: defaultRuleSetKeyFn,
namespaceTag: defaultNamespaceTag,
defaultNamespace: defaultDefaultNamespace,
matchRangePast: defaultMatchRangePast,
}
}
func (o *options) SetClockOptions(value clock.Options) Options {
opts := *o
opts.clockOpts = value
return &opts
}
func (o *options) ClockOptions() clock.Options {
return o.clockOpts
}
func (o *options) SetInstrumentOptions(value instrument.Options) Options {
opts := *o
opts.instrumentOpts = value
return &opts
}
func (o *options) InstrumentOptions() instrument.Options {
return o.instrumentOpts
}
func (o *options) SetRuleSetOptions(value rules.Options) Options {
opts := *o
opts.ruleSetOpts = value
return &opts
}
func (o *options) RuleSetOptions() rules.Options {
return o.ruleSetOpts
}
func (o *options) SetInitWatchTimeout(value time.Duration) Options {
opts := *o
opts.initWatchTimeout = value
return &opts
}
func (o *options) InitWatchTimeout() time.Duration {
return o.initWatchTimeout
}
func (o *options) SetKVStore(value kv.Store) Options {
opts := *o
opts.kvStore = value
return &opts
}
func (o *options) KVStore() kv.Store {
return o.kvStore
}
func (o *options) SetNamespacesKey(value string) Options {
opts := *o
opts.namespacesKey = value
return &opts
}
func (o *options) NamespacesKey() string {
return o.namespacesKey
}
func (o *options) SetRuleSetKeyFn(value RuleSetKeyFn) Options {
opts := *o
opts.ruleSetKeyFn = value
return &opts
}
func (o *options) RuleSetKeyFn() RuleSetKeyFn {
return o.ruleSetKeyFn
}
func (o *options) SetNamespaceTag(value []byte) Options {
opts := *o
opts.namespaceTag = value
return &opts
}
func (o *options) NamespaceTag() []byte {
return o.namespaceTag
}
func (o *options) SetDefaultNamespace(value []byte) Options {
opts := *o
opts.defaultNamespace = value
return &opts
}
func (o *options) DefaultNamespace() []byte {
return o.defaultNamespace
}
func (o *options) SetMatchRangePast(value time.Duration) Options {
opts := *o
opts.matchRangePast = value
return &opts
}
func (o *options) MatchRangePast() time.Duration {
return o.matchRangePast
}
func (o *options) SetOnNamespaceAddedFn(value OnNamespaceAddedFn) Options {
opts := *o
opts.onNamespaceAddedFn = value
return &opts
}
func (o *options) OnNamespaceAddedFn() OnNamespaceAddedFn {
return o.onNamespaceAddedFn
}
func (o *options) SetOnNamespaceRemovedFn(value OnNamespaceRemovedFn) Options {
opts := *o
opts.onNamespaceRemovedFn = value
return &opts
}
func (o *options) OnNamespaceRemovedFn() OnNamespaceRemovedFn {
return o.onNamespaceRemovedFn
}
func (o *options) SetOnRuleSetUpdatedFn(value OnRuleSetUpdatedFn) Options {
opts := *o
opts.onRuleSetUpdatedFn = value
return &opts
}
func (o *options) OnRuleSetUpdatedFn() OnRuleSetUpdatedFn {
return o.onRuleSetUpdatedFn
}
func defaultRuleSetKeyFn(namespace []byte) string {
return fmt.Sprintf(defaultRuleSetKeyFormat, namespace)
}