-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
metric_to_envelopes.go
238 lines (197 loc) · 7.47 KB
/
metric_to_envelopes.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
// Copyright 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 azuremonitorexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter"
import (
"time"
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)
type metricPacker struct {
logger *zap.Logger
}
type timedMetricDataPoint struct {
dataPoint *contracts.DataPoint
timestamp pcommon.Timestamp
}
type metricTimedData interface {
getTimedDataPoints() []*timedMetricDataPoint
}
// MetricToEnvelopes packages metrics into a slice of Application Insight envelopes.
func (packer *metricPacker) MetricToEnvelopes(metric pmetric.Metric, resource pcommon.Resource, instrumentationScope pcommon.InstrumentationScope) []*contracts.Envelope {
var envelopes []*contracts.Envelope
mtd := packer.getMetricTimedData(metric)
if mtd != nil {
for _, timedDataPoint := range mtd.getTimedDataPoints() {
envelope := contracts.NewEnvelope()
envelope.Tags = make(map[string]string)
envelope.Time = toTime(timedDataPoint.timestamp).Format(time.RFC3339Nano)
metricData := contracts.NewMetricData()
dataPoint := timedDataPoint.dataPoint
metricData.Metrics = []*contracts.DataPoint{dataPoint}
metricData.Properties = make(map[string]string)
envelope.Name = metricData.EnvelopeName("")
data := contracts.NewData()
data.BaseData = metricData
data.BaseType = metricData.BaseType()
envelope.Data = data
resourceAttributes := resource.Attributes()
applyResourcesToDataProperties(metricData.Properties, resourceAttributes)
applyInstrumentationScopeValueToDataProperties(metricData.Properties, instrumentationScope)
applyCloudTagsToEnvelope(envelope, resourceAttributes)
packer.sanitize(func() []string { return metricData.Sanitize() })
packer.sanitize(func() []string { return envelope.Sanitize() })
packer.sanitize(func() []string { return contracts.SanitizeTags(envelope.Tags) })
packer.logger.Debug("Metric is packed", zap.String("name", dataPoint.Name), zap.Any("value", dataPoint.Value))
envelopes = append(envelopes, envelope)
}
}
return envelopes
}
func (packer *metricPacker) sanitize(sanitizeFunc func() []string) {
for _, warning := range sanitizeFunc() {
packer.logger.Warn(warning)
}
}
func newMetricPacker(logger *zap.Logger) *metricPacker {
packer := &metricPacker{
logger: logger,
}
return packer
}
func (packer metricPacker) getMetricTimedData(metric pmetric.Metric) metricTimedData {
switch metric.Type() {
case pmetric.MetricTypeGauge:
return newScalarMetric(metric.Name(), metric.Gauge().DataPoints())
case pmetric.MetricTypeSum:
return newScalarMetric(metric.Name(), metric.Sum().DataPoints())
case pmetric.MetricTypeHistogram:
return newHistogramMetric(metric.Name(), metric.Histogram().DataPoints())
case pmetric.MetricTypeExponentialHistogram:
return newExponentialHistogramMetric(metric.Name(), metric.ExponentialHistogram().DataPoints())
case pmetric.MetricTypeSummary:
return newSummaryMetric(metric.Name(), metric.Summary().DataPoints())
}
packer.logger.Debug("Unsupported metric type", zap.Any("Metric Type", metric.Type()))
return nil
}
type scalarMetric struct {
name string
dataPointSlice pmetric.NumberDataPointSlice
}
func newScalarMetric(name string, dataPointSlice pmetric.NumberDataPointSlice) *scalarMetric {
return &scalarMetric{
name: name,
dataPointSlice: dataPointSlice,
}
}
func (m scalarMetric) getTimedDataPoints() []*timedMetricDataPoint {
timedDataPoints := make([]*timedMetricDataPoint, m.dataPointSlice.Len())
for i := 0; i < m.dataPointSlice.Len(); i++ {
numberDataPoint := m.dataPointSlice.At(i)
dataPoint := contracts.NewDataPoint()
dataPoint.Name = m.name
dataPoint.Value = numberDataPoint.DoubleValue()
dataPoint.Count = 1
dataPoint.Kind = contracts.Measurement
timedDataPoints[i] = &timedMetricDataPoint{
dataPoint: dataPoint,
timestamp: numberDataPoint.Timestamp(),
}
}
return timedDataPoints
}
type histogramMetric struct {
name string
dataPointSlice pmetric.HistogramDataPointSlice
}
func newHistogramMetric(name string, dataPointSlice pmetric.HistogramDataPointSlice) *histogramMetric {
return &histogramMetric{
name: name,
dataPointSlice: dataPointSlice,
}
}
func (m histogramMetric) getTimedDataPoints() []*timedMetricDataPoint {
timedDataPoints := make([]*timedMetricDataPoint, m.dataPointSlice.Len())
for i := 0; i < m.dataPointSlice.Len(); i++ {
histogramDataPoint := m.dataPointSlice.At(i)
dataPoint := contracts.NewDataPoint()
dataPoint.Name = m.name
dataPoint.Value = histogramDataPoint.Sum()
dataPoint.Kind = contracts.Aggregation
dataPoint.Min = histogramDataPoint.Min()
dataPoint.Max = histogramDataPoint.Max()
dataPoint.Count = int(histogramDataPoint.Count())
timedDataPoints[i] = &timedMetricDataPoint{
dataPoint: dataPoint,
timestamp: histogramDataPoint.Timestamp(),
}
}
return timedDataPoints
}
type exponentialHistogramMetric struct {
name string
dataPointSlice pmetric.ExponentialHistogramDataPointSlice
}
func newExponentialHistogramMetric(name string, dataPointSlice pmetric.ExponentialHistogramDataPointSlice) *exponentialHistogramMetric {
return &exponentialHistogramMetric{
name: name,
dataPointSlice: dataPointSlice,
}
}
func (m exponentialHistogramMetric) getTimedDataPoints() []*timedMetricDataPoint {
timedDataPoints := make([]*timedMetricDataPoint, m.dataPointSlice.Len())
for i := 0; i < m.dataPointSlice.Len(); i++ {
exponentialHistogramDataPoint := m.dataPointSlice.At(i)
dataPoint := contracts.NewDataPoint()
dataPoint.Name = m.name
dataPoint.Value = exponentialHistogramDataPoint.Sum()
dataPoint.Kind = contracts.Aggregation
dataPoint.Min = exponentialHistogramDataPoint.Min()
dataPoint.Max = exponentialHistogramDataPoint.Max()
dataPoint.Count = int(exponentialHistogramDataPoint.Count())
timedDataPoints[i] = &timedMetricDataPoint{
dataPoint: dataPoint,
timestamp: exponentialHistogramDataPoint.Timestamp(),
}
}
return timedDataPoints
}
type summaryMetric struct {
name string
dataPointSlice pmetric.SummaryDataPointSlice
}
func newSummaryMetric(name string, dataPointSlice pmetric.SummaryDataPointSlice) *summaryMetric {
return &summaryMetric{
name: name,
dataPointSlice: dataPointSlice,
}
}
func (m summaryMetric) getTimedDataPoints() []*timedMetricDataPoint {
timedDataPoints := make([]*timedMetricDataPoint, m.dataPointSlice.Len())
for i := 0; i < m.dataPointSlice.Len(); i++ {
summaryDataPoint := m.dataPointSlice.At(i)
dataPoint := contracts.NewDataPoint()
dataPoint.Name = m.name
dataPoint.Value = summaryDataPoint.Sum()
dataPoint.Kind = contracts.Aggregation
dataPoint.Count = int(summaryDataPoint.Count())
timedDataPoints[i] = &timedMetricDataPoint{
dataPoint: dataPoint,
timestamp: summaryDataPoint.Timestamp(),
}
}
return timedDataPoints
}