Debian Codegraph @

tools/kafka-console-producer/kafka-console-producer.go

raw
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io"
7 "log"
8 "os"
9 "strings"
10
11 "github.com/rcrowley/go-metrics"
12
13 "github.com/IBM/sarama"
14 "github.com/IBM/sarama/tools/tls"
15 )
16
17 var (
18 brokerList = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The comma separated list of brokers in the Kafka cluster. You can also set the KAFKA_PEERS environment variable")
19 headers = flag.String("headers", "", "The headers of the message to produce. Example: -headers=foo:bar,bar:foo")
20 topic = flag.String("topic", "", "REQUIRED: the topic to produce to")
21 key = flag.String("key", "", "The key of the message to produce. Can be empty.")
22 value = flag.String("value", "", "REQUIRED: the value of the message to produce. You can also provide the value on stdin.")
23 partitioner = flag.String("partitioner", "", "The partitioning scheme to use. Can be `hash`, `manual`, or `random`")
24 partition = flag.Int("partition", -1, "The partition to produce to.")
25 verbose = flag.Bool("verbose", false, "Turn on sarama logging to stderr")
26 showMetrics = flag.Bool("metrics", false, "Output metrics on successful publish to stderr")
27 silent = flag.Bool("silent", false, "Turn off printing the message's topic, partition, and offset to stdout")
28 tlsEnabled = flag.Bool("tls-enabled", false, "Whether to enable TLS")
29 tlsSkipVerify = flag.Bool("tls-skip-verify", false, "Whether skip TLS server cert verification")
30 tlsClientCert = flag.String("tls-client-cert", "", "Client cert for client authentication (use with -tls-enabled and -tls-client-key)")
31 tlsClientKey = flag.String("tls-client-key", "", "Client key for client authentication (use with tls-enabled and -tls-client-cert)")
32
33 logger = log.New(os.Stderr, "", log.LstdFlags)
34 )
35
36 func main() {
37 flag.Parse()
38
39 if *brokerList == "" {
40 printUsageErrorAndExit("no -brokers specified. Alternatively, set the KAFKA_PEERS environment variable")
41 }
42
43 if *topic == "" {
44 printUsageErrorAndExit("no -topic specified")
45 }
46
47 if *verbose {
48 sarama.Logger = logger
49 }
50
51 config := sarama.NewConfig()
52 config.Producer.RequiredAcks = sarama.WaitForAll
53 config.Producer.Return.Successes = true
54
55 if *tlsEnabled {
56 tlsConfig, err := tls.NewConfig(*tlsClientCert, *tlsClientKey)
57 if err != nil {
58 printErrorAndExit(69, "Failed to create TLS config: %s", err)
59 }
60
61 config.Net.TLS.Enable = true
62 config.Net.TLS.Config = tlsConfig
63 config.Net.TLS.Config.InsecureSkipVerify = *tlsSkipVerify
64 }
65
66 switch *partitioner {
67 case "":
68 if *partition >= 0 {
69 config.Producer.Partitioner = sarama.NewManualPartitioner
70 } else {
71 config.Producer.Partitioner = sarama.NewHashPartitioner
72 }
73 case "hash":
74 config.Producer.Partitioner = sarama.NewHashPartitioner
75 case "random":
76 config.Producer.Partitioner = sarama.NewRandomPartitioner
77 case "manual":
78 config.Producer.Partitioner = sarama.NewManualPartitioner
79 if *partition == -1 {
80 printUsageErrorAndExit("-partition is required when partitioning manually")
81 }
82 default:
83 printUsageErrorAndExit(fmt.Sprintf("Partitioner %s not supported.", *partitioner))
84 }
85
86 message := &sarama.ProducerMessage{Topic: *topic, Partition: int32(*partition)}
87
88 if *key != "" {
89 message.Key = sarama.StringEncoder(*key)
90 }
91
92 if *value != "" {
93 message.Value = sarama.StringEncoder(*value)
94 } else if stdinAvailable() {
95 bytes, err := io.ReadAll(os.Stdin)
96 if err != nil {
97 printErrorAndExit(66, "Failed to read data from the standard input: %s", err)
98 }
99 message.Value = sarama.ByteEncoder(bytes)
100 } else {
101 printUsageErrorAndExit("-value is required, or you have to provide the value on stdin")
102 }
103
104 if *headers != "" {
105 var hdrs []sarama.RecordHeader
106 for h := range strings.SplitSeq(*headers, ",") {
107 if header := strings.Split(h, ":"); len(header) != 2 {
108 printUsageErrorAndExit("-header should be key:value. Example: -headers=foo:bar,bar:foo")
109 } else {
110 hdrs = append(hdrs, sarama.RecordHeader{
111 Key: []byte(header[0]),
112 Value: []byte(header[1]),
113 })
114 }
115 }
116
117 if len(hdrs) != 0 {
118 message.Headers = hdrs
119 }
120 }
121
122 producer, err := sarama.NewSyncProducer(strings.Split(*brokerList, ","), config)
123 if err != nil {
124 printErrorAndExit(69, "Failed to open Kafka producer: %s", err)
125 }
126 defer func() {
127 if err := producer.Close(); err != nil {
128 logger.Println("Failed to close Kafka producer cleanly:", err)
129 }
130 }()
131
132 partition, offset, err := producer.SendMessage(message)
133 if err != nil {
134 printErrorAndExit(69, "Failed to produce message: %s", err)
135 } else if !*silent {
136 fmt.Printf("topic=%s\tpartition=%d\toffset=%d\n", *topic, partition, offset)
137 }
138 if *showMetrics {
139 metrics.WriteOnce(config.MetricRegistry, os.Stderr)
140 }
141 }
142
143 func printErrorAndExit(code int, format string, values ...interface{}) {
144 fmt.Fprintf(os.Stderr, "ERROR: %s\n", fmt.Sprintf(format, values...))
145 fmt.Fprintln(os.Stderr)
146 os.Exit(code)
147 }
148
149 func printUsageErrorAndExit(message string) {
150 fmt.Fprintln(os.Stderr, "ERROR:", message)
151 fmt.Fprintln(os.Stderr)
152 fmt.Fprintln(os.Stderr, "Available command line options:")
153 flag.PrintDefaults()
154 os.Exit(64)
155 }
156
157 func stdinAvailable() bool {
158 stat, _ := os.Stdin.Stat()
159 return (stat.Mode() & os.ModeCharDevice) == 0
160 }