Skip to content

Commit

Permalink
Merge pull request #449 from zs1379/prometheus
Browse files Browse the repository at this point in the history
support Prometheus
  • Loading branch information
flike authored Sep 4, 2018
2 parents 94a86c5 + ac9a36c commit f3cf302
Show file tree
Hide file tree
Showing 117 changed files with 30,994 additions and 4 deletions.
11 changes: 11 additions & 0 deletions cmd/kingshard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/flike/kingshard/core/hack"
"github.com/flike/kingshard/proxy/server"
"github.com/flike/kingshard/web"
"github.com/flike/kingshard/monitor"
)

var configFile *string = flag.String("config", "/etc/ks.yaml", "kingshard config file")
Expand Down Expand Up @@ -97,6 +98,7 @@ func main() {

var svr *server.Server
var apiSvr *web.ApiServer
var prometheusSvr *monitor.Prometheus
svr, err = server.NewServer(cfg)
if err != nil {
golog.Error("main", "main", err.Error(), 0)
Expand All @@ -112,6 +114,14 @@ func main() {
svr.Close()
return
}
prometheusSvr, err = monitor.NewPrometheus(cfg.PrometheusAddr, svr)
if err != nil {
golog.Error("main", "main", err.Error(), 0)
golog.GlobalSysLogger.Close()
golog.GlobalSqlLogger.Close()
svr.Close()
return
}

sc := make(chan os.Signal, 1)
signal.Notify(sc,
Expand Down Expand Up @@ -144,6 +154,7 @@ func main() {
}
}()
go apiSvr.Run()
go prometheusSvr.Run()
svr.Run()
}

Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var configFileName string

//整个config文件对应的结构
type Config struct {
Addr string `yaml:"addr"`
UserList []UserConfig `yaml:"user_list"`
Addr string `yaml:"addr"`
PrometheusAddr string `yaml:"prometheus_addr"`
UserList []UserConfig `yaml:"user_list"`

WebAddr string `yaml:"web_addr"`
WebUser string `yaml:"web_user"`
Expand Down
4 changes: 2 additions & 2 deletions core/hack/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package hack

const (
Version = "2018-05-13 10:15:54 +0800 @7d96bb8"
Compile = "2018-05-13 10:16:37 +0800 by go version go1.10.2 darwin/amd64"
Version = "2018-06-11 17:46:38 +0800 @d861c77"
Compile = "2018-09-04 19:53:20 +0800 by go version go1.9 darwin/amd64"
)
3 changes: 3 additions & 0 deletions etc/ks.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# server listen addr
addr : 0.0.0.0:9696

# prometheus server listen addr
prometheus_addr : 0.0.0.0:7080

# server user and password
user_list:
-
Expand Down
3 changes: 3 additions & 0 deletions etc/unshard.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# server listen addr
addr : 0.0.0.0:9696

# prometheus server listen addr
prometheus_addr : 0.0.0.0:7080

# server user and password
user_list:
-
Expand Down
102 changes: 102 additions & 0 deletions monitor/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2016 The kingshard Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package monitor

import (
"net/http"
"time"
"strconv"

"github.com/flike/kingshard/proxy/server"
"github.com/flike/kingshard/core/golog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

type Prometheus struct {
addr string
svr *server.Server
}

//新建prometheus实例
func NewPrometheus(addr string, svr *server.Server) (*Prometheus, error) {
prometheus := new(Prometheus)
prometheus.addr = addr
prometheus.svr = svr

golog.Info("prometheus", "Run", "Prometheus running", 0,
"address",
addr)

return prometheus, nil
}

//启动prometheus的http监控
func (p *Prometheus) Run() {
data := p.svr.GetMonitorData()

for addr, data := range data {
label := make(map[string]string)

label["addr"] = addr
label["type"] = data["type"]

idleConn := data["idleConn"]
maxConn := data["maxConn"]

idleConnGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "idleConn",
Help: "the db idle connection",
ConstLabels: label,
},
)
prometheus.MustRegister(idleConnGauge)

maxConnGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "maxConn",
Help: "the max connection config",
ConstLabels: label,
},
)
prometheus.MustRegister(maxConnGauge)

go func() {
for {
idleConnValue, _ := strconv.ParseFloat(idleConn, 10)
idleConnGauge.Set(idleConnValue)
time.Sleep(5 * time.Second)
}
}()

go func() {
for {
maxConnValue, _ := strconv.ParseFloat(maxConn, 10)
maxConnGauge.Set(maxConnValue)
time.Sleep(5 * time.Second)
}
}()
}

mux := http.NewServeMux()

mux.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(p.addr, mux)

if err != nil {
golog.Error("prometheus", "Run", err.Error(), 0)
}
}
27 changes: 27 additions & 0 deletions proxy/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,30 @@ func (s *Server) UpdateConfig(newCfg *config.Config) {
//version update
s.configVer += 1
}

func (s *Server) GetMonitorData() map[string]map[string]string{
data := make(map[string]map[string]string)

// get all node's monitor data
for _, node := range s.nodes {
//get master monitor data
dbData := make(map[string]string)
dbData["idleConn"] = fmt.Sprintf("%d", node.Master.IdleConnCount())
dbData["maxConn"] = fmt.Sprintf("%d", node.Cfg.MaxConnNum)
dbData["type"] = "master"

data[node.Master.Addr()] = dbData

//get all slave monitor data
for _, slaveNode := range node.Slave {
slaveDbData := make(map[string]string)
slaveDbData["idleConn"] = fmt.Sprintf("%d", slaveNode.IdleConnCount())
slaveDbData["maxConn"] = fmt.Sprintf("%d", node.Cfg.MaxConnNum)
slaveDbData["type"] = "slave"

data[slaveNode.Addr()] = slaveDbData
}
}

return data
}
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f3cf302

Please sign in to comment.