Skip to content

Commit

Permalink
Cleanup and fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
foxdalas committed Jul 28, 2019
1 parent 50c2882 commit f734543
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,15 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

db, err := sql.Open("mysql", e.sphinx)
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}
// will close DB connection while return
defer db.Close()

rows, err := db.Query("SHOW STATUS")
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}
variables := make(map[string]string)
Expand All @@ -439,7 +439,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
var counter string
err = rows.Scan(&metric, &counter)
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}
variables[metric] = counter
Expand Down Expand Up @@ -527,7 +527,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

indexes, err := db.Query("SHOW TABLES")
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}

Expand All @@ -536,7 +536,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
var index_type string
err = indexes.Scan(&index, &index_type)
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}
databases[index] = index_type
Expand All @@ -553,7 +553,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
}
metrics, err := db.Query("SHOW INDEX " + index + " STATUS")
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}

Expand All @@ -562,7 +562,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
var value string
err := metrics.Scan(&metric, &value)
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}
switch {
Expand All @@ -588,7 +588,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

threads_rows, err := db.Query("SHOW THREADS")
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}

Expand All @@ -602,7 +602,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
var info string
err := threads_rows.Scan(&tid, &proto, &state, &time, &info)
if err != nil {
printerror(e, err, ch)
log.Error(err)
return
}
threads[state] = append(threads[state], tid)
Expand Down Expand Up @@ -635,11 +635,6 @@ func parse(stat string) float64 {
return v
}

func printerror(e *Exporter, err error, ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0)
return
}

func main() {
var (
address = kingpin.Flag("sphinx.address", "Sphinx server address.").Default("127.0.0.1").String()
Expand Down

0 comments on commit f734543

Please sign in to comment.