forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
165 lines (154 loc) · 4.47 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
// Copyright 2020, 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:https://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 elastic contains an opentelemetry-collector exporter
// for Elastic APM.
// nolint:gocritic
package elastic // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticexporter/internal/translator/elastic"
import (
"sort"
"strings"
"time"
"go.elastic.co/apm/model"
"go.elastic.co/fastjson"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
// EncodeMetrics encodes an OpenTelemetry metrics slice, and instrumentation
// library information, as one or more metricset lines, writing to w.
//
// TODO(axw) otlpLibrary is currently not used. We should consider recording
// it as metadata.
func EncodeMetrics(otlpMetrics pmetric.MetricSlice, otlpLibrary pcommon.InstrumentationScope, w *fastjson.Writer) (dropped int, _ error) {
var metricsets metricsets
for i := 0; i < otlpMetrics.Len(); i++ {
metric := otlpMetrics.At(i)
name := metric.Name()
switch metric.DataType() {
case pmetric.MetricDataTypeGauge:
doubleGauge := metric.Gauge()
dps := doubleGauge.DataPoints()
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
var val float64
switch dp.ValueType() {
case pmetric.NumberDataPointValueTypeDouble:
val = dp.DoubleVal()
case pmetric.NumberDataPointValueTypeInt:
val = float64(dp.IntVal())
}
metricsets.upsert(model.Metrics{
Timestamp: asTime(dp.Timestamp()),
Labels: asStringMap(dp.Attributes()),
Samples: map[string]model.Metric{name: {
Value: val,
}},
})
}
case pmetric.MetricDataTypeSum:
doubleSum := metric.Sum()
dps := doubleSum.DataPoints()
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
var val float64
switch dp.ValueType() {
case pmetric.NumberDataPointValueTypeDouble:
val = dp.DoubleVal()
case pmetric.NumberDataPointValueTypeInt:
val = float64(dp.IntVal())
}
metricsets.upsert(model.Metrics{
Timestamp: asTime(dp.Timestamp()),
Labels: asStringMap(dp.Attributes()),
Samples: map[string]model.Metric{name: {
Value: val,
}},
})
}
case pmetric.MetricDataTypeHistogram:
// TODO(axw) requires https://github.com/elastic/apm-server/issues/3195
doubleHistogram := metric.Histogram()
dropped += doubleHistogram.DataPoints().Len()
default:
// Unknown type, so just increment dropped by 1 as a best effort.
dropped++
}
}
for _, metricset := range metricsets {
w.RawString(`{"metricset":`)
if err := metricset.MarshalFastJSON(w); err != nil {
return dropped, err
}
w.RawString("}\n")
}
return dropped, nil
}
func asTime(in pcommon.Timestamp) model.Time {
return model.Time(time.Unix(0, int64(in)))
}
func asStringMap(in pcommon.Map) model.StringMap {
var out model.StringMap
in.Sort()
in.Range(func(k string, v pcommon.Value) bool {
out = append(out, model.StringMapItem{
Key: k,
Value: v.AsString(),
})
return true
})
return out
}
type metricsets []model.Metrics
func (ms *metricsets) upsert(m model.Metrics) {
i := ms.search(m)
if i < len(*ms) && compareMetricsets((*ms)[i], m) == 0 {
existing := (*ms)[i]
for k, v := range m.Samples {
existing.Samples[k] = v
}
} else {
head := (*ms)[:i]
tail := append([]model.Metrics{m}, (*ms)[i:]...)
*ms = append(head, tail...)
}
}
func (ms *metricsets) search(m model.Metrics) int {
return sort.Search(len(*ms), func(i int) bool {
return compareMetricsets((*ms)[i], m) >= 0
})
}
func compareMetricsets(a, b model.Metrics) int {
atime, btime := time.Time(a.Timestamp), time.Time(b.Timestamp)
if atime.Before(btime) {
return -1
} else if atime.After(btime) {
return 1
}
n := len(a.Labels) - len(b.Labels)
switch {
case n < 0:
return -1
case n > 0:
return 1
}
for i, la := range a.Labels {
lb := b.Labels[i]
if n := strings.Compare(la.Key, lb.Key); n != 0 {
return n
}
if n := strings.Compare(la.Value, lb.Value); n != 0 {
return n
}
}
return 0
}