-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
metric.go
307 lines (278 loc) · 11.4 KB
/
metric.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
// 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/internal"
otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1"
)
type AggregationTemporality int32
const (
AggregationTemporalityUnspecified = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED)
AggregationTemporalityDelta = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA)
AggregationTemporalityCumulative = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE)
)
func (at AggregationTemporality) String() string {
return otlpmetrics.AggregationTemporality(at).String()
}
// Metrics is an opaque interface that allows transition to the new internal Metrics data, but also facilitate 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 *otlpcollectormetrics.ExportMetricsServiceRequest
}
// NewMetrics creates a new Metrics.
func NewMetrics() Metrics {
return Metrics{orig: &otlpcollectormetrics.ExportMetricsServiceRequest{}}
}
// 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)}
}
// MetricsFromOtlpProtoBytes converts the OTLP Collector ExportMetricsServiceRequest
// ProtoBuf bytes to Metrics.
//
// Returns an invalid Metrics instance if error is not nil.
func MetricsFromOtlpProtoBytes(data []byte) (Metrics, error) {
req := otlpcollectormetrics.ExportMetricsServiceRequest{}
if err := req.Unmarshal(data); err != nil {
return Metrics{}, err
}
return Metrics{orig: &req}, nil
}
// 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)
}
// ToOtlpProtoBytes converts this Metrics to the OTLP Collector ExportMetricsServiceRequest
// ProtoBuf bytes.
//
// Returns an nil byte-array if error is not nil.
func (md Metrics) ToOtlpProtoBytes() ([]byte, error) {
return md.orig.Marshal()
}
// Clone returns a copy of MetricData.
func (md Metrics) Clone() Metrics {
cloneMd := NewMetrics()
md.ResourceMetrics().CopyTo(cloneMd.ResourceMetrics())
return cloneMd
}
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
}
// OtlpProtoSize returns the size in bytes of this Metrics encoded as OTLP Collector
// ExportMetricsServiceRequest ProtoBuf bytes.
func (md Metrics) OtlpProtoSize() int {
return md.orig.Size()
}
// MetricAndDataPointCount calculates the total number of metrics and data points.
func (md Metrics) MetricAndDataPointCount() (metricCount int, 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)
metrics := ilm.Metrics()
metricCount += metrics.Len()
ms := ilm.Metrics()
for k := 0; k < ms.Len(); k++ {
m := ms.At(k)
switch m.DataType() {
case MetricDataTypeIntGauge:
dataPointCount += m.IntGauge().DataPoints().Len()
case MetricDataTypeDoubleGauge:
dataPointCount += m.DoubleGauge().DataPoints().Len()
case MetricDataTypeIntSum:
dataPointCount += m.IntSum().DataPoints().Len()
case MetricDataTypeDoubleSum:
dataPointCount += m.DoubleSum().DataPoints().Len()
case MetricDataTypeIntHistogram:
dataPointCount += m.IntHistogram().DataPoints().Len()
case MetricDataTypeDoubleHistogram:
dataPointCount += m.DoubleHistogram().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
MetricDataTypeIntGauge
MetricDataTypeDoubleGauge
MetricDataTypeIntSum
MetricDataTypeDoubleSum
MetricDataTypeIntHistogram
MetricDataTypeDoubleHistogram
MetricDataTypeSummary
)
func (mdt MetricDataType) String() string {
switch mdt {
case MetricDataTypeNone:
return "None"
case MetricDataTypeIntGauge:
return "IntGauge"
case MetricDataTypeDoubleGauge:
return "DoubleGauge"
case MetricDataTypeIntSum:
return "IntSum"
case MetricDataTypeDoubleSum:
return "DoubleSum"
case MetricDataTypeIntHistogram:
return "IntHistogram"
case MetricDataTypeDoubleHistogram:
return "DoubleHistogram"
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_IntGauge:
return MetricDataTypeIntGauge
case *otlpmetrics.Metric_DoubleGauge:
return MetricDataTypeDoubleGauge
case *otlpmetrics.Metric_IntSum:
return MetricDataTypeIntSum
case *otlpmetrics.Metric_DoubleSum:
return MetricDataTypeDoubleSum
case *otlpmetrics.Metric_IntHistogram:
return MetricDataTypeIntHistogram
case *otlpmetrics.Metric_DoubleHistogram:
return MetricDataTypeDoubleHistogram
case *otlpmetrics.Metric_DoubleSummary:
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 MetricDataTypeIntGauge:
ms.orig.Data = &otlpmetrics.Metric_IntGauge{IntGauge: &otlpmetrics.IntGauge{}}
case MetricDataTypeDoubleGauge:
ms.orig.Data = &otlpmetrics.Metric_DoubleGauge{DoubleGauge: &otlpmetrics.DoubleGauge{}}
case MetricDataTypeIntSum:
ms.orig.Data = &otlpmetrics.Metric_IntSum{IntSum: &otlpmetrics.IntSum{}}
case MetricDataTypeDoubleSum:
ms.orig.Data = &otlpmetrics.Metric_DoubleSum{DoubleSum: &otlpmetrics.DoubleSum{}}
case MetricDataTypeIntHistogram:
ms.orig.Data = &otlpmetrics.Metric_IntHistogram{IntHistogram: &otlpmetrics.IntHistogram{}}
case MetricDataTypeDoubleHistogram:
ms.orig.Data = &otlpmetrics.Metric_DoubleHistogram{DoubleHistogram: &otlpmetrics.DoubleHistogram{}}
case MetricDataTypeSummary:
ms.orig.Data = &otlpmetrics.Metric_DoubleSummary{DoubleSummary: &otlpmetrics.DoubleSummary{}}
}
}
// IntGauge returns the data as IntGauge.
// Calling this function when DataType() != MetricDataTypeIntGauge will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) IntGauge() IntGauge {
return newIntGauge(ms.orig.Data.(*otlpmetrics.Metric_IntGauge).IntGauge)
}
// DoubleGauge returns the data as DoubleGauge.
// Calling this function when DataType() != MetricDataTypeDoubleGauge will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) DoubleGauge() DoubleGauge {
return newDoubleGauge(ms.orig.Data.(*otlpmetrics.Metric_DoubleGauge).DoubleGauge)
}
// IntSum returns the data as IntSum.
// Calling this function when DataType() != MetricDataTypeIntSum will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) IntSum() IntSum {
return newIntSum(ms.orig.Data.(*otlpmetrics.Metric_IntSum).IntSum)
}
// DoubleSum returns the data as DoubleSum.
// Calling this function when DataType() != MetricDataTypeDoubleSum will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) DoubleSum() DoubleSum {
return newDoubleSum(ms.orig.Data.(*otlpmetrics.Metric_DoubleSum).DoubleSum)
}
// IntHistogram returns the data as IntHistogram.
// Calling this function when DataType() != MetricDataTypeIntHistogram will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) IntHistogram() IntHistogram {
return newIntHistogram(ms.orig.Data.(*otlpmetrics.Metric_IntHistogram).IntHistogram)
}
// DoubleHistogram returns the data as DoubleHistogram.
// Calling this function when DataType() != MetricDataTypeDoubleHistogram will cause a panic.
// Calling this function on zero-initialized Metric will cause a panic.
func (ms Metric) DoubleHistogram() DoubleHistogram {
return newDoubleHistogram(ms.orig.Data.(*otlpmetrics.Metric_DoubleHistogram).DoubleHistogram)
}
// 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_DoubleSummary).DoubleSummary)
}
func copyData(src, dest *otlpmetrics.Metric) {
switch srcData := (src).Data.(type) {
case *otlpmetrics.Metric_IntGauge:
data := &otlpmetrics.Metric_IntGauge{IntGauge: &otlpmetrics.IntGauge{}}
newIntGauge(srcData.IntGauge).CopyTo(newIntGauge(data.IntGauge))
dest.Data = data
case *otlpmetrics.Metric_DoubleGauge:
data := &otlpmetrics.Metric_DoubleGauge{DoubleGauge: &otlpmetrics.DoubleGauge{}}
newDoubleGauge(srcData.DoubleGauge).CopyTo(newDoubleGauge(data.DoubleGauge))
dest.Data = data
case *otlpmetrics.Metric_IntSum:
data := &otlpmetrics.Metric_IntSum{IntSum: &otlpmetrics.IntSum{}}
newIntSum(srcData.IntSum).CopyTo(newIntSum(data.IntSum))
dest.Data = data
case *otlpmetrics.Metric_DoubleSum:
data := &otlpmetrics.Metric_DoubleSum{DoubleSum: &otlpmetrics.DoubleSum{}}
newDoubleSum(srcData.DoubleSum).CopyTo(newDoubleSum(data.DoubleSum))
dest.Data = data
case *otlpmetrics.Metric_IntHistogram:
data := &otlpmetrics.Metric_IntHistogram{IntHistogram: &otlpmetrics.IntHistogram{}}
newIntHistogram(srcData.IntHistogram).CopyTo(newIntHistogram(data.IntHistogram))
dest.Data = data
case *otlpmetrics.Metric_DoubleHistogram:
data := &otlpmetrics.Metric_DoubleHistogram{DoubleHistogram: &otlpmetrics.DoubleHistogram{}}
newDoubleHistogram(srcData.DoubleHistogram).CopyTo(newDoubleHistogram(data.DoubleHistogram))
dest.Data = data
case *otlpmetrics.Metric_DoubleSummary:
data := &otlpmetrics.Metric_DoubleSummary{DoubleSummary: &otlpmetrics.DoubleSummary{}}
newSummary(srcData.DoubleSummary).CopyTo(newSummary(data.DoubleSummary))
dest.Data = data
}
}