public class Observer extends Agent implements MessageListener
To use, simply add the agent to a container:
container.add("observer", new Observer());
The web interface is served by the application's own web server if it has
one, and on DEFAULT_PORT otherwise. Pass a port explicitly to
override that:
container.add("observer", new Observer(8082));
The URL to browse to is logged when the agent starts, and is readable as the
url parameter of the agent.
The agent installs a MessageListener on the container. The listener
never consumes a message, and never throws, and so observation is
transparent to the agents being observed. Since the listener is called
before delivery is attempted, the observer sees messages that are
sent, not messages that are successfully delivered. A message sent to
a topic is seen once, addressed to the topic, and not once per subscriber.
An observer only sees traffic in its own container. In a distributed setup, add one observer per container.
Messages are serialized using the same JSON representation that remote containers and gateways use, wrapped in an envelope carrying a sequence number and timestamps:
{"time": 1767225600000, "ptime": 12345, "seq": 7,
"action": "send", "message": {"clazz": "...", "data": {...}}}
One JSON object is published per line.
The agent also tracks the endpoints (agents and topics) it has seen traffic to or from, and publishes that list to the web interface, which uses it to let the user choose what to display. Endpoints are discovered before any filtering is applied, so the list is complete even when messages are being dropped.
Since fjåge containers can be chatty, messages may be filtered in the
container, before serialization, using an ObserverFilter. The filter
may be set programmatically, or from the web interface. It is common to all
connected web clients.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
Observer.Endpoint
An endpoint the observer has seen traffic to or from, and how much of it.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
AUTO_PORT
Port setting denoting that the application's web server is to be used.
|
protected WebSocketHubConnector |
conn |
protected java.lang.String |
context |
protected java.util.concurrent.atomic.AtomicLong |
count |
protected java.lang.Thread |
ctrlThread |
static java.lang.String |
DEFAULT_CONTEXT
Default context path for the web interface.
|
static int |
DEFAULT_MAX_RATE
Default maximum number of messages published per second.
|
static int |
DEFAULT_PORT
Port used for the web interface when no port is specified and the
application does not already run a web server.
|
protected java.util.concurrent.atomic.AtomicLong |
dropped |
protected boolean |
enabled |
protected java.util.Map<java.lang.String,Observer.Endpoint> |
endpoints |
protected ObserverFilter |
filter |
protected com.google.gson.Gson |
gson |
protected java.util.List<org.eclipse.jetty.server.handler.ContextHandler> |
handlers |
protected long |
lastStats |
protected long |
lastStatsCount |
protected long |
lastStatsDropped |
protected int |
maxRate |
protected java.io.OutputStream |
out |
protected Platform |
pf |
protected int |
port |
protected static java.lang.String |
RESOURCE |
protected long |
seq |
protected WebServer |
server |
protected static long |
STATS_INTERVAL |
protected int |
windowCount |
protected long |
windowStart |
protected java.lang.Object |
wlock |
| Constructor and Description |
|---|
Observer()
Creates an observer agent serving a web interface on the same port as the
application's web server, if it has one, and on
DEFAULT_PORT
otherwise. |
Observer(int port)
Creates an observer agent serving a web interface on a specified port.
|
Observer(int port,
java.lang.String context)
Creates an observer agent serving a web interface on a specified port
and context path.
|
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
allow(long t)
Rate limiter, called with wlock held.
|
protected int |
appPort()
Port of the application's web server, so that the observer can share it
rather than open a port of its own.
|
void |
clearEndpoints()
Forgets the endpoints seen so far, and tells the web interface to do the
same.
|
protected void |
discover(Message msg)
Notes the endpoints involved in a message, publishing any newly seen ones.
|
int |
getConnections() |
long |
getCount() |
long |
getDropped() |
boolean |
getEnabled() |
java.lang.String[] |
getEndpoints()
Gets the endpoints (agents and topics) that the observer has seen traffic
to or from.
|
ObserverFilter |
getFilter()
Gets the filter currently applied to observed messages.
|
int |
getMaxRate() |
java.lang.String |
getUrl() |
protected void |
handleControl(java.lang.String s) |
void |
init()
Called by the container when the agent is started.
|
boolean |
onReceive(Message msg)
Called by the container for every message sent.
|
protected void |
publish(java.lang.String json)
Publishes a line to all connected web clients, called with wlock held.
|
protected void |
publishEndpoints(java.util.List<java.lang.String> subset)
Publishes the endpoints seen so far, or a subset of newly seen ones.
|
protected void |
publishSafely(java.lang.String json)
Publishes a line to all connected web clients, swallowing any error.
|
protected void |
publishState() |
protected void |
publishStats() |
protected java.lang.String |
serialize(Message msg,
long t,
long n)
Serializes a message, wrapped in an observer envelope.
|
boolean |
setEnabled(boolean b) |
void |
setFilter(ObserverFilter f)
Sets the filter to apply to observed messages, in the container, before
serialization.
|
int |
setMaxRate(int r) |
void |
shutdown()
Called by the container when the agent is terminated.
|
protected void |
startControlThread()
Web clients share a single input stream on the hub connector, and there is
no way to reply to an individual client.
|
java.lang.String |
toString()
Gets a string representation of the agent.
|
add, agent, agentForService, agentForService, agentsForService, agentsForService, block, block, clone, currentTimeMillis, delay, deregister, deregister, die, getAgentID, getContainer, getName, getPlatform, getState, getStore, getType, getYieldDuringReceive, nanoTime, platformSend, prepareRequest, println, receive, receive, receive, receive, receive, receive, receive, receive, register, register, request, request, run, send, setLogLevel, setQueueSize, setYieldDuringReceive, stop, subscribe, topic, topic, topic, topic, topic, topic, unsubscribe, wakepublic static final int DEFAULT_PORT
public static final int AUTO_PORT
public static final java.lang.String DEFAULT_CONTEXT
public static final int DEFAULT_MAX_RATE
protected static final java.lang.String RESOURCE
protected static final long STATS_INTERVAL
protected int port
protected final java.lang.String context
protected final com.google.gson.Gson gson
protected final java.lang.Object wlock
protected final java.util.Map<java.lang.String,Observer.Endpoint> endpoints
protected WebServer server
protected WebSocketHubConnector conn
protected java.io.OutputStream out
protected java.util.List<org.eclipse.jetty.server.handler.ContextHandler> handlers
protected java.lang.Thread ctrlThread
protected Platform pf
protected volatile boolean enabled
protected volatile int maxRate
protected volatile ObserverFilter filter
protected final java.util.concurrent.atomic.AtomicLong count
protected final java.util.concurrent.atomic.AtomicLong dropped
protected long seq
protected long windowStart
protected int windowCount
protected long lastStats
protected long lastStatsCount
protected long lastStatsDropped
public Observer()
DEFAULT_PORT
otherwise. The port is resolved when the agent starts, so that it does not
matter whether the application's web server is created before or after the
observer is added to the container.public Observer(int port)
port - port to serve the web interface on, or AUTO_PORT to
use the application's web server.public Observer(int port,
java.lang.String context)
port - port to serve the web interface on, or AUTO_PORT to
use the application's web server.context - context path to serve the web interface at.public void init()
Agentprotected int appPort()
DEFAULT_PORT.public void shutdown()
Agentpublic boolean onReceive(Message msg)
onReceive in interface MessageListenermsg - received message.protected void discover(Message msg)
protected java.lang.String serialize(Message msg, long t, long n)
protected boolean allow(long t)
protected void publish(java.lang.String json)
throws java.io.IOException
java.io.IOExceptionprotected void publishSafely(java.lang.String json)
protected void publishEndpoints(java.util.List<java.lang.String> subset)
subset - endpoint names to publish, null to publish the full list.protected void publishState()
protected void publishStats()
protected void startControlThread()
protected void handleControl(java.lang.String s)
public boolean getEnabled()
public boolean setEnabled(boolean b)
public long getCount()
public long getDropped()
public int getMaxRate()
public int setMaxRate(int r)
public int getConnections()
public java.lang.String getUrl()
public ObserverFilter getFilter()
public void setFilter(ObserverFilter f)
f - filter to apply, null to observe all messages.public java.lang.String[] getEndpoints()
#.public void clearEndpoints()