Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add config option to not collect logs #2

Draft
wants to merge 1 commit into
base: stackhpc-upstream-master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions collector/chassis_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func createChassisMetricMap() map[string]Metric {
}

// NewChassisCollector returns a collector that collecting chassis statistics
func NewChassisCollector(redfishClient *gofish.APIClient, logger *log.Entry) *ChassisCollector {
func NewChassisCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *ChassisCollector {
// get service from redfish client

return &ChassisCollector{
Expand Down Expand Up @@ -267,7 +267,7 @@ func parseChassisTemperature(ch chan<- prometheus.Metric, chassisID string, chas
chassisTemperatureStatus := chassisTemperature.Status
chassisTemperatureLabelvalues := []string{"temperature", chassisID, chassisTemperatureSensorName, chassisTemperatureSensorID}

chassisTemperatureStatusHealth :=chassisTemperatureStatus.Health
chassisTemperatureStatusHealth := chassisTemperatureStatus.Health
if chassisTemperatureStatusHealthValue, ok := parseCommonStatusHealth(chassisTemperatureStatusHealth); ok {
ch <- prometheus.MustNewConstMetric(chassisMetrics["chassis_temperature_sensor_health"].desc, prometheus.GaugeValue, chassisTemperatureStatusHealthValue, chassisTemperatureLabelvalues...)
}
Expand Down
2 changes: 1 addition & 1 deletion collector/manager_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func createManagerMetricMap() map[string]Metric {
}

// NewManagerCollector returns a collector that collecting memory statistics
func NewManagerCollector(redfishClient *gofish.APIClient, logger *log.Entry) *ManagerCollector {
func NewManagerCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *ManagerCollector {
return &ManagerCollector{
redfishClient: redfishClient,
metrics: managerMetrics,
Expand Down
8 changes: 4 additions & 4 deletions collector/redfish_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ type RedfishCollector struct {
}

// NewRedfishCollector return RedfishCollector
func NewRedfishCollector(host string, username string, password string, logger *log.Entry) *RedfishCollector {
func NewRedfishCollector(host string, username string, password string, collectLogs bool, logger *log.Entry) *RedfishCollector {
var collectors map[string]prometheus.Collector
collectorLogCtx := logger
redfishClient, err := newRedfishClient(host, username, password)
if err != nil {
collectorLogCtx.WithError(err).Error("error creating redfish client")
} else {
chassisCollector := NewChassisCollector(redfishClient, collectorLogCtx)
systemCollector := NewSystemCollector(redfishClient, collectorLogCtx)
managerCollector := NewManagerCollector(redfishClient, collectorLogCtx)
chassisCollector := NewChassisCollector(redfishClient, collectLogs, collectorLogCtx)
systemCollector := NewSystemCollector(redfishClient, collectLogs, collectorLogCtx)
managerCollector := NewManagerCollector(redfishClient, collectLogs, collectorLogCtx)

collectors = map[string]prometheus.Collector{"chassis": chassisCollector, "system": systemCollector, "manager": managerCollector}
}
Expand Down
12 changes: 9 additions & 3 deletions collector/system_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
type SystemCollector struct {
redfishClient *gofish.APIClient
metrics map[string]Metric
collectLogs bool
prometheus.Collector
collectorScrapeStatus *prometheus.GaugeVec
Log *log.Entry
Expand Down Expand Up @@ -100,10 +101,11 @@ func createSystemMetricMap() map[string]Metric {
}

// NewSystemCollector returns a collector that collecting memory statistics
func NewSystemCollector(redfishClient *gofish.APIClient, logger *log.Entry) *SystemCollector {
func NewSystemCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *SystemCollector {
return &SystemCollector{
redfishClient: redfishClient,
metrics: systemMetrics,
collectLogs: collectLogs,
Log: logger.WithFields(log.Fields{
"collector": "SystemCollector",
}),
Expand All @@ -129,6 +131,7 @@ func (s *SystemCollector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements prometheus.Collector.
func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) {
collectorLogContext := s.Log
collectLogs := s.collectLogs
//get service
service := s.redfishClient.Service

Expand Down Expand Up @@ -177,7 +180,7 @@ func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) {
if systemTotalMemoryHealthStateValue, ok := parseCommonStatusHealth(systemTotalMemoryHealthState); ok {
ch <- prometheus.MustNewConstMetric(s.metrics["system_total_memory_health_state"].desc, prometheus.GaugeValue, systemTotalMemoryHealthStateValue, systemLabelValues...)
}

// get system OdataID
//systemOdataID := system.ODataID

Expand Down Expand Up @@ -357,7 +360,10 @@ func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) {
}

// process log services
logServices, err := system.LogServices()
logServices := nil
if collectLogs {
logServices, err := system.LogServices()
}
if err != nil {
systemLogContext.WithField("operation", "system.LogServices()").WithError(err).Error("error getting log services from system")
} else if logServices == nil {
Expand Down
18 changes: 14 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

type Config struct {
Hosts map[string]HostConfig `yaml:"hosts"`
Groups map[string]HostConfig `yaml:"groups"`
Loglevel string `yaml:"loglevel"`
Hosts map[string]HostConfig `yaml:"hosts"`
Groups map[string]HostConfig `yaml:"groups"`
Loglevel string `yaml:"loglevel"`
Collectlogs *bool `yaml:"collectlogs,omitempty"`
}

type SafeConfig struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ func (sc *SafeConfig) HostConfigForGroup(group string) (*HostConfig, error) {
return &HostConfig{}, fmt.Errorf("no credentials found for group %s", group)
}

func (sc *SafeConfig) AppLogLevel() (string) {
func (sc *SafeConfig) AppLogLevel() string {
sc.Lock()
defer sc.Unlock()
logLevel := sc.C.Loglevel
Expand All @@ -80,3 +81,12 @@ func (sc *SafeConfig) AppLogLevel() (string) {
}
return "info"
}

func (sc *SafeConfig) CollectLogs() bool {
sc.Lock()
defer sc.Unlock()
if sc.C.Collectlogs == nil {
return true
}
return *sc.C.Collectlogs
}
1 change: 1 addition & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ groups:
password: group1_pass
# loglevel can be one of "debug", "info", "warn", "error", or "fatal"
# loglevel: info
collectlogs: true
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func metricsHandler() http.HandlerFunc {
}
}

collector := collector.NewRedfishCollector(target, hostConfig.Username, hostConfig.Password, targetLoggerCtx)
collector := collector.NewRedfishCollector(target, hostConfig.Username, hostConfig.Password, sc.CollectLogs(), targetLoggerCtx)
registry.MustRegister(collector)
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
Expand Down