-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
174 lines (146 loc) · 4.19 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/rabbitmq/amqp091-go"
)
var (
uri = flag.String("uri", "amqp://guest:guest@localhost:5672/", "AMQP URI")
insecureTLS = flag.Bool("insecure-tls", false, "Insecure TLS mode: don't check certificates")
queue = flag.String("queue", "", "AMQP queue name")
ack = flag.Bool("ack", false, "Acknowledge messages")
maxMessages = flag.Uint("max-messages", 1000, "Maximum number of messages to dump or 0 for unlimited")
outputDir = flag.String("output-dir", ".", "Directory in which to save the dumped messages")
full = flag.Bool("full", false, "Dump the message, its properties and headers")
verbose = flag.Bool("verbose", false, "Print progress")
)
func main() {
flag.Parse()
if flag.NArg() > 0 {
fmt.Fprintf(os.Stderr, "Error: Unused command line arguments detected.\n")
flag.Usage()
os.Exit(2)
}
err := dumpMessagesFromQueue(*uri, *queue, *maxMessages, *outputDir)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func dial(amqpURI string) (*amqp091.Connection, error) {
verboseLog(fmt.Sprintf("Dialing %q", amqpURI))
if *insecureTLS && strings.HasPrefix(amqpURI, "amqps://") {
tlsConfig := new(tls.Config)
tlsConfig.InsecureSkipVerify = true
conn, err := amqp091.DialTLS(amqpURI, tlsConfig)
return conn, err
}
conn, err := amqp091.Dial(amqpURI)
return conn, err
}
func dumpMessagesFromQueue(amqpURI string, queueName string, maxMessages uint, outputDir string) error {
if queueName == "" {
return fmt.Errorf("Must supply queue name")
}
conn, err := dial(amqpURI)
if err != nil {
return fmt.Errorf("Dial: %s", err)
}
defer func() {
conn.Close()
verboseLog("AMQP connection closed")
}()
channel, err := conn.Channel()
if err != nil {
return fmt.Errorf("Channel: %s", err)
}
verboseLog(fmt.Sprintf("Pulling messages from queue %q", queueName))
for messagesReceived := uint(0); maxMessages == 0 || messagesReceived < maxMessages; messagesReceived++ {
msg, ok, err := channel.Get(queueName,
*ack, // autoAck
)
if err != nil {
return fmt.Errorf("Queue get: %s", err)
}
if !ok {
verboseLog("No more messages in queue")
break
}
err = saveMessageToFile(msg.Body, outputDir, messagesReceived)
if err != nil {
return fmt.Errorf("Save message: %s", err)
}
if *full {
err = savePropsAndHeadersToFile(msg, outputDir, messagesReceived)
if err != nil {
return fmt.Errorf("Save props and headers: %s", err)
}
}
}
return nil
}
func saveMessageToFile(body []byte, outputDir string, counter uint) error {
filePath := generateFilePath(outputDir, counter)
err := ioutil.WriteFile(filePath, body, 0644)
if err != nil {
return err
}
fmt.Println(filePath)
return nil
}
func getProperties(msg amqp091.Delivery) map[string]interface{} {
props := map[string]interface{}{
"app_id": msg.AppId,
"content_encoding": msg.ContentEncoding,
"content_type": msg.ContentType,
"correlation_id": msg.CorrelationId,
"delivery_mode": msg.DeliveryMode,
"expiration": msg.Expiration,
"message_id": msg.MessageId,
"priority": msg.Priority,
"reply_to": msg.ReplyTo,
"type": msg.Type,
"user_id": msg.UserId,
"exchange": msg.Exchange,
"routing_key": msg.RoutingKey,
}
if !msg.Timestamp.IsZero() {
props["timestamp"] = msg.Timestamp.String()
}
for k, v := range props {
if v == "" {
delete(props, k)
}
}
return props
}
func savePropsAndHeadersToFile(msg amqp091.Delivery, outputDir string, counter uint) error {
extras := make(map[string]interface{})
extras["properties"] = getProperties(msg)
extras["headers"] = msg.Headers
data, err := json.MarshalIndent(extras, "", " ")
if err != nil {
return err
}
filePath := generateFilePath(outputDir, counter) + "-headers+properties.json"
err = ioutil.WriteFile(filePath, data, 0644)
if err != nil {
return err
}
fmt.Println(filePath)
return nil
}
func generateFilePath(outputDir string, counter uint) string {
return path.Join(outputDir, fmt.Sprintf("msg-%04d", counter))
}
func verboseLog(msg string) {
if *verbose {
fmt.Println("*", msg)
}
}