Skip to content

Commit e610600

Browse files
committed
statsd监控api调用
1 parent d38ba32 commit e610600

31 files changed

+2757
-1
lines changed

Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.example.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
"mongodb": { /*mongodb连接地址, 可以不填,不填的话,就没有数据统计功能*/
2121
"URL" : ""
2222
},
23+
"statsd": {
24+
"URL": "", /*statsd服务连接地址,如127.0.0.1:8125*/
25+
"Prefix": "" /*statsd客户端前缀,可留空*/
26+
},
2327
"go": {
2428
"APIPoweredBy" : "Golang123 API",/*后台go加的X-Powered-By*/
2529
"SiteName" : "Golang中文社区", /*网站名称*/

config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,23 @@ func initServer() {
147147
ServerConfig.LogFile = ServerConfig.LogDir + ymdStr + ".log"
148148
}
149149

150+
type statsdConfig struct {
151+
URL string
152+
Prefix string
153+
}
154+
155+
// StatsDConfig statsd相关配置
156+
var StatsDConfig statsdConfig
157+
158+
func initStatsd() {
159+
utils.SetStructByJSON(&StatsDConfig, jsonData["statsd"].(map[string]interface{}))
160+
}
161+
150162
func init() {
151163
initJSON()
152164
initDB()
153165
initRedis()
154166
initMongo()
155167
initServer()
168+
initStatsd()
156169
}

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
_ "github.com/jinzhu/gorm/dialects/mysql"
1010
"github.com/shen100/golang123/config"
1111
"github.com/shen100/golang123/cron"
12+
"github.com/shen100/golang123/middleware"
1213
"github.com/shen100/golang123/model"
1314
"github.com/shen100/golang123/router"
1415
)
@@ -43,6 +44,8 @@ func main() {
4344
// Recovery middleware recovers from any panics and writes a 500 if there was one.
4445
app.Use(gin.Recovery())
4546

47+
app.Use(middleware.APIStatsD())
48+
4649
router.Route(app)
4750

4851
if config.ServerConfig.StatsEnabled {

middleware/apistatsd.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package middleware
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"regexp"
7+
"strings"
8+
"time"
9+
10+
"github.com/gin-gonic/gin"
11+
"github.com/shen100/golang123/config"
12+
"github.com/shen100/golang123/model"
13+
)
14+
15+
func getReqPath(c *gin.Context) string {
16+
pathArr := strings.Split(c.Request.URL.Path, "/")
17+
for i := len(pathArr) - 1; i >= 0; i-- {
18+
if pathArr[i] == "" {
19+
pathArr = append(pathArr[:i], pathArr[i+1:]...)
20+
}
21+
}
22+
for i, path := range pathArr {
23+
if matched, err := regexp.MatchString("^[0-9]+$", path); matched && err == nil {
24+
pathArr[i] = "id"
25+
}
26+
}
27+
pathArr = append([]string{strings.ToLower(c.Request.Method)}, pathArr...)
28+
return strings.Join(pathArr, "_")
29+
}
30+
31+
// APIStatsD 统计api请求
32+
func APIStatsD() gin.HandlerFunc {
33+
return func(c *gin.Context) {
34+
t := time.Now()
35+
c.Next()
36+
37+
if config.StatsDConfig.URL == "" {
38+
return
39+
}
40+
41+
duration := time.Since(t)
42+
durationMS := int64(duration / 1e6) // 转成毫秒
43+
44+
reqPath := getReqPath(c)
45+
if err := (*model.StatsdClient).Timing(reqPath, durationMS, 1); err != nil {
46+
fmt.Println(err)
47+
}
48+
49+
status := c.Writer.Status()
50+
if status != http.StatusGatewayTimeout && durationMS > 5000 {
51+
timeoutReqPath := strings.Join([]string{"timeout", reqPath}, ":")
52+
if err := (*model.StatsdClient).Inc(timeoutReqPath, 1, 1); err != nil {
53+
fmt.Println(err)
54+
}
55+
}
56+
}
57+
}

model/statsd.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package model
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/cactus/go-statsd-client/statsd"
8+
"github.com/shen100/golang123/config"
9+
)
10+
11+
// StatsdClient statsd 客户端
12+
var StatsdClient *statsd.Statter
13+
14+
func init() {
15+
if config.StatsDConfig.URL == "" {
16+
return
17+
}
18+
client, err := statsd.NewBufferedClient(config.StatsDConfig.URL, config.StatsDConfig.Prefix, 300*time.Millisecond, 512)
19+
if err != nil {
20+
fmt.Println(err.Error())
21+
}
22+
StatsdClient = &client
23+
}

vendor/github.com/cactus/go-statsd-client/.travis.yml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cactus/go-statsd-client/CHANGELOG.md

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cactus/go-statsd-client/LICENSE.md

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cactus/go-statsd-client/README.md

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)