-
Notifications
You must be signed in to change notification settings - Fork 2
/
registry_unix.go
106 lines (86 loc) 路 2.61 KB
/
registry_unix.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
// Copyright 2015-2023 Bleemeo
//
// bleemeo.com an infrastructure monitoring solution in the Cloud
//
// 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
package registry
import (
"strings"
"github.com/bleemeo/glouton/facts/container-runtime/veth"
"github.com/bleemeo/glouton/logger"
"github.com/bleemeo/glouton/prometheus/exporter/node"
"github.com/bleemeo/glouton/types"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
// AddNodeExporter add a node_exporter to collector.
func (r *Registry) AddNodeExporter(option node.Option, vethProvider *veth.Provider) error {
collector, err := node.NewCollector(option)
if err != nil {
return err
}
reg := prometheus.NewRegistry()
err = reg.Register(collector)
if err != nil {
return err
}
_, err = r.RegisterGatherer(
RegistrationOption{
Description: "node_exporter",
JitterSeed: baseJitter,
Interval: defaultInterval,
DisablePeriodicGather: r.option.MetricFormat != types.MetricFormatPrometheus,
},
nodeGatherer{
gatherer: reg,
vethProvider: vethProvider,
},
)
return err
}
// nodeGatherer adds containerID label to container interfaces.
type nodeGatherer struct {
gatherer prometheus.Gatherer
vethProvider *veth.Provider
}
func (ng nodeGatherer) Gather() ([]*dto.MetricFamily, error) {
mfs, err := ng.gatherer.Gather()
if err != nil {
return nil, err
}
labelMetaContainerID := types.LabelMetaContainerID
for _, mf := range mfs {
if !strings.HasPrefix(mf.GetName(), "node_network") {
continue
}
for i, metric := range mf.GetMetric() {
for _, label := range metric.GetLabel() {
if label.GetName() != "device" {
continue
}
containerID, err := ng.vethProvider.ContainerID(label.GetValue())
if err != nil {
logger.V(1).Printf("Failed to get container interfaces: %s", err)
continue
}
containerLabel := &dto.LabelPair{
Name: &labelMetaContainerID,
Value: &containerID,
}
mf.Metric[i].Label = append(mf.GetMetric()[i].GetLabel(), containerLabel)
}
}
}
return mfs, err
}