refactoring

This commit is contained in:
Daniel Czerwonk 2017-11-30 22:35:14 +01:00
parent 22ac426781
commit baeb9c804e
9 changed files with 37 additions and 141 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea
*.iml
bird_exporter

View File

@ -1,26 +0,0 @@
package bgp
import (
"github.com/czerwonk/bird_exporter/protocol"
"github.com/prometheus/client_golang/prometheus"
)
var exporter map[int]*protocol.GenericProtocolMetricExporter
type BgpMetricExporter struct {
}
func init() {
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
exporter[4] = protocol.NewGenericProtocolMetricExporter("bgp4_session")
exporter[6] = protocol.NewGenericProtocolMetricExporter("bgp6_session")
}
func (*BgpMetricExporter) Describe(ch chan<- *prometheus.Desc) {
exporter[4].Describe(ch)
exporter[6].Describe(ch)
}
func (*BgpMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
exporter[p.IpVersion].Export(p, ch)
}

View File

@ -1,26 +0,0 @@
package device
import (
"github.com/czerwonk/bird_exporter/protocol"
"github.com/prometheus/client_golang/prometheus"
)
var exporter map[int]*protocol.GenericProtocolMetricExporter
type DeviceMetricExporter struct {
}
func init() {
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
exporter[4] = protocol.NewGenericProtocolMetricExporter("device4")
exporter[6] = protocol.NewGenericProtocolMetricExporter("device6")
}
func (m *DeviceMetricExporter) Describe(ch chan<- *prometheus.Desc) {
exporter[4].Describe(ch)
exporter[6].Describe(ch)
}
func (m *DeviceMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
exporter[p.IpVersion].Export(p, ch)
}

View File

@ -1,26 +0,0 @@
package direct
import (
"github.com/czerwonk/bird_exporter/protocol"
"github.com/prometheus/client_golang/prometheus"
)
var exporter map[int]*protocol.GenericProtocolMetricExporter
type DirectMetricExporter struct {
}
func init() {
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
exporter[4] = protocol.NewGenericProtocolMetricExporter("direct4")
exporter[6] = protocol.NewGenericProtocolMetricExporter("direct6")
}
func (*DirectMetricExporter) Describe(ch chan<- *prometheus.Desc) {
exporter[4].Describe(ch)
exporter[6].Describe(ch)
}
func (*DirectMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
exporter[p.IpVersion].Export(p, ch)
}

View File

@ -1,26 +0,0 @@
package kernel
import (
"github.com/czerwonk/bird_exporter/protocol"
"github.com/prometheus/client_golang/prometheus"
)
var exporter map[int]*protocol.GenericProtocolMetricExporter
type KernelMetricExporter struct {
}
func init() {
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
exporter[4] = protocol.NewGenericProtocolMetricExporter("kernel4")
exporter[6] = protocol.NewGenericProtocolMetricExporter("kernel6")
}
func (*KernelMetricExporter) Describe(ch chan<- *prometheus.Desc) {
exporter[4].Describe(ch)
exporter[6].Describe(ch)
}
func (*KernelMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
exporter[p.IpVersion].Export(p, ch)
}

View File

@ -11,7 +11,7 @@ import (
"github.com/prometheus/common/log"
)
const version string = "0.9.0"
const version string = "1.0.0"
var (
showVersion = flag.Bool("version", false, "Print version information.")

View File

@ -1,13 +1,8 @@
package main
import (
"github.com/czerwonk/bird_exporter/bgp"
"github.com/czerwonk/bird_exporter/device"
"github.com/czerwonk/bird_exporter/direct"
"github.com/czerwonk/bird_exporter/kernel"
"github.com/czerwonk/bird_exporter/ospf"
"github.com/czerwonk/bird_exporter/protocol"
"github.com/czerwonk/bird_exporter/static"
"github.com/prometheus/client_golang/prometheus"
)
@ -23,12 +18,12 @@ type MetricCollector struct {
func NewMetricCollectorForProtocols(protocols []*protocol.Protocol) *MetricCollector {
e := map[int]MetricExporter{
protocol.BGP: &bgp.BgpMetricExporter{},
protocol.Device: &device.DeviceMetricExporter{},
protocol.Direct: &direct.DirectMetricExporter{},
protocol.Kernel: &kernel.KernelMetricExporter{},
protocol.BGP: protocol.NewMetricExporter("bgp4_session", "bgp6_session"),
protocol.Device: protocol.NewMetricExporter("device4", "device6"),
protocol.Direct: protocol.NewMetricExporter("direct4", "direct6"),
protocol.Kernel: protocol.NewMetricExporter("kernel4", "kernel6"),
protocol.OSPF: &ospf.OspfMetricExporter{},
protocol.Static: &static.StaticMetricExporter{},
protocol.Static: protocol.NewMetricExporter("static4", "static6"),
}
return &MetricCollector{protocols: protocols, exporters: e}

30
protocol/exporter.go Normal file
View File

@ -0,0 +1,30 @@
package protocol
import (
"github.com/prometheus/client_golang/prometheus"
)
type ProtocolMetricExporter struct {
ipv4Exporter *GenericProtocolMetricExporter
ipv6Exporter *GenericProtocolMetricExporter
}
func NewMetricExporter(prefixIpv4, prefixIpv6 string) *ProtocolMetricExporter {
return &ProtocolMetricExporter{
ipv4Exporter: NewGenericProtocolMetricExporter(prefixIpv4),
ipv6Exporter: NewGenericProtocolMetricExporter(prefixIpv6),
}
}
func (e *ProtocolMetricExporter) Describe(ch chan<- *prometheus.Desc) {
e.ipv4Exporter.Describe(ch)
e.ipv6Exporter.Describe(ch)
}
func (e *ProtocolMetricExporter) Export(p *Protocol, ch chan<- prometheus.Metric) {
if p.IpVersion == 4 {
e.ipv4Exporter.Export(p, ch)
} else {
e.ipv6Exporter.Export(p, ch)
}
}

View File

@ -1,26 +0,0 @@
package static
import (
"github.com/czerwonk/bird_exporter/protocol"
"github.com/prometheus/client_golang/prometheus"
)
var exporter map[int]*protocol.GenericProtocolMetricExporter
type StaticMetricExporter struct {
}
func init() {
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
exporter[4] = protocol.NewGenericProtocolMetricExporter("static4")
exporter[6] = protocol.NewGenericProtocolMetricExporter("static6")
}
func (m *StaticMetricExporter) Describe(ch chan<- *prometheus.Desc) {
exporter[4].Describe(ch)
exporter[6].Describe(ch)
}
func (m *StaticMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric) {
exporter[p.IpVersion].Export(p, ch)
}