Skip to content

Commit

Permalink
Update dependencies and implement structured logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tynany committed Sep 13, 2024
1 parent ad34a2f commit a1a2eff
Show file tree
Hide file tree
Showing 13 changed files with 624 additions and 632 deletions.
65 changes: 35 additions & 30 deletions collector/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/Juniper/go-netconf/netconf"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -55,11 +56,13 @@ var (
)

// BGPCollector collects BGP metrics, implemented as per the Collector interface.
type BGPCollector struct{}
type BGPCollector struct {
logger log.Logger
}

// NewBGPCollector returns a new BGPCollector
func NewBGPCollector() *BGPCollector {
return &BGPCollector{}
func NewBGPCollector(logger log.Logger) *BGPCollector {
return &BGPCollector{logger: logger}
}

// Name of the collector.
Expand Down Expand Up @@ -138,12 +141,13 @@ func (c *BGPCollector) Get(ch chan<- prometheus.Metric, conf Config) ([]error, f
bgpPeerInterfaces,
routeInstances,
routeInstanceNames,
c.logger,
); err != nil {
totalBGPErrors++
errors = append(errors, err)
}

if err := processBGPNeighborNetconfReply(replyNeighbor, ch); err != nil {
if err := processBGPNeighborNetconfReply(replyNeighbor, ch, c.logger); err != nil {
totalBGPErrors++
errors = append(errors, err)
}
Expand Down Expand Up @@ -186,7 +190,7 @@ func getBgpPeerInterface(reply *netconf.RPCReply) (map[string]string, error) {
return peerToInterface, nil
}

func processBGPNeighborNetconfReply(reply *netconf.RPCReply, ch chan<- prometheus.Metric) error {
func processBGPNeighborNetconfReply(reply *netconf.RPCReply, ch chan<- prometheus.Metric, logger log.Logger) error {
var netconfReply bgpNeighborRPCReply
if err := xml.Unmarshal([]byte(reply.RawReply), &netconfReply); err != nil {
return fmt.Errorf("could not unmarshal netconf reply xml: %s", err)
Expand Down Expand Up @@ -214,7 +218,7 @@ func processBGPNeighborNetconfReply(reply *netconf.RPCReply, ch chan<- prometheu
strings.TrimSpace(peerData.PeerCfgRti.Text),
strings.TrimSpace(peerData.NlriTypePeer),
}
newGauge(ch, bgpDesc["PeerRIBAdvertisedPrefixCount"], peerData.BGPRIB.AdvertisedPrefixCount, peerLabels...)
newGauge(logger, ch, bgpDesc["PeerRIBAdvertisedPrefixCount"], peerData.BGPRIB.AdvertisedPrefixCount, peerLabels...)
}
}
return nil
Expand All @@ -229,6 +233,7 @@ func processBGPNetconfReply(
bgpPeerInterfaces map[string]string,
routeInstances map[string]string,
routeInstanceNames map[string]string,
logger log.Logger,
) error {

var netconfReply bgpNeighborRPCReply
Expand All @@ -250,27 +255,27 @@ func processBGPNetconfReply(
return fmt.Errorf("could not unmarshal netconf reply xml: %s", err)
}
ribLabels := []string{routeInstanceBGP}
newGauge(ch, bgpDesc["GroupCount"], netconfReplyInstance.BgpInformation.GroupCount, ribLabels...)
newGauge(ch, bgpDesc["PeerCount"], netconfReplyInstance.BgpInformation.PeerCount, ribLabels...)
newGauge(ch, bgpDesc["DownPeerCount"], netconfReplyInstance.BgpInformation.DownPeerCount, ribLabels...)
newGauge(logger, ch, bgpDesc["GroupCount"], netconfReplyInstance.BgpInformation.GroupCount, ribLabels...)
newGauge(logger, ch, bgpDesc["PeerCount"], netconfReplyInstance.BgpInformation.PeerCount, ribLabels...)
newGauge(logger, ch, bgpDesc["DownPeerCount"], netconfReplyInstance.BgpInformation.DownPeerCount, ribLabels...)
for routeInstance := range routeInstances {
if routeInstanceBGP == routeInstances[routeInstance] {
for _, ribData := range netconfReplyInstance.BgpInformation.BgpRib {
if ribData.Name == routeInstance {
if val, ok := routeInstanceCheck[routeInstanceBGP]; !ok {
routeInstanceCheck[routeInstanceBGP] = val
newGauge(ch, bgpDesc["RIBTotalPrefixCount"], ribData.TotalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBHistoryPrefixCount"], ribData.HistoryPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBDampedPrefixCount"], ribData.DampedPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBTotalExternalPrefixCount"], ribData.TotalExternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBActiveExternalPrefixCount"], ribData.ActiveExternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBAcceptedExternalPrefixCount"], ribData.AcceptedExternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBSuppressedExternalPrefixCount"], ribData.SuppressedExternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBTotalInternalPrefixCount"], ribData.TotalInternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBActiveInternalPrefixCount"], ribData.ActiveInternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBAcceptedInternalPrefixCount"], ribData.AcceptedInternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBSuppressedInternalPrefixCount"], ribData.SuppressedInternalPrefixCount, ribLabels...)
newGauge(ch, bgpDesc["RIBPendingPrefixCount"], ribData.PendingPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBTotalPrefixCount"], ribData.TotalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBHistoryPrefixCount"], ribData.HistoryPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBDampedPrefixCount"], ribData.DampedPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBTotalExternalPrefixCount"], ribData.TotalExternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBActiveExternalPrefixCount"], ribData.ActiveExternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBAcceptedExternalPrefixCount"], ribData.AcceptedExternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBSuppressedExternalPrefixCount"], ribData.SuppressedExternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBTotalInternalPrefixCount"], ribData.TotalInternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBActiveInternalPrefixCount"], ribData.ActiveInternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBAcceptedInternalPrefixCount"], ribData.AcceptedInternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBSuppressedInternalPrefixCount"], ribData.SuppressedInternalPrefixCount, ribLabels...)
newGauge(logger, ch, bgpDesc["RIBPendingPrefixCount"], ribData.PendingPrefixCount, ribLabels...)
}
}
}
Expand Down Expand Up @@ -327,15 +332,15 @@ func processBGPNetconfReply(
ch <- prometheus.MustNewConstMetric(bgpDesc["PeerPeerState"], prometheus.GaugeValue, 0.0, peerRIBLabels...)
}

newCounter(ch, bgpDesc["PeerInputMessages"], peerData.InputMessages.Text, peerRIBLabels...)
newCounter(ch, bgpDesc["PeerOutputMessages"], peerData.OutputMessages.Text, peerRIBLabels...)
newGauge(ch, bgpDesc["PeerRouteQueueCount"], peerData.RouteQueueCount.Text, peerRIBLabels...)
newCounter(ch, bgpDesc["PeerFlapCount"], peerData.FlapCount.Text, peerRIBLabels...)
newGauge(ch, bgpDesc["PeerElapsedTime"], peerData.ElapsedTime.Seconds, peerRIBLabels...)
newGauge(ch, bgpDesc["PeerRIBActivePrefixCount"], peerData.BGPRIB.ActivePrefixCount, peerRIBLabels...)
newGauge(ch, bgpDesc["PeerRIBReceivedPrefixCount"], peerData.BGPRIB.ReceivedPrefixCount, peerRIBLabels...)
newGauge(ch, bgpDesc["PeerRIBAcceptedPrefixCount"], peerData.BGPRIB.AcceptedPrefixCount, peerRIBLabels...)
newGauge(ch, bgpDesc["PeerRIBSuppressedPrefixCount"], peerData.BGPRIB.SuppressedPrefixCount, peerRIBLabels...)
newCounter(logger, ch, bgpDesc["PeerInputMessages"], peerData.InputMessages.Text, peerRIBLabels...)
newCounter(logger, ch, bgpDesc["PeerOutputMessages"], peerData.OutputMessages.Text, peerRIBLabels...)
newGauge(logger, ch, bgpDesc["PeerRouteQueueCount"], peerData.RouteQueueCount.Text, peerRIBLabels...)
newCounter(logger, ch, bgpDesc["PeerFlapCount"], peerData.FlapCount.Text, peerRIBLabels...)
newGauge(logger, ch, bgpDesc["PeerElapsedTime"], peerData.ElapsedTime.Seconds, peerRIBLabels...)
newGauge(logger, ch, bgpDesc["PeerRIBActivePrefixCount"], peerData.BGPRIB.ActivePrefixCount, peerRIBLabels...)
newGauge(logger, ch, bgpDesc["PeerRIBReceivedPrefixCount"], peerData.BGPRIB.ReceivedPrefixCount, peerRIBLabels...)
newGauge(logger, ch, bgpDesc["PeerRIBAcceptedPrefixCount"], peerData.BGPRIB.AcceptedPrefixCount, peerRIBLabels...)
newGauge(logger, ch, bgpDesc["PeerRIBSuppressedPrefixCount"], peerData.BGPRIB.SuppressedPrefixCount, peerRIBLabels...)
}

for peerType, count := range peerTypes {
Expand Down
25 changes: 14 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"sync"
"time"

"github.com/prometheus/common/log"
"github.com/go-kit/log"
"github.com/go-kit/log/level"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -48,13 +49,15 @@ type Config struct {
type Exporter struct {
Collectors []Collector
config Config
logger log.Logger
}

// NewExporter returns a new Exporter.
func NewExporter(collectors []Collector, config Config) (*Exporter, error) {
func NewExporter(collectors []Collector, config Config, logger log.Logger) (*Exporter, error) {
return &Exporter{
Collectors: collectors,
config: config,
logger: logger,
}, nil
}

Expand All @@ -66,12 +69,12 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
wg := &sync.WaitGroup{}
for _, collector := range e.Collectors {
wg.Add(1)
go e.runCollector(ch, collector, wg)
go e.runCollector(ch, collector, wg, e.logger)
}
wg.Wait()
}

func (e *Exporter) runCollector(ch chan<- prometheus.Metric, collector Collector, wg *sync.WaitGroup) {
func (e *Exporter) runCollector(ch chan<- prometheus.Metric, collector Collector, wg *sync.WaitGroup, logger log.Logger) {
defer wg.Done()
collectorName := collector.Name()

Expand All @@ -84,7 +87,7 @@ func (e *Exporter) runCollector(ch chan<- prometheus.Metric, collector Collector
if len(errors) > 0 {
ch <- prometheus.MustNewConstMetric(junosDesc["CollectorUp"], prometheus.GaugeValue, 0, collector.Name())
for _, err := range errors {
log.Errorf("collector %q scrape failed: %s", collectorName, err)
level.Error(logger).Log("msg", "collector scrape failed", "collector", collectorName, "err", err)
}
} else {
ch <- prometheus.MustNewConstMetric(junosDesc["CollectorUp"], prometheus.GaugeValue, 1, collectorName)
Expand All @@ -106,32 +109,32 @@ func colPromDesc(subsystem string, metricName string, metricDescription string,
return prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, metricName), metricDescription, labels, nil)
}

func newGauge(ch chan<- prometheus.Metric, descName *prometheus.Desc, metric string, labels ...string) {
func newGauge(logger log.Logger, ch chan<- prometheus.Metric, descName *prometheus.Desc, metric string, labels ...string) {
if metric != "" {
i, err := strconv.ParseFloat(strings.TrimSpace(metric), 64)
if err != nil {
log.Errorf("could not convert metric to float64: %s", err)
level.Error(logger).Log("msg", "could not convert metric to float64", "err", err)
}
ch <- prometheus.MustNewConstMetric(descName, prometheus.GaugeValue, i, labels...)
}
}

func newCounter(ch chan<- prometheus.Metric, descName *prometheus.Desc, metric string, labels ...string) {
func newCounter(logger log.Logger, ch chan<- prometheus.Metric, descName *prometheus.Desc, metric string, labels ...string) {
if metric != "" {
i, err := strconv.ParseFloat(strings.TrimSpace(metric), 64)
if err != nil {
log.Errorf("could not convert metric to float64: %s", err)
level.Error(logger).Log("msg", "could not convert metric to float64", "err", err)
}
ch <- prometheus.MustNewConstMetric(descName, prometheus.CounterValue, i, labels...)
}
}

func newGaugeMB(ch chan<- prometheus.Metric, descName *prometheus.Desc, metric string, labels ...string) {
func newGaugeMB(logger log.Logger, ch chan<- prometheus.Metric, descName *prometheus.Desc, metric string, labels ...string) {
if metric != "" {
re := regexp.MustCompile("[0-9]+")
i, err := strconv.ParseFloat(strings.TrimSpace(re.FindString(metric)), 64)
if err != nil {
log.Errorf("could not convert metric to float64: %s", err)
level.Error(logger).Log("msg", "could not convert metric to float64", "err", err)
}

ch <- prometheus.MustNewConstMetric(descName, prometheus.GaugeValue, i*1000000, labels...)
Expand Down
28 changes: 16 additions & 12 deletions collector/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/Juniper/go-netconf/netconf"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -29,11 +30,13 @@ var (
)

// EnvCollector collects environment metrics, implemented as per the Collector interface.
type EnvCollector struct{}
type EnvCollector struct {
logger log.Logger
}

// NewEnvCollector returns a new EnvCollector.
func NewEnvCollector() *EnvCollector {
return &EnvCollector{}
func NewEnvCollector(logger log.Logger) *EnvCollector {
return &EnvCollector{logger: logger}
}

// Name of the collector.
Expand Down Expand Up @@ -68,7 +71,7 @@ func (c *EnvCollector) Get(ch chan<- prometheus.Metric, conf Config) ([]error, f
return errors, totalEnvErrors
}

if err := processEnvNetconfReply(replyEnv, replyEnvTempThreshold, ch); err != nil {
if err := processEnvNetconfReply(replyEnv, replyEnvTempThreshold, ch, c.logger); err != nil {
totalEnvErrors++
errors = append(errors, err)
}
Expand All @@ -80,6 +83,7 @@ func processEnvNetconfReply(
replyEnv *netconf.RPCReply,
replyEnvTempThreshold *netconf.RPCReply,
ch chan<- prometheus.Metric,
logger log.Logger,
) error {
var netconfEnvReply envRPCReply
var netconfEnvTempThresholdReply envTempThresholdRPCReply
Expand All @@ -95,7 +99,7 @@ func processEnvNetconfReply(
envStatus = 1.0
}
ch <- prometheus.MustNewConstMetric(envDesc["Status"], prometheus.GaugeValue, envStatus, labels...)
newGauge(ch, envDesc["Temp"], envData.Temperature.Temp, labels...)
newGauge(logger, ch, envDesc["Temp"], envData.Temperature.Temp, labels...)
}
// ** unmarshal show chassis temperature-thresholds <get-environment-information> END ** //

Expand All @@ -107,13 +111,13 @@ func processEnvNetconfReply(
for _, envTempThresholdData := range netconfEnvTempThresholdReply.EnvTempThresholdInformation.EnvTempThreshold {
labels := []string{strings.TrimSpace(envTempThresholdData.Name.Text)}

newGauge(ch, envDesc["FanNormalSpeed"], envTempThresholdData.FanNormalSpeed.Text, labels...)
newGauge(ch, envDesc["FanHighSpeed"], envTempThresholdData.FanHighSpeed.Text, labels...)
newGauge(ch, envDesc["BadFanYellowAlarm"], envTempThresholdData.BadFanYellowAlarm.Text, labels...)
newGauge(ch, envDesc["BadFanRedAlarm"], envTempThresholdData.BadFanRedAlarm.Text, labels...)
newGauge(ch, envDesc["YellowAlarm"], envTempThresholdData.YellowAlarm.Text, labels...)
newGauge(ch, envDesc["RedAlarm"], envTempThresholdData.RedAlarm.Text, labels...)
newGauge(ch, envDesc["FireShutdown"], envTempThresholdData.FireShutdown.Text, labels...)
newGauge(logger, ch, envDesc["FanNormalSpeed"], envTempThresholdData.FanNormalSpeed.Text, labels...)
newGauge(logger, ch, envDesc["FanHighSpeed"], envTempThresholdData.FanHighSpeed.Text, labels...)
newGauge(logger, ch, envDesc["BadFanYellowAlarm"], envTempThresholdData.BadFanYellowAlarm.Text, labels...)
newGauge(logger, ch, envDesc["BadFanRedAlarm"], envTempThresholdData.BadFanRedAlarm.Text, labels...)
newGauge(logger, ch, envDesc["YellowAlarm"], envTempThresholdData.YellowAlarm.Text, labels...)
newGauge(logger, ch, envDesc["RedAlarm"], envTempThresholdData.RedAlarm.Text, labels...)
newGauge(logger, ch, envDesc["FireShutdown"], envTempThresholdData.FireShutdown.Text, labels...)
}

// ** unmarshal show chassis temperature-thresholds <get-temperature-threshold-information> END ** //
Expand Down
9 changes: 6 additions & 3 deletions collector/fpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/Juniper/go-netconf/netconf"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -30,11 +31,13 @@ var (
)

// FPCCollector collects environment metrics, implemented as per the Collector interface.
type FPCCollector struct{}
type FPCCollector struct {
logger log.Logger
}

// NewFPCCollector returns a new FPCCollector.
func NewFPCCollector() *FPCCollector {
return &FPCCollector{}
func NewFPCCollector(logger log.Logger) *FPCCollector {
return &FPCCollector{logger: logger}
}

// Name of the collector.
Expand Down
Loading

0 comments on commit a1a2eff

Please sign in to comment.