mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-10 00:29:01 +02:00
637a6a3628
* add warden rpc
26 lines
681 B
Go
26 lines
681 B
Go
package warden
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
nmd "github.com/bilibili/kratos/pkg/net/rpc/warden/internal/metadata"
|
|
"github.com/bilibili/kratos/pkg/stat/sys/cpu"
|
|
|
|
"google.golang.org/grpc"
|
|
gmd "google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
func (s *Server) stats() grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req interface{}, args *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
resp, err = handler(ctx, req)
|
|
var cpustat cpu.Stat
|
|
cpu.ReadStat(&cpustat)
|
|
if cpustat.Usage != 0 {
|
|
trailer := gmd.Pairs([]string{nmd.CPUUsage, strconv.FormatInt(int64(cpustat.Usage), 10)}...)
|
|
grpc.SetTrailer(ctx, trailer)
|
|
}
|
|
return
|
|
}
|
|
}
|