Fix: Return the correct list of available Java events
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Wed, 9 Sep 2015 19:07:24 +0000 (15:07 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 22 Oct 2015 21:23:46 +0000 (17:23 -0400)
The "lttng list -j/-l" command should list the events that are currently
offered by Java application and available to be enabled.

Due to some confusion in the implementation of the corresponding agent
command response, it was actually returning the list of events that were
enabled in the tracing session.

Rectify this by sending the list of loggers of the corresponding domain
that have one or more LTTng log handlers attached. The interface method
was also renamed from listEnabledEvents() to listAvailableEvents() to
make it more representative.

Fixes: #933
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/AbstractLttngAgent.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java
liblttng-ust-java-agent/java/lttng-ust-agent-jul/org/lttng/ust/agent/jul/LttngJulAgent.java
liblttng-ust-java-agent/java/lttng-ust-agent-log4j/org/lttng/ust/agent/log4j/LttngLog4jAgent.java

index 8531eaec7c3b8ddab23f0b7aa1e668ffc3376124..e97a7bdb222b172b62524fa7245f400287dd3d5b 100644 (file)
@@ -18,9 +18,8 @@
 
 package org.lttng.ust.agent;
 
+import java.util.Collection;
 import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Map;
 import java.util.NavigableMap;
 import java.util.Set;
@@ -234,19 +233,11 @@ public abstract class AbstractLttngAgent<T extends ILttngHandler>
                return decrementEventCount(eventName, enabledEvents);
        }
 
+       /*
+        * Implementation of this method is domain-specific.
+        */
        @Override
-       public Iterable<String> listEnabledEvents() {
-               List<String> events = new LinkedList<String>();
-
-               if (enabledWildcards.get() > 0) {
-                       events.add(WILDCARD);
-               }
-               for (String prefix : enabledEventPrefixes.keySet()) {
-                       events.add(new String(prefix + WILDCARD));
-               }
-               events.addAll(enabledEvents.keySet());
-               return events;
-       }
+       public abstract Collection<String> listAvailableEvents();
 
        @Override
        public boolean isEventEnabled(String eventName) {
index 031d4e048863c4e74802e03a90029c17401b6896..ef0c11d95656db784c48a738fb5174454356121b 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.lttng.ust.agent.client;
 
+import java.util.Collection;
+
 import org.lttng.ust.agent.session.EventRule;
 
 /**
@@ -53,9 +55,12 @@ public interface ILttngTcpClientListener {
        boolean eventDisabled(String eventName);
 
        /**
-        * List the all events currently enabled in the current tracing sessions.
+        * List the events that are available in the agent's tracing domain.
+        *
+        * In Java terms, this means loggers that have at least one LTTng log
+        * handler of their corresponding domain attached.
         *
-        * @return The list of enabled events
+        * @return The list of available events
         */
-       Iterable<String> listEnabledEvents();
+       Collection<String> listAvailableEvents();
 }
index c06eaaad95e09db12565d841a7b75c88ada37194..a8bc15209e8c3e4072d5f6a437d0a8921a2a4812 100644 (file)
@@ -20,8 +20,7 @@ package org.lttng.ust.agent.client;
 
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collection;
 
 /**
  * Session daemon command asking the Java agent to list its registered loggers,
@@ -34,11 +33,10 @@ class SessiondListLoggersCommand implements ISessiondCommand {
 
        @Override
        public LttngAgentResponse execute(ILttngTcpClientListener agent) {
-               final List<String> loggerList = new ArrayList<String>();
+               final Collection<String> loggerList = agent.listAvailableEvents();
                int dataSize = 0;
 
-               for (String event : agent.listEnabledEvents()) {
-                       loggerList.add(event);
+               for (String event : agent.listAvailableEvents()) {
                        dataSize += event.length() + 1;
                }
 
@@ -49,10 +47,10 @@ class SessiondListLoggersCommand implements ISessiondCommand {
 
                private final static int SIZE = 12;
 
-               private final List<String> loggers;
+               private final Collection<String> loggers;
                private final int dataSize;
 
-               public SessiondListLoggersResponse(List<String> loggers, int dataSize) {
+               public SessiondListLoggersResponse(Collection<String> loggers, int dataSize) {
                        this.loggers = loggers;
                        this.dataSize = dataSize;
                }
index fa92f64541b04788af12015bc3fa8d30343466d9..597ae6b3e8228a694b2cf1ac532e181705c40555 100644 (file)
 
 package org.lttng.ust.agent.jul;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
 import org.lttng.ust.agent.AbstractLttngAgent;
 
 /**
@@ -39,4 +47,39 @@ class LttngJulAgent extends AbstractLttngAgent<LttngLogHandler> {
                return instance;
        }
 
+       @Override
+       public Collection<String> listAvailableEvents() {
+               List<String> ret = new ArrayList<String>();
+
+               List<String> loggersNames = Collections.list(LogManager.getLogManager().getLoggerNames());
+               for (String name : loggersNames) {
+                       /*
+                        * Skip the root logger. An empty string is not a valid event name
+                        * in LTTng.
+                        */
+                       if (name.equals("")) {
+                               continue;
+                       }
+
+                       /*
+                        * Check if that logger has at least one LTTng JUL handler attached.
+                        */
+                       Logger logger = Logger.getLogger(name);
+                       if (hasLttngHandlerAttached(logger)) {
+                               ret.add(name);
+                       }
+               }
+
+               return ret;
+       }
+
+       private static boolean hasLttngHandlerAttached(Logger logger) {
+               for (Handler handler : logger.getHandlers()) {
+                       if (handler instanceof LttngLogHandler) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
 }
index 5dd846872308eb2d46c1f1dc94175f7b9acf4eec..7dcff9ec4e582721eba2c85675d56671884aa390 100644 (file)
 
 package org.lttng.ust.agent.log4j;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.log4j.Appender;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
 import org.lttng.ust.agent.AbstractLttngAgent;
 
 /**
@@ -40,4 +48,33 @@ class LttngLog4jAgent extends AbstractLttngAgent<LttngLogAppender> {
                return instance;
        }
 
+       @Override
+       public Collection<String> listAvailableEvents() {
+               List<String> ret = new ArrayList<String>();
+
+               @SuppressWarnings("unchecked")
+               List<Logger> loggers = Collections.list(LogManager.getCurrentLoggers());
+               for (Logger logger : loggers) {
+                       /*
+                        * Check if that logger has at least one LTTng log4j appender
+                        * attached.
+                        */
+                       if (hasLttngAppenderAttached(logger)) {
+                               ret.add(logger.getName());
+                       }
+               }
+
+               return ret;
+       }
+
+       private static boolean hasLttngAppenderAttached(Logger logger) {
+               @SuppressWarnings("unchecked")
+               List<Appender> appenders = Collections.list(logger.getAllAppenders());
+               for (Appender appender : appenders) {
+                       if (appender instanceof LttngLogAppender) {
+                               return true;
+                       }
+               }
+               return false;
+       }
 }
This page took 0.02918 seconds and 4 git commands to generate.