-
Notifications
You must be signed in to change notification settings - Fork 6
/
types.go
200 lines (172 loc) · 4.55 KB
/
types.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
// Based on https://github.com/kubernetes-retired/heapster/blob/master/metrics/core/types.go
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.
// Copyright 2018-2019 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package metrics
import (
"time"
)
type Type int8
const (
Cumulative Type = iota
Gauge
Delta
)
func (t *Type) String() string {
switch *t {
case Cumulative:
return "cumulative"
case Gauge:
return "gauge"
case Delta:
return "delta"
}
return ""
}
type Unit int8
const (
Count Unit = iota
Bytes
Milliseconds
Nanoseconds
Millicores
)
func (u Unit) String() string {
switch u {
case Bytes:
return "bytes"
case Milliseconds:
return "ms"
case Nanoseconds:
return "ns"
case Millicores:
return "millicores"
}
return ""
}
// ValueType is used to discriminate whether a Value is an int64 or a float64
type ValueType int8
const (
ValueInt64 ValueType = iota
ValueFloat
)
func (valueType *ValueType) String() string {
switch *valueType {
case ValueInt64:
return "int64"
case ValueFloat:
return "double"
}
return ""
}
// Value represents a metric value that is either a float64 or an int64
type Value struct {
IntValue int64
FloatValue float64
ValueType ValueType
}
func (v *Value) GetValue() interface{} {
if ValueInt64 == v.ValueType {
return v.IntValue
} else if ValueFloat == v.ValueType {
return v.FloatValue
} else {
return nil
}
}
func (v *Value) IsZeroValue() bool {
if ValueInt64 == v.ValueType {
return v.IntValue == 0
} else if ValueFloat == v.ValueType {
return v.FloatValue == 0
} else {
return true
}
}
// LabeledValue is a metric value that is either a float64 or an int64 and that has it's own name and values
type LabeledValue struct {
Name string
Labels map[string]string
Value
}
func (l *LabeledValue) GetValue() interface{} {
if ValueInt64 == l.ValueType {
return l.IntValue
} else if ValueFloat == l.ValueType {
return l.FloatValue
} else {
return nil
}
}
// Source produces metric batches
type Source interface {
AutoDiscovered() bool
Name() string
Scrape() (*Batch, error)
Cleanup()
}
// SourceProvider produces metric sources
type SourceProvider interface {
GetMetricsSources() []Source
Name() string
CollectionInterval() time.Duration
Timeout() time.Duration
}
// Sink exports metric batches
type Sink interface {
// Export data to the external storage. The function should be synchronous/blocking and finish only
// after the given Batch was written. This will allow sink manager to push data only to these
// sinks that finished writing the previous data.
Export(*Batch)
}
// Processor should not store any references to anything in *Batch because it is assumed that Processor does not own that memory.
// Other components are free to mutably update *Batch after Process is run.
type Processor interface {
Name() string
Process(*Batch) (*Batch, error)
}
// ProviderHandler is an interface for dynamically adding and removing MetricSourceProviders
type ProviderHandler interface {
AddProvider(provider SourceProvider)
DeleteProvider(name string)
}
type ProviderFactory interface {
Name() string
Build(cfg interface{}) (SourceProvider, error)
}
type ConfigurableSourceProvider interface {
Configure(interval, timeout time.Duration)
}
// DefaultSourceProvider handle the common providers configuration
type DefaultSourceProvider struct {
collectionInterval time.Duration
timeout time.Duration
}
// CollectionInterval return the provider collection interval configuration
func (sp *DefaultSourceProvider) CollectionInterval() time.Duration {
return sp.collectionInterval
}
// Timeout return the provider timeout configuration
func (sp *DefaultSourceProvider) Timeout() time.Duration {
return sp.timeout
}
// Configure the 'collectionInterval' and 'timeout' values
func (sp *DefaultSourceProvider) Configure(interval, timeout time.Duration) {
sp.collectionInterval = interval // forces default collection interval if zero
sp.timeout = timeout
if sp.timeout == 0 {
sp.timeout = 30 * time.Second
}
}