1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

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
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() {

View file

@ -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))...)
}

View file

@ -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")
}

View file

@ -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
}