当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
14 Star 24 Fork 12

pippo / redis-exporter
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
redis_exporter.go 2.85 KB
一键复制 编辑 原始数据 按行查看 历史
pippo 提交于 2018-06-30 23:08 . first blood
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"gitee.com/pippozq/redis-exporter/collector"
"gitee.com/pippozq/redis-exporter/collector/core"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"os"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":19100").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
redisFile = kingpin.Flag("redis.file", "Path under which to expose metrics.").Default(os.Getenv("REDIS_FILE")).String()
)
func init() {
prometheus.MustRegister(version.NewCollector("redis_exporter"))
}
func handler(w http.ResponseWriter, r *http.Request) {
filters := r.URL.Query()["collect[]"]
log.Debugln("collect query:", filters)
nc, err := collector.NewRedisCollector(filters...)
if err != nil {
log.Warnln("Couldn't create", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Couldn't create %s", err)))
return
}
registry := prometheus.NewRegistry()
err = registry.Register(nc)
if err != nil {
log.Errorln("Couldn't register collector:", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't register collector: %s", err)))
return
}
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.InstrumentMetricHandler(
registry,
promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError,
}),
)
h.ServeHTTP(w, r)
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("aliyun_db_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting redis_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
log.Infoln("Init Redis Pool", version.BuildContext())
core.InitRedisConfig(*redisFile)
log.Infoln("Init Redis Done", version.BuildContext())
nc, err := collector.NewRedisCollector()
if err != nil {
log.Fatalf("Couldn't create collector: %s", err)
}
log.Infof("Enabled collectors:")
for n := range nc.Collectors {
log.Infof(" - %s", n)
}
http.HandleFunc("/metrics", handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Node Exporter</title></head>
<body>
<h1>Node Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
err = http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}
Go
1
https://gitee.com/pippozq/redis-exporter.git
git@gitee.com:pippozq/redis-exporter.git
pippozq
redis-exporter
redis-exporter
master

搜索帮助