-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathhistogram.go
More file actions
100 lines (82 loc) · 2.73 KB
/
histogram.go
File metadata and controls
100 lines (82 loc) · 2.73 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package datapoints // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/datapoints"
import (
"fmt"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/elasticsearch"
)
type Histogram struct {
pmetric.HistogramDataPoint
elasticsearch.MappingHintGetter
metric pmetric.Metric
}
func NewHistogram(metric pmetric.Metric, dp pmetric.HistogramDataPoint) Histogram {
return Histogram{
HistogramDataPoint: dp,
MappingHintGetter: elasticsearch.NewMappingHintGetter(dp.Attributes()),
metric: metric,
}
}
func (dp Histogram) Value() (pcommon.Value, error) {
if dp.HasMappingHint(elasticsearch.HintAggregateMetricDouble) {
vm := pcommon.NewValueMap()
m := vm.Map()
m.PutDouble("sum", dp.Sum())
m.PutInt("value_count", safeUint64ToInt64(dp.Count()))
return vm, nil
}
return histogramToValue(dp.HistogramDataPoint, dp.metric)
}
func (dp Histogram) DynamicTemplate(_ pmetric.Metric) string {
if dp.HasMappingHint(elasticsearch.HintAggregateMetricDouble) {
return "summary"
}
return "histogram"
}
func (dp Histogram) DocCount() uint64 {
return dp.Count()
}
func (dp Histogram) Metric() pmetric.Metric {
return dp.metric
}
func histogramToValue(dp pmetric.HistogramDataPoint, metric pmetric.Metric) (pcommon.Value, error) {
// Histogram conversion function is from
// https://github.com/elastic/apm-data/blob/3b28495c3cbdc0902983134276eb114231730249/input/otlp/metrics.go#L277
bucketCounts := dp.BucketCounts()
explicitBounds := dp.ExplicitBounds()
if bucketCounts.Len() != explicitBounds.Len()+1 || explicitBounds.Len() == 0 {
return pcommon.Value{}, fmt.Errorf("invalid histogram data point %q", metric.Name())
}
vm := pcommon.NewValueMap()
m := vm.Map()
counts := m.PutEmptySlice("counts")
values := m.PutEmptySlice("values")
values.EnsureCapacity(bucketCounts.Len())
counts.EnsureCapacity(bucketCounts.Len())
for i, count := range bucketCounts.All() {
if count == 0 {
continue
}
var value float64
switch i {
// (-infinity, explicit_bounds[i]]
case 0:
value = explicitBounds.At(i)
if value > 0 {
value /= 2
}
// (explicit_bounds[i], +infinity)
case bucketCounts.Len() - 1:
value = explicitBounds.At(i - 1)
// [explicit_bounds[i-1], explicit_bounds[i])
default:
// Use the midpoint between the boundaries.
value = explicitBounds.At(i-1) + (explicitBounds.At(i)-explicitBounds.At(i-1))/2.0
}
counts.AppendEmpty().SetInt(safeUint64ToInt64(count))
values.AppendEmpty().SetDouble(value)
}
return vm, nil
}