diff --git a/commonspace/space.go b/commonspace/space.go index ecb81a80..77b3dc4b 100644 --- a/commonspace/space.go +++ b/commonspace/space.go @@ -62,6 +62,7 @@ type HandleMessage struct { Deadline time.Time SenderId string Message *spacesyncproto.ObjectSyncMessage + PeerCtx context.Context } 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 = logger.CtxWithFields(ctx, zap.Uint64("msgId", msg.Id), zap.String("senderId", msg.SenderId)) 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() { diff --git a/metric/log.go b/metric/log.go index 12a18424..23e76591 100644 --- a/metric/log.go +++ b/metric/log.go @@ -1,6 +1,7 @@ package metric import ( + "github.com/anytypeio/any-sync/net/peer" "go.uber.org/zap" "golang.org/x/net/context" "time" @@ -11,11 +12,11 @@ func Method(val string) 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 { - return zap.Int64("totalMs", val.Milliseconds()) + return zap.Float64("totalDur", val.Seconds()) } func SpaceId(val string) zap.Field { @@ -26,14 +27,24 @@ func ObjectId(val string) zap.Field { return zap.String("objectId", val) } +func PeerId(val string) zap.Field { + return zap.String("peerId", val) +} + func Identity(val string) zap.Field { return zap.String("identity", val) } -func IP(val string) zap.Field { - return zap.String("ip", val) +func Addr(val string) zap.Field { + return zap.String("addr", val) } -func (m *metric) RequestLog(ctx context.Context, fields ...zap.Field) { - m.rpcLog.InfoCtx(ctx, "", fields...) +func (m *metric) RequestLog(ctx context.Context, rpc string, fields ...zap.Field) { + 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))...) } diff --git a/metric/log_test.go b/metric/log_test.go index 0bad30a0..50835795 100644 --- a/metric/log_test.go +++ b/metric/log_test.go @@ -1 +1,12 @@ 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") +} diff --git a/metric/metric.go b/metric/metric.go index 32f29ae5..d5319982 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -24,7 +24,7 @@ func New() Metric { type Metric interface { Registry() *prometheus.Registry WrapDRPCHandler(h drpc.Handler) drpc.Handler - RequestLog(ctx context.Context, fields ...zap.Field) + RequestLog(ctx context.Context, rpc string, fields ...zap.Field) app.ComponentRunnable }