-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtranslate_docker_metrics_processor.go
157 lines (137 loc) · 6.64 KB
/
translate_docker_metrics_processor.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sumologicprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/sumologicprocessor"
import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)
// translateTelegrafMetricsProcessor translates metric names from OpenTelemetry to Sumo Logic convention
type translateDockerMetricsProcessor struct {
shouldTranslate bool
}
// metricsTranslations maps Telegraf metric names to corresponding names in Sumo Logic convention
var dockerMetricsTranslations = map[string]string{
"container.cpu.percent": "cpu_percentage",
"container.cpu.usage.system": "system_cpu_usage",
"container.cpu.usage.percpu": "cpu_usage.percpu_usage",
"container.cpu.usage.total": "cpu_usage.total_usage",
"container.cpu.usage.kernelmode": "cpu_usage.usage_in_kernelmode",
"container.cpu.usage.usermode": "cpu_usage.usage_in_usermode",
"container.cpu.throttling_data.periods": "throttling_data.periods",
"container.cpu.throttling_data.throttled_periods": "throttling_data.throttled_periods",
"container.cpu.throttling_data.throttled_time": "throttling_data.throttled_time",
"container.memory.usage.limit": "limit",
"container.memory.usage.max": "max_usage",
"container.memory.percent": "memory_percentage",
"container.memory.usage.total": "usage",
"container.memory.active_anon": "stats.active_anon",
"container.memory.active_file": "stats.active_file",
"container.memory.cache": "stats.cache",
"container.memory.hierarchical_memory_limit": "stats.hierarchical_memory_limit",
"container.memory.inactive_anon": "stats.inactive_anon",
"container.memory.inactive_file": "stats.inactive_file",
"container.memory.mapped_file": "stats.mapped_file",
"container.memory.pgfault": "stats.pgfault",
"container.memory.pgmajfault": "stats.pgmajfault",
"container.memory.pgpgin": "stats.pgpgin",
"container.memory.pgpgout": "stats.pgpgout",
"container.memory.rss": "stats.rss",
"container.memory.rss_huge": "stats.rss_huge",
"container.memory.unevictable": "stats.unevictable",
"container.memory.writeback": "stats.writeback",
"container.memory.total_active_anon": "stats.total_active_anon",
"container.memory.total_active_file": "stats.total_active_file",
"container.memory.total_cache": "stats.total_cache",
"container.memory.total_inactive_anon": "stats.total_inactive_anon",
"container.memory.total_mapped_file": "stats.total_mapped_file",
"container.memory.total_pgfault": "stats.total_pgfault",
"container.memory.total_pgmajfault": "stats.total_pgmajfault",
"container.memory.total_pgpgin": "stats.total_pgpgin",
"container.memory.total_pgpgout": "stats.total_pgpgout",
"container.memory.total_rss": "stats.total_rss",
"container.memory.total_rss_huge": "stats.total_rss_huge",
"container.memory.total_unevictable": "stats.total_unevictable",
"container.memory.total_writeback": "stats.total_writeback",
"container.blockio.io_merged_recursive": "io_merged_recursive",
"container.blockio.io_queued_recursive": "io_queue_recursive",
"container.blockio.io_service_bytes_recursive": "io_service_bytes_recursive",
"container.blockio.io_service_time_recursive": "io_service_time_recursive",
"container.blockio.io_serviced_recursive": "io_serviced_recursive",
"container.blockio.io_time_recursive": "io_time_recursive",
"container.blockio.io_wait_time_recursive": "io_wait_time_recursive",
"container.blockio.sectors_recursive": "sectors_recursive",
}
var dockerReasourceAttributeTranslations = map[string]string{
"container.id": "container.FullID",
"container.image.name": "container.ImageName",
"container.name": "container.Name",
}
func newTranslateDockerMetricsProcessor(shouldTranslate bool) *translateDockerMetricsProcessor {
return &translateDockerMetricsProcessor{
shouldTranslate: shouldTranslate,
}
}
func (proc *translateDockerMetricsProcessor) processLogs(_ plog.Logs) error {
// No-op, this subprocessor doesn't process logs.
return nil
}
func (proc *translateDockerMetricsProcessor) processMetrics(metrics pmetric.Metrics) error {
if !proc.shouldTranslate {
return nil
}
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
rm := metrics.ResourceMetrics().At(i)
translateDockerResourceAttributes(rm.Resource().Attributes())
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
metricsSlice := rm.ScopeMetrics().At(j).Metrics()
for k := 0; k < metricsSlice.Len(); k++ {
translateDockerMetric(metricsSlice.At(k))
}
}
}
return nil
}
func (proc *translateDockerMetricsProcessor) processTraces(_ ptrace.Traces) error {
// No-op, this subprocessor doesn't process traces.
return nil
}
func (proc *translateDockerMetricsProcessor) isEnabled() bool {
return proc.shouldTranslate
}
func (*translateDockerMetricsProcessor) ConfigPropertyName() string {
return "translate_docker_metrics"
}
func translateDockerMetric(m pmetric.Metric) {
name, exists := dockerMetricsTranslations[m.Name()]
if exists {
m.SetName(name)
}
}
func translateDockerResourceAttributes(attributes pcommon.Map) {
result := pcommon.NewMap()
result.EnsureCapacity(attributes.Len())
attributes.Range(func(otKey string, value pcommon.Value) bool {
if sumoKey, ok := dockerReasourceAttributeTranslations[otKey]; ok {
// Only insert if it doesn't exist yet to prevent overwriting.
// We have to do it this way since the final return value is not
// ready yet to rely on .Insert() not overwriting.
if _, exists := attributes.Get(sumoKey); !exists {
if _, ok := result.Get(sumoKey); !ok {
value.CopyTo(result.PutEmpty(sumoKey))
}
} else {
if _, ok := result.Get(otKey); !ok {
value.CopyTo(result.PutEmpty(otKey))
}
}
} else {
if _, ok := result.Get(otKey); !ok {
value.CopyTo(result.PutEmpty(otKey))
}
}
return true
})
result.CopyTo(attributes)
}