Skip to content

Commit

Permalink
Refactor logging (#1)
Browse files Browse the repository at this point in the history
* added context on logs
* change some logging from "error" to info when its expected to have no data (no disk on some storage controllers)
  • Loading branch information
mjavier2k authored Oct 21, 2020
1 parent 49dc837 commit 34aafa4
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ redfish_exporter

# Editor backup files
*~

# config file
config.yml
45 changes: 24 additions & 21 deletions collector/chassis_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"sync"

"github.com/apex/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/stmcginnis/gofish"
"github.com/stmcginnis/gofish/redfish"
)
Expand Down Expand Up @@ -183,20 +183,23 @@ type ChassisCollector struct {
redfishClient *gofish.APIClient
metrics map[string]chassisMetric
collectorScrapeStatus *prometheus.GaugeVec
Log *log.Entry
}

type chassisMetric struct {
desc *prometheus.Desc
}

// NewChassisCollector returns a collector that collecting chassis statistics
func NewChassisCollector(namespace string, redfishClient *gofish.APIClient) *ChassisCollector {

func NewChassisCollector(namespace string, redfishClient *gofish.APIClient, logger *log.Entry) *ChassisCollector {
// get service from redfish client

return &ChassisCollector{
redfishClient: redfishClient,
metrics: chassisMetrics,
Log: logger.WithFields(log.Fields{
"collector": "ChassisCollector",
}),
collectorScrapeStatus: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -219,15 +222,17 @@ func (c *ChassisCollector) Describe(ch chan<- *prometheus.Desc) {

// Collect implemented prometheus.Collector
func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {

collectorLogContext := c.Log
service := c.redfishClient.Service

// get a list of chassis from service
if chassises, err := service.Chassis(); err != nil {
log.Infof("Errors Getting chassis from service : %s", err)
collectorLogContext.WithField("operation", "service.Chassis()").WithError(err).Error("error getting chassis from service")
} else {
// process the chassises
for _, chassis := range chassises {
chassisLogContext := collectorLogContext.WithField("Chassis", chassis.ID)
chassisLogContext.Info("collector scrape started")
chassisID := chassis.ID
chassisStatus := chassis.Status
chassisStatusState := chassisStatus.State
Expand All @@ -242,9 +247,9 @@ func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {

chassisThermal, err := chassis.Thermal()
if err != nil {
log.Infof("Errors Getting Thermal from chassis, error is : %s ", err)
chassisLogContext.WithField("operation", "chassis.Thermal()").WithError(err).Error("error getting thermal data from chassis")
} else if chassisThermal == nil {
log.Info("Errors Getting Thermal from chassis, no Thermal data found")
chassisLogContext.WithField("operation", "chassis.Thermal()").Info("no thermal data found")
} else {
// process temperature
chassisTemperatures := chassisThermal.Temperatures
Expand All @@ -267,10 +272,9 @@ func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {

chassisPowerInfo, err := chassis.Power()
if err != nil {
log.Infof("Errors Getting powerinfo from chassis : %s", err)
chassisLogContext.WithField("operation", "chassis.Power()").WithError(err).Error("error getting power data from chassis")
} else if chassisPowerInfo == nil {

log.Info("Errors Getting powerinfo from chassis, no powerinfo found")
chassisLogContext.WithField("operation", "chassis.Power()").Info("no power data found")
} else {
// power votages
chassisPowerInfoVoltages := chassisPowerInfo.Voltages
Expand Down Expand Up @@ -302,19 +306,18 @@ func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {
// process NetapAdapter

networkAdapters, err := chassis.NetworkAdapters()

if err != nil {
log.Infof("Errors Getting NetworkAdapters from chassis : %s", err)
chassisLogContext.WithField("operation", "chassis.NetworkAdapters()").WithError(err).Error("error getting network adapters data from chassis")
} else if networkAdapters == nil {
log.Info("Errors Getting NetworkAdapters from chassis, no NetworkAdapters data found")
chassisLogContext.WithField("operation", "chassis.NetworkAdapters()").Info("no network adapters data found")
} else {
wg5 := &sync.WaitGroup{}
wg5.Add(len(networkAdapters))

for _, networkAdapter := range networkAdapters {

go parseNetworkAdapter(ch, chassisID, networkAdapter, wg5)

if err = parseNetworkAdapter(ch, chassisID, networkAdapter, wg5); err != nil {
chassisLogContext.WithField("operation", "chassis.NetworkAdapters()").WithError(err).Error("error getting network ports from network adapter")
}
}
}

Expand All @@ -327,11 +330,11 @@ func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {
ChassisPhysicalSecurityLabelValues := []string{"physical_security", chassisID, physicalSecurityIntrusionSensorNumber, physicalSecurityIntrusionSensor}
ch <- prometheus.MustNewConstMetric(chassisMetrics["chassis_physical_security_sensor_rearm_method"].desc, prometheus.GaugeValue, phySecReArmMethod, ChassisPhysicalSecurityLabelValues...)
}

}

chassisLogContext.Info("collector scrape completed")
}
}

c.collectorScrapeStatus.WithLabelValues("chassis").Set(float64(1))
}

Expand Down Expand Up @@ -417,7 +420,7 @@ func parseChassisPowerInfoPowerSupply(ch chan<- prometheus.Metric, chassisID str
ch <- prometheus.MustNewConstMetric(chassisMetrics["chassis_power_powersupply_power_capacity_watts"].desc, prometheus.GaugeValue, float64(chassisPowerInfoPowerSupplyPowerCapacityWatts), chassisPowerSupplyLabelvalues...)
}

func parseNetworkAdapter(ch chan<- prometheus.Metric, chassisID string, networkAdapter *redfish.NetworkAdapter, wg *sync.WaitGroup) {
func parseNetworkAdapter(ch chan<- prometheus.Metric, chassisID string, networkAdapter *redfish.NetworkAdapter, wg *sync.WaitGroup) error {

defer wg.Done()
networkAdapterName := networkAdapter.Name
Expand All @@ -433,15 +436,15 @@ func parseNetworkAdapter(ch chan<- prometheus.Metric, chassisID string, networkA
}

if networkPorts, err := networkAdapter.NetworkPorts(); err != nil {
log.Infof("Errors Getting Network port from networkAdapter : %s", err)
return err
} else {
wg6 := &sync.WaitGroup{}
wg6.Add(len(networkPorts))
for _, networkPort := range networkPorts {
go parseNetworkPort(ch, chassisID, networkPort, networkAdapterName, networkAdapterID, wg6)
}

}
return nil
}

func parseNetworkPort(ch chan<- prometheus.Metric, chassisID string, networkPort *redfish.NetworkPort, networkAdapterName string, networkAdapterID string, wg *sync.WaitGroup) {
Expand Down
16 changes: 11 additions & 5 deletions collector/manager_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package collector
import (
"fmt"

"github.com/apex/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/stmcginnis/gofish"
)

Expand Down Expand Up @@ -52,13 +52,17 @@ type ManagerCollector struct {
metrics map[string]managerMetric
collectorScrapeStatus *prometheus.GaugeVec
collectorScrapeDuration *prometheus.SummaryVec
Log *log.Entry
}

// NewManagerCollector returns a collector that collecting memory statistics
func NewManagerCollector(namespace string, redfishClient *gofish.APIClient) *ManagerCollector {
func NewManagerCollector(namespace string, redfishClient *gofish.APIClient, logger *log.Entry) *ManagerCollector {
return &ManagerCollector{
redfishClient: redfishClient,
metrics: managerMetrics,
Log: logger.WithFields(log.Fields{
"collector": "ManagerCollector",
}),
collectorScrapeStatus: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -81,17 +85,18 @@ func (m *ManagerCollector) Describe(ch chan<- *prometheus.Desc) {

// Collect implemented prometheus.Collector
func (m *ManagerCollector) Collect(ch chan<- prometheus.Metric) {
collectorLogContext := m.Log
//get service
service := m.redfishClient.Service

// get a list of managers from service
if managers, err := service.Managers(); err != nil {
log.Infof("Errors Getting managers from service : %s", err)
collectorLogContext.WithField("operation", "service.Managers()").WithError(err).Error("error getting managers from service")
} else {

for _, manager := range managers {
managerLogContext := collectorLogContext.WithField("Manager", manager.ID)
managerLogContext.Info("collector scrape started")
// overall manager metrics

ManagerID := manager.ID
managerName := manager.Name
managerModel := manager.Model
Expand All @@ -112,6 +117,7 @@ func (m *ManagerCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(m.metrics["manager_power_state"].desc, prometheus.GaugeValue, managerPowerStateValue, ManagerLabelValues...)

}
managerLogContext.Info("collector scrape completed")
}
m.collectorScrapeStatus.WithLabelValues("manager").Set(float64(1))

Expand Down
15 changes: 7 additions & 8 deletions collector/redfish_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync"
"time"

"github.com/apex/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
gofish "github.com/stmcginnis/gofish"
gofishcommon "github.com/stmcginnis/gofish/common"
redfish "github.com/stmcginnis/gofish/redfish"
Expand Down Expand Up @@ -40,16 +40,16 @@ type RedfishCollector struct {
}

// NewRedfishCollector return RedfishCollector
func NewRedfishCollector(host string, username string, password string) *RedfishCollector {
func NewRedfishCollector(host string, username string, password string, logger *log.Entry) *RedfishCollector {
var collectors map[string]prometheus.Collector

collectorLogCtx := logger
redfishClient, err := newRedfishClient(host, username, password)
if err != nil {
log.Infof("Errors occours when creating redfish client: %s", err)
collectorLogCtx.WithError(err).Error("error creating redfish client")
} else {
chassisCollector := NewChassisCollector(namespace, redfishClient)
systemCollector := NewSystemCollector(namespace, redfishClient)
managerCollector := NewManagerCollector(namespace, redfishClient)
chassisCollector := NewChassisCollector(namespace, redfishClient, collectorLogCtx)
systemCollector := NewSystemCollector(namespace, redfishClient, collectorLogCtx)
managerCollector := NewManagerCollector(namespace, redfishClient, collectorLogCtx)

collectors = map[string]prometheus.Collector{"chassis": chassisCollector, "system": systemCollector, "manager": managerCollector}
}
Expand Down Expand Up @@ -113,7 +113,6 @@ func newRedfishClient(host string, username string, password string) (*gofish.AP
}
redfishClient, err := gofish.Connect(config)
if err != nil {
log.Infof("Errors occours when creating redfish client: %s", err)
return nil, err
}
return redfishClient, nil
Expand Down
Loading

0 comments on commit 34aafa4

Please sign in to comment.