Add GRPC and gin server monitoring logic

pull/1337/head
lin.huang 2 years ago
parent 477e1f2b6d
commit 4001fb17dd

@ -17,6 +17,8 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
ginProm "github.com/openimsdk/open-im-server/v3/pkg/common/ginPrometheus"
prom_metrics "github.com/openimsdk/open-im-server/v3/pkg/common/prom_metrics"
"net" "net"
_ "net/http/pprof" _ "net/http/pprof"
"strconv" "strconv"
@ -81,6 +83,11 @@ func run(port int) error {
} }
log.ZInfo(context.Background(), "api register public config to discov success") log.ZInfo(context.Background(), "api register public config to discov success")
router := api.NewGinRouter(client, rdb) router := api.NewGinRouter(client, rdb)
//////////////////////////////
p := ginProm.NewPrometheus("app", prom_metrics.G_api_metrics.MetricList())
p.SetListenAddress(":90")
p.Use(router)
/////////////////////////////////
log.ZInfo(context.Background(), "api init router success") log.ZInfo(context.Background(), "api init router success")
var address string var address string
if config.Config.Api.ListenIP != "" { if config.Config.Api.ListenIP != "" {

@ -26,3 +26,17 @@ func NewGrpcPromObj(cusMetrics []prometheus.Collector) (*prometheus.Registry, *g
reg.MustRegister(cusMetrics...) reg.MustRegister(cusMetrics...)
return reg, grpcMetrics, nil return reg, grpcMetrics, nil
} }
func GetGrpcCusMetrics(name string) (*GrpcCusMetricsMap, error) {
switch name {
case "MessageGateway":
return G_grpc_msggateway_metrics, nil
case "User":
return G_grpc_msggateway_metrics, nil
case "Msg":
return G_grpc_msggateway_metrics, nil
case "Conversation":
return G_grpc_msggateway_metrics, nil
default:
return G_grpc_msggateway_metrics, nil
}
}

@ -16,7 +16,12 @@ package startrpc
import ( import (
"fmt" "fmt"
"github.com/openimsdk/open-im-server/v3/pkg/common/prom_metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net" "net"
"net/http"
"strconv" "strconv"
"github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/openimsdk/open-im-server/v3/pkg/common/config"
@ -29,7 +34,6 @@ import (
"github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/discoveryregistry"
"github.com/OpenIMSDK/tools/mw" "github.com/OpenIMSDK/tools/mw"
"github.com/OpenIMSDK/tools/network" "github.com/OpenIMSDK/tools/network"
"github.com/OpenIMSDK/tools/prome"
"github.com/OpenIMSDK/tools/utils" "github.com/OpenIMSDK/tools/utils"
) )
@ -61,16 +65,19 @@ func Start(
if err != nil { if err != nil {
return err return err
} }
var reg *prometheus.Registry
var metric *grpcprometheus.ServerMetrics
// ctx 中间件 // ctx 中间件
if config.Config.Prometheus.Enable { if config.Config.Prometheus.Enable {
prome.NewGrpcRequestCounter() //////////////////////////
prome.NewGrpcRequestFailedCounter() cusMetrics, err := prom_metrics.GetGrpcCusMetrics(rpcRegisterName)
prome.NewGrpcRequestSuccessCounter() if err != nil {
unaryInterceptor := mw.InterceptChain(grpcprometheus.UnaryServerInterceptor, mw.RpcServerInterceptor) fmt.Println("prom_metrics.GetGrpcCusMetrics error")
options = append(options, []grpc.ServerOption{ return err
grpc.StreamInterceptor(grpcprometheus.StreamServerInterceptor), }
grpc.UnaryInterceptor(unaryInterceptor), reg, metric, err = prom_metrics.NewGrpcPromObj(cusMetrics.MetricList())
}...) options = append(options, mw.GrpcServer(), grpc.StreamInterceptor(metric.StreamServerInterceptor()),
grpc.UnaryInterceptor(metric.UnaryServerInterceptor()))
} else { } else {
options = append(options, mw.GrpcServer()) options = append(options, mw.GrpcServer())
} }
@ -80,6 +87,7 @@ func Start(
if err != nil { if err != nil {
return utils.Wrap1(err) return utils.Wrap1(err)
} }
metric.InitializeMetrics(srv)
err = client.Register( err = client.Register(
rpcRegisterName, rpcRegisterName,
registerIP, registerIP,
@ -91,8 +99,10 @@ func Start(
} }
go func() { go func() {
if config.Config.Prometheus.Enable && prometheusPort != 0 { if config.Config.Prometheus.Enable && prometheusPort != 0 {
if err := prome.StartPrometheusSrv(prometheusPort); err != nil { // Create a HTTP server for prometheus.
panic(err.Error()) httpServer := &http.Server{Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}), Addr: fmt.Sprintf("0.0.0.0:%d", 90)}
if err := httpServer.ListenAndServe(); err != nil {
log.Fatal("Unable to start a http server.")
} }
} }
}() }()

Loading…
Cancel
Save