Created
May 3, 2012 22:50
-
-
Save dguardado/2590141 to your computer and use it in GitHub Desktop.
A Sample JAX-RS resource intended to illustrate usage of the Metrics API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hdd; | |
import com.google.common.collect.ImmutableList; | |
import com.google.common.collect.Iterables; | |
import com.yammer.metrics.Metrics; | |
import com.yammer.metrics.annotation.ExceptionMetered; | |
import com.yammer.metrics.annotation.Gauge; | |
import com.yammer.metrics.annotation.Metered; | |
import com.yammer.metrics.annotation.Timed; | |
import com.yammer.metrics.core.Counter; | |
import com.yammer.metrics.core.Histogram; | |
import com.yammer.metrics.reporting.ConsoleReporter; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.Response; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicLong; | |
@Path("/sample") | |
public class SampleResource { | |
// Uniform Histogram | |
private static final Histogram resultSizes | |
= Metrics.newHistogram(SampleResource.class, "resultSizes", false); | |
private static final Counter activeSessions | |
= Metrics.newCounter(SampleResource.class, "activeSessions"); | |
static { | |
ConsoleReporter.enable(5, TimeUnit.MINUTES); | |
} | |
private final Iterable<Integer> searchResults = Iterables.cycle(1, 2, 3, 4, 5); | |
private final AtomicLong sessionIdSequence = new AtomicLong(0); | |
private final Random random = new Random(); | |
@GET | |
@ExceptionMetered | |
@Path("/search-results") | |
public List<Integer> getSearchResults() { | |
List<Integer> results | |
= ImmutableList.copyOf( | |
Iterables.limit(searchResults, random.nextInt(25))); | |
resultSizes.update(results.size()); | |
return results; | |
} | |
@GET | |
@Metered | |
@ExceptionMetered | |
@Path("/high-volume-resource") | |
public String getHighVolumeResource() { | |
try { | |
// Do some stuff, maybe throw an exception, but mostly: | |
Thread.sleep(random.nextLong(1000)); | |
return "OH HI!"; | |
} catch (Exception e) { | |
throw new WebApplicationException(e, | |
Response.serverError().entity("Oh NOES!").build()); | |
} | |
} | |
@GET | |
@Timed | |
@ExceptionMetered | |
@Path("/high-latency-resource") | |
public String getHighLatencyResource() { | |
try { | |
// Do some stuff, maybe throw an exception, but mostly: | |
Thread.sleep(random.nextLong(1000)); | |
return "OH HI!"; | |
} catch (Exception e) { | |
throw new WebApplicationException(e, | |
Response.serverError().entity("Oh NOES!").build()); | |
} | |
} | |
@POST | |
@ExceptionMetered | |
@Path("/session") | |
public long startSession() { | |
long id = sessionIdSequence.incrementAndGet(); | |
activeSessions.inc(); | |
return id; | |
} | |
@DELETE | |
@ExceptionMetered | |
@Path("/session/{id}") | |
public String endSession(@PathParam("id") long id) { | |
// delete session here | |
activeSessions.dec(); | |
return "Deleted!"; | |
} | |
@Gauge | |
public long highestSessionId() { | |
return sessionIdSequence.get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment