-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
system.go
192 lines (183 loc) · 7.56 KB
/
system.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metrics"
import (
"strings"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)
const (
// divMebibytes specifies the number of bytes in a mebibyte.
divMebibytes = 1024 * 1024
// divPercentage specifies the division necessary for converting fractions to percentages.
divPercentage = 0.01
// otelNamespacePrefix specifies the namespace used for OpenTelemetry host metrics.
otelNamespacePrefix = "otel."
)
// copySystemMetric copies the metric from src by giving it a new name. If div differs from 1, it scales all
// data points.
//
// Warning: this is not a deep copy. Only some fields are fully copied, others remain shared. This is intentional.
// Do not alter the returned metric (or the source one) after copying.
func copySystemMetric(src datadogV2.MetricSeries, name string, div float64) datadogV2.MetricSeries {
return copySystemMetricWithUnit(src, name, div, "")
}
// copySystemMetricWithUnit is equivalent to copySystemMetric, but allows changing the unit.
func copySystemMetricWithUnit(src datadogV2.MetricSeries, name string, div float64, unit string) datadogV2.MetricSeries {
cp := src
cp.Metric = name
// No need to set cp.Interval if cp.Type is gauge.
cp.Type = datadogV2.METRICINTAKETYPE_GAUGE.Ptr()
if div == 0 || div == 1 || len(src.Points) == 0 {
// division by 0 or 1 should not have an impact
return cp
}
if unit != "" {
cp.Unit = &unit
}
cp.Points = make([]datadogV2.MetricPoint, len(src.Points))
for i, dp := range src.Points {
cp.Points[i].Timestamp = dp.Timestamp
if dp.Value != nil {
newdp := *dp.Value / div
cp.Points[i].Value = &newdp
}
}
return cp
}
// extractSystemMetrics takes an OpenTelemetry metric m and extracts Datadog system metrics from it,
// if m is a valid system metric. The boolean argument reports whether any system metrics were extractd.
func extractSystemMetrics(m datadogV2.MetricSeries) []datadogV2.MetricSeries {
var series []datadogV2.MetricSeries
switch m.Metric {
case "system.cpu.load_average.1m":
series = append(series, copySystemMetric(m, "system.load.1", 1))
case "system.cpu.load_average.5m":
series = append(series, copySystemMetric(m, "system.load.5", 1))
case "system.cpu.load_average.15m":
series = append(series, copySystemMetric(m, "system.load.15", 1))
case "system.cpu.utilization":
for _, tag := range m.Tags {
switch tag {
case "state:idle":
series = append(series, copySystemMetric(m, "system.cpu.idle", divPercentage))
case "state:user":
series = append(series, copySystemMetric(m, "system.cpu.user", divPercentage))
case "state:system":
series = append(series, copySystemMetric(m, "system.cpu.system", divPercentage))
case "state:wait":
series = append(series, copySystemMetric(m, "system.cpu.iowait", divPercentage))
case "state:steal":
series = append(series, copySystemMetric(m, "system.cpu.stolen", divPercentage))
}
}
case "system.memory.usage":
series = append(series, copySystemMetric(m, "system.mem.total", divMebibytes))
for _, tag := range m.Tags {
switch tag {
case "state:free", "state:cached", "state:buffered":
series = append(series, copySystemMetric(m, "system.mem.usable", divMebibytes))
}
}
case "system.network.io":
for _, tag := range m.Tags {
switch tag {
case "direction:receive":
series = append(series, copySystemMetric(m, "system.net.bytes_rcvd", 1))
case "direction:transmit":
series = append(series, copySystemMetric(m, "system.net.bytes_sent", 1))
}
}
case "system.paging.usage":
for _, tag := range m.Tags {
switch tag {
case "state:free":
series = append(series, copySystemMetric(m, "system.swap.free", divMebibytes))
case "state:used":
series = append(series, copySystemMetric(m, "system.swap.used", divMebibytes))
}
}
case "system.filesystem.utilization":
series = append(series, copySystemMetric(m, "system.disk.in_use", 1))
}
return series
}
// PrepareSystemMetrics prepends system hosts metrics with the otel.* prefix to identify
// them as part of the Datadog OpenTelemetry Integration. It also extracts Datadog compatible
// system metrics and returns the full set of metrics to be used.
func PrepareSystemMetrics(ms []datadogV2.MetricSeries) []datadogV2.MetricSeries {
series := ms
for i, m := range ms {
if !strings.HasPrefix(m.Metric, "system.") &&
!strings.HasPrefix(m.Metric, "process.") {
// not a system metric
continue
}
series = append(series, extractSystemMetrics(m)...)
// all existing system metrics need to be prepended
newname := otelNamespacePrefix + m.Metric
series[i].Metric = newname
}
return series
}
// PrepareContainerMetrics converts OTEL container.* metrics to Datadog container
// metrics.
func PrepareContainerMetrics(ms []datadogV2.MetricSeries) []datadogV2.MetricSeries {
series := ms
for _, m := range ms {
if !strings.HasPrefix(m.Metric, "container.") {
// not what we're looking for
continue
}
switch m.Metric {
case "container.cpu.usage.total":
series = append(series, copySystemMetricWithUnit(m, "container.cpu.usage", 1, "nanocore"))
case "container.cpu.usage.usermode":
series = append(series, copySystemMetricWithUnit(m, "container.cpu.user", 1, "nanocore"))
case "container.cpu.usage.system":
series = append(series, copySystemMetricWithUnit(m, "container.cpu.system", 1, "nanocore"))
case "container.cpu.throttling_data.throttled_time":
series = append(series, copySystemMetric(m, "container.cpu.throttled", 1))
case "container.cpu.throttling_data.throttled_periods":
series = append(series, copySystemMetric(m, "container.cpu.throttled.periods", 1))
case "container.memory.usage.total":
series = append(series, copySystemMetric(m, "container.memory.usage", 1))
case "container.memory.active_anon":
series = append(series, copySystemMetric(m, "container.memory.kernel", 1))
case "container.memory.hierarchical_memory_limit":
series = append(series, copySystemMetric(m, "container.memory.limit", 1))
case "container.memory.usage.limit":
series = append(series, copySystemMetric(m, "container.memory.soft_limit", 1))
case "container.memory.total_cache":
series = append(series, copySystemMetric(m, "container.memory.cache", 1))
case "container.memory.total_swap":
series = append(series, copySystemMetric(m, "container.memory.swap", 1))
case "container.blockio.io_service_bytes_recursive":
for _, tag := range m.Tags {
switch tag {
case "operation:write":
series = append(series, copySystemMetric(m, "container.io.write", 1))
case "operation:read":
series = append(series, copySystemMetric(m, "container.io.read", 1))
}
}
case "container.blockio.io_serviced_recursive":
for _, tag := range m.Tags {
switch tag {
case "operation:write":
series = append(series, copySystemMetric(m, "container.io.write.operations", 1))
case "operation:read":
series = append(series, copySystemMetric(m, "container.io.read.operations", 1))
}
}
case "container.network.io.usage.tx_bytes":
series = append(series, copySystemMetric(m, "container.net.sent", 1))
case "container.network.io.usage.tx_packets":
series = append(series, copySystemMetric(m, "container.net.sent.packets", 1))
case "container.network.io.usage.rx_bytes":
series = append(series, copySystemMetric(m, "container.net.rcvd", 1))
case "container.network.io.usage.rx_packets":
series = append(series, copySystemMetric(m, "container.net.rcvd.packets", 1))
}
}
return series
}