request log

This commit is contained in:
Sergey Cherepanov 2023-05-04 17:39:23 +02:00 committed by Mikhail Iudin
parent 39a8c59f83
commit 0664c310fa
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
4 changed files with 33 additions and 8 deletions

View File

@ -62,6 +62,7 @@ type HandleMessage struct {
Deadline time.Time Deadline time.Time
SenderId string SenderId string
Message *spacesyncproto.ObjectSyncMessage Message *spacesyncproto.ObjectSyncMessage
PeerCtx context.Context
} }
func (m HandleMessage) LogFields(fields ...zap.Field) []zap.Field { func (m HandleMessage) LogFields(fields ...zap.Field) []zap.Field {
@ -407,7 +408,9 @@ func (s *space) handleMessage(msg HandleMessage) {
ctx := peer.CtxWithPeerId(context.Background(), msg.SenderId) ctx := peer.CtxWithPeerId(context.Background(), msg.SenderId)
ctx = logger.CtxWithFields(ctx, zap.Uint64("msgId", msg.Id), zap.String("senderId", msg.SenderId)) ctx = logger.CtxWithFields(ctx, zap.Uint64("msgId", msg.Id), zap.String("senderId", msg.SenderId))
defer func() { defer func() {
s.metric.RequestLog(ctx, msg.LogFields(zap.Error(err))...) s.metric.RequestLog(msg.PeerCtx, "space.streamOp", msg.LogFields(
zap.Error(err),
)...)
}() }()
if !msg.Deadline.IsZero() { if !msg.Deadline.IsZero() {

View File

@ -1,6 +1,7 @@
package metric package metric
import ( import (
"github.com/anytypeio/any-sync/net/peer"
"go.uber.org/zap" "go.uber.org/zap"
"golang.org/x/net/context" "golang.org/x/net/context"
"time" "time"
@ -11,11 +12,11 @@ func Method(val string) zap.Field {
} }
func QueueDur(val time.Duration) zap.Field { func QueueDur(val time.Duration) zap.Field {
return zap.Int64("queueMs", val.Milliseconds()) return zap.Float64("queueDur", val.Seconds())
} }
func TotalDur(val time.Duration) zap.Field { func TotalDur(val time.Duration) zap.Field {
return zap.Int64("totalMs", val.Milliseconds()) return zap.Float64("totalDur", val.Seconds())
} }
func SpaceId(val string) zap.Field { func SpaceId(val string) zap.Field {
@ -26,14 +27,24 @@ func ObjectId(val string) zap.Field {
return zap.String("objectId", val) return zap.String("objectId", val)
} }
func PeerId(val string) zap.Field {
return zap.String("peerId", val)
}
func Identity(val string) zap.Field { func Identity(val string) zap.Field {
return zap.String("identity", val) return zap.String("identity", val)
} }
func IP(val string) zap.Field { func Addr(val string) zap.Field {
return zap.String("ip", val) return zap.String("addr", val)
} }
func (m *metric) RequestLog(ctx context.Context, fields ...zap.Field) { func (m *metric) RequestLog(ctx context.Context, rpc string, fields ...zap.Field) {
m.rpcLog.InfoCtx(ctx, "", fields...) peerId, _ := peer.CtxPeerId(ctx)
ak, _ := peer.CtxPubKey(ctx)
var acc string
if ak != nil {
acc = ak.Account()
}
m.rpcLog.Info("", append(fields, Addr(peer.CtxPeerAddr(ctx)), PeerId(peerId), Identity(acc), Method(rpc))...)
} }

View File

@ -1 +1,12 @@
package metric package metric
import (
"context"
"github.com/anytypeio/any-sync/app/logger"
"testing"
)
func TestLog(t *testing.T) {
m := &metric{rpcLog: logger.NewNamed("rpcLog")}
m.RequestLog(context.Background(), "rpc")
}

View File

@ -24,7 +24,7 @@ func New() Metric {
type Metric interface { type Metric interface {
Registry() *prometheus.Registry Registry() *prometheus.Registry
WrapDRPCHandler(h drpc.Handler) drpc.Handler WrapDRPCHandler(h drpc.Handler) drpc.Handler
RequestLog(ctx context.Context, fields ...zap.Field) RequestLog(ctx context.Context, rpc string, fields ...zap.Field)
app.ComponentRunnable app.ComponentRunnable
} }