-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from zs1379/prometheus
support Prometheus
- Loading branch information
Showing
117 changed files
with
30,994 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.