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

Update Dependencies - Part 2 #103

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 18 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,32 @@ const (
)

type InfluxMetrics struct {
Host string `mapstructure:"host"`
Database string `mapstructure:"database"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Enabled bool `mapstructure:"enabled"`
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Database string `mapstructure:"database"`
Measurement string `mapstructure:"measurement"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
AlignTimestamps bool `mapstructure:"align_timestamps"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these two new values have to be added to func setConfigDefaults(v *viper.Viper) ?

}

func (influxMetricsConfig *InfluxMetrics) validateAndLog() {

// validate
if influxMetricsConfig.Host == "" {
log.Fatalf(`Despite being enabled, influx metrics came with no host info: config.metrics.influx.host = "".`)
}
if influxMetricsConfig.Database == "" {
log.Fatalf(`Despite being enabled, influx metrics came with no database info: config.metrics.influx.database = "".`)
}
if influxMetricsConfig.Measurement == "" {
log.Fatalf(`Despite being enabled, influx metrics came with no measurement info: config.metrics.influx.measurement = "".`)
}

// log
log.Infof("config.metrics.influx.host: %s", influxMetricsConfig.Host)
log.Infof("config.metrics.influx.database: %s", influxMetricsConfig.Database)
log.Infof("config.metrics.influx.measurement: %s", influxMetricsConfig.Measurement)
log.Infof("config.metrics.influx.align_timestamps: %v", influxMetricsConfig.AlignTimestamps)
}

type PrometheusMetrics struct {
Expand All @@ -281,7 +290,7 @@ type PrometheusMetrics struct {
}

func (promMetricsConfig *PrometheusMetrics) validateAndLog() {

// validate
if promMetricsConfig.Port == 0 {
log.Fatalf(`Despite being enabled, prometheus metrics came with an empty port number: config.metrics.prometheus.port = 0`)
}
Expand All @@ -291,6 +300,8 @@ func (promMetricsConfig *PrometheusMetrics) validateAndLog() {
if promMetricsConfig.Subsystem == "" {
log.Fatalf(`Despite being enabled, prometheus metrics came with an empty subsystem value: config.metrics.prometheus.subsystem = \"\".`)
}

// log
log.Infof("config.metrics.prometheus.namespace: %s", promMetricsConfig.Namespace)
log.Infof("config.metrics.prometheus.subsystem: %s", promMetricsConfig.Subsystem)
log.Infof("config.metrics.prometheus.port: %d", promMetricsConfig.Port)
Expand Down
147 changes: 81 additions & 66 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ func TestCheckMetricsEnabled(t *testing.T) {
msg: "config.metrics.influx.database: database-value",
lvl: logrus.InfoLevel,
},
{
msg: "config.metrics.influx.measurement: measurement-value",
lvl: logrus.InfoLevel,
},
{
msg: "config.metrics.influx.align_timestamps: false",
lvl: logrus.InfoLevel,
},
}

// test cases
Expand Down Expand Up @@ -284,8 +292,9 @@ func TestCheckMetricsEnabled(t *testing.T) {
//Standard elements of the config.Metrics object are set so test cases only modify what's relevant to them
cfg := &Metrics{
Influx: InfluxMetrics{
Host: "http://fakeurl.com",
Database: "database-value",
Host: "http://fakeurl.com",
Database: "database-value",
Measurement: "measurement-value",
},
Prometheus: PrometheusMetrics{
Port: 8080,
Expand Down Expand Up @@ -489,95 +498,101 @@ func TestInfluxValidateAndLog(t *testing.T) {
}
testCases := []aTest{
{
description: "[0] both InfluxDB host and database blank, expect error",
description: "All Required Fields Missing",
influxConfig: &InfluxMetrics{
Host: "",
Database: "",
Host: "",
Database: "",
Measurement: "",
},
//out
expectError: true,
expectedLogInfo: []logComponents{
{
msg: `Despite being enabled, influx metrics came with no host info: config.metrics.influx.host = "".`,
lvl: logrus.FatalLevel,
},
{
msg: `Despite being enabled, influx metrics came with no database info: config.metrics.influx.database = "".`,
lvl: logrus.FatalLevel,
},
{
msg: "config.metrics.influx.host: ",
lvl: logrus.InfoLevel,
},
{
msg: "config.metrics.influx.database: ",
lvl: logrus.InfoLevel,
},
{lvl: logrus.FatalLevel, msg: `Despite being enabled, influx metrics came with no host info: config.metrics.influx.host = "".`},
{lvl: logrus.FatalLevel, msg: `Despite being enabled, influx metrics came with no database info: config.metrics.influx.database = "".`},
{lvl: logrus.FatalLevel, msg: `Despite being enabled, influx metrics came with no measurement info: config.metrics.influx.measurement = "".`},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.host: "},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.database: "},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.measurement: "},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.align_timestamps: false"},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restructured the code to put each log expectation on its own line, which made it easier for me to read and understand.

},
},
{
description: "[1] InfluxDB host blank, expect error",
description: "Host Missing",
influxConfig: &InfluxMetrics{
Host: "",
Database: "database-value",
Host: "",
Database: "database-value",
Measurement: "measurement-value",
},
//out
expectError: true,
expectedLogInfo: []logComponents{
{
msg: `Despite being enabled, influx metrics came with no host info: config.metrics.influx.host = "".`,
lvl: logrus.FatalLevel,
},
{
msg: "config.metrics.influx.host: ",
lvl: logrus.InfoLevel,
},
{
msg: "config.metrics.influx.database: database-value",
lvl: logrus.InfoLevel,
},
{lvl: logrus.FatalLevel, msg: `Despite being enabled, influx metrics came with no host info: config.metrics.influx.host = "".`},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.host: "},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.database: database-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.measurement: measurement-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.align_timestamps: false"},
},
},
{
description: "[2] InfluxDB database blank, expect error",
description: "Database Missing",
influxConfig: &InfluxMetrics{
Host: "http://fakeurl.com",
Database: "",
Host: "http://fakeurl.com",
Database: "",
Measurement: "measurement-value",
},
//out
expectError: true,
expectedLogInfo: []logComponents{
{
msg: `Despite being enabled, influx metrics came with no database info: config.metrics.influx.database = "".`,
lvl: logrus.FatalLevel,
},
{
msg: "config.metrics.influx.host: http://fakeurl.com",
lvl: logrus.InfoLevel,
},
{
msg: "config.metrics.influx.database: ",
lvl: logrus.InfoLevel,
},
{lvl: logrus.FatalLevel, msg: `Despite being enabled, influx metrics came with no database info: config.metrics.influx.database = "".`},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.host: http://fakeurl.com"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.database: "},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.measurement: measurement-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.align_timestamps: false"},
},
},
{
description: "[3] Valid InfluxDB host and database, expect log.Info",
description: "Measurement Missing",
influxConfig: &InfluxMetrics{
Host: "http://fakeurl.com",
Database: "database-value",
Host: "http://fakeurl.com",
Database: "database-value",
Measurement: "",
},
expectError: true,
expectedLogInfo: []logComponents{
{lvl: logrus.FatalLevel, msg: `Despite being enabled, influx metrics came with no measurement info: config.metrics.influx.measurement = "".`},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.host: http://fakeurl.com"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.database: database-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.measurement: "},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.align_timestamps: false"},
},
},
{
description: "All Required Fields Provided",
influxConfig: &InfluxMetrics{
Host: "http://fakeurl.com",
Database: "database-value",
Measurement: "measurement-value",
AlignTimestamps: true,
},
//out
expectError: false,
expectedLogInfo: []logComponents{
{
msg: "config.metrics.influx.host: http://fakeurl.com",
lvl: logrus.InfoLevel,
},
{
msg: "config.metrics.influx.database: database-value",
lvl: logrus.InfoLevel,
},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.host: http://fakeurl.com"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.database: database-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.measurement: measurement-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.align_timestamps: true"},
},
},
{
description: "Align Timestamps",
influxConfig: &InfluxMetrics{
Host: "http://fakeurl.com",
Database: "database-value",
Measurement: "measurement-value",
AlignTimestamps: true,
},
expectError: false,
expectedLogInfo: []logComponents{
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.host: http://fakeurl.com"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.database: database-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.measurement: measurement-value"},
{lvl: logrus.InfoLevel, msg: "config.metrics.influx.align_timestamps: true"},
},
},
}
Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ require (
github.com/golang/snappy v0.0.4
github.com/google/gomemcache v0.0.0-20210709172713-c1c93e4523ee
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
github.com/influxdata/influxdb v1.6.0 // indirect
github.com/julienschmidt/httprouter v1.3.0
github.com/magiconair/properties v1.8.0 // indirect
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.18.1 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model v0.2.0
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/rs/cors v1.8.2
github.com/sirupsen/logrus v1.4.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.1.1 // indirect
github.com/spf13/cast v1.2.0 // indirect
github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/spf13/viper v1.0.2
github.com/stretchr/testify v1.7.0
github.com/vrischmann/go-metrics-influxdb v0.0.0-20160917065939-43af8332c303
github.com/stretchr/testify v1.7.1
github.com/vrischmann/go-metrics-influxdb v0.1.1
github.com/yuin/gopher-lua v0.0.0-20180630135845-46796da1b0b4 // indirect
)
Loading