-
Notifications
You must be signed in to change notification settings - Fork 33
/
metric_server.interceptor.go
68 lines (54 loc) · 1.65 KB
/
metric_server.interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package interceptoropentelemetry
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
grpc_ "github.com/kaydxh/golang/go/net/grpc"
time_ "github.com/kaydxh/golang/go/time"
logs_ "github.com/kaydxh/golang/pkg/logs"
resource_ "github.com/kaydxh/golang/pkg/middleware/resource"
"google.golang.org/grpc"
)
func UnaryServerMetricInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return handleMetric(info, resource_.HandlerWithContext[any, any](handler))(ctx, req)
}
}
func HandleMetric[REQ any, RESP any](
handler resource_.HandlerWithContext[REQ, RESP],
) resource_.HandlerWithContext[REQ, RESP] {
return handleMetric(nil, handler)
}
func handleMetric[REQ any, RESP any](
info *grpc.UnaryServerInfo,
handler resource_.HandlerWithContext[REQ, RESP],
) resource_.HandlerWithContext[REQ, RESP] {
return func(ctx context.Context, req REQ) (RESP, error) {
tc := time_.New(true)
ctx = resource_.AddKeysContext(ctx, resource_.AddAttrKeysContext, resource_.AddMetricKeysContext)
resp, err := handler(ctx, req)
var method string
if info != nil {
method = info.FullMethod
} else {
method, _ = runtime.RPCMethod(ctx)
}
resource_.ReportMetric(ctx,
resource_.Dimension{
CalleeMethod: method,
Error: err,
},
tc.Elapse(),
)
tc.Tick(method)
logger := logs_.GetLogger(ctx)
peerAddr, _ := grpc_.GetIPFromContext(ctx)
summary := func() {
logger.WithField("cost", tc.String()).Infof(
"called by peer addr: %v",
peerAddr.String(),
)
}
defer summary()
return resp, err
}
}