private void writeInstrument(final Instrument instrument, final Writer writer)
      throws IOException {
    final StringBuilder attrs = new StringBuilder();
    appendAttr(attrs, "name", instrument.getName());
    appendAttr(attrs, "value", instrument.getValue());
    getOtherAttrs(attrs, instrument);

    writer.write("{\"instrument\":{");
    writer.write(attrs.toString());
    writer.write("}");
  }
  private void getOtherAttrs(final StringBuilder attrs, final Instrument instrument) {
    if (instrument instanceof OpInstrument) {
      appendAttr(attrs, "type", "opInstrument");

      OpInstrument opInstrument = (OpInstrument) instrument;
      appendAttr(attrs, "count", opInstrument.getInvocationCount());
      appendAttr(attrs, "time", opInstrument.getMillisecondsTaken());
      appendAttr(attrs, "size", opInstrument.getResultSetSize());
      appendAttr(attrs, "cpu", opInstrument.getCpuTime());
    } else if (instrument instanceof CacheInstrument) {
      appendAttr(attrs, "type", "cacheInstrument");

      CacheInstrument cacheInstrument = (CacheInstrument) instrument;
      appendAttr(attrs, "misses", cacheInstrument.getMisses());
      appendAttr(attrs, "missTime", cacheInstrument.getMissTime());
      appendAttr(attrs, "hits", cacheInstrument.getHits());
      appendAttr(attrs, "size", cacheInstrument.getCacheSize());
      appendAttr(attrs, "hitMissRatio", String.valueOf(cacheInstrument.getHitMissRatio()));
    } else if (instrument instanceof AbsoluteCounter) {
      appendAttr(attrs, "type", "absoluteCounter");
    } else if (instrument instanceof DerivedCounter) {
      appendAttr(attrs, "type", "derivedCounter");
    } else if (instrument instanceof Counter) {
      appendAttr(attrs, "type", "counter");
    } else if (instrument instanceof Gauge) {
      appendAttr(attrs, "type", "gauge");
    } else {
      appendAttr(attrs, "type", instrument.getClass().getName());
    }
  }