This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
/
message.go
155 lines (135 loc) · 4.53 KB
/
message.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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kafka
import (
"context"
"encoding/binary"
"math"
nethttp "net/http"
"regexp"
"strconv"
"strings"
"github.com/Shopify/sarama"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/protocol/http"
"github.com/cloudevents/sdk-go/v2/protocol/kafka_sarama"
"go.uber.org/zap"
sourcesv1alpha1 "knative.dev/eventing-contrib/kafka/source/pkg/apis/sources/v1alpha1"
)
func (a *Adapter) ConsumerMessageToHttpRequest(ctx context.Context, cm *sarama.ConsumerMessage, req *nethttp.Request, logger *zap.Logger) error {
msg := kafka_sarama.NewMessageFromConsumerMessage(cm)
defer func() {
err := msg.Finish(nil)
if err != nil {
logger.Warn("Something went wrong while trying to finalizing the message", zap.Error(err))
}
}()
if msg.ReadEncoding() != binding.EncodingUnknown {
// Message is a CloudEvent -> Encode directly to HTTP
return http.WriteRequest(ctx, msg, req)
}
// Message is not a CloudEvent -> We need to translate it to a valid CloudEvent
kafkaMsg := msg
event := cloudevents.NewEvent()
event.SetID(makeEventId(cm.Partition, cm.Offset))
event.SetTime(cm.Timestamp)
event.SetType(sourcesv1alpha1.KafkaEventType)
event.SetSource(sourcesv1alpha1.KafkaEventSource(a.config.Namespace, a.config.Name, cm.Topic))
event.SetSubject(makeEventSubject(cm.Partition, cm.Offset))
dumpKafkaMetaToEvent(&event, a.keyTypeMapper, cm.Key, kafkaMsg)
err := event.SetData(kafkaMsg.ContentType, kafkaMsg.Value)
if err != nil {
return err
}
return http.WriteRequest(ctx, binding.ToMessage(&event), req)
}
func makeEventId(partition int32, offset int64) string {
var str strings.Builder
str.WriteString("partition:")
str.WriteString(strconv.Itoa(int(partition)))
str.WriteString("/offset:")
str.WriteString(strconv.FormatInt(offset, 10))
return str.String()
}
// KafkaEventSubject returns the Kafka CloudEvent subject of the message.
func makeEventSubject(partition int32, offset int64) string {
var str strings.Builder
str.WriteString("partition:")
str.WriteString(strconv.Itoa(int(partition)))
str.WriteByte('#')
str.WriteString(strconv.FormatInt(offset, 10))
return str.String()
}
var replaceBadCharacters = regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString
func dumpKafkaMetaToEvent(event *cloudevents.Event, keyTypeMapper func([]byte) interface{}, key []byte, msg *kafka_sarama.Message) {
if key != nil {
event.SetExtension("key", keyTypeMapper(key))
}
for k, v := range msg.Headers {
event.SetExtension("kafkaheader"+replaceBadCharacters(k, ""), string(v))
}
}
func getKeyTypeMapper(keyType string) func([]byte) interface{} {
var keyTypeMapper func([]byte) interface{}
switch keyType {
case "int":
keyTypeMapper = func(by []byte) interface{} {
// Took from https://github.com/axbaretto/kafka/blob/master/clients/src/main/java/org/apache/kafka/common/serialization/LongDeserializer.java
if len(by) == 4 {
var res int32
for _, b := range by {
res <<= 8
res |= int32(b & 0xFF)
}
return res
} else if len(by) == 8 {
var res int64
for _, b := range by {
res <<= 8
res |= int64(b & 0xFF)
}
return res
} else {
// Fallback to byte array
return by
}
}
case "float":
keyTypeMapper = func(by []byte) interface{} {
// BigEndian is specified in https://kafka.apache.org/protocol#protocol_types
// Number is converted to string because
if len(by) == 4 {
intermediate := binary.BigEndian.Uint32(by)
fl := math.Float32frombits(intermediate)
return strconv.FormatFloat(float64(fl), 'f', -1, 64)
} else if len(by) == 8 {
intermediate := binary.BigEndian.Uint64(by)
fl := math.Float64frombits(intermediate)
return strconv.FormatFloat(fl, 'f', -1, 64)
} else {
// Fallback to byte array
return by
}
}
case "byte-array":
keyTypeMapper = func(bytes []byte) interface{} {
return bytes
}
default:
keyTypeMapper = func(bytes []byte) interface{} {
return string(bytes)
}
}
return keyTypeMapper
}