forked from m3db/m3
/
types.go
231 lines (200 loc) · 6.83 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
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
// Copyright (c) 2016 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 aggregated
import (
"errors"
"fmt"
"time"
"github.com/m3db/m3/src/metrics/generated/proto/metricpb"
"github.com/m3db/m3/src/metrics/metadata"
"github.com/m3db/m3/src/metrics/metric"
"github.com/m3db/m3/src/metrics/metric/id"
"github.com/m3db/m3/src/metrics/policy"
)
var (
errNilForwardedMetricWithMetadataProto = errors.New("nil forwarded metric with metadata proto message")
errNilTimedMetricWithMetadataProto = errors.New("nil timed metric with metadata proto message")
)
// Metric is a metric, which is essentially a named value at certain time.
type Metric struct {
Type metric.Type
ID id.RawID
TimeNanos int64
Value float64
}
// ToProto converts the metric to a protobuf message in place.
func (m Metric) ToProto(pb *metricpb.TimedMetric) error {
if err := m.Type.ToProto(&pb.Type); err != nil {
return err
}
pb.Id = m.ID
pb.TimeNanos = m.TimeNanos
pb.Value = m.Value
return nil
}
// FromProto converts the protobuf message to a metric in place.
func (m *Metric) FromProto(pb metricpb.TimedMetric) error {
if err := m.Type.FromProto(pb.Type); err != nil {
return err
}
m.ID = pb.Id
m.TimeNanos = pb.TimeNanos
m.Value = pb.Value
return nil
}
// String is the string representation of a metric.
func (m Metric) String() string {
return fmt.Sprintf(
"{id:%s,timestamp:%s,value:%f}",
m.ID.String(),
time.Unix(0, m.TimeNanos).String(),
m.Value,
)
}
// ChunkedMetric is a metric with a chunked ID.
type ChunkedMetric struct {
id.ChunkedID
TimeNanos int64
Value float64
}
// RawMetric is a metric in its raw form (e.g., encoded bytes associated with
// a metric object).
type RawMetric interface {
// ID is the metric identifier.
ID() (id.RawID, error)
// TimeNanos is the metric timestamp in nanoseconds.
TimeNanos() (int64, error)
// Value is the metric value.
Value() (float64, error)
// Metric is the metric object represented by the raw metric.
Metric() (Metric, error)
// Bytes are the bytes backing this raw metric.
Bytes() []byte
// Reset resets the raw data.
Reset(data []byte)
}
// MetricWithStoragePolicy is a metric with applicable storage policy.
type MetricWithStoragePolicy struct {
Metric
policy.StoragePolicy
}
// ToProto converts the chunked metric with storage policy to a protobuf message in place.
func (m MetricWithStoragePolicy) ToProto(pb *metricpb.TimedMetricWithStoragePolicy) error {
if err := m.Metric.ToProto(&pb.TimedMetric); err != nil {
return err
}
return m.StoragePolicy.ToProto(&pb.StoragePolicy)
}
// FromProto converts the protobuf message to a chunked metric with storage policy in place.
func (m *MetricWithStoragePolicy) FromProto(pb metricpb.TimedMetricWithStoragePolicy) error {
if err := m.Metric.FromProto(pb.TimedMetric); err != nil {
return err
}
return m.StoragePolicy.FromProto(pb.StoragePolicy)
}
// String is the string representation of a metric with storage policy.
func (m MetricWithStoragePolicy) String() string {
return fmt.Sprintf("{metric:%s,policy:%s}", m.Metric.String(), m.StoragePolicy.String())
}
// ChunkedMetricWithStoragePolicy is a chunked metric with applicable storage policy.
type ChunkedMetricWithStoragePolicy struct {
ChunkedMetric
policy.StoragePolicy
}
// ForwardedMetric is a forwarded metric.
type ForwardedMetric struct {
Type metric.Type
ID id.RawID
TimeNanos int64
Values []float64
}
// ToProto converts the forwarded metric to a protobuf message in place.
func (m ForwardedMetric) ToProto(pb *metricpb.ForwardedMetric) error {
if err := m.Type.ToProto(&pb.Type); err != nil {
return err
}
pb.Id = m.ID
pb.TimeNanos = m.TimeNanos
pb.Values = m.Values
return nil
}
// FromProto converts the protobuf message to a forwarded metric in place.
func (m *ForwardedMetric) FromProto(pb metricpb.ForwardedMetric) error {
if err := m.Type.FromProto(pb.Type); err != nil {
return err
}
m.ID = pb.Id
m.TimeNanos = pb.TimeNanos
m.Values = pb.Values
return nil
}
// String is a string representation of the forwarded metric.
func (m ForwardedMetric) String() string {
return fmt.Sprintf(
"{id:%s,timestamp:%s,values:%v}",
m.ID.String(),
time.Unix(0, m.TimeNanos).String(),
m.Values,
)
}
// ForwardedMetricWithMetadata is a forwarded metric with metadata.
type ForwardedMetricWithMetadata struct {
ForwardedMetric
metadata.ForwardMetadata
}
// ToProto converts the forwarded metric with metadata to a protobuf message in place.
func (fm ForwardedMetricWithMetadata) ToProto(pb *metricpb.ForwardedMetricWithMetadata) error {
if err := fm.ForwardedMetric.ToProto(&pb.Metric); err != nil {
return err
}
return fm.ForwardMetadata.ToProto(&pb.Metadata)
}
// FromProto converts the protobuf message to a forwarded metric with metadata in place.
func (fm *ForwardedMetricWithMetadata) FromProto(pb *metricpb.ForwardedMetricWithMetadata) error {
if pb == nil {
return errNilForwardedMetricWithMetadataProto
}
if err := fm.ForwardedMetric.FromProto(pb.Metric); err != nil {
return err
}
return fm.ForwardMetadata.FromProto(pb.Metadata)
}
// TimedMetricWithMetadata is a timed metric with metadata.
type TimedMetricWithMetadata struct {
Metric
metadata.TimedMetadata
}
// ToProto converts the timed metric with metadata to a protobuf message in place.
func (tm TimedMetricWithMetadata) ToProto(pb *metricpb.TimedMetricWithMetadata) error {
if err := tm.Metric.ToProto(&pb.Metric); err != nil {
return err
}
return tm.TimedMetadata.ToProto(&pb.Metadata)
}
// FromProto converts the protobuf message to a timed metric with metadata in place.
func (tm *TimedMetricWithMetadata) FromProto(pb *metricpb.TimedMetricWithMetadata) error {
if pb == nil {
return errNilTimedMetricWithMetadataProto
}
if err := tm.Metric.FromProto(pb.Metric); err != nil {
return err
}
return tm.TimedMetadata.FromProto(pb.Metadata)
}