Real-time error monitoring in Google app engine
February 16, 2012 2 Comments
Yesterday, I showed you how your Google app engine application can send a mail whenever an error occurs (an uncaught Exception for example).
Today we are going to see how to monitor your application in real-time using Google talk.
Of course, the Google talk client has a little icon that tells you how many unread emails you have:
But that’s not enough: we want the GAE application to send a “google talk message” each time an error occurs. This can easily be done using the XMPP service.
First, we define a servlet to handle all exceptions:
<servlet> <servlet-name>ShowErrorServlet</servlet-name> <servlet-class>fr.kubaski.servlet.ShowError</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowErrorServlet</servlet-name> <url-pattern>/showerror</url-pattern> </servlet-mapping> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/showerror</location> </error-page>
And here is the code for the servlet:
public class ShowError extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // note that 't' cannot be null since this servlet is defined as the error handler in web.xml: Throwable t = (Throwable) req.getAttribute("javax.servlet.error.exception"); JID jid = new JID("theadmin@gmail.com"); String msgBody = "an error occured: " + t.getMessage(); Message msg = new MessageBuilder() .withRecipientJids(jid) .withBody(msgBody) .build(); boolean isSent = false; XMPPService xmpp = XMPPServiceFactory.getXMPPService(); if (xmpp.getPresence(jid).isAvailable()) { SendResponse status = xmpp.sendMessage(msg); isSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS); } if (!isSent) { // error handling } getServletContext().getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp); } }
In the code sample above, “theadmin@gmail.com” is the email of an administrator of the GAE application: replace it with yours !
Now you need to invite your GAE application from the Google talk client (exactly like you would invite a friend using his email). To do this, just invite [APPLICATION_IDENTIFIER]@appspot.com. In my case, my application is http://atestapplication.appspot.com so I need to invite atestapplication@appspot.com:
Now generate an exception in your application so that the error servlet is invoked and you will receive an instant notification:
Laurent KUBASKI