|
4 | 4 | "encoding/base64" |
5 | 5 | "encoding/hex" |
6 | 6 | "sort" |
| 7 | + "time" |
7 | 8 | "unicode/utf8" |
8 | 9 |
|
9 | 10 | "github.com/IBM/sarama" |
@@ -62,14 +63,63 @@ func encodeRecordHeaders(headers []*sarama.RecordHeader) map[string]string { |
62 | 63 | } |
63 | 64 |
|
64 | 65 | key := *encodeBytes(header.Key, "") |
65 | | - value := *encodeBytes(header.Value, "") |
| 66 | + var value string |
| 67 | + if encodedValue := encodeBytes(decodeAMQPValue(header.Value), ""); encodedValue != nil { |
| 68 | + value = *encodedValue |
| 69 | + } |
66 | 70 |
|
67 | 71 | data[key] = value |
68 | 72 | } |
69 | 73 |
|
70 | 74 | return data |
71 | 75 | } |
72 | 76 |
|
| 77 | +// decodeAMQPValue attempts to decode AMQP-encoded header values. |
| 78 | +// Azure Event Hubs wraps Kafka header values in AMQP type descriptors when using the Kafka interface. |
| 79 | +// This not moved to the azure plugin, because of the required RPC overhead per header value. |
| 80 | +// For regular Kafka servers, this function returns the input unchanged. |
| 81 | +func decodeAMQPValue(data []byte) []byte { |
| 82 | + if len(data) < 2 { |
| 83 | + return data |
| 84 | + } |
| 85 | + |
| 86 | + switch data[0] { |
| 87 | + case 0xa1: // str8: string with 1-byte length |
| 88 | + length := int(data[1]) |
| 89 | + if len(data) >= 2+length { |
| 90 | + return data[2 : 2+length] |
| 91 | + } |
| 92 | + case 0xb1: // str32: string with 4-byte length |
| 93 | + if len(data) >= 6 { |
| 94 | + length := int(data[1])<<24 | int(data[2])<<16 | int(data[3])<<8 | int(data[4]) |
| 95 | + if len(data) >= 5+length { |
| 96 | + return data[5 : 5+length] |
| 97 | + } |
| 98 | + } |
| 99 | + case 0xa0: // vbin8: binary with 1-byte length |
| 100 | + length := int(data[1]) |
| 101 | + if len(data) >= 2+length { |
| 102 | + return data[2 : 2+length] |
| 103 | + } |
| 104 | + case 0xb0: // vbin32: binary with 4-byte length |
| 105 | + if len(data) >= 6 { |
| 106 | + length := int(data[1])<<24 | int(data[2])<<16 | int(data[3])<<8 | int(data[4]) |
| 107 | + if len(data) >= 5+length { |
| 108 | + return data[5 : 5+length] |
| 109 | + } |
| 110 | + } |
| 111 | + case 0x83: // timestamp: 64-bit milliseconds since Unix epoch |
| 112 | + if len(data) >= 9 { |
| 113 | + ms := int64(data[1])<<56 | int64(data[2])<<48 | int64(data[3])<<40 | int64(data[4])<<32 | |
| 114 | + int64(data[5])<<24 | int64(data[6])<<16 | int64(data[7])<<8 | int64(data[8]) |
| 115 | + t := time.UnixMilli(ms) |
| 116 | + return []byte(t.Format(time.RFC3339)) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return data |
| 121 | +} |
| 122 | + |
73 | 123 | func toSortedArray(headers map[string]string) []string { |
74 | 124 |
|
75 | 125 | var column []string |
|
0 commit comments