Skip to content

Commit

Permalink
Slight modification of MetricsServlet to catch Exception instead of j…
Browse files Browse the repository at this point in the history
…ust JsonMappingException in gauge evaluations. If a gauge throws an exception in a relatively small response, Jetty catches it before any buffers are flushed and returns an HTTP 500 with a suitable error page. If a gauge throws an exception in a larger response, after some of the buffer has been flushed to the client, Jetty cuts off the response stream, resulting in an HTTP 200 with a partial JSON response.

This modification to MetricsServiet allows a gauge to throw an exception and not trigger a cut off JSON response.
  • Loading branch information
Ryan Kennedy committed Jun 23, 2011
1 parent fbc87d2 commit f22ada1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Expand Up @@ -268,11 +268,11 @@ private void writeGauge(JsonGenerator json, GaugeMetric gauge) throws IOExceptio
{
json.writeStringField("type", "gauge");
json.writeFieldName("value");
final Object value = gauge.value();
try {
final Object value = gauge.value();
json.writeObject(value);
} catch (JsonMappingException e) {
json.writeString("unknown value type: " + value.getClass());
} catch (Exception e) {
json.writeString("error reading gauge: " + e.getMessage());
}
}
json.writeEndObject();
Expand Down
Expand Up @@ -8,6 +8,9 @@ import com.yammer.metrics.reporting.MetricsServlet
object TestServer extends Instrumented {
val counter1 = metrics.counter("wah", "doody")
val counter2 = metrics.counter("woo")
val asplodingGauge = metrics.gauge[Int]("boo") {
throw new RuntimeException("asplode!")
}

def main(args: Array[String]) {
val server = new Server(8080)
Expand Down

0 comments on commit f22ada1

Please sign in to comment.