You've already forked bind_exporter
mirror of
https://github.com/prometheus-community/bind_exporter.git
synced 2025-07-31 21:24:22 +03:00
Use prometheus log library
This introduces two new flags -log.level and -log.format. The latter allows the configuration of syslog logging.
This commit is contained in:
@ -3,19 +3,15 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
|
||||||
logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"log/syslog"
|
"github.com/prometheus/common/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -28,12 +24,8 @@ type VecInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gaugeMetrics = map[string]string{
|
gaugeMetrics = map[string]string{}
|
||||||
// "example": "example help text",
|
counterMetrics = map[string]string{}
|
||||||
}
|
|
||||||
counterMetrics = map[string]string{
|
|
||||||
// "example": "example help text",
|
|
||||||
}
|
|
||||||
counterVecMetrics = map[string]*VecInfo{
|
counterVecMetrics = map[string]*VecInfo{
|
||||||
"incoming_requests": {
|
"incoming_requests": {
|
||||||
help: "number of inbound requests made",
|
help: "number of inbound requests made",
|
||||||
@ -45,14 +37,7 @@ var (
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
gaugeVecMetrics = map[string]*VecInfo{
|
gaugeVecMetrics = map[string]*VecInfo{}
|
||||||
/*
|
|
||||||
"example_metric": &VecInfo{
|
|
||||||
help: "help_text",
|
|
||||||
labels: []string{"extra label"},
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Exporter collects Binds stats from the given server and exports
|
// Exporter collects Binds stats from the given server and exports
|
||||||
@ -182,14 +167,14 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|||||||
resp, err := e.client.Get(e.URI)
|
resp, err := e.client.Get(e.URI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.up.Set(0)
|
e.up.Set(0)
|
||||||
log.Println("Error while querying Bind:", err)
|
log.Error("Error while querying Bind:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to read Bind xml response body:", err)
|
log.Error("Failed to read Bind xml response body:", err)
|
||||||
e.up.Set(0)
|
e.up.Set(0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -198,7 +183,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|||||||
|
|
||||||
err = xml.Unmarshal([]byte(body), &root)
|
err = xml.Unmarshal([]byte(body), &root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error: %v", err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,26 +228,6 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initLogging() {
|
|
||||||
// Log as JSON instead of the default ASCII formatter.
|
|
||||||
log.SetFormatter(&log.JSONFormatter{})
|
|
||||||
|
|
||||||
// Output to stderr instead of stdout, could also be a file.
|
|
||||||
log.SetOutput(os.Stdout)
|
|
||||||
|
|
||||||
// Only log the warning severity or above.
|
|
||||||
//log.SetLevel(log.InfoLevel)
|
|
||||||
|
|
||||||
//Also log to syslog
|
|
||||||
hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Unable to connect to local syslog daemon")
|
|
||||||
} else {
|
|
||||||
log.AddHook(hook)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
listenAddress = flag.String("web.listen-address", ":9109", "Address to listen on for web interface and telemetry.")
|
listenAddress = flag.String("web.listen-address", ":9109", "Address to listen on for web interface and telemetry.")
|
||||||
@ -272,12 +237,10 @@ func main() {
|
|||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
initLogging()
|
|
||||||
|
|
||||||
exporter := NewExporter(*bindURI, *bindTimeout)
|
exporter := NewExporter(*bindURI, *bindTimeout)
|
||||||
prometheus.MustRegister(exporter)
|
prometheus.MustRegister(exporter)
|
||||||
|
|
||||||
log.Println("Starting Server:", *listenAddress)
|
log.Info("Starting Server:", *listenAddress)
|
||||||
http.Handle(*metricsPath, prometheus.Handler())
|
http.Handle(*metricsPath, prometheus.Handler())
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte(`<html>
|
w.Write([]byte(`<html>
|
||||||
|
Reference in New Issue
Block a user