-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
metricdata.go
158 lines (131 loc) · 3.55 KB
/
metricdata.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
// 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 main
import (
"fmt"
"go.opentelemetry.io/collector/model/pdata"
)
var (
_ MetricData = &gauge{}
_ MetricData = &sum{}
_ MetricData = &histogram{}
)
// MetricData is generic interface for all metric datatypes.
type MetricData interface {
Type() string
HasMonotonic() bool
HasAggregated() bool
HasMetricValueType() bool
}
// Aggregated defines a metric aggregation type.
type Aggregated struct {
// Aggregation describes if the aggregator reports delta changes
// since last report time, or cumulative changes since a fixed start time.
Aggregation string `mapstructure:"aggregation" validate:"oneof=delta cumulative"`
}
// Type gets the metric aggregation type.
func (agg Aggregated) Type() string {
switch agg.Aggregation {
case "delta":
return "pdata.MetricAggregationTemporalityDelta"
case "cumulative":
return "pdata.MetricAggregationTemporalityCumulative"
default:
return "pdata.MetricAggregationTemporalityUnknown"
}
}
// Mono defines the metric monotonicity.
type Mono struct {
// Monotonic is true if the sum is monotonic.
Monotonic bool `mapstructure:"monotonic"`
}
// MetricValueType defines the metric number type.
type MetricValueType struct {
// ValueType is type of the metric number, options are "double", "int".
ValueType pdata.MetricValueType `validate:"required"`
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (mvt *MetricValueType) UnmarshalText(text []byte) error {
switch vtStr := string(text); vtStr {
case "int":
mvt.ValueType = pdata.MetricValueTypeInt
case "double":
mvt.ValueType = pdata.MetricValueTypeDouble
default:
return fmt.Errorf("invalid value_type: %q", vtStr)
}
return nil
}
// Type returns name of the datapoint type.
func (mvt MetricValueType) String() string {
return mvt.ValueType.String()
}
// BasicType returns name of a golang basic type for the datapoint type.
func (mvt MetricValueType) BasicType() string {
switch mvt.ValueType {
case pdata.MetricValueTypeInt:
return "int64"
case pdata.MetricValueTypeDouble:
return "float64"
default:
return ""
}
}
type gauge struct {
MetricValueType `mapstructure:"value_type"`
}
func (d gauge) Type() string {
return "Gauge"
}
func (d gauge) HasMonotonic() bool {
return false
}
func (d gauge) HasAggregated() bool {
return false
}
func (d gauge) HasMetricValueType() bool {
return true
}
type sum struct {
Aggregated `mapstructure:",squash"`
Mono `mapstructure:",squash"`
MetricValueType `mapstructure:"value_type"`
}
func (d sum) Type() string {
return "Sum"
}
func (d sum) HasMonotonic() bool {
return true
}
func (d sum) HasAggregated() bool {
return true
}
func (d sum) HasMetricValueType() bool {
return true
}
type histogram struct {
Aggregated `mapstructure:",squash"`
}
func (d histogram) Type() string {
return "Histogram"
}
func (d histogram) HasMonotonic() bool {
return false
}
func (d histogram) HasAggregated() bool {
return true
}
func (d histogram) HasMetricValueType() bool {
return false
}