|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 IBM, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package encode |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "github.com/netobserv/flowlogs2metrics/pkg/api" |
| 23 | + "github.com/netobserv/flowlogs2metrics/pkg/config" |
| 24 | + kafkago "github.com/segmentio/kafka-go" |
| 25 | + log "github.com/sirupsen/logrus" |
| 26 | + "golang.org/x/net/context" |
| 27 | + "time" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultReadTimeoutSeconds = int64(10) |
| 32 | + defaultWriteTimeoutSeconds = int64(10) |
| 33 | +) |
| 34 | + |
| 35 | +type kafkaWriteMessage interface { |
| 36 | + WriteMessages(ctx context.Context, msgs ...kafkago.Message) error |
| 37 | +} |
| 38 | + |
| 39 | +type encodeKafka struct { |
| 40 | + kafkaParams api.EncodeKafka |
| 41 | + kafkaWriter kafkaWriteMessage |
| 42 | +} |
| 43 | + |
| 44 | +// Encode writes entries to kafka topic |
| 45 | +func (r *encodeKafka) Encode(in []config.GenericMap) []interface{} { |
| 46 | + log.Debugf("entering encodeKafka Encode, #items = %d", len(in)) |
| 47 | + var msgs []kafkago.Message |
| 48 | + msgs = make([]kafkago.Message, 0) |
| 49 | + out := make([]interface{}, 0) |
| 50 | + for _, entry := range in { |
| 51 | + var entryByteArray []byte |
| 52 | + entryByteArray, _ = json.Marshal(entry) |
| 53 | + msg := kafkago.Message{ |
| 54 | + Value: entryByteArray, |
| 55 | + } |
| 56 | + msgs = append(msgs, msg) |
| 57 | + out = append(out, entry) |
| 58 | + } |
| 59 | + err := r.kafkaWriter.WriteMessages(context.Background(), msgs...) |
| 60 | + if err != nil { |
| 61 | + log.Errorf("encodeKafka error: %v", err) |
| 62 | + } |
| 63 | + return out |
| 64 | +} |
| 65 | + |
| 66 | +// NewEncodeKafka create a new writer to kafka |
| 67 | +func NewEncodeKafka() (Encoder, error) { |
| 68 | + log.Debugf("entering NewIngestKafka") |
| 69 | + encodeKafkaString := config.Opt.PipeLine.Encode.Kafka |
| 70 | + log.Debugf("encodeKafkaString = %s", encodeKafkaString) |
| 71 | + var jsonEncodeKafka api.EncodeKafka |
| 72 | + err := json.Unmarshal([]byte(encodeKafkaString), &jsonEncodeKafka) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + var balancer kafkago.Balancer |
| 78 | + switch jsonEncodeKafka.Balancer { |
| 79 | + case api.KafkaEncodeBalancerName("RoundRobin"): |
| 80 | + balancer = &kafkago.RoundRobin{} |
| 81 | + case api.KafkaEncodeBalancerName("LeastBytes"): |
| 82 | + balancer = &kafkago.LeastBytes{} |
| 83 | + case api.KafkaEncodeBalancerName("Hash"): |
| 84 | + balancer = &kafkago.Hash{} |
| 85 | + case api.KafkaEncodeBalancerName("Crc32"): |
| 86 | + balancer = &kafkago.CRC32Balancer{} |
| 87 | + case api.KafkaEncodeBalancerName("Murmur2"): |
| 88 | + balancer = &kafkago.Murmur2Balancer{} |
| 89 | + default: |
| 90 | + balancer = nil |
| 91 | + } |
| 92 | + |
| 93 | + readTimeoutSecs := defaultReadTimeoutSeconds |
| 94 | + if jsonEncodeKafka.ReadTimeout != 0 { |
| 95 | + readTimeoutSecs = jsonEncodeKafka.ReadTimeout |
| 96 | + } |
| 97 | + |
| 98 | + writeTimeoutSecs := defaultWriteTimeoutSeconds |
| 99 | + if jsonEncodeKafka.WriteTimeout != 0 { |
| 100 | + writeTimeoutSecs = jsonEncodeKafka.WriteTimeout |
| 101 | + } |
| 102 | + |
| 103 | + // connect to the kafka server |
| 104 | + kafkaWriter := kafkago.Writer{ |
| 105 | + Addr: kafkago.TCP(jsonEncodeKafka.Address), |
| 106 | + Topic: jsonEncodeKafka.Topic, |
| 107 | + Balancer: balancer, |
| 108 | + ReadTimeout: time.Duration(readTimeoutSecs) * time.Second, |
| 109 | + WriteTimeout: time.Duration(writeTimeoutSecs) * time.Second, |
| 110 | + BatchSize: jsonEncodeKafka.BatchSize, |
| 111 | + BatchBytes: jsonEncodeKafka.BatchBytes, |
| 112 | + } |
| 113 | + |
| 114 | + return &encodeKafka{ |
| 115 | + kafkaParams: jsonEncodeKafka, |
| 116 | + kafkaWriter: &kafkaWriter, |
| 117 | + }, nil |
| 118 | +} |
0 commit comments