Skip to content

Commit a2db98e

Browse files
authored
chore: Migrate metric descriptors to package vars (prometheus#3575)
Migrate various metric descriptors to package vars to avoid dynamic runtime allocs. Signed-off-by: Ben Kochie <superq@gmail.com>
1 parent 121f37e commit a2db98e

11 files changed

Lines changed: 413 additions & 463 deletions

collector/arp_linux.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ var (
3535
type arpCollector struct {
3636
fs procfs.FS
3737
deviceFilter deviceFilter
38-
entries *prometheus.Desc
3938
logger *slog.Logger
4039
}
4140

4241
func init() {
4342
registerCollector("arp", defaultEnabled, NewARPCollector)
4443
}
4544

45+
var (
46+
arpEntries = prometheus.NewDesc(
47+
prometheus.BuildFQName(namespace, "arp", "entries"),
48+
"ARP entries by device",
49+
[]string{"device"}, nil,
50+
)
51+
)
52+
4653
// NewARPCollector returns a new Collector exposing ARP stats.
4754
func NewARPCollector(logger *slog.Logger) (Collector, error) {
4855
fs, err := procfs.NewFS(*procPath)
@@ -53,12 +60,7 @@ func NewARPCollector(logger *slog.Logger) (Collector, error) {
5360
return &arpCollector{
5461
fs: fs,
5562
deviceFilter: newDeviceFilter(*arpDeviceExclude, *arpDeviceInclude),
56-
entries: prometheus.NewDesc(
57-
prometheus.BuildFQName(namespace, "arp", "entries"),
58-
"ARP entries by device",
59-
[]string{"device"}, nil,
60-
),
61-
logger: logger,
63+
logger: logger,
6264
}, nil
6365
}
6466

@@ -123,7 +125,7 @@ func (c *arpCollector) Update(ch chan<- prometheus.Metric) error {
123125
continue
124126
}
125127
ch <- prometheus.MustNewConstMetric(
126-
c.entries, prometheus.GaugeValue, float64(entryCount), device)
128+
arpEntries, prometheus.GaugeValue, float64(entryCount), device)
127129
}
128130

129131
return nil

collector/buddyinfo.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ const (
3030

3131
type buddyinfoCollector struct {
3232
fs procfs.FS
33-
desc *prometheus.Desc
3433
logger *slog.Logger
3534
}
3635

3736
func init() {
3837
registerCollector("buddyinfo", defaultDisabled, NewBuddyinfoCollector)
3938
}
4039

41-
// NewBuddyinfoCollector returns a new Collector exposing buddyinfo stats.
42-
func NewBuddyinfoCollector(logger *slog.Logger) (Collector, error) {
43-
desc := prometheus.NewDesc(
40+
var (
41+
buddyinfoBlocks = prometheus.NewDesc(
4442
prometheus.BuildFQName(namespace, buddyInfoSubsystem, "blocks"),
4543
"Count of free blocks according to size.",
4644
[]string{"node", "zone", "size"}, nil,
4745
)
46+
)
47+
48+
// NewBuddyinfoCollector returns a new Collector exposing buddyinfo stats.
49+
func NewBuddyinfoCollector(logger *slog.Logger) (Collector, error) {
4850
fs, err := procfs.NewFS(*procPath)
4951
if err != nil {
5052
return nil, fmt.Errorf("failed to open procfs: %w", err)
5153
}
52-
return &buddyinfoCollector{fs, desc, logger}, nil
54+
return &buddyinfoCollector{fs, logger}, nil
5355
}
5456

5557
// Update calls (*buddyinfoCollector).getBuddyInfo to get the platform specific
@@ -64,7 +66,7 @@ func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) error {
6466
for _, entry := range buddyInfo {
6567
for size, value := range entry.Sizes {
6668
ch <- prometheus.MustNewConstMetric(
67-
c.desc,
69+
buddyinfoBlocks,
6870
prometheus.GaugeValue, value,
6971
entry.Node, entry.Zone, strconv.Itoa(size),
7072
)

collector/cgroups_linux.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,35 @@ import (
2626
const cgroupsCollectorSubsystem = "cgroups"
2727

2828
type cgroupSummaryCollector struct {
29-
fs procfs.FS
30-
cgroups *prometheus.Desc
31-
enabled *prometheus.Desc
32-
logger *slog.Logger
29+
fs procfs.FS
30+
logger *slog.Logger
3331
}
3432

3533
func init() {
3634
registerCollector(cgroupsCollectorSubsystem, defaultDisabled, NewCgroupSummaryCollector)
3735
}
3836

37+
var (
38+
cgroupsCgroups = prometheus.NewDesc(
39+
prometheus.BuildFQName(namespace, cgroupsCollectorSubsystem, "cgroups"),
40+
"Current cgroup number of the subsystem.",
41+
[]string{"subsys_name"}, nil,
42+
)
43+
cgroupsEnabled = prometheus.NewDesc(
44+
prometheus.BuildFQName(namespace, cgroupsCollectorSubsystem, "enabled"),
45+
"Current cgroup number of the subsystem.",
46+
[]string{"subsys_name"}, nil,
47+
)
48+
)
49+
3950
// NewCgroupSummaryCollector returns a new Collector exposing a summary of cgroups.
4051
func NewCgroupSummaryCollector(logger *slog.Logger) (Collector, error) {
4152
fs, err := procfs.NewFS(*procPath)
4253
if err != nil {
4354
return nil, fmt.Errorf("failed to open procfs: %w", err)
4455
}
4556
return &cgroupSummaryCollector{
46-
fs: fs,
47-
cgroups: prometheus.NewDesc(
48-
prometheus.BuildFQName(namespace, cgroupsCollectorSubsystem, "cgroups"),
49-
"Current cgroup number of the subsystem.",
50-
[]string{"subsys_name"}, nil,
51-
),
52-
enabled: prometheus.NewDesc(
53-
prometheus.BuildFQName(namespace, cgroupsCollectorSubsystem, "enabled"),
54-
"Current cgroup number of the subsystem.",
55-
[]string{"subsys_name"}, nil,
56-
),
57+
fs: fs,
5758
logger: logger,
5859
}, nil
5960
}
@@ -65,8 +66,8 @@ func (c *cgroupSummaryCollector) Update(ch chan<- prometheus.Metric) error {
6566
return err
6667
}
6768
for _, cs := range cgroupSummarys {
68-
ch <- prometheus.MustNewConstMetric(c.cgroups, prometheus.GaugeValue, float64(cs.Cgroups), cs.SubsysName)
69-
ch <- prometheus.MustNewConstMetric(c.enabled, prometheus.GaugeValue, float64(cs.Enabled), cs.SubsysName)
69+
ch <- prometheus.MustNewConstMetric(cgroupsCgroups, prometheus.GaugeValue, float64(cs.Cgroups), cs.SubsysName)
70+
ch <- prometheus.MustNewConstMetric(cgroupsEnabled, prometheus.GaugeValue, float64(cs.Enabled), cs.SubsysName)
7071
}
7172
return nil
7273
}

collector/conntrack_linux.go

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@ import (
2626
)
2727

2828
type conntrackCollector struct {
29-
current *prometheus.Desc
30-
limit *prometheus.Desc
31-
found *prometheus.Desc
32-
invalid *prometheus.Desc
33-
ignore *prometheus.Desc
34-
insert *prometheus.Desc
35-
insertFailed *prometheus.Desc
36-
drop *prometheus.Desc
37-
earlyDrop *prometheus.Desc
38-
searchRestart *prometheus.Desc
39-
logger *slog.Logger
29+
logger *slog.Logger
4030
}
4131

4232
type conntrackStatistics struct {
@@ -54,59 +44,62 @@ func init() {
5444
registerCollector("conntrack", defaultEnabled, NewConntrackCollector)
5545
}
5646

47+
var (
48+
conntrackCurrent = prometheus.NewDesc(
49+
prometheus.BuildFQName(namespace, "", "nf_conntrack_entries"),
50+
"Number of currently allocated flow entries for connection tracking.",
51+
nil, nil,
52+
)
53+
conntrackLimit = prometheus.NewDesc(
54+
prometheus.BuildFQName(namespace, "", "nf_conntrack_entries_limit"),
55+
"Maximum size of connection tracking table.",
56+
nil, nil,
57+
)
58+
conntrackFound = prometheus.NewDesc(
59+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_found"),
60+
"Number of searched entries which were successful.",
61+
nil, nil,
62+
)
63+
conntrackInvalid = prometheus.NewDesc(
64+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_invalid"),
65+
"Number of packets seen which can not be tracked.",
66+
nil, nil,
67+
)
68+
conntrackIgnore = prometheus.NewDesc(
69+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_ignore"),
70+
"Number of packets seen which are already connected to a conntrack entry.",
71+
nil, nil,
72+
)
73+
conntrackInsert = prometheus.NewDesc(
74+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_insert"),
75+
"Number of entries inserted into the list.",
76+
nil, nil,
77+
)
78+
conntrackInsertFailed = prometheus.NewDesc(
79+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_insert_failed"),
80+
"Number of entries for which list insertion was attempted but failed.",
81+
nil, nil,
82+
)
83+
conntrackDrop = prometheus.NewDesc(
84+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_drop"),
85+
"Number of packets dropped due to conntrack failure.",
86+
nil, nil,
87+
)
88+
conntrackEarlyDrop = prometheus.NewDesc(
89+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_early_drop"),
90+
"Number of dropped conntrack entries to make room for new ones, if maximum table size was reached.",
91+
nil, nil,
92+
)
93+
conntrackSearchRestart = prometheus.NewDesc(
94+
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_search_restart"),
95+
"Number of conntrack table lookups which had to be restarted due to hashtable resizes.",
96+
nil, nil,
97+
)
98+
)
99+
57100
// NewConntrackCollector returns a new Collector exposing conntrack stats.
58101
func NewConntrackCollector(logger *slog.Logger) (Collector, error) {
59102
return &conntrackCollector{
60-
current: prometheus.NewDesc(
61-
prometheus.BuildFQName(namespace, "", "nf_conntrack_entries"),
62-
"Number of currently allocated flow entries for connection tracking.",
63-
nil, nil,
64-
),
65-
limit: prometheus.NewDesc(
66-
prometheus.BuildFQName(namespace, "", "nf_conntrack_entries_limit"),
67-
"Maximum size of connection tracking table.",
68-
nil, nil,
69-
),
70-
found: prometheus.NewDesc(
71-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_found"),
72-
"Number of searched entries which were successful.",
73-
nil, nil,
74-
),
75-
invalid: prometheus.NewDesc(
76-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_invalid"),
77-
"Number of packets seen which can not be tracked.",
78-
nil, nil,
79-
),
80-
ignore: prometheus.NewDesc(
81-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_ignore"),
82-
"Number of packets seen which are already connected to a conntrack entry.",
83-
nil, nil,
84-
),
85-
insert: prometheus.NewDesc(
86-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_insert"),
87-
"Number of entries inserted into the list.",
88-
nil, nil,
89-
),
90-
insertFailed: prometheus.NewDesc(
91-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_insert_failed"),
92-
"Number of entries for which list insertion was attempted but failed.",
93-
nil, nil,
94-
),
95-
drop: prometheus.NewDesc(
96-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_drop"),
97-
"Number of packets dropped due to conntrack failure.",
98-
nil, nil,
99-
),
100-
earlyDrop: prometheus.NewDesc(
101-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_early_drop"),
102-
"Number of dropped conntrack entries to make room for new ones, if maximum table size was reached.",
103-
nil, nil,
104-
),
105-
searchRestart: prometheus.NewDesc(
106-
prometheus.BuildFQName(namespace, "", "nf_conntrack_stat_search_restart"),
107-
"Number of conntrack table lookups which had to be restarted due to hashtable resizes.",
108-
nil, nil,
109-
),
110103
logger: logger,
111104
}, nil
112105
}
@@ -117,36 +110,36 @@ func (c *conntrackCollector) Update(ch chan<- prometheus.Metric) error {
117110
return c.handleErr(err)
118111
}
119112
ch <- prometheus.MustNewConstMetric(
120-
c.current, prometheus.GaugeValue, float64(value))
113+
conntrackCurrent, prometheus.GaugeValue, float64(value))
121114

122115
value, err = readUintFromFile(procFilePath("sys/net/netfilter/nf_conntrack_max"))
123116
if err != nil {
124117
return c.handleErr(err)
125118
}
126119
ch <- prometheus.MustNewConstMetric(
127-
c.limit, prometheus.GaugeValue, float64(value))
120+
conntrackLimit, prometheus.GaugeValue, float64(value))
128121

129122
conntrackStats, err := getConntrackStatistics()
130123
if err != nil {
131124
return c.handleErr(err)
132125
}
133126

134127
ch <- prometheus.MustNewConstMetric(
135-
c.found, prometheus.GaugeValue, float64(conntrackStats.found))
128+
conntrackFound, prometheus.GaugeValue, float64(conntrackStats.found))
136129
ch <- prometheus.MustNewConstMetric(
137-
c.invalid, prometheus.GaugeValue, float64(conntrackStats.invalid))
130+
conntrackInvalid, prometheus.GaugeValue, float64(conntrackStats.invalid))
138131
ch <- prometheus.MustNewConstMetric(
139-
c.ignore, prometheus.GaugeValue, float64(conntrackStats.ignore))
132+
conntrackIgnore, prometheus.GaugeValue, float64(conntrackStats.ignore))
140133
ch <- prometheus.MustNewConstMetric(
141-
c.insert, prometheus.GaugeValue, float64(conntrackStats.insert))
134+
conntrackInsert, prometheus.GaugeValue, float64(conntrackStats.insert))
142135
ch <- prometheus.MustNewConstMetric(
143-
c.insertFailed, prometheus.GaugeValue, float64(conntrackStats.insertFailed))
136+
conntrackInsertFailed, prometheus.GaugeValue, float64(conntrackStats.insertFailed))
144137
ch <- prometheus.MustNewConstMetric(
145-
c.drop, prometheus.GaugeValue, float64(conntrackStats.drop))
138+
conntrackDrop, prometheus.GaugeValue, float64(conntrackStats.drop))
146139
ch <- prometheus.MustNewConstMetric(
147-
c.earlyDrop, prometheus.GaugeValue, float64(conntrackStats.earlyDrop))
140+
conntrackEarlyDrop, prometheus.GaugeValue, float64(conntrackStats.earlyDrop))
148141
ch <- prometheus.MustNewConstMetric(
149-
c.searchRestart, prometheus.GaugeValue, float64(conntrackStats.searchRestart))
142+
conntrackSearchRestart, prometheus.GaugeValue, float64(conntrackStats.searchRestart))
150143
return nil
151144
}
152145

@@ -159,7 +152,7 @@ func (c *conntrackCollector) handleErr(err error) error {
159152
}
160153

161154
func getConntrackStatistics() (*conntrackStatistics, error) {
162-
c := conntrackStatistics{}
155+
s := conntrackStatistics{}
163156

164157
fs, err := procfs.NewFS(*procPath)
165158
if err != nil {
@@ -172,15 +165,15 @@ func getConntrackStatistics() (*conntrackStatistics, error) {
172165
}
173166

174167
for _, connStat := range connStats {
175-
c.found += connStat.Found
176-
c.invalid += connStat.Invalid
177-
c.ignore += connStat.Ignore
178-
c.insert += connStat.Insert
179-
c.insertFailed += connStat.InsertFailed
180-
c.drop += connStat.Drop
181-
c.earlyDrop += connStat.EarlyDrop
182-
c.searchRestart += connStat.SearchRestart
168+
s.found += connStat.Found
169+
s.invalid += connStat.Invalid
170+
s.ignore += connStat.Ignore
171+
s.insert += connStat.Insert
172+
s.insertFailed += connStat.InsertFailed
173+
s.drop += connStat.Drop
174+
s.earlyDrop += connStat.EarlyDrop
175+
s.searchRestart += connStat.SearchRestart
183176
}
184177

185-
return &c, nil
178+
return &s, nil
186179
}

0 commit comments

Comments
 (0)