-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
159 lines (136 loc) · 3.18 KB
/
identity.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
package identity
import (
"hash"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
type resource = Resource
type Resource struct {
attrs pcommon.Map
}
func OfResource(r pcommon.Resource) Resource {
return Resource{attrs: r.Attributes()}
}
func (ra Resource) Equals(rb Resource) bool {
return equalMap(ra.attrs, rb.attrs)
}
func (r Resource) WriteHash(h hash.Hash) {
sum := pdatautil.MapHash(r.attrs)
h.Write(sum[:])
}
type scope = Scope
type Scope struct {
resource
name string
version string
attrs pcommon.Map
}
func OfScope(r Resource, s pcommon.InstrumentationScope) Scope {
return Scope{
resource: r,
name: s.Name(),
version: s.Version(),
attrs: s.Attributes(),
}
}
func (sa Scope) Equals(sb Scope) bool {
return sa.resource.Equals(sb.resource) &&
sa.name == sb.name &&
sa.version == sb.version &&
equalMap(sa.attrs, sb.attrs)
}
func (s Scope) WriteHash(h hash.Hash) {
s.resource.WriteHash(h)
h.Write([]byte(s.name))
h.Write([]byte(s.version))
sum := pdatautil.MapHash(s.attrs)
h.Write(sum[:])
}
type metric = Metric
type Metric struct {
scope
name string
unit string
typ pmetric.MetricType
monotonic bool
temporality pmetric.AggregationTemporality
}
func OfMetric(s Scope, m pmetric.Metric) Metric {
id := Metric{
scope: s,
name: m.Name(),
unit: m.Unit(),
typ: m.Type(),
}
switch m.Type() {
case pmetric.MetricTypeSum:
sum := m.Sum()
id.monotonic = sum.IsMonotonic()
id.temporality = sum.AggregationTemporality()
case pmetric.MetricTypeHistogram:
hist := m.Histogram()
id.monotonic = true
id.temporality = hist.AggregationTemporality()
case pmetric.MetricTypeExponentialHistogram:
exp := m.ExponentialHistogram()
id.monotonic = true
id.temporality = exp.AggregationTemporality()
}
return id
}
func (ma Metric) Equals(mb Metric) bool {
if !ma.scope.Equals(mb.scope) ||
ma.name != mb.name ||
ma.unit != mb.unit ||
ma.typ != mb.typ {
return false
}
switch ma.typ {
case pmetric.MetricTypeEmpty:
return true
case pmetric.MetricTypeGauge:
return true
case pmetric.MetricTypeSum:
return ma.monotonic == mb.monotonic && ma.temporality == mb.temporality
case pmetric.MetricTypeHistogram:
return ma.temporality == mb.temporality
case pmetric.MetricTypeExponentialHistogram:
return ma.temporality == mb.temporality
case pmetric.MetricTypeSummary:
return true
default:
return false
}
}
func (m Metric) WriteHash(h hash.Hash) {
m.scope.WriteHash(h)
h.Write([]byte(m.name))
h.Write([]byte(m.unit))
var monotonic byte
if m.monotonic {
monotonic = 1
}
h.Write([]byte{byte(m.typ), monotonic, byte(m.temporality)})
}
type Stream struct {
metric
attrs pcommon.Map
}
func OfStream[DP dataPoint](m Metric, dp DP) Stream {
return Stream{
metric: m,
attrs: dp.Attributes(),
}
}
type dataPoint interface {
Attributes() pcommon.Map
}
func (sa Stream) Equals(sb Stream) bool {
return sa.metric.Equals(sb.metric) && equalMap(sa.attrs, sb.attrs)
}
func (s Stream) WriteHash(h hash.Hash) {
s.metric.WriteHash(h)
sum := pdatautil.MapHash(s.attrs)
h.Write(sum[:])
}