Rewrite metrics buffering and flushing to use agents