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 (
"context"
"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/http/pprof"
"strconv"
@ -81,6 +83,11 @@ func run(port int) error {
}
log.ZInfo(context.Background(), "api register public config to discov success")
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")
var address string
if config.Config.Api.ListenIP != "" {

@ -26,3 +26,17 @@ func NewGrpcPromObj(cusMetrics []prometheus.Collector) (*prometheus.Registry, *g
reg.MustRegister(cusMetrics...)
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 (
"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/http"
"strconv"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
@ -29,7 +34,6 @@ import (
"github.com/OpenIMSDK/tools/discoveryregistry"
"github.com/OpenIMSDK/tools/mw"
"github.com/OpenIMSDK/tools/network"
"github.com/OpenIMSDK/tools/prome"
"github.com/OpenIMSDK/tools/utils"
)
@ -61,16 +65,19 @@ func Start(
if err != nil {
return err
}
var reg *prometheus.Registry
var metric *grpcprometheus.ServerMetrics
// ctx 中间件
if config.Config.Prometheus.Enable {
prome.NewGrpcRequestCounter()
prome.NewGrpcRequestFailedCounter()
prome.NewGrpcRequestSuccessCounter()
unaryInterceptor := mw.InterceptChain(grpcprometheus.UnaryServerInterceptor, mw.RpcServerInterceptor)
options = append(options, []grpc.ServerOption{
grpc.StreamInterceptor(grpcprometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(unaryInterceptor),
}...)
//////////////////////////
cusMetrics, err := prom_metrics.GetGrpcCusMetrics(rpcRegisterName)
if err != nil {
fmt.Println("prom_metrics.GetGrpcCusMetrics error")
return err
}
reg, metric, err = prom_metrics.NewGrpcPromObj(cusMetrics.MetricList())
options = append(options, mw.GrpcServer(), grpc.StreamInterceptor(metric.StreamServerInterceptor()),
grpc.UnaryInterceptor(metric.UnaryServerInterceptor()))
} else {
options = append(options, mw.GrpcServer())
}
@ -80,6 +87,7 @@ func Start(
if err != nil {
return utils.Wrap1(err)
}
metric.InitializeMetrics(srv)
err = client.Register(
rpcRegisterName,
registerIP,
@ -91,8 +99,10 @@ func Start(
}
go func() {
if config.Config.Prometheus.Enable && prometheusPort != 0 {
if err := prome.StartPrometheusSrv(prometheusPort); err != nil {
panic(err.Error())
// Create a HTTP server for prometheus.
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