Debian Codegraph @

metric_collector.go

raw symbols SCIP
1 package main
2
3 import (
4 "github.com/czerwonk/bird_exporter/client"
5 "github.com/czerwonk/bird_exporter/metrics"
6 "github.com/czerwonk/bird_exporter/protocol"
7 "github.com/prometheus/client_golang/prometheus"
8 log "github.com/sirupsen/logrus"
9 )
10
11 type MetricCollector struct {
12 exporters map[protocol.Proto][]metrics.MetricExporter
13 client *client.BirdClient
14 enabledProtocols protocol.Proto
15 newFormat bool
16 status *metrics.StatusExporter
17 }
18
19 func NewMetricCollector(newFormat bool, enabledProtocols protocol.Proto, descriptionLabels bool, socketPath string) *MetricCollector {
20 c := getClient()
21 var e map[protocol.Proto][]metrics.MetricExporter
22
23 if newFormat {
24 e = exportersForDefault(c, descriptionLabels)
25 } else {
26 e = exportersForLegacy(c)
27 }
28
29 return &MetricCollector{
30 exporters: e,
31 client: c,
32 enabledProtocols: enabledProtocols,
33 newFormat: newFormat,
34 status: metrics.NewStatusExporter(c, socketPath),
35 }
36 }
37
38 func getClient() *client.BirdClient {
39 o := &client.BirdClientOptions{
40 BirdSocket: *birdSocket,
41 Bird6Socket: *bird6Socket,
42 Bird6Enabled: *bird6Enabled,
43 BirdEnabled: *birdEnabled,
44 BirdV2: *birdV2,
45 }
46
47 return &client.BirdClient{Options: o}
48 }
49
50 func exportersForLegacy(c *client.BirdClient) map[protocol.Proto][]metrics.MetricExporter {
51 l := metrics.NewLegacyLabelStrategy()
52
53 return map[protocol.Proto][]metrics.MetricExporter{
54 protocol.BGP: {metrics.NewLegacyMetricExporter("bgp4_session", "bgp6_session", l)},
55 protocol.Direct: {metrics.NewLegacyMetricExporter("direct4", "direct6", l)},
56 protocol.Kernel: {metrics.NewLegacyMetricExporter("kernel4", "kernel6", l)},
57 protocol.OSPF: {metrics.NewLegacyMetricExporter("ospf", "ospfv3", l), metrics.NewOSPFExporter("", c)},
58 protocol.Static: {metrics.NewLegacyMetricExporter("static4", "static6", l)},
59 protocol.Babel: {metrics.NewLegacyMetricExporter("babel4", "babel6", l)},
60 protocol.RPKI: {metrics.NewLegacyMetricExporter("rpki4", "rpki6", l)},
61 protocol.BFD: {metrics.NewBFDExporter(c)},
62 }
63 }
64
65 func exportersForDefault(c *client.BirdClient, descriptionLabels bool) map[protocol.Proto][]metrics.MetricExporter {
66 l := metrics.NewDefaultLabelStrategy(descriptionLabels, *descriptionLabelsRegex)
67 e := metrics.NewGenericProtocolMetricExporter("bird_protocol", true, l)
68
69 return map[protocol.Proto][]metrics.MetricExporter{
70 protocol.BGP: {e},
71 protocol.Direct: {e},
72 protocol.Kernel: {e},
73 protocol.OSPF: {e, metrics.NewOSPFExporter("bird_", c)},
74 protocol.Static: {e},
75 protocol.Babel: {e},
76 protocol.RPKI: {e},
77 protocol.BFD: {metrics.NewBFDExporter(c)},
78 }
79 }
80
81 var socketQueryDesc = prometheus.NewDesc(
82 "bird_socket_query_success",
83 "Result of querying bird socket: 0 = failed, 1 = suceeded",
84 nil,
85 nil,
86 )
87
88 func (m *MetricCollector) Describe(ch chan<- *prometheus.Desc) {
89
90 ch <- socketQueryDesc
91
92 for _, v := range m.exporters {
93 for _, e := range v {
94 e.Describe(ch)
95 }
96 }
97 if m.status != nil {
98 m.status.Describe(ch)
99 }
100 }
101
102 func (m *MetricCollector) Collect(ch chan<- prometheus.Metric) {
103
104 protocols, err := m.client.GetProtocols()
105
106 var queryResult float64 = 1
107 if err != nil {
108 queryResult = 0
109 }
110 ch <- prometheus.MustNewConstMetric(socketQueryDesc, prometheus.GaugeValue, queryResult)
111
112 if err != nil {
113 log.Errorln(err)
114 return
115 }
116
117 for _, p := range protocols {
118 if p.Proto == protocol.PROTO_UNKNOWN || (m.enabledProtocols&p.Proto != p.Proto) {
119 continue
120 }
121
122 for _, e := range m.exporters[p.Proto] {
123 e.Export(p, ch, m.newFormat)
124 }
125 }
126
127 if m.status != nil {
128 m.status.Collect(ch)
129 }
130 }