rohangarg commented on code in PR #12915:
URL: https://github.com/apache/druid/pull/12915#discussion_r952193797


##########
extensions-contrib/kafka-emitter/src/main/java/org/apache/druid/emitter/kafka/KafkaEmitter.java:
##########
@@ -166,14 +169,14 @@ private void sendToKafka(final String topic, 
MemoryBoundLinkedBlockingQueue<Stri
   public void emit(final Event event)
   {
     if (event != null) {
-      ImmutableMap.Builder<String, Object> resultBuilder = 
ImmutableMap.<String, Object>builder().putAll(event.toMap());
-      if (config.getClusterName() != null) {
-        resultBuilder.put("clusterName", config.getClusterName());
-      }
-      Map<String, Object> result = resultBuilder.build();
-
       try {
-        String resultJson = jsonMapper.writeValueAsString(result);
+        EventMap map = event.toMap();
+        if (config.getClusterName() != null) {
+          map.put("clusterName", config.getClusterName());

Review Comment:
   the map should be copied to a new structure before modification - it is 
better to not expect the object to be mutable



##########
core/src/main/java/org/apache/druid/java/util/emitter/core/EventMap.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.druid.java.util.emitter.core;
+
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * EventMap can be safely serialzed to JSON using Jackson serializer as it 
respects the polymorphic annotations
+ * on entires (unlike standard Map). The example of polymorphic class is a 
query interface, where different native
+ * query types are resolved by additional field called "queryType". This 
implementation esures that the annotation on

Review Comment:
   `This implementation *ensures*`



##########
extensions-contrib/kafka-emitter/src/test/java/org/apache/druid/emitter/kafka/EventToJsonSerializerTest.java:
##########
@@ -0,0 +1,276 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.druid.emitter.kafka;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.jackson.DefaultObjectMapper;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.java.util.emitter.core.Event;
+import org.apache.druid.java.util.emitter.core.EventMap;
+import org.apache.druid.java.util.emitter.service.AlertEvent;
+import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.spec.MultipleIntervalSegmentSpec;
+import org.apache.druid.query.timeseries.TimeseriesQuery;
+import org.apache.druid.segment.VirtualColumns;
+import org.apache.druid.server.QueryStats;
+import org.apache.druid.server.RequestLogLine;
+import org.apache.druid.server.log.DefaultRequestLogEventBuilderFactory;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Collections;
+
+
+public class EventToJsonSerializerTest

Review Comment:
   doubt : I couldn't understand why this change is kept in kafka-emitter 
extension and not core. is there a reason for it?



##########
server/src/main/java/org/apache/druid/server/log/DefaultRequestLogEvent.java:
##########
@@ -55,23 +55,26 @@ public final class DefaultRequestLogEvent implements 
RequestLogEvent
    */
   @JsonValue(value = false)
   @Override
-  public Map<String, Object> toMap()
+  public EventMap toMap()
   {
-    final Map<String, Object> map = new HashMap<>();
-    map.put("feed", getFeed());
-    map.put("timestamp", getCreatedTime());
-    map.put("service", getService());
-    map.put("host", getHost());
-    if (getQuery() != null) {
-      map.put("query", getQuery());
-    }
+    final EventMap.Builder builder = EventMap
+        .builder()
+        .put("feed", getFeed())
+        .put("timestamp", getCreatedTime())
+        .put("service", getService())
+        .put("host", getHost())
+        .putNonNull("query", getQuery());
+
     if (getSql() != null) {
-      map.put("sql", getSql());
-      map.put("sqlQueryContext", getSqlQueryContext());
+      builder.put("sql", getSql())
+             .put("sqlQueryContext", getSqlQueryContext());
     }
-    map.put("remoteAddr", getRemoteAddr());
-    map.put("queryStats", getQueryStats());
-    return map;
+
+    builder
+        .put("remoteAddr", getRemoteAddr())

Review Comment:
   the indentation seems a bit weird. The first put can be on the same line



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to