You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
683 B
25 lines
683 B
package warden
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
nmd "github.com/go-kratos/kratos/pkg/net/rpc/warden/internal/metadata"
|
|
"github.com/go-kratos/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
|
|
}
|
|
}
|
|
|