Porting back PR 1081 from logstash repository

See https://github.com/logstash/logstash/pull/1081 for history.