abhishekagarwal87 commented on code in PR #13363:
URL: https://github.com/apache/druid/pull/13363#discussion_r1026126200


##########
docs/configuration/index.md:
##########
@@ -483,6 +484,14 @@ Instead use `recipientBaseUrlPattern` described in the 
table below.
 
 To use graphite as emitter set `druid.emitter=graphite`. For configuration 
details, see [Graphite emitter](../development/extensions-contrib/graphite.md) 
for the Graphite emitter Druid extension.
 
+##### Switching Emitter
+
+To use switching as emitter set `druid.emitter=switching`. 
+
+|Property|Description|Default|
+|--------|-----------|-------|
+|`druid.emitter.switching.emitters`|Map of feed to list of emitter modules to 
load that will be used for each feed, e.g., {"metrics":["http"], 
"alerts":["logging"]}|{}|

Review Comment:
   can you add a tiny note here that this is supposed to be in `JSON` format? 



##########
core/src/main/java/org/apache/druid/java/util/emitter/core/SwitchingEmitter.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.google.common.collect.ImmutableSet;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStop;
+import org.apache.druid.java.util.common.logger.Logger;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class SwitchingEmitter implements Emitter
+{
+
+  private static final Logger log = new Logger(SwitchingEmitter.class);
+
+  private final Emitter[] defaultEmitters;
+
+  private final Map<String, List<Emitter>> feedToEmitters;
+  private final Set<Emitter> knownEmitters;
+
+  public SwitchingEmitter(Map<String, List<Emitter>> feedToEmitters, Emitter[] 
defaultEmitter)
+  {
+    this.feedToEmitters = feedToEmitters;
+    this.defaultEmitters = defaultEmitter;
+    ImmutableSet.Builder<Emitter> emittersSetBuilder = new 
ImmutableSet.Builder<>();
+    emittersSetBuilder.addAll(Arrays.stream(defaultEmitter).iterator());
+    for (List<Emitter> emitterList : feedToEmitters.values()) {
+      for (Emitter emitter : emitterList) {
+        emittersSetBuilder.add(emitter);
+      }
+    }
+    this.knownEmitters = emittersSetBuilder.build();
+  }
+
+  @Override
+  @LifecycleStart
+  public void start()
+  {
+    log.info("Starting Switching Emitter.");
+
+    for (Emitter e : knownEmitters) {
+      log.info("Starting emitter %s.", e.getClass().getName());
+      e.start();
+    }
+  }
+
+  @Override
+  public void emit(Event event)
+  {
+    // linear search is likely faster than hashed lookup
+    // todo dont use a hashmap here. use something that will be more efficient 
for this kind of lookups
+    for (Map.Entry<String, List<Emitter>> feedToEmitters : 
feedToEmitters.entrySet()) {
+      if (feedToEmitters.getKey().equals(event.getFeed())) {
+        for (Emitter emitter : feedToEmitters.getValue()) {
+          emitter.emit(event);
+        }
+        return;
+      }
+    }
+    for (Emitter emitter : defaultEmitters) {
+      emitter.emit(event);
+    }
+  }
+
+  @Override
+  public void flush() throws IOException
+  {
+    boolean fail = false;
+    log.info("Flushing Switching Emitter.");

Review Comment:
   what frequency is this logging going to be at? 



##########
docs/configuration/index.md:
##########
@@ -483,6 +484,14 @@ Instead use `recipientBaseUrlPattern` described in the 
table below.
 
 To use graphite as emitter set `druid.emitter=graphite`. For configuration 
details, see [Graphite emitter](../development/extensions-contrib/graphite.md) 
for the Graphite emitter Druid extension.
 
+##### Switching Emitter
+
+To use switching as emitter set `druid.emitter=switching`. 
+
+|Property|Description|Default|
+|--------|-----------|-------|
+|`druid.emitter.switching.emitters`|Map of feed to list of emitter modules to 
load that will be used for each feed, e.g., {"metrics":["http"], 
"alerts":["logging"]}|{}|
+|`druid.emitter.switching.defaultEmitters`|List of emitter modules to load 
that will be used if there is no emitter specifically designated to the event 
feed, e.g., ["logging","http"].|[]|

Review Comment:
   so that event feed will go to all the default emitters? 



##########
core/src/main/java/org/apache/druid/java/util/emitter/core/SwitchingEmitter.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.google.common.collect.ImmutableSet;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStop;
+import org.apache.druid.java.util.common.logger.Logger;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class SwitchingEmitter implements Emitter

Review Comment:
   can you please add some javadocs to this class?



##########
server/src/main/java/org/apache/druid/server/emitter/SwitchingEmitterConfig.java:
##########
@@ -0,0 +1,31 @@
+package org.apache.druid.server.emitter;

Review Comment:
   it's probably fine with the current structure. I think, what needs to 
initialize the composite emitter is in `server` and the emitter logic itself is 
in `core`. 



-- 
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