001 package com.khubla.pragmatach.plugin.statsd; 002 003 import org.slf4j.Logger; 004 import org.slf4j.LoggerFactory; 005 006 import com.khubla.pragmatach.framework.api.Request; 007 import com.khubla.pragmatach.framework.application.Application; 008 import com.khubla.pragmatach.framework.lifecycle.LifecycleListener; 009 import com.khubla.pragmatach.framework.router.PragmatachRoute; 010 import com.timgroup.statsd.NonBlockingStatsDClient; 011 import com.timgroup.statsd.StatsDClient; 012 013 /** 014 * @author tome 015 */ 016 public class StatsDClientImplementation implements LifecycleListener { 017 /** 018 * logger 019 */ 020 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 021 /** 022 * the client 023 */ 024 private static final StatsDClient statsDClient = getStatsDClient(); 025 026 public static StatsDClient getStatsdclient() { 027 return statsDClient; 028 } 029 030 /** 031 * get the client 032 */ 033 private static StatsDClient getStatsDClient() { 034 try { 035 final String prefix = Application.getConfiguration().getParameter("statsd.prefix"); 036 final String host = Application.getConfiguration().getParameter("statsd.host"); 037 final String port = Application.getConfiguration().getParameter("statsd.port"); 038 if ((null != prefix) && (null != host) && (null != port)) { 039 return new NonBlockingStatsDClient(prefix, host, Integer.parseInt(port)); 040 } else { 041 return null; 042 } 043 } catch (final Exception e) { 044 throw new ExceptionInInitializerError(e); 045 } 046 } 047 048 @Override 049 public void beginRequest(PragmatachRoute pragmatachRoute, Request request) { 050 if (null != statsDClient) { 051 /* 052 * request count 053 */ 054 statsDClient.incrementCounter("TotalRequests"); 055 } else { 056 logger.info("started: " + request.getMethod() + " request for: " + request.getURI() + " routed to " + pragmatachRoute.getDescription()); 057 } 058 } 059 060 @Override 061 public void endRequest(PragmatachRoute pragmatachRoute, Request request) { 062 if (null != statsDClient) { 063 } else { 064 logger.info("completed: " + request.getMethod() + " request for: " + request.getURI() + " routed to " + pragmatachRoute.getDescription()); 065 } 066 } 067 }