-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta.go
93 lines (71 loc) · 2.29 KB
/
meta.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
package meta
import (
"context"
"strings"
tmeta "github.com/alexfalkowski/go-service/transport/meta"
"github.com/alexfalkowski/go-service/transport/nsq/handler"
"github.com/alexfalkowski/go-service/transport/nsq/message"
"github.com/alexfalkowski/go-service/transport/nsq/producer"
"github.com/google/uuid"
)
// NewHandler for meta.
func NewHandler(h handler.Handler) *Handler {
return &Handler{Handler: h}
}
// Handler for meta.
type Handler struct {
handler.Handler
}
func (h *Handler) Handle(ctx context.Context, message *message.Message) error {
ctx = tmeta.WithUserAgent(ctx, extractUserAgent(ctx, message.Headers))
requestID := extractRequestID(ctx, message.Headers)
if requestID == "" {
requestID = uuid.New().String()
}
ctx = tmeta.WithRequestID(ctx, requestID)
ctx = tmeta.WithRemoteAddress(ctx, extractRemoteAddress(ctx, message.Headers))
return h.Handler.Handle(ctx, message)
}
// NewProducer for meta.
func NewProducer(userAgent string, p producer.Producer) *Producer {
return &Producer{userAgent: userAgent, Producer: p}
}
// Producer for meta.
type Producer struct {
userAgent string
producer.Producer
}
func (p *Producer) Publish(ctx context.Context, topic string, message *message.Message) error {
userAgent := tmeta.UserAgent(ctx)
if userAgent == "" {
userAgent = p.userAgent
}
message.Headers["user-agent"] = userAgent
ctx = tmeta.WithUserAgent(ctx, userAgent)
requestID := tmeta.RequestID(ctx)
if requestID == "" {
requestID = uuid.New().String()
}
message.Headers["request-id"] = requestID
ctx = tmeta.WithRequestID(ctx, requestID)
ctx = tmeta.WithRemoteAddress(ctx, extractRemoteAddress(ctx, message.Headers))
return p.Producer.Publish(ctx, topic, message)
}
func extractUserAgent(ctx context.Context, headers message.Headers) string {
if userAgent := headers["user-agent"]; userAgent != "" {
return userAgent
}
return tmeta.UserAgent(ctx)
}
func extractRequestID(ctx context.Context, headers message.Headers) string {
if requestID := headers["request-id"]; requestID != "" {
return requestID
}
return tmeta.RequestID(ctx)
}
func extractRemoteAddress(ctx context.Context, headers message.Headers) string {
if forwardedFor := headers["forwarded-for"]; forwardedFor != "" {
return strings.Split(forwardedFor, ",")[0]
}
return tmeta.RemoteAddress(ctx)
}