-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
metrics.go
142 lines (119 loc) · 4.9 KB
/
metrics.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
// Copyright 2020, 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.
//go:build !windows
// +build !windows
package podmanreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/podmanreceiver"
import (
"fmt"
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)
type point struct {
intVal uint64
doubleVal float64
attributes map[string]string
}
func containerStatsToMetrics(ts time.Time, container container, stats *containerStats) pmetric.Metrics {
pbts := pcommon.NewTimestampFromTime(ts)
md := pmetric.NewMetrics()
rs := md.ResourceMetrics().AppendEmpty()
resourceAttr := rs.Resource().Attributes()
resourceAttr.PutStr(conventions.AttributeContainerRuntime, "podman")
resourceAttr.PutStr(conventions.AttributeContainerName, stats.Name)
resourceAttr.PutStr(conventions.AttributeContainerID, stats.ContainerID)
resourceAttr.PutStr(conventions.AttributeContainerImageName, container.Image)
ms := rs.ScopeMetrics().AppendEmpty().Metrics()
appendIOMetrics(ms, stats, pbts)
appendCPUMetrics(ms, stats, pbts)
appendNetworkMetrics(ms, stats, pbts)
appendMemoryMetrics(ms, stats, pbts)
return md
}
func appendMemoryMetrics(ms pmetric.MetricSlice, stats *containerStats, ts pcommon.Timestamp) {
gaugeI(ms, "memory.usage.limit", "By", []point{{intVal: stats.MemLimit}}, ts)
gaugeI(ms, "memory.usage.total", "By", []point{{intVal: stats.MemUsage}}, ts)
gaugeF(ms, "memory.percent", "1", []point{{doubleVal: stats.MemPerc}}, ts)
}
func appendNetworkMetrics(ms pmetric.MetricSlice, stats *containerStats, ts pcommon.Timestamp) {
sum(ms, "network.io.usage.tx_bytes", "By", []point{{intVal: stats.NetInput}}, ts)
sum(ms, "network.io.usage.rx_bytes", "By", []point{{intVal: stats.NetOutput}}, ts)
}
func appendIOMetrics(ms pmetric.MetricSlice, stats *containerStats, ts pcommon.Timestamp) {
sum(ms, "blockio.io_service_bytes_recursive.write", "By", []point{{intVal: stats.BlockOutput}}, ts)
sum(ms, "blockio.io_service_bytes_recursive.read", "By", []point{{intVal: stats.BlockInput}}, ts)
}
func appendCPUMetrics(ms pmetric.MetricSlice, stats *containerStats, ts pcommon.Timestamp) {
sum(ms, "cpu.usage.system", "ns", []point{{intVal: stats.CPUSystemNano}}, ts)
sum(ms, "cpu.usage.total", "ns", []point{{intVal: stats.CPUNano}}, ts)
gaugeF(ms, "cpu.percent", "1", []point{{doubleVal: stats.CPU}}, ts)
points := make([]point, len(stats.PerCPU))
for i, cpu := range stats.PerCPU {
points[i] = point{
intVal: cpu,
attributes: map[string]string{
"core": fmt.Sprintf("cpu%d", i),
},
}
}
sum(ms, "cpu.usage.percpu", "ns", points, ts)
}
func initMetric(ms pmetric.MetricSlice, name, unit string) pmetric.Metric {
m := ms.AppendEmpty()
m.SetName(fmt.Sprintf("container.%s", name))
m.SetUnit(unit)
return m
}
func sum(ilm pmetric.MetricSlice, metricName string, unit string, points []point, ts pcommon.Timestamp) {
metric := initMetric(ilm, metricName, unit)
sum := metric.SetEmptySum()
sum.SetIsMonotonic(true)
sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
dataPoints := sum.DataPoints()
for _, pt := range points {
dataPoint := dataPoints.AppendEmpty()
dataPoint.SetTimestamp(ts)
dataPoint.SetIntValue(int64(pt.intVal))
setDataPointAttributes(dataPoint, pt.attributes)
}
}
func gauge(ms pmetric.MetricSlice, metricName string, unit string) pmetric.NumberDataPointSlice {
metric := initMetric(ms, metricName, unit)
gauge := metric.SetEmptyGauge()
return gauge.DataPoints()
}
func gaugeI(ms pmetric.MetricSlice, metricName string, unit string, points []point, ts pcommon.Timestamp) {
dataPoints := gauge(ms, metricName, unit)
for _, pt := range points {
dataPoint := dataPoints.AppendEmpty()
dataPoint.SetTimestamp(ts)
dataPoint.SetIntValue(int64(pt.intVal))
setDataPointAttributes(dataPoint, pt.attributes)
}
}
func gaugeF(ms pmetric.MetricSlice, metricName string, unit string, points []point, ts pcommon.Timestamp) {
dataPoints := gauge(ms, metricName, unit)
for _, pt := range points {
dataPoint := dataPoints.AppendEmpty()
dataPoint.SetTimestamp(ts)
dataPoint.SetDoubleValue(pt.doubleVal)
setDataPointAttributes(dataPoint, pt.attributes)
}
}
func setDataPointAttributes(dataPoint pmetric.NumberDataPoint, attributes map[string]string) {
for k, v := range attributes {
dataPoint.Attributes().PutStr(k, v)
}
}