Metrics: A New Way to Monitorize Your Application
Join the DZone community and get the full member experience.
Join For FreeWhen you are running long term
applications like web applications, it is good to know some statistics
about them, like number of requests served, request durations, or the
number active requests. But also some more generic information like the
state of your internal collections, how many times some portion of code
is being executed, or health checks like database availability, or any
kind of connection to an external system.
To install Metrics, we only have to add metrics dependency. In this example we are going to use Maven.
<dependencies> <dependency> <groupId>com.yammer.metrics</groupId> <artifactId>metrics-core</artifactId> <version>2.2.0</version> </dependency> </dependencies>
- Gauges: an instantaneous measurement of a discrete value.
- Counters: a value that can be incremented and decremented. Can be used in queues to monitorize the remaining number of pending jobs.
- Meters: measure the rate of events over time. You can specify the rate unit, the scope of events or event type.
- Histograms: measure the statistical distribution of values in a stream of data.
- Timers: measure the amount of time it takes to execute a piece of code and the distribution of its duration.
- Healthy checks: as his name suggests, it centralize our service's healthy checks of external systems.
The main class where Measures will be applied is called MetricsApplication and is the responsible of connecting to Google and sending the entered word.
public class MetricsApplication { //Counter private final Counter numberOfSendCharacters = Metrics.newCounter(MetricsApplication.class, "Total-Number-Of-Characters"); //Meter private final Meter sendMessages = Metrics.newMeter(MetricsApplication.class, "Sent-Messages", "Send", TimeUnit.SECONDS); //Timer private final Timer responseTime = Metrics.newTimer(MetricsApplication.class, "Response-Time"); private LinkedList<String> historyOfQueries = new LinkedList<String>(); { //Gauge Metrics.newGauge(MetricsApplication.class, "lastQuery", new Gauge<String>() { @Override public String value() { return historyOfQueries.getLast(); } }); } public void sendQuery(String message) throws FailingHttpStatusCodeException, MalformedURLException, IOException { updateMetrics(message); TimerContext timerContext = responseTime.time(); sendQueryToGoogle(message); timerContext.stop(); } private void sendQueryToGoogle(String message) throws FailingHttpStatusCodeException, MalformedURLException, IOException { WebClient webClient = new WebClient(); HtmlPage currentPage = webClient.getPage("http://www.google.com/"); //Get the query input text HtmlInput queryInput = currentPage.getElementByName("q"); queryInput.setValueAttribute(message); //Submit the form by pressing the submit button HtmlSubmitInput submitBtn = currentPage.getElementByName("btnG"); currentPage = submitBtn.click(); } private void updateMetrics(String message) { numberOfSendCharacters.inc(message.length()); sendMessages.mark(); historyOfQueries.addLast(message); } }
Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments