backport pull #920

via:
    curl https://github.com/logstash/logstash/pull/920.diff | patch -p1
backport pull #913 to 1.3.x

via:
    curl https://github.com/logstash/logstash/pull/913.diff | patch -p1