|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.github.fhuss.kafka.examples.consumer; |
| 18 | + |
| 19 | +import com.github.fhuss.kafka.examples.consumer.internal.Time; |
| 20 | +import org.apache.kafka.clients.consumer.ConsumerInterceptor; |
| 21 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 22 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 23 | +import org.apache.kafka.clients.consumer.OffsetAndMetadata; |
| 24 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 25 | +import org.apache.kafka.clients.producer.Producer; |
| 26 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 27 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 28 | +import org.apache.kafka.common.TopicPartition; |
| 29 | +import org.apache.kafka.common.errors.InterruptException; |
| 30 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 31 | + |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.concurrent.atomic.AtomicInteger; |
| 35 | + |
| 36 | +public class AuditConsumerInterceptor<K, V> implements ConsumerInterceptor<K, V> { |
| 37 | + |
| 38 | + // Record Metadata |
| 39 | + private static final String TRACKING_PARTITION = "partition"; |
| 40 | + private static final String TRACKING_OFFSET = "offset"; |
| 41 | + private static final String TRACKING_TIMESTAMP = "timestamp"; |
| 42 | + private static final String TRACKING_TOPIC = "topic"; |
| 43 | + |
| 44 | + private static final String JSON_OPEN_BRACKET = "{"; |
| 45 | + private static final String JSON_CLOSE_BRACKET = "}"; |
| 46 | + |
| 47 | + private String originalsClientId; |
| 48 | + |
| 49 | + private AuditInterceptorConfig configs; |
| 50 | + |
| 51 | + private Producer<String, String> producer; |
| 52 | + |
| 53 | + @Override |
| 54 | + public ConsumerRecords<K, V> onConsume(ConsumerRecords<K, V> records) { |
| 55 | + records.forEach(r -> { |
| 56 | + final String value = getJsonTrackingMessage(r); |
| 57 | + producer.send(new ProducerRecord<>(configs.getAuditTopic(), value)); |
| 58 | + }); |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onCommit(Map<TopicPartition, OffsetAndMetadata> offsets) { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + private String getJsonTrackingMessage(ConsumerRecord<K, V> record) { |
| 68 | + return JSON_OPEN_BRACKET + |
| 69 | + "\"" + "timestamp" + "\":\"" + Time.SYSTEM.milliseconds() + "\"" + |
| 70 | + "\",client\":" + |
| 71 | + JSON_OPEN_BRACKET + |
| 72 | + "\"" + "clientId" + "\":\"" + originalsClientId + "\"" + |
| 73 | + ",\"" + "applicationId" + "\":\"" + configs.getAuditApplicationId() + "\"" + |
| 74 | + ",\"" + "type" + "\":\"consumer\"" + |
| 75 | + JSON_CLOSE_BRACKET + |
| 76 | + ",\"record\":" + |
| 77 | + JSON_OPEN_BRACKET + |
| 78 | + "\"" + TRACKING_PARTITION + "\":\"" + record.partition() + "\"" + |
| 79 | + ",\"" + TRACKING_TOPIC + "\":\"" + record.topic() + "\"" + |
| 80 | + ",\"" + TRACKING_OFFSET + "\":\"" + record.offset() + "\"" + |
| 81 | + ",\"" + TRACKING_TIMESTAMP + "\":\"" + record.timestamp() + "\"" + |
| 82 | + JSON_CLOSE_BRACKET + |
| 83 | + JSON_CLOSE_BRACKET; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void close() { |
| 88 | + if (this.producer != null) { |
| 89 | + try { |
| 90 | + this.producer.close(); |
| 91 | + } catch (InterruptException e) { |
| 92 | + Thread.currentThread().interrupt(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void configure(Map<String, ?> configs) { |
| 99 | + final Map<String, Object> copyConfigs = new HashMap<>(configs); |
| 100 | + // Drop interceptor classes configuration to not introduce loop. |
| 101 | + copyConfigs.remove(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG); |
| 102 | + |
| 103 | + this.originalsClientId = (String) configs.get(ProducerConfig.CLIENT_ID_CONFIG); |
| 104 | + |
| 105 | + String interceptorClientId = (originalsClientId == null) ? |
| 106 | + "interceptor-consumer-" + ClientIdGenerator.nextClientId() : |
| 107 | + "interceptor-" + originalsClientId; |
| 108 | + |
| 109 | + copyConfigs.put(ProducerConfig.CLIENT_ID_CONFIG, interceptorClientId); |
| 110 | + |
| 111 | + this.configs = new AuditInterceptorConfig(copyConfigs); |
| 112 | + |
| 113 | + copyConfigs.putAll(this.configs.getOverrideProducerConfigs()); |
| 114 | + |
| 115 | + // Enforce some properties to get a non-blocking producer; |
| 116 | + copyConfigs.put(ProducerConfig.RETRIES_CONFIG, "0"); |
| 117 | + copyConfigs.put(ProducerConfig.ACKS_CONFIG, "1"); |
| 118 | + copyConfigs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, "0"); |
| 119 | + copyConfigs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); |
| 120 | + copyConfigs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); |
| 121 | + |
| 122 | + this.producer = new KafkaProducer<>(copyConfigs); |
| 123 | + } |
| 124 | + |
| 125 | + private static class ClientIdGenerator { |
| 126 | + |
| 127 | + private static final AtomicInteger IDS = new AtomicInteger(0); |
| 128 | + |
| 129 | + static int nextClientId() { |
| 130 | + return IDS.getAndIncrement(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments