Skip to content

Commit

Permalink
Fix descriptor registration.
Browse files Browse the repository at this point in the history
Counters are added a later point and then prometheus will complain that they are not registered when they are added.
  • Loading branch information
Gerrit91 committed Mar 10, 2023
1 parent 5aa2979 commit e661cc4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 79 deletions.
84 changes: 84 additions & 0 deletions descriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import "github.com/prometheus/client_golang/prometheus"

var (
counterBytesDesc = prometheus.NewDesc(
"nftables_counter_bytes",
"Bytes, matched by counter",
[]string{
"name",
"table",
"family",
},
nil,
)
counterPacketsDesc = prometheus.NewDesc(
"nftables_counter_packets",
"Packets, matched by counter",
[]string{
"name",
"table",
"family",
},
nil,
)
tableChainsDesc = prometheus.NewDesc(
"nftables_table_chains",
"Count chains in table",
[]string{
"name",
"family",
},
nil,
)
chainRulesDesc = prometheus.NewDesc(
"nftables_chain_rules",
"Count rules in chain",
[]string{
"name",
"family",
"table",
"handle",
},
nil,
)
ruleBytesDesc = prometheus.NewDesc(
"nftables_rule_bytes",
"Bytes, matched by rule per rule comment",
[]string{
"chain",
"family",
"table",
"input_interfaces",
"output_interfaces",
"source_addresses",
"destination_addresses",
"source_ports",
"destination_ports",
"comment",
"action",
"handle",
},
nil,
)
rulePacketsDesc = prometheus.NewDesc(
"nftables_rule_packets",
"Packets, matched by rule per rule comment",
[]string{
"chain",
"family",
"table",
"input_interfaces",
"output_interfaces",
"source_addresses",
"destination_addresses",
"source_ports",
"destination_ports",
"comment",
"action",
"handle",
},
nil,
)
)
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ type nftablesManagerCollector struct {

// Describe sends the super-set of all possible descriptors of metrics
func (i nftablesManagerCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(i, ch)
ch <- counterBytesDesc
ch <- counterPacketsDesc
ch <- tableChainsDesc
ch <- chainRulesDesc
ch <- ruleBytesDesc
ch <- rulePacketsDesc
}

// Collect is called by the Prometheus registry when collecting metrics.
Expand Down
78 changes: 0 additions & 78 deletions nftables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,6 @@ func newNFTables(json gjson.Result, ch chan<- prometheus.Metric) nftables {

// Collect metrics
func (nft nftables) Collect() {
counterBytesDesc := prometheus.NewDesc(
"nftables_counter_bytes",
"Bytes, matched by counter",
[]string{
"name",
"table",
"family",
},
nil,
)
counterPacketsDesc := prometheus.NewDesc(
"nftables_counter_packets",
"Packets, matched by counter",
[]string{
"name",
"table",
"family",
},
nil,
)
tableChainsDesc := prometheus.NewDesc(
"nftables_table_chains",
"Count chains in table",
[]string{
"name",
"family",
},
nil,
)
chainRulesDesc := prometheus.NewDesc(
"nftables_chain_rules",
"Count rules in chain",
[]string{
"name",
"family",
"table",
"handle",
},
nil,
)
tables := nft.json.Get("#.table").Array()
chains := nft.json.Get("#.chain").Array()
rules := nft.json.Get("#.rule").Array()
Expand Down Expand Up @@ -271,25 +231,6 @@ func (nft nftables) setRuleCounters(rule nftablesRule) {
sourcePorts := nft.arrayToTag(rule.Ports.Source)
destinationPorts := nft.arrayToTag(rule.Ports.Destination)

ruleBytesDesc := prometheus.NewDesc(
"nftables_rule_bytes",
"Bytes, matched by rule per rule comment",
[]string{
"chain",
"family",
"table",
"input_interfaces",
"output_interfaces",
"source_addresses",
"destination_addresses",
"source_ports",
"destination_ports",
"comment",
"action",
"handle",
},
nil,
)
// logger.Verbose(fmt.Sprintf("%s.%s.%s => %s:%s:%s -> %s:%s:%s = %f, %s, %s", rule.Chain, rule.Family, rule.Table, InputInterfaces, SourceAddresses, SourcePorts, OutputInterfaces, DestinationAddresses, DestinationPorts, rule.Couters.Bytes, rule.Action, rule.Comment))
nft.ch <- prometheus.MustNewConstMetric(
ruleBytesDesc,
Expand All @@ -309,25 +250,6 @@ func (nft nftables) setRuleCounters(rule nftablesRule) {
rule.Handle,
)

rulePacketsDesc := prometheus.NewDesc(
"nftables_rule_packets",
"Packets, matched by rule per rule comment",
[]string{
"chain",
"family",
"table",
"input_interfaces",
"output_interfaces",
"source_addresses",
"destination_addresses",
"source_ports",
"destination_ports",
"comment",
"action",
"handle",
},
nil,
)
nft.ch <- prometheus.MustNewConstMetric(
rulePacketsDesc,
prometheus.CounterValue,
Expand Down

0 comments on commit e661cc4

Please sign in to comment.