-
Notifications
You must be signed in to change notification settings - Fork 3
/
zap.go
116 lines (95 loc) · 2.98 KB
/
zap.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package zap
import (
"context"
"time"
"github.com/alexfalkowski/go-service/meta"
stime "github.com/alexfalkowski/go-service/time"
"github.com/alexfalkowski/go-service/transport/nsq/handler"
"github.com/alexfalkowski/go-service/transport/nsq/message"
"github.com/alexfalkowski/go-service/transport/nsq/producer"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
nsqID = "nsq.id"
nsqBody = "nsq.body"
nsqTimestamp = "nsq.timestamp"
nsqAttempts = "nsq.attempts"
nsqAddress = "nsq.address"
nsqDuration = "nsq.duration_ms"
nsqStartTime = "nsq.start_time"
nsqTopic = "nsq.topic"
nsqChannel = "nsq.channel"
component = "component"
nsqComponent = "nsq"
consumerKind = "consumer"
producerKind = "producer"
)
// NewHandler for zap.
func NewHandler(topic, channel string, logger *zap.Logger, handler handler.Handler) *Handler {
return &Handler{topic: topic, channel: channel, logger: logger, Handler: handler}
}
// Handler for zap.
type Handler struct {
topic, channel string
logger *zap.Logger
handler.Handler
}
func (h *Handler) Handle(ctx context.Context, message *message.Message) error {
start := time.Now().UTC()
err := h.Handler.Handle(ctx, message)
fields := []zapcore.Field{
zap.Int64(nsqDuration, stime.ToMilliseconds(time.Since(start))),
zap.String(nsqStartTime, start.Format(time.RFC3339)),
zap.String(nsqTopic, h.topic),
zap.String(nsqChannel, h.channel),
zap.ByteString(nsqID, message.ID[:]),
zap.ByteString(nsqBody, message.Body),
zap.Int64(nsqTimestamp, message.Timestamp),
zap.Uint16(nsqAttempts, message.Attempts),
zap.String(nsqAddress, message.NSQDAddress),
zap.String("span.kind", consumerKind),
zap.String(component, nsqComponent),
}
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
if err != nil {
fields = append(fields, zap.Error(err))
h.logger.Error("finished call with error", fields...)
return err
}
h.logger.Info("finished call with success", fields...)
return nil
}
// NewProducer for zap.
func NewProducer(logger *zap.Logger, producer producer.Producer) *Producer {
return &Producer{logger: logger, Producer: producer}
}
// Producer for zap.
type Producer struct {
logger *zap.Logger
producer.Producer
}
func (p *Producer) Publish(ctx context.Context, topic string, message *message.Message) error {
start := time.Now().UTC()
err := p.Producer.Publish(ctx, topic, message)
fields := []zapcore.Field{
zap.Int64(nsqDuration, stime.ToMilliseconds(time.Since(start))),
zap.String(nsqStartTime, start.Format(time.RFC3339)),
zap.String(nsqTopic, topic),
zap.ByteString(nsqBody, message.Body),
zap.String("span.kind", producerKind),
zap.String(component, nsqComponent),
}
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
if err != nil {
fields = append(fields, zap.Error(err))
p.logger.Error("finished call with error", fields...)
return err
}
p.logger.Info("finished call with success", fields...)
return nil
}