• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

m3db / m3metrics / 1159

pending completion
1159

push

travis-ci

web-flow
Add metric type field to aggregated metric, remove OwnsID (#160)

59 of 59 new or added lines in 7 files covered. (100.0%)

6613 of 7438 relevant lines covered (88.91%)

49461.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.11
/metric/unaggregated/types.go
1
// Copyright (c) 2016 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 unaggregated
22

23
import (
24
        "errors"
25
        "fmt"
26

27
        "github.com/m3db/m3metrics/generated/proto/metricpb"
28
        "github.com/m3db/m3metrics/metadata"
29
        "github.com/m3db/m3metrics/metric"
30
        "github.com/m3db/m3metrics/metric/id"
31
        "github.com/m3db/m3metrics/policy"
32
        "github.com/m3db/m3x/pool"
33
)
34

35
var (
36
        errNilCounterWithMetadatasProto    = errors.New("nil counter with metadatas proto message")
37
        errNilBatchTimerWithMetadatasProto = errors.New("nil batch timer with metadatas proto message")
38
        errNilGaugeWithMetadatasProto      = errors.New("nil gauge with metadatas proto message")
39
)
40

41
// Counter is a counter containing the counter ID and the counter value.
42
type Counter struct {
43
        ID    id.RawID
44
        Value int64
45
}
46

47
// ToUnion converts the counter to a metric union.
48
func (c Counter) ToUnion() MetricUnion {
2✔
49
        return MetricUnion{
2✔
50
                Type:       metric.CounterType,
2✔
51
                ID:         c.ID,
2✔
52
                CounterVal: c.Value,
2✔
53
        }
2✔
54
}
2✔
55

56
// ToProto converts the counter to a protobuf message in place.
57
func (c Counter) ToProto(pb *metricpb.Counter) {
8✔
58
        pb.Id = c.ID
8✔
59
        pb.Value = c.Value
8✔
60
}
8✔
61

62
// FromProto converts the protobuf message to a counter in place.
63
func (c *Counter) FromProto(pb metricpb.Counter) {
8✔
64
        c.ID = pb.Id
8✔
65
        c.Value = pb.Value
8✔
66
}
8✔
67

68
// BatchTimer is a timer containing the timer ID and a list of timer values.
69
type BatchTimer struct {
70
        ID     id.RawID
71
        Values []float64
72
}
73

74
// ToUnion converts the batch timer to a metric union.
75
func (t BatchTimer) ToUnion() MetricUnion {
2✔
76
        return MetricUnion{
2✔
77
                Type:          metric.TimerType,
2✔
78
                ID:            t.ID,
2✔
79
                BatchTimerVal: t.Values,
2✔
80
        }
2✔
81
}
2✔
82

83
// ToProto converts the batch timer to a protobuf message in place.
84
func (t BatchTimer) ToProto(pb *metricpb.BatchTimer) {
8✔
85
        pb.Id = t.ID
8✔
86
        pb.Values = t.Values
8✔
87
}
8✔
88

89
// FromProto converts the protobuf message to a batch timer in place.
90
func (t *BatchTimer) FromProto(pb metricpb.BatchTimer) {
8✔
91
        t.ID = pb.Id
8✔
92
        t.Values = pb.Values
8✔
93
}
8✔
94

95
// Gauge is a gauge containing the gauge ID and the value at certain time.
96
type Gauge struct {
97
        ID    id.RawID
98
        Value float64
99
}
100

101
// ToUnion converts the gauge to a metric union.
102
func (g Gauge) ToUnion() MetricUnion {
2✔
103
        return MetricUnion{
2✔
104
                Type:     metric.GaugeType,
2✔
105
                ID:       g.ID,
2✔
106
                GaugeVal: g.Value,
2✔
107
        }
2✔
108
}
2✔
109

110
// ToProto converts the gauge to a protobuf message in place.
111
func (g Gauge) ToProto(pb *metricpb.Gauge) {
8✔
112
        pb.Id = g.ID
8✔
113
        pb.Value = g.Value
8✔
114
}
8✔
115

116
// FromProto converts the protobuf message to a gauge in place.
117
func (g *Gauge) FromProto(pb metricpb.Gauge) {
8✔
118
        g.ID = pb.Id
8✔
119
        g.Value = pb.Value
8✔
120
}
8✔
121

122
// CounterWithPoliciesList is a counter with applicable policies list.
123
type CounterWithPoliciesList struct {
124
        Counter
125
        policy.PoliciesList
126
}
127

128
// BatchTimerWithPoliciesList is a batch timer with applicable policies list.
129
type BatchTimerWithPoliciesList struct {
130
        BatchTimer
131
        policy.PoliciesList
132
}
133

134
// GaugeWithPoliciesList is a gauge with applicable policies list.
135
type GaugeWithPoliciesList struct {
136
        Gauge
137
        policy.PoliciesList
138
}
139

140
// CounterWithMetadatas is a counter with applicable metadatas.
141
type CounterWithMetadatas struct {
142
        Counter
143
        metadata.StagedMetadatas
144
}
145

146
// ToProto converts the counter with metadatas to a protobuf message in place.
147
func (cm CounterWithMetadatas) ToProto(pb *metricpb.CounterWithMetadatas) error {
6✔
148
        if err := cm.StagedMetadatas.ToProto(&pb.Metadatas); err != nil {
8✔
149
                return err
2✔
150
        }
2✔
151
        cm.Counter.ToProto(&pb.Counter)
4✔
152
        return nil
4✔
153
}
154

155
// FromProto converts the protobuf message to a counter with metadatas in place.
156
func (cm *CounterWithMetadatas) FromProto(pb *metricpb.CounterWithMetadatas) error {
8✔
157
        if pb == nil {
10✔
158
                return errNilCounterWithMetadatasProto
2✔
159
        }
2✔
160
        if err := cm.StagedMetadatas.FromProto(pb.Metadatas); err != nil {
8✔
161
                return err
2✔
162
        }
2✔
163
        cm.Counter.FromProto(pb.Counter)
4✔
164
        return nil
4✔
165
}
166

167
// BatchTimerWithMetadatas is a batch timer with applicable metadatas.
168
type BatchTimerWithMetadatas struct {
169
        BatchTimer
170
        metadata.StagedMetadatas
171
}
172

173
// ToProto converts the batch timer with metadatas to a protobuf message in place.
174
func (bm BatchTimerWithMetadatas) ToProto(pb *metricpb.BatchTimerWithMetadatas) error {
6✔
175
        if err := bm.StagedMetadatas.ToProto(&pb.Metadatas); err != nil {
8✔
176
                return err
2✔
177
        }
2✔
178
        bm.BatchTimer.ToProto(&pb.BatchTimer)
4✔
179
        return nil
4✔
180
}
181

182
// FromProto converts the protobuf message to a batch timer with metadatas in place.
183
func (bm *BatchTimerWithMetadatas) FromProto(pb *metricpb.BatchTimerWithMetadatas) error {
8✔
184
        if pb == nil {
10✔
185
                return errNilBatchTimerWithMetadatasProto
2✔
186
        }
2✔
187
        if err := bm.StagedMetadatas.FromProto(pb.Metadatas); err != nil {
8✔
188
                return err
2✔
189
        }
2✔
190
        bm.BatchTimer.FromProto(pb.BatchTimer)
4✔
191
        return nil
4✔
192
}
193

194
// GaugeWithMetadatas is a gauge with applicable metadatas.
195
type GaugeWithMetadatas struct {
196
        Gauge
197
        metadata.StagedMetadatas
198
}
199

200
// ToProto converts the gauge with metadatas to a protobuf message in place.
201
func (gm GaugeWithMetadatas) ToProto(pb *metricpb.GaugeWithMetadatas) error {
6✔
202
        if err := gm.StagedMetadatas.ToProto(&pb.Metadatas); err != nil {
8✔
203
                return err
2✔
204
        }
2✔
205
        gm.Gauge.ToProto(&pb.Gauge)
4✔
206
        return nil
4✔
207
}
208

209
// FromProto converts the protobuf message to a gauge with metadatas in place.
210
func (gm *GaugeWithMetadatas) FromProto(pb *metricpb.GaugeWithMetadatas) error {
8✔
211
        if pb == nil {
10✔
212
                return errNilGaugeWithMetadatasProto
2✔
213
        }
2✔
214
        if err := gm.StagedMetadatas.FromProto(pb.Metadatas); err != nil {
8✔
215
                return err
2✔
216
        }
2✔
217
        gm.Gauge.FromProto(pb.Gauge)
4✔
218
        return nil
4✔
219
}
220

221
// MetricUnion is a union of different types of metrics, only one of which is valid
222
// at any given time. The actual type of the metric depends on the type field,
223
// which determines which value field is valid. Note that if the timer values are
224
// allocated from a pool, the TimerValPool should be set to the originating pool,
225
// and the caller is responsible for returning the timer values to the pool.
226
type MetricUnion struct {
227
        Type          metric.Type
228
        ID            id.RawID
229
        CounterVal    int64
230
        BatchTimerVal []float64
231
        GaugeVal      float64
232
        TimerValPool  pool.FloatsPool
233
}
234

235
var emptyMetricUnion MetricUnion
236

237
// String is the string representation of a metric union.
238
func (m *MetricUnion) String() string {
×
239
        switch m.Type {
×
240
        case metric.CounterType:
×
241
                return fmt.Sprintf("{type:%s,id:%s,value:%d}", m.Type, m.ID.String(), m.CounterVal)
×
242
        case metric.TimerType:
×
243
                return fmt.Sprintf("{type:%s,id:%s,value:%v}", m.Type, m.ID.String(), m.BatchTimerVal)
×
244
        case metric.GaugeType:
×
245
                return fmt.Sprintf("{type:%s,id:%s,value:%f}", m.Type, m.ID.String(), m.GaugeVal)
×
246
        default:
×
247
                return fmt.Sprintf(
×
248
                        "{type:%d,id:%s,counterVal:%d,batchTimerVal:%v,gaugeVal:%f}",
×
249
                        m.Type, m.ID.String(), m.CounterVal, m.BatchTimerVal, m.GaugeVal,
×
250
                )
×
251
        }
252
}
253

254
// Reset resets the metric union.
255
func (m *MetricUnion) Reset() { *m = emptyMetricUnion }
×
256

257
// Counter returns the counter metric.
258
func (m *MetricUnion) Counter() Counter { return Counter{ID: m.ID, Value: m.CounterVal} }
×
259

260
// BatchTimer returns the batch timer metric.
261
func (m *MetricUnion) BatchTimer() BatchTimer { return BatchTimer{ID: m.ID, Values: m.BatchTimerVal} }
×
262

263
// Gauge returns the gauge metric.
264
func (m *MetricUnion) Gauge() Gauge { return Gauge{ID: m.ID, Value: m.GaugeVal} }
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc