-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
metrics.go
325 lines (288 loc) · 11.5 KB
/
metrics.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright The OpenTelemetry Authors
//
// 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.
package pdata // import "go.opentelemetry.io/collector/model/pdata"
import (
"go.opentelemetry.io/collector/model/internal"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
)
// MetricsMarshaler marshals pdata.Metrics into bytes.
type MetricsMarshaler interface {
// MarshalMetrics the given pdata.Metrics into bytes.
// If the error is not nil, the returned bytes slice cannot be used.
MarshalMetrics(md Metrics) ([]byte, error)
}
// MetricsUnmarshaler unmarshalls bytes into pdata.Metrics.
type MetricsUnmarshaler interface {
// UnmarshalMetrics the given bytes into pdata.Metrics.
// If the error is not nil, the returned pdata.Metrics cannot be used.
UnmarshalMetrics(buf []byte) (Metrics, error)
}
// MetricsSizer is an optional interface implemented by the MetricsMarshaler,
// that calculates the size of a marshaled Metrics.
type MetricsSizer interface {
// MetricsSize returns the size in bytes of a marshaled Metrics.
MetricsSize(md Metrics) int
}
// Metrics is an opaque interface that allows transition to the new internal Metrics data, but also facilitates the
// transition to the new components, especially for traces.
//
// Outside of the core repository, the metrics pipeline cannot be converted to the new model since data.MetricData is
// part of the internal package.
type Metrics struct {
orig *otlpmetrics.MetricsData
}
// NewMetrics creates a new Metrics.
func NewMetrics() Metrics {
return Metrics{orig: &otlpmetrics.MetricsData{}}
}
// MetricsFromInternalRep creates Metrics from the internal representation.
// Should not be used outside this module.
func MetricsFromInternalRep(wrapper internal.MetricsWrapper) Metrics {
return Metrics{orig: internal.MetricsToOtlp(wrapper)}
}
// InternalRep returns internal representation of the Metrics.
// Should not be used outside this module.
func (md Metrics) InternalRep() internal.MetricsWrapper {
return internal.MetricsFromOtlp(md.orig)
}
// Clone returns a copy of MetricData.
func (md Metrics) Clone() Metrics {
cloneMd := NewMetrics()
md.ResourceMetrics().CopyTo(cloneMd.ResourceMetrics())
return cloneMd
}
// ResourceMetrics returns the ResourceMetricsSlice associated with this Metrics.
func (md Metrics) ResourceMetrics() ResourceMetricsSlice {
return newResourceMetricsSlice(&md.orig.ResourceMetrics)
}
// MetricCount calculates the total number of metrics.
func (md Metrics) MetricCount() int {
metricCount := 0
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
ilms := rm.InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
metricCount += ilm.Metrics().Len()
}
}
return metricCount
}
// DataPointCount calculates the total number of data points.
func (md Metrics) DataPointCount() (dataPointCount int) {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
ilms := rm.InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
ms := ilm.Metrics()
for k := 0; k < ms.Len(); k++ {
m := ms.At(k)
switch m.DataType() {
case MetricDataTypeGauge:
dataPointCount += m.Gauge().DataPoints().Len()
case MetricDataTypeSum:
dataPointCount += m.Sum().DataPoints().Len()
case MetricDataTypeHistogram:
dataPointCount += m.Histogram().DataPoints().Len()
case MetricDataTypeSummary:
dataPointCount += m.Summary().DataPoints().Len()
}
}
}
}
return
}
// MetricDataType specifies the type of data in a Metric.
type MetricDataType int32
const (
MetricDataTypeNone MetricDataType = iota
MetricDataTypeGauge
MetricDataTypeSum
MetricDataTypeHistogram
MetricDataTypeSummary
)
// String returns the string representation of the MetricDataType.
func (mdt MetricDataType) String() string {
switch mdt {
case MetricDataTypeNone:
return "None"
case MetricDataTypeGauge:
return "Gauge"
case MetricDataTypeSum:
return "Sum"
case MetricDataTypeHistogram:
return "Histogram"
case MetricDataTypeSummary:
return "Summary"
}
return ""
}
// DataType returns the type of the data for this Metric.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) DataType() MetricDataType {
switch ms.orig.Data.(type) {
case *otlpmetrics.Metric_Gauge:
return MetricDataTypeGauge
case *otlpmetrics.Metric_Sum:
return MetricDataTypeSum
case *otlpmetrics.Metric_Histogram:
return MetricDataTypeHistogram
case *otlpmetrics.Metric_Summary:
return MetricDataTypeSummary
}
return MetricDataTypeNone
}
// SetDataType clears any existing data and initialize it with an empty data of the given type.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) SetDataType(ty MetricDataType) {
switch ty {
case MetricDataTypeGauge:
ms.orig.Data = &otlpmetrics.Metric_Gauge{Gauge: &otlpmetrics.Gauge{}}
case MetricDataTypeSum:
ms.orig.Data = &otlpmetrics.Metric_Sum{Sum: &otlpmetrics.Sum{}}
case MetricDataTypeHistogram:
ms.orig.Data = &otlpmetrics.Metric_Histogram{Histogram: &otlpmetrics.Histogram{}}
case MetricDataTypeSummary:
ms.orig.Data = &otlpmetrics.Metric_Summary{Summary: &otlpmetrics.Summary{}}
}
}
// Gauge returns the data as Gauge.
// Calling this function when DataType() != MetricDataTypeGauge will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) Gauge() Gauge {
return newGauge(ms.orig.Data.(*otlpmetrics.Metric_Gauge).Gauge)
}
// Sum returns the data as Sum.
// Calling this function when DataType() != MetricDataTypeSum will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) Sum() Sum {
return newSum(ms.orig.Data.(*otlpmetrics.Metric_Sum).Sum)
}
// Histogram returns the data as Histogram.
// Calling this function when DataType() != MetricDataTypeHistogram will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) Histogram() Histogram {
return newHistogram(ms.orig.Data.(*otlpmetrics.Metric_Histogram).Histogram)
}
// Summary returns the data as Summary.
// Calling this function when DataType() != MetricDataTypeSummary will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) Summary() Summary {
return newSummary(ms.orig.Data.(*otlpmetrics.Metric_Summary).Summary)
}
func copyData(src, dest *otlpmetrics.Metric) {
switch srcData := (src).Data.(type) {
case *otlpmetrics.Metric_Gauge:
data := &otlpmetrics.Metric_Gauge{Gauge: &otlpmetrics.Gauge{}}
newGauge(srcData.Gauge).CopyTo(newGauge(data.Gauge))
dest.Data = data
case *otlpmetrics.Metric_Sum:
data := &otlpmetrics.Metric_Sum{Sum: &otlpmetrics.Sum{}}
newSum(srcData.Sum).CopyTo(newSum(data.Sum))
dest.Data = data
case *otlpmetrics.Metric_Histogram:
data := &otlpmetrics.Metric_Histogram{Histogram: &otlpmetrics.Histogram{}}
newHistogram(srcData.Histogram).CopyTo(newHistogram(data.Histogram))
dest.Data = data
case *otlpmetrics.Metric_Summary:
data := &otlpmetrics.Metric_Summary{Summary: &otlpmetrics.Summary{}}
newSummary(srcData.Summary).CopyTo(newSummary(data.Summary))
dest.Data = data
}
}
// MetricAggregationTemporality defines how a metric aggregator reports aggregated values.
// It describes how those values relate to the time interval over which they are aggregated.
type MetricAggregationTemporality int32
const (
// MetricAggregationTemporalityUnspecified is the default MetricAggregationTemporality, it MUST NOT be used.
MetricAggregationTemporalityUnspecified = MetricAggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED)
// MetricAggregationTemporalityDelta is a MetricAggregationTemporality for a metric aggregator which reports changes since last report time.
MetricAggregationTemporalityDelta = MetricAggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA)
// MetricAggregationTemporalityCumulative is a MetricAggregationTemporality for a metric aggregator which reports changes since a fixed start time.
MetricAggregationTemporalityCumulative = MetricAggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE)
)
// String returns the string representation of the MetricAggregationTemporality.
func (at MetricAggregationTemporality) String() string {
return otlpmetrics.AggregationTemporality(at).String()
}
// MetricDataPointFlags defines how a metric aggregator reports aggregated values.
// It describes how those values relate to the time interval over which they are aggregated.
type MetricDataPointFlags uint32
const (
// MetricDataPointFlagsNone is the default MetricDataPointFlags.
MetricDataPointFlagsNone = MetricDataPointFlags(otlpmetrics.DataPointFlags_FLAG_NONE)
)
// NewMetricDataPointFlags returns a new MetricDataPointFlags combining the flags passed
// in as parameters.
func NewMetricDataPointFlags(flags ...MetricDataPointFlag) MetricDataPointFlags {
var flag MetricDataPointFlags
for _, f := range flags {
flag |= MetricDataPointFlags(f)
}
return flag
}
// HasFlag returns true if the MetricDataPointFlags contains the specified flag
func (d MetricDataPointFlags) HasFlag(flag MetricDataPointFlag) bool {
return d&MetricDataPointFlags(flag) != 0
}
// String returns the string representation of the MetricDataPointFlags.
func (d MetricDataPointFlags) String() string {
return otlpmetrics.DataPointFlags(d).String()
}
// MetricDataPointFlag allow users to configure DataPointFlags. This is achieved via NewMetricDataPointFlags.
// The separation between MetricDataPointFlags and MetricDataPointFlag exists to prevent users accidentally
// comparing the value of individual flags with MetricDataPointFlags. Instead, users must use the HasFlag method.
type MetricDataPointFlag uint32
const (
// MetricDataPointFlagsNoRecordedValue is flag for a metric aggregator which reports changes since last report time.
MetricDataPointFlagNoRecordedValue = MetricDataPointFlag(otlpmetrics.DataPointFlags_FLAG_NO_RECORDED_VALUE)
)
// MetricValueType specifies the type of NumberDataPoint.
type MetricValueType int32
const (
MetricValueTypeNone MetricValueType = iota
MetricValueTypeInt
MetricValueTypeDouble
)
// Type returns the type of the value for this NumberDataPoint.
// Calling this function on zero-initialized NumberDataPoint will cause a panic.
func (ms NumberDataPoint) Type() MetricValueType {
if ms.orig.Value == nil {
return MetricValueTypeNone
}
switch ms.orig.Value.(type) {
case *otlpmetrics.NumberDataPoint_AsDouble:
return MetricValueTypeDouble
case *otlpmetrics.NumberDataPoint_AsInt:
return MetricValueTypeInt
}
return MetricValueTypeNone
}
// Type returns the type of the value for this Exemplar.
// Calling this function on zero-initialized Exemplar will cause a panic.
func (ms Exemplar) Type() MetricValueType {
if ms.orig.Value == nil {
return MetricValueTypeNone
}
switch ms.orig.Value.(type) {
case *otlpmetrics.Exemplar_AsDouble:
return MetricValueTypeDouble
case *otlpmetrics.Exemplar_AsInt:
return MetricValueTypeInt
}
return MetricValueTypeNone
}