Add labels from protocol descriptions.

This commit is contained in:
Oliver Herms 2020-02-18 19:55:16 +01:00
parent 6ba0275f6e
commit 3655df1d23
13 changed files with 174 additions and 144 deletions

4
go.mod
View File

@ -3,8 +3,8 @@ module github.com/czerwonk/bird_exporter
require (
github.com/czerwonk/bird_socket v0.0.0-20170701072538-fe8194eb5598
github.com/czerwonk/testutils v0.0.0-20170526233935-dd9dabe360d4
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/prometheus/client_golang v1.2.1
github.com/prometheus/common v0.7.0
)
go 1.13

1
go.sum
View File

@ -53,6 +53,7 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.2.1 h1:JnMpQc6ppsNgw9QPAGF6Dod479itz7lvlsMzzNayLOI=
github.com/prometheus/client_golang v1.2.1/go.mod h1:XMU6Z2MjaRKVu/dC1qupJI9SiNkDYzz3xecMgSW/F+U=
github.com/prometheus/client_golang v1.4.1 h1:FFSuS004yOQEtDdTq+TAOLP5xUq63KqAFYyOi8zA+Y8=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=

View File

@ -27,9 +27,10 @@ var (
enableStatic = flag.Bool("proto.static", true, "Enables metrics for protocol Static")
enableDirect = flag.Bool("proto.direct", true, "Enables metrics for protocol Direct")
// pre bird 2.0
bird6Socket = flag.String("bird.socket6", "/var/run/bird6.ctl", "Socket to communicate with bird6 routing daemon (not compatible with -bird.v2)")
birdEnabled = flag.Bool("bird.ipv4", true, "Get protocols from bird (not compatible with -bird.v2)")
bird6Enabled = flag.Bool("bird.ipv6", true, "Get protocols from bird6 (not compatible with -bird.v2)")
bird6Socket = flag.String("bird.socket6", "/var/run/bird6.ctl", "Socket to communicate with bird6 routing daemon (not compatible with -bird.v2)")
birdEnabled = flag.Bool("bird.ipv4", true, "Get protocols from bird (not compatible with -bird.v2)")
bird6Enabled = flag.Bool("bird.ipv6", true, "Get protocols from bird6 (not compatible with -bird.v2)")
descriptionLabels = flag.Bool("format.description-labels", false, "Add labels from protocol descriptions.")
)
func init() {
@ -85,7 +86,7 @@ func startServer() {
func handleMetricsRequest(w http.ResponseWriter, r *http.Request) {
reg := prometheus.NewRegistry()
p := enabledProtocols()
c := NewMetricCollector(*newFormat, p)
c := NewMetricCollector(*newFormat, p, *descriptionLabels)
reg.MustRegister(c)
promhttp.HandlerFor(reg, promhttp.HandlerOpts{

View File

@ -12,19 +12,25 @@ type MetricCollector struct {
exporters map[int][]metrics.MetricExporter
client *client.BirdClient
enabledProtocols int
newFormat bool
}
func NewMetricCollector(newFormat bool, enabledProtocols int) *MetricCollector {
func NewMetricCollector(newFormat bool, enabledProtocols int, descriptionLabels bool) *MetricCollector {
c := getClient()
var e map[int][]metrics.MetricExporter
if newFormat {
e = exportersForDefault(c)
e = exportersForDefault(c, descriptionLabels)
} else {
e = exportersForLegacy(c)
}
return &MetricCollector{exporters: e, client: c, enabledProtocols: enabledProtocols}
return &MetricCollector{
exporters: e,
client: c,
enabledProtocols: enabledProtocols,
newFormat: newFormat,
}
}
func getClient() *client.BirdClient {
@ -40,7 +46,7 @@ func getClient() *client.BirdClient {
}
func exportersForLegacy(c *client.BirdClient) map[int][]metrics.MetricExporter {
l := &metrics.LegacyLabelStrategy{}
l := metrics.NewLegacyLabelStrategy()
return map[int][]metrics.MetricExporter{
protocol.BGP: {metrics.NewLegacyMetricExporter("bgp4_session", "bgp6_session", l)},
@ -51,8 +57,8 @@ func exportersForLegacy(c *client.BirdClient) map[int][]metrics.MetricExporter {
}
}
func exportersForDefault(c *client.BirdClient) map[int][]metrics.MetricExporter {
l := &metrics.DefaultLabelStrategy{}
func exportersForDefault(c *client.BirdClient, descriptionLabels bool) map[int][]metrics.MetricExporter {
l := metrics.NewDefaultLabelStrategy(descriptionLabels)
e := metrics.NewGenericProtocolMetricExporter("bird_protocol", true, l)
return map[int][]metrics.MetricExporter{
@ -85,7 +91,7 @@ func (m *MetricCollector) Collect(ch chan<- prometheus.Metric) {
}
for _, e := range m.exporters[p.Proto] {
e.Export(p, ch)
e.Export(p, ch, m.newFormat)
}
}
}

View File

@ -1,21 +1,66 @@
package metrics
import (
"strings"
"github.com/czerwonk/bird_exporter/protocol"
)
// DefaultLabelStrategy defines the labels to add to an metric and its data retrieval method
type DefaultLabelStrategy struct {
descriptionLabels bool
}
func NewDefaultLabelStrategy(descriptionLabels bool) *DefaultLabelStrategy {
return &DefaultLabelStrategy{
descriptionLabels: descriptionLabels,
}
}
// LabelNames returns the list of label names
func (*DefaultLabelStrategy) LabelNames() []string {
return []string{"name", "proto", "ip_version", "import_filter", "export_filter"}
func (d *DefaultLabelStrategy) LabelNames(p *protocol.Protocol) []string {
res := []string{"name", "proto", "ip_version", "import_filter", "export_filter"}
if d.descriptionLabels && p.Description != "" {
res = append(res, labelKeysFromDescription(p.Description)...)
}
return res
}
// LabelValues returns the values for a protocol
func (*DefaultLabelStrategy) LabelValues(p *protocol.Protocol) []string {
return []string{p.Name, protoString(p), p.IPVersion, p.ImportFilter, p.ExportFilter}
func (d *DefaultLabelStrategy) LabelValues(p *protocol.Protocol) []string {
res := []string{p.Name, protoString(p), p.IPVersion, p.ImportFilter, p.ExportFilter}
if d.descriptionLabels && p.Description != "" {
res = append(res, labelValuesFromDescription(p.Description)...)
}
return res
}
func labelKeysFromDescription(desc string) (res []string) {
for _, x := range strings.Split(desc, ",") {
tmp := strings.Split(x, "=")
if len(tmp) != 2 {
continue
}
res = append(res, tmp[0])
}
return
}
func labelValuesFromDescription(desc string) (res []string) {
for _, x := range strings.Split(desc, ",") {
tmp := strings.Split(x, "=")
if len(tmp) != 2 {
continue
}
res = append(res, tmp[1])
}
return
}
func protoString(p *protocol.Protocol) string {

View File

@ -7,137 +7,92 @@ import (
// GenericProtocolMetricExporter exports metrics retrieved from Bird routing daemon
type GenericProtocolMetricExporter struct {
labelStrategy LabelStrategy
upDesc *prometheus.Desc
importCountDesc *prometheus.Desc
exportCountDesc *prometheus.Desc
filterCountDesc *prometheus.Desc
preferredCountDesc *prometheus.Desc
uptimeDesc *prometheus.Desc
updatesImportReceiveCountDesc *prometheus.Desc
updatesImportRejectCountDesc *prometheus.Desc
updatesImportFilterCountDesc *prometheus.Desc
updatesImportIgnoreCountDesc *prometheus.Desc
updatesImportAcceptCountDesc *prometheus.Desc
withdrawsImportReceiveCountDesc *prometheus.Desc
withdrawsImportRejectCountDesc *prometheus.Desc
withdrawsImportFilterCountDesc *prometheus.Desc
withdrawsImportIgnoreCountDesc *prometheus.Desc
withdrawsImportAcceptCountDesc *prometheus.Desc
updatesExportReceiveCountDesc *prometheus.Desc
updatesExportRejectCountDesc *prometheus.Desc
updatesExportFilterCountDesc *prometheus.Desc
updatesExportIgnoreCountDesc *prometheus.Desc
updatesExportAcceptCountDesc *prometheus.Desc
withdrawsExportReceiveCountDesc *prometheus.Desc
withdrawsExportRejectCountDesc *prometheus.Desc
withdrawsExportFilterCountDesc *prometheus.Desc
withdrawsExportIgnoreCountDesc *prometheus.Desc
withdrawsExportAcceptCountDesc *prometheus.Desc
labelStrategy LabelStrategy
prefix string
}
// NewGenericProtocolMetricExporter creates a new instance of GenericProtocolMetricExporter
func NewGenericProtocolMetricExporter(prefix string, newNaming bool, labelStrategy LabelStrategy) *GenericProtocolMetricExporter {
m := &GenericProtocolMetricExporter{labelStrategy: labelStrategy}
m.initDesc(prefix, newNaming)
m := &GenericProtocolMetricExporter{
prefix: prefix,
labelStrategy: labelStrategy,
}
return m
}
func (m *GenericProtocolMetricExporter) initDesc(prefix string, newNaming bool) {
labels := m.labelStrategy.LabelNames()
m.upDesc = prometheus.NewDesc(prefix+"_up", "Protocol is up", labels, nil)
func (m *GenericProtocolMetricExporter) Describe(ch chan<- *prometheus.Desc) {
}
func (m *GenericProtocolMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric, newNaming bool) {
labels := m.labelStrategy.LabelNames(p)
var importCountDesc *prometheus.Desc
var exportCountDesc *prometheus.Desc
var filterCountDesc *prometheus.Desc
var preferredCountDesc *prometheus.Desc
upDesc := prometheus.NewDesc(m.prefix+"_up", "Protocol is up", labels, nil)
if newNaming {
m.importCountDesc = prometheus.NewDesc(prefix+"_prefix_import_count", "Number of imported routes", labels, nil)
m.exportCountDesc = prometheus.NewDesc(prefix+"_prefix_export_count", "Number of exported routes", labels, nil)
m.filterCountDesc = prometheus.NewDesc(prefix+"_prefix_filter_count", "Number of filtered routes", labels, nil)
m.preferredCountDesc = prometheus.NewDesc(prefix+"_prefix_preferred_count", "Number of preferred routes", labels, nil)
importCountDesc = prometheus.NewDesc(m.prefix+"_prefix_import_count", "Number of imported routes", labels, nil)
exportCountDesc = prometheus.NewDesc(m.prefix+"_prefix_export_count", "Number of exported routes", labels, nil)
filterCountDesc = prometheus.NewDesc(m.prefix+"_prefix_filter_count", "Number of filtered routes", labels, nil)
preferredCountDesc = prometheus.NewDesc(m.prefix+"_prefix_preferred_count", "Number of preferred routes", labels, nil)
} else {
m.importCountDesc = prometheus.NewDesc(prefix+"_prefix_count_import", "Number of imported routes", labels, nil)
m.exportCountDesc = prometheus.NewDesc(prefix+"_prefix_count_export", "Number of exported routes", labels, nil)
m.filterCountDesc = prometheus.NewDesc(prefix+"_prefix_count_filter", "Number of filtered routes", labels, nil)
m.preferredCountDesc = prometheus.NewDesc(prefix+"_prefix_count_preferred", "Number of preferred routes", labels, nil)
importCountDesc = prometheus.NewDesc(m.prefix+"_prefix_count_import", "Number of imported routes", labels, nil)
exportCountDesc = prometheus.NewDesc(m.prefix+"_prefix_count_export", "Number of exported routes", labels, nil)
filterCountDesc = prometheus.NewDesc(m.prefix+"_prefix_count_filter", "Number of filtered routes", labels, nil)
preferredCountDesc = prometheus.NewDesc(m.prefix+"_prefix_count_preferred", "Number of preferred routes", labels, nil)
}
m.uptimeDesc = prometheus.NewDesc(prefix+"_uptime", "Uptime of the protocol in seconds", labels, nil)
m.updatesImportIgnoreCountDesc = prometheus.NewDesc(prefix+"_changes_update_import_ignore_count", "Number of incoming updates being ignored", labels, nil)
m.updatesImportAcceptCountDesc = prometheus.NewDesc(prefix+"_changes_update_import_accept_count", "Number of incoming updates being accepted", labels, nil)
m.updatesImportFilterCountDesc = prometheus.NewDesc(prefix+"_changes_update_import_filter_count", "Number of incoming updates being filtered", labels, nil)
m.updatesImportRejectCountDesc = prometheus.NewDesc(prefix+"_changes_update_import_reject_count", "Number of incoming updates being rejected", labels, nil)
m.updatesImportReceiveCountDesc = prometheus.NewDesc(prefix+"_changes_update_import_receive_count", "Number of received updates", labels, nil)
m.withdrawsImportIgnoreCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_import_ignore_count", "Number of incoming withdraws being ignored", labels, nil)
m.withdrawsImportAcceptCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_import_accept_count", "Number of incoming withdraws being accepted", labels, nil)
m.withdrawsImportFilterCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_import_filter_count", "Number of incoming withdraws being filtered", labels, nil)
m.withdrawsImportRejectCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_import_reject_count", "Number of incoming withdraws being rejected", labels, nil)
m.withdrawsImportReceiveCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_import_receive_count", "Number of received withdraws", labels, nil)
m.updatesExportIgnoreCountDesc = prometheus.NewDesc(prefix+"_changes_update_export_ignore_count", "Number of outgoing updates being ignored", labels, nil)
m.updatesExportAcceptCountDesc = prometheus.NewDesc(prefix+"_changes_update_export_accept_count", "Number of outgoing updates being accepted", labels, nil)
m.updatesExportFilterCountDesc = prometheus.NewDesc(prefix+"_changes_update_export_filter_count", "Number of outgoing updates being filtered", labels, nil)
m.updatesExportRejectCountDesc = prometheus.NewDesc(prefix+"_changes_update_export_reject_count", "Number of outgoing updates being rejected", labels, nil)
m.updatesExportReceiveCountDesc = prometheus.NewDesc(prefix+"_changes_update_export_receive_count", "Number of sent updates", labels, nil)
m.withdrawsExportIgnoreCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_export_ignore_count", "Number of outgoing withdraws being ignored", labels, nil)
m.withdrawsExportAcceptCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_export_accept_count", "Number of outgoing withdraws being accepted", labels, nil)
m.withdrawsExportFilterCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_export_filter_count", "Number of outgoing withdraws being filtered", labels, nil)
m.withdrawsExportRejectCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_export_reject_count", "Number of outgoing withdraws being rejected", labels, nil)
m.withdrawsExportReceiveCountDesc = prometheus.NewDesc(prefix+"_changes_withdraw_export_receive_count", "Number of outgoing withdraws", labels, nil)
}
uptimeDesc := prometheus.NewDesc(m.prefix+"_uptime", "Uptime of the protocol in seconds", labels, nil)
updatesImportIgnoreCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_import_ignore_count", "Number of incoming updates being ignored", labels, nil)
updatesImportAcceptCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_import_accept_count", "Number of incoming updates being accepted", labels, nil)
updatesImportFilterCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_import_filter_count", "Number of incoming updates being filtered", labels, nil)
updatesImportRejectCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_import_reject_count", "Number of incoming updates being rejected", labels, nil)
updatesImportReceiveCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_import_receive_count", "Number of received updates", labels, nil)
withdrawsImportIgnoreCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_import_ignore_count", "Number of incoming withdraws being ignored", labels, nil)
withdrawsImportAcceptCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_import_accept_count", "Number of incoming withdraws being accepted", labels, nil)
withdrawsImportFilterCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_import_filter_count", "Number of incoming withdraws being filtered", labels, nil)
withdrawsImportRejectCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_import_reject_count", "Number of incoming withdraws being rejected", labels, nil)
withdrawsImportReceiveCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_import_receive_count", "Number of received withdraws", labels, nil)
updatesExportIgnoreCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_export_ignore_count", "Number of outgoing updates being ignored", labels, nil)
updatesExportAcceptCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_export_accept_count", "Number of outgoing updates being accepted", labels, nil)
updatesExportFilterCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_export_filter_count", "Number of outgoing updates being filtered", labels, nil)
updatesExportRejectCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_export_reject_count", "Number of outgoing updates being rejected", labels, nil)
updatesExportReceiveCountDesc := prometheus.NewDesc(m.prefix+"_changes_update_export_receive_count", "Number of sent updates", labels, nil)
withdrawsExportIgnoreCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_export_ignore_count", "Number of outgoing withdraws being ignored", labels, nil)
withdrawsExportAcceptCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_export_accept_count", "Number of outgoing withdraws being accepted", labels, nil)
withdrawsExportFilterCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_export_filter_count", "Number of outgoing withdraws being filtered", labels, nil)
withdrawsExportRejectCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_export_reject_count", "Number of outgoing withdraws being rejected", labels, nil)
withdrawsExportReceiveCountDesc := prometheus.NewDesc(m.prefix+"_changes_withdraw_export_receive_count", "Number of outgoing withdraws", labels, nil)
func (m *GenericProtocolMetricExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- m.upDesc
ch <- m.importCountDesc
ch <- m.exportCountDesc
ch <- m.filterCountDesc
ch <- m.preferredCountDesc
ch <- m.uptimeDesc
ch <- m.updatesImportReceiveCountDesc
ch <- m.updatesImportRejectCountDesc
ch <- m.updatesImportFilterCountDesc
ch <- m.updatesImportIgnoreCountDesc
ch <- m.updatesImportAcceptCountDesc
ch <- m.updatesExportReceiveCountDesc
ch <- m.updatesExportRejectCountDesc
ch <- m.updatesExportFilterCountDesc
ch <- m.updatesExportIgnoreCountDesc
ch <- m.updatesExportAcceptCountDesc
ch <- m.withdrawsImportIgnoreCountDesc
ch <- m.withdrawsImportAcceptCountDesc
ch <- m.withdrawsImportFilterCountDesc
ch <- m.withdrawsImportRejectCountDesc
ch <- m.withdrawsImportReceiveCountDesc
ch <- m.withdrawsExportIgnoreCountDesc
ch <- m.withdrawsExportAcceptCountDesc
ch <- m.withdrawsExportFilterCountDesc
ch <- m.withdrawsExportRejectCountDesc
ch <- m.withdrawsExportReceiveCountDesc
}
func (m *GenericProtocolMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
l := m.labelStrategy.LabelValues(p)
ch <- prometheus.MustNewConstMetric(m.upDesc, prometheus.GaugeValue, float64(p.Up), l...)
ch <- prometheus.MustNewConstMetric(m.importCountDesc, prometheus.GaugeValue, float64(p.Imported), l...)
ch <- prometheus.MustNewConstMetric(m.exportCountDesc, prometheus.GaugeValue, float64(p.Exported), l...)
ch <- prometheus.MustNewConstMetric(m.filterCountDesc, prometheus.GaugeValue, float64(p.Filtered), l...)
ch <- prometheus.MustNewConstMetric(m.preferredCountDesc, prometheus.GaugeValue, float64(p.Preferred), l...)
ch <- prometheus.MustNewConstMetric(m.uptimeDesc, prometheus.GaugeValue, float64(p.Uptime), l...)
ch <- prometheus.MustNewConstMetric(m.updatesImportReceiveCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Received), l...)
ch <- prometheus.MustNewConstMetric(m.updatesImportRejectCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Rejected), l...)
ch <- prometheus.MustNewConstMetric(m.updatesImportFilterCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Filtered), l...)
ch <- prometheus.MustNewConstMetric(m.updatesImportAcceptCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Accepted), l...)
ch <- prometheus.MustNewConstMetric(m.updatesImportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Ignored), l...)
ch <- prometheus.MustNewConstMetric(m.updatesExportReceiveCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Received), l...)
ch <- prometheus.MustNewConstMetric(m.updatesExportRejectCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Rejected), l...)
ch <- prometheus.MustNewConstMetric(m.updatesExportFilterCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Filtered), l...)
ch <- prometheus.MustNewConstMetric(m.updatesExportAcceptCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Accepted), l...)
ch <- prometheus.MustNewConstMetric(m.updatesExportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Ignored), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsImportReceiveCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Received), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsImportRejectCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Rejected), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsImportFilterCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Filtered), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsImportAcceptCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Accepted), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsImportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Ignored), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsExportReceiveCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Received), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsExportRejectCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Rejected), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsExportFilterCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Filtered), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsExportAcceptCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Accepted), l...)
ch <- prometheus.MustNewConstMetric(m.withdrawsExportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Ignored), l...)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, float64(p.Up), l...)
ch <- prometheus.MustNewConstMetric(importCountDesc, prometheus.GaugeValue, float64(p.Imported), l...)
ch <- prometheus.MustNewConstMetric(exportCountDesc, prometheus.GaugeValue, float64(p.Exported), l...)
ch <- prometheus.MustNewConstMetric(filterCountDesc, prometheus.GaugeValue, float64(p.Filtered), l...)
ch <- prometheus.MustNewConstMetric(preferredCountDesc, prometheus.GaugeValue, float64(p.Preferred), l...)
ch <- prometheus.MustNewConstMetric(uptimeDesc, prometheus.GaugeValue, float64(p.Uptime), l...)
ch <- prometheus.MustNewConstMetric(updatesImportReceiveCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Received), l...)
ch <- prometheus.MustNewConstMetric(updatesImportRejectCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Rejected), l...)
ch <- prometheus.MustNewConstMetric(updatesImportFilterCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Filtered), l...)
ch <- prometheus.MustNewConstMetric(updatesImportAcceptCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Accepted), l...)
ch <- prometheus.MustNewConstMetric(updatesImportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ImportUpdates.Ignored), l...)
ch <- prometheus.MustNewConstMetric(updatesExportReceiveCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Received), l...)
ch <- prometheus.MustNewConstMetric(updatesExportRejectCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Rejected), l...)
ch <- prometheus.MustNewConstMetric(updatesExportFilterCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Filtered), l...)
ch <- prometheus.MustNewConstMetric(updatesExportAcceptCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Accepted), l...)
ch <- prometheus.MustNewConstMetric(updatesExportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ExportUpdates.Ignored), l...)
ch <- prometheus.MustNewConstMetric(withdrawsImportReceiveCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Received), l...)
ch <- prometheus.MustNewConstMetric(withdrawsImportRejectCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Rejected), l...)
ch <- prometheus.MustNewConstMetric(withdrawsImportFilterCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Filtered), l...)
ch <- prometheus.MustNewConstMetric(withdrawsImportAcceptCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Accepted), l...)
ch <- prometheus.MustNewConstMetric(withdrawsImportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ImportWithdraws.Ignored), l...)
ch <- prometheus.MustNewConstMetric(withdrawsExportReceiveCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Received), l...)
ch <- prometheus.MustNewConstMetric(withdrawsExportRejectCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Rejected), l...)
ch <- prometheus.MustNewConstMetric(withdrawsExportFilterCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Filtered), l...)
ch <- prometheus.MustNewConstMetric(withdrawsExportAcceptCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Accepted), l...)
ch <- prometheus.MustNewConstMetric(withdrawsExportIgnoreCountDesc, prometheus.GaugeValue, float64(p.ExportWithdraws.Ignored), l...)
}

View File

@ -5,8 +5,8 @@ import "github.com/czerwonk/bird_exporter/protocol"
// LabelStrategy abstracts the label generation for protocol metrics
type LabelStrategy interface {
// LabelNames is the list of label names
LabelNames() []string
// Label values is the list of values for the labels specified in `LabelNames()`
LabelNames(p *protocol.Protocol) []string
// Label values is the list of values for the labels specified in `LabelNames()`
LabelValues(p *protocol.Protocol) []string
}

View File

@ -22,10 +22,10 @@ func (e *LegacyMetricExporter) Describe(ch chan<- *prometheus.Desc) {
e.ipv6Exporter.Describe(ch)
}
func (e *LegacyMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
func (e *LegacyMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric, newFormat bool) {
if p.IPVersion == "4" {
e.ipv4Exporter.Export(p, ch)
e.ipv4Exporter.Export(p, ch, false)
} else {
e.ipv6Exporter.Export(p, ch)
e.ipv6Exporter.Export(p, ch, false)
}
}

View File

@ -5,7 +5,11 @@ import "github.com/czerwonk/bird_exporter/protocol"
type LegacyLabelStrategy struct {
}
func (*LegacyLabelStrategy) LabelNames() []string {
func NewLegacyLabelStrategy() *LegacyLabelStrategy {
return &LegacyLabelStrategy{}
}
func (*LegacyLabelStrategy) LabelNames(p *protocol.Protocol) []string {
return []string{"name"}
}

View File

@ -7,5 +7,5 @@ import (
type MetricExporter interface {
Describe(ch chan<- *prometheus.Desc)
Export(p *protocol.Protocol, ch chan<- prometheus.Metric)
Export(p *protocol.Protocol, ch chan<- prometheus.Metric, newFormat bool)
}

View File

@ -55,7 +55,7 @@ func (m *ospfMetricExporter) describe(ipVersion string, ch chan<- *prometheus.De
ch <- d.neighborAdjacentCountDesc
}
func (m *ospfMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
func (m *ospfMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric, newFormat bool) {
d := m.descriptions[p.IPVersion]
ch <- prometheus.MustNewConstMetric(d.runningDesc, prometheus.GaugeValue, p.Attributes["running"], p.Name)

View File

@ -15,6 +15,7 @@ import (
var (
protocolRegex *regexp.Regexp
descriptionRegex *regexp.Regexp
routeRegex *regexp.Regexp
uptimeRegex *regexp.Regexp
routeChangeRegex *regexp.Regexp
@ -32,6 +33,7 @@ type context struct {
func init() {
protocolRegex = regexp.MustCompile(`^(?:1002\-)?([^\s]+)\s+(BGP|OSPF|Direct|Device|Kernel|Static)\s+([^\s]+)\s+([^\s]+)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|[^\s]+)(?:\s+(.*?))?$`)
descriptionRegex = regexp.MustCompile(`Description:\s+(.*)`)
routeRegex = regexp.MustCompile(`^\s+Routes:\s+(\d+) imported, (?:(\d+) filtered, )?(\d+) exported(?:, (\d+) preferred)?`)
uptimeRegex = regexp.MustCompile(`^(?:((\d+):(\d{2}):(\d{2}))|(\d+)|(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))$`)
routeChangeRegex = regexp.MustCompile(`(Import|Export) (updates|withdraws):\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s*`)
@ -49,6 +51,7 @@ func ParseProtocols(data []byte, ipVersion string) []*protocol.Protocol {
var handlers = []func(*context){
handleEmptyLine,
parseLineForProtocol,
parseLineForDescription,
parseLineForChannel,
parseLineForRoutes,
parseLineForRouteChanges,
@ -97,6 +100,20 @@ func parseLineForProtocol(c *context) {
c.handled = true
}
func parseLineForDescription(c *context) {
match := descriptionRegex.FindStringSubmatch(c.line)
if match == nil {
return
}
if len(match) <= 1 {
return
}
c.current.Description = strings.Join(match[1:], " ")
}
func parseProto(val string) int {
switch val {
case "BGP":

View File

@ -11,6 +11,7 @@ const (
type Protocol struct {
Name string
Description string
IPVersion string
ImportFilter string
ExportFilter string