Skip to content

Commit

Permalink
metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomeng79 committed Nov 13, 2018
1 parent 6ada777 commit f6f4c01
Show file tree
Hide file tree
Showing 22 changed files with 2,416 additions and 57 deletions.
36 changes: 34 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

# Mac
.DS_Store

# IDEA
.idea

# Glide
vendor

#bench gen
*/*.test
*/*.profile
*/*.out
*/intarray.txt
*/*.svg
.idea
coverage.txt
tmp
bin
Expand Down
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ fmt :
@echo "格式化代码"
@gofmt -l -w ./


.PHONY : vendor
vendor :
@echo "创建vendor"
@go mod vendor
@echo "结束vendor"

.PHONY : test
test :
@echo "检查代码"
@go vet ./...
@echo "测试代码"
@go test -race -coverprofile=coverage.txt -covermode=atomic ./...


@go test -mod=vendor -race -coverprofile=coverage.txt -covermode=atomic ./...


.PHONY : build
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
|配置|默认值->yaml->env|
|日志|可选插件(zap logors),集成了链路跟踪[go-log](https://github.com/xiaomeng79/go-log)|
|链路跟踪|OpenTracing [Jaeger](https://github.com/jaegertracing/jaeger)|
|监控||
|监控|[go-metrics](https://github.com/rcrowley/go-metrics)|
|打包|[bindata](https://github.com/jteeuwen/go-bindata)|
|编码|[protoc-gen-micro](https://github.com/micro/protoc-gen-micro)|
|部署|docker docker-compose k8s|
Expand Down Expand Up @@ -57,6 +57,7 @@

## 编译
```go
make vendor
make allbuild
```
## 本地docker-compose运行
Expand All @@ -74,4 +75,6 @@ curl -X POST http://127.0.0.1:8888/common/v1/login -H 'Cache-Controlapplicat

## 查看效果

1. 链路跟踪:http://127.0.0.1:16686 [本地效果](http://127.0.0.1:16686)
1. 链路跟踪:http://127.0.0.1:16686 [本地效果](http://127.0.0.1:16686)

1. 监控:http://127.0.0.1:3000 [本地效果](http://127.0.0.1:3000) 用户名:admin 密码:admin
32 changes: 31 additions & 1 deletion api/common/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/labstack/echo/middleware"
"github.com/micro/go-web"
"github.com/xiaomeng79/go-example/cinit"
"github.com/xiaomeng79/go-example/internal/metrics"
"github.com/xiaomeng79/go-log"
"net/http"
"time"
Expand All @@ -27,7 +28,6 @@ func Run() {
web.RegisterTTL(time.Second*30),
web.RegisterInterval(time.Second*15),
)

log.Info("创建服务:名称:" + serviceName + ",版本:" + serviceVersion)
// 定义Service动作操作
service.Init()
Expand All @@ -53,6 +53,36 @@ func Run() {
}))
e.Use(Opentracing)

//metrics
// Metrics
if cinit.Config.Metrics.Enable == "yes" {

/* Pull模式
e.Use(prometheus.MetricsFunc(
prometheus.Namespace("common_api"),
))
*/

// Push模式
m := metrics.NewMetrics()
e.Use(MetricsFunc(m))
m.MemStats()
// InfluxDB
m.InfluxDBWithTags(
time.Duration(cinit.Config.Metrics.Duration)*time.Second,
cinit.Config.Metrics.Url,
cinit.Config.Metrics.Database,
cinit.Config.Metrics.UserName,
cinit.Config.Metrics.Password,
map[string]string{"service": serviceName},
)

// Graphite
//addr, _ := net.ResolveTCPAddr("tcp", Conf.Metrics.Address)
//m.Graphite(Conf.Metrics.FreqSec*time.Second, "echo-web.node."+hostname, addr)

}

//加验证JWT路由组,版本v1
g1 := e.Group("/common/v1", JWT)
g1.POST("/userinfo", userinfo)
Expand Down
39 changes: 39 additions & 0 deletions api/common/middle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package common
import (
"context"
"github.com/labstack/echo"
"github.com/rcrowley/go-metrics"
"github.com/xiaomeng79/go-example/cinit"
"github.com/xiaomeng79/go-example/internal/jwt"
metrics2 "github.com/xiaomeng79/go-example/internal/metrics"
"github.com/xiaomeng79/go-example/internal/trace"
"github.com/xiaomeng79/go-log"
"strconv"
"strings"
"time"
)

//opentracing中间件
Expand Down Expand Up @@ -83,3 +87,38 @@ func VerifyParam(next echo.HandlerFunc) echo.HandlerFunc {
return next(c)
}
}

//metrics
func MetricsFunc(m *metrics2.Metrics) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
res := c.Response()
start := time.Now()
if err := next(c); err != nil {
c.Error(err)
}
stop := time.Now()

latency := stop.Sub(start)
status := res.Status
//Total request count.
meter := metrics.GetOrRegisterMeter("status."+strconv.Itoa(status), m.GetRegistry())
meter.Mark(1)

//Request size in bytes.
//meter = metrics.GetOrRegisterMeter(m.WithPrefix("status."+strconv.Itoa(status)), m.GetRegistry())
//meter.Mark(req.ContentLength)

//Request duration in nanoseconds.
h := metrics.GetOrRegisterHistogram("h_status."+strconv.Itoa(status), m.GetRegistry(),
metrics.NewExpDecaySample(1028, 0.015))
h.Update(latency.Nanoseconds())

//Response size in bytes.
//meter = metrics.GetOrRegisterMeter(m.WithPrefix("status."+strconv.Itoa(status)), m.GetRegistry())
//meter.Mark(res.Size)

return nil
}
}
}
2 changes: 1 addition & 1 deletion api/common/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func login(c echo.Context) error {
return RpcErr(c, err)
}
//通过验证,设置JWT
s, err := jwt.Encode(jwt.JWTMsg{UserId:_rsp.Id, UserName:_rsp.Username})
s, err := jwt.Encode(jwt.JWTMsg{UserId: _rsp.Id, UserName: _rsp.Username})
if err != nil {
log.Error(err.Error(), ctx)
return HandleError(c, ServiceError, err.Error())
Expand Down
9 changes: 9 additions & 0 deletions cinit/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ var Config = struct {
Password string `default:"root"`
PoolLimit int `default:"4096"` //连接池限制
}
//metrics config
Metrics struct {
Enable string `default:"yes"` //是否启用:yes 启用 no 停用
Duration int `default:"5"` //单位秒
Url string `default:"http://127.0.0.1:8086"`
Database string `default:"test"`
UserName string `default:""`
Password string `default:""`
}
}{}

//初始化配置文件
Expand Down
2 changes: 1 addition & 1 deletion data/bindata.go

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

Loading

0 comments on commit f6f4c01

Please sign in to comment.