Skip to content

Commit

Permalink
app pprof server 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyu mao committed Feb 20, 2024
1 parent dda472c commit 158cb31
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 41 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ vet:
# go vet -vettool=`which shadow` ./...
go vet ./...

## bench 并发测试
## bench: 并发测试
.PHONY: bench
bench:
go test ./... -test.bench . -test.benchmem=true
Expand All @@ -47,10 +47,16 @@ bench:
fmt:
gofmt -w -l .


## lint: golangci-lint
.PHONY: lint
lint:
golangci-lint cache clean
golangci-lint run


## check: fmt lint vet
.PHONY: check
check: fmt lint vet

## help: Show this help info.
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ go get github.com/cr-mao/lori@v0.0.3
```


### 3.组件选用
- 服务注册发现 consul
### 3.组件
- 服务注册发现
- consul
- 其他可自行实现接口进行扩充
- 指标监控 prometheus
- 日志 zap

- 内置请求耗时Histogram中间件
- 其他指标收集,可自行实现接口扩充
- 日志
- zap

### 4. 如何使用
见example目录
Expand All @@ -30,5 +34,3 @@ go get github.com/cr-mao/lori@v0.0.3
- [due](https://github.com/dobyte/due)
- [kratos](https://github.com/go-kratos/kratos)
- [iam极客时间go语言项目实战](https://github.com/marmotedu/iam)


34 changes: 20 additions & 14 deletions docker/docker-cmpose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
version: "3"

services:
consul:
image: bitnami/consul:latest
container_name: dev-consul
platform: linux/x86_64
restart: always
ports:
- '8300:8300'
- '8301:8301'
- '8301:8301/udp'
- '8500:8500'
- '8600:8600'
- '8600:8600/udp'
volumes:
- ./consul:/bitnami
networks:
- middleware
# mysql:
# image: mysql:5.7
# container_name: dev-mysql
Expand Down Expand Up @@ -39,20 +55,6 @@ services:
# - '2380:2380'
# networks:
# - middleware
consul:
image: bitnami/consul:latest
container_name: dev-consul
platform: linux/x86_64
restart: always
ports:
- '8300:8300'
- '8301:8301'
- '8301:8301/udp'
- '8500:8500'
- '8600:8600'
- '8600:8600/udp'
volumes:
- ./consul:/bitnami
# redis-cluster:
# image: grokzen/redis-cluster:latest
# container_name: dev-redis-cluster
Expand All @@ -63,6 +65,8 @@ services:
# - SENTINEL=true
# ports:
# - '7000-7005:7000-7005'
# networks:
# - middleware
# nats:
# image: nats:latest
# container_name: dev-nats
Expand All @@ -72,6 +76,8 @@ services:
# - '4222:4222'
# - '6222:6222'
# - '8222:8222'
# networks:
# - middleware
networks:
middleware:
driver: bridge
11 changes: 9 additions & 2 deletions example/app_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
"context"
"github.com/cr-mao/lori"
"github.com/cr-mao/lori/example/proto"
"github.com/cr-mao/lori/internal/netlib"
"github.com/cr-mao/lori/log"
"github.com/cr-mao/lori/pprof_server"
"github.com/cr-mao/lori/registry/consul"
"github.com/hashicorp/consul/api"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -44,9 +47,11 @@ func TestAppServer(t *testing.T) {
}
r := consul.New(cli, consul.WithHealthCheck(true))
grpcServer := grpc.NewServer(grpc.WithAddress("0.0.0.0:8081"))
pprofPort, _ := netlib.AssignRandPort()
pprofServer := pprof_server.NewPProf("0.0.0.0:" + strconv.Itoa(pprofPort))
registerServer(grpcServer)
app := lori.New(lori.WithName("lori-app"),
lori.WithServer(grpcServer),
lori.WithServer(grpcServer, pprofServer),
lori.WithRegistrar(r),
lori.WithRegistrarTimeout(time.Second*5),
)
Expand All @@ -66,9 +71,11 @@ func TestAppServer2(t *testing.T) {
}
r := consul.New(cli, consul.WithHealthCheck(true))
grpcServer := grpc.NewServer(grpc.WithAddress("0.0.0.0:8082"))
pprofPort, _ := netlib.AssignRandPort()
pprofServer := pprof_server.NewPProf("0.0.0.0:" + strconv.Itoa(pprofPort))
registerServer(grpcServer)
app := lori.New(lori.WithName("lori-app"),
lori.WithServer(grpcServer),
lori.WithServer(grpcServer, pprofServer),
lori.WithRegistrar(r),
lori.WithRegistrarTimeout(time.Second*5),
)
Expand Down
48 changes: 48 additions & 0 deletions pprof_server/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
User: cr-mao
Date: 2024/2/20 11:55
Email: crmao@qq.com
Desc: pprof.go
*/
package pprof_server

import (
"context"
"fmt"

"net/http"
_ "net/http/pprof"

"github.com/cr-mao/lori/log"
"github.com/cr-mao/lori/transport"
)

var _ transport.Server = &pprof{}

type pprof struct {
addr string
}

func NewPProf(addr string) *pprof {
return &pprof{
addr: addr,
}
}

func (p *pprof) Name() string {
return "pprof"
}

func (p *pprof) Start(_ context.Context) error {
log.Debug("pprof addr:", p.addr)
fmt.Println("pprof addr:", p.addr)
err := http.ListenAndServe(p.addr, nil)
if err != nil {
log.Errorf("pprof server start failed: %v", err)
}
return err
}

func (p *pprof) Stop(ctx context.Context) error {
return nil
}
11 changes: 6 additions & 5 deletions transport/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func TLSConfig(c *tls.Config) ServerOption {
o.tlsConf = c
}
}
func WithEnableProfiling(profiling bool) ServerOption {
return func(s *Server) {
s.enableProfiling = profiling
}
}

//func WithEnableProfiling(profiling bool) ServerOption {
// return func(s *Server) {
// s.enableProfiling = profiling
// }
//}

func WithMode(mode string) ServerOption {
return func(s *Server) {
Expand Down
26 changes: 13 additions & 13 deletions transport/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/cr-mao/lori/metric"
"github.com/cr-mao/lori/transport"
mids "github.com/cr-mao/lori/transport/http/middlewares"
"github.com/cr-mao/lori/transport/http/pprof"
)

var _ transport.Endpointer = (*Server)(nil)
Expand All @@ -37,9 +36,10 @@ type Server struct {
mode string

//是否开启pprof接口, 默认开启, 如果开启会自动添加 /debug/pprof 接口
enableProfiling bool
// 整个app 自动进行pprof,推荐这么用
//enableProfiling bool

//是否开启metrics接口, 默认开启, 如果开启会自动添加 /metrics 接口, prometheus , todo 优化成一个接口。
//是否开启metrics接口, 默认开启, 如果开启会自动添加 /metrics 接口, prometheus
metric metric.GinMetric

//中间件
Expand All @@ -55,13 +55,13 @@ type Server struct {

func NewServer(opts ...ServerOption) *Server {
srv := &Server{
network: "tcp",
address: ":0",
mode: "debug",
enableProfiling: true,
Engine: gin.New(), //纯的,没有logger,和default 。
serviceName: "lori-gin-http",
timeout: time.Second * 5, //默认5秒
network: "tcp",
address: ":0",
mode: "debug",
//enableProfiling: true,
Engine: gin.New(), //纯的,没有logger,和default 。
serviceName: "lori-gin-http",
timeout: time.Second * 5, //默认5秒
}
for _, o := range opts {
o(srv)
Expand Down Expand Up @@ -128,9 +128,9 @@ func (s *Server) Endpoint() (*url.URL, error) {
// start rest server
func (s *Server) Start(ctx context.Context) error {
//根据配置初始化pprof路由
if s.enableProfiling {
pprof.Register(s.Engine)
}
//if s.enableProfiling {
// pprof.Register(s.Engine)
//}
if s.metric != nil {
s.metric.Use(s.Engine)
}
Expand Down

0 comments on commit 158cb31

Please sign in to comment.