forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram_utils.go
More file actions
142 lines (123 loc) · 5.17 KB
/
histogram_utils.go
File metadata and controls
142 lines (123 loc) · 5.17 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package signalfxexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter"
import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)
// removeAccessToken removes the SFX access token label if found in the give resource metric as a resource attribute
func removeAccessToken(dest pmetric.ResourceMetrics) {
dest.Resource().Attributes().RemoveIf(func(k string, _ pcommon.Value) bool {
return k == splunk.SFxAccessTokenLabel
})
}
// matchedHistogramResourceMetrics returns a map with the keys set to the index of resource Metrics containing
// Histogram metric type.
// The value is another map consisting of the ScopeMetric indices in the RM which contain Histogram metric type as keys
// and the value as an int slice with indices of the Histogram metric within the given scope.
// Example output {1: {1: [0,2]}}.
// The above output can be interpreted as Resource at index 1 contains Histogram metrics.
// Within this resource specifically the scope metric at index 1 contain Histograms.
// Lastly, the scope metric at index 1 has two Histogram type metric which can be found at index 0 and 2.
func matchedHistogramResourceMetrics(md pmetric.Metrics) (matchedRMIdx map[int]map[int][]int) {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
matchedSMIdx := matchedHistogramScopeMetrics(rm)
if len(matchedSMIdx) > 0 {
if matchedRMIdx == nil {
matchedRMIdx = map[int]map[int][]int{}
}
matchedRMIdx[i] = matchedSMIdx
}
}
return matchedRMIdx
}
// matchedHistogramScopeMetrics returns a map with keys equal to the ScopeMetric indices in the input resource metric
// which contain Histogram metric type.
// And the value is an int slice with indices of the Histogram metric within the keyed scope metric.
// Example output {1: [0,2]}.
// The above output can be interpreted as scope metrics at index 1 contains Histogram metrics.
// And that the scope metric at index 1 has two Histogram type metric which can be found at index 0 and 2.
func matchedHistogramScopeMetrics(rm pmetric.ResourceMetrics) (matchedSMIdx map[int][]int) {
ilms := rm.ScopeMetrics()
for i := 0; i < ilms.Len(); i++ {
ilm := ilms.At(i)
matchedMetricsIdx := matchedHistogramMetrics(ilm)
if len(matchedMetricsIdx) > 0 {
if matchedSMIdx == nil {
matchedSMIdx = map[int][]int{}
}
matchedSMIdx[i] = matchedMetricsIdx
}
}
return matchedSMIdx
}
// matchedHistogramMetrics returns an int slice with indices of metrics which are of Histogram type
// within the input scope metric.
// Example output [0,2].
// The above output can be interpreted as input scope metric has Histogram type metric at index 0 and 2.
func matchedHistogramMetrics(ilm pmetric.ScopeMetrics) (matchedMetricsIdx []int) {
ms := ilm.Metrics()
for i := 0; i < ms.Len(); i++ {
metric := ms.At(i)
if metric.Type() == pmetric.MetricTypeHistogram {
matchedMetricsIdx = append(matchedMetricsIdx, i)
}
}
return matchedMetricsIdx
}
// getHistograms returns new Metrics slice containing only Histogram metrics found in the input
// and the count of histogram metrics
// This function also adds the host ID attribute to the resource if it can be derived from the resource attributes
func getHistograms(md pmetric.Metrics) (pmetric.Metrics, int) {
matchedMetricsIdxes := matchedHistogramResourceMetrics(md)
matchedRmCount := len(matchedMetricsIdxes)
if matchedRmCount == 0 {
return pmetric.Metrics{}, 0
}
metricCount := 0
srcRms := md.ResourceMetrics()
dest := pmetric.NewMetrics()
dstRms := dest.ResourceMetrics()
dstRms.EnsureCapacity(matchedRmCount)
// Iterate over those ResourceMetrics which were found to contain histograms
for rmIdx, ilmMap := range matchedMetricsIdxes {
srcRm := srcRms.At(rmIdx)
if hostID, ok := splunk.ResourceToHostID(srcRm.Resource()); ok && hostID.Key != splunk.HostIDKeyHost {
srcRm.Resource().Attributes().PutStr(string(hostID.Key), hostID.ID)
}
// Copy resource metric properties to dest
destRm := dstRms.AppendEmpty()
srcRm.Resource().CopyTo(destRm.Resource())
destRm.SetSchemaUrl(srcRm.SchemaUrl())
// Remove Sfx access token
removeAccessToken(destRm)
matchedIlmCount := len(ilmMap)
destIlms := destRm.ScopeMetrics()
destIlms.EnsureCapacity(matchedIlmCount)
srcIlms := srcRm.ScopeMetrics()
// Iterate over ScopeMetrics which were found to contain histograms
for ilmIdx, metricIdxes := range ilmMap {
srcIlm := srcIlms.At(ilmIdx)
// Copy scope properties to dest
destIlm := destIlms.AppendEmpty()
srcIlm.Scope().CopyTo(destIlm.Scope())
destIlm.SetSchemaUrl(srcIlm.SchemaUrl())
matchedMetricCount := len(metricIdxes)
destMs := destIlm.Metrics()
destMs.EnsureCapacity(matchedMetricCount)
srcMs := srcIlm.Metrics()
// Iterate over Metrics which contain histograms
for _, srcIdx := range metricIdxes {
srcMetric := srcMs.At(srcIdx)
// Copy metric properties to dest
destMetric := destMs.AppendEmpty()
srcMetric.CopyTo(destMetric)
metricCount++
}
}
}
return dest, metricCount
}