Class: LogStash::Outputs::Http
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Http
- Includes:
- PluginMixins::HttpClient
- Defined in:
- lib/logstash/outputs/http.rb
Defined Under Namespace
Classes: RetryTimerTask
Constant Summary collapse
- VALID_METHODS =
["put", "post", "patch", "delete", "get", "head"]
- RETRYABLE_MANTICORE_EXCEPTIONS =
[ ::Manticore::Timeout, ::Manticore::SocketException, ::Manticore::ClientProtocolException, ::Manticore::ResolutionFailure, ::Manticore::SocketTimeout ]
Instance Method Summary collapse
- #close ⇒ Object
- #log_error_response(response, url, event) ⇒ Object
- #log_retryable_response(response) ⇒ Object
-
#multi_receive(events) ⇒ Object
def register.
- #register ⇒ Object
- #send_event(event, attempt) ⇒ Object
- #send_events(events) ⇒ Object
- #send_json_batch(events) ⇒ Object
- #sleep_for_attempt(attempt) ⇒ Object
Instance Method Details
#close ⇒ Object
317 318 319 320 |
# File 'lib/logstash/outputs/http.rb', line 317 def close @timer.cancel client.close end |
#log_error_response(response, url, event) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/logstash/outputs/http.rb', line 172 def log_error_response(response, url, event) log_failure( "Encountered non-2xx HTTP code #{response.code}", :response_code => response.code, :url => url, :event => event ) end |
#log_retryable_response(response) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/logstash/outputs/http.rb', line 164 def log_retryable_response(response) if (response.code == 429) @logger.debug? && @logger.debug("Encountered a 429 response, will retry. This is not serious, just flow control via HTTP") else @logger.warn("Encountered a retryable HTTP request in HTTP output, will retry", :code => response.code, :body => response.body) end end |
#multi_receive(events) ⇒ Object
def register
119 120 121 122 123 124 125 126 |
# File 'lib/logstash/outputs/http.rb', line 119 def multi_receive(events) return if events.empty? if @format == "json_batch" send_json_batch(events) else send_events(events) end end |
#register ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/logstash/outputs/http.rb', line 89 def register @http_method = @http_method.to_sym # We count outstanding requests with this queue # This queue tracks the requests to create backpressure # When this queue is empty no new requests may be sent, # tokens must be added back by the client on success @request_tokens = SizedQueue.new(@pool_max) @pool_max.times {|t| @request_tokens << true } @requests = Array.new if @content_type.nil? case @format when "form" ; @content_type = "application/x-www-form-urlencoded" when "json" ; @content_type = "application/json" when "json_batch" ; @content_type = "application/json" when "message" ; @content_type = "text/plain" end end @headers["Content-Type"] = @content_type validate_format! # Run named Timer as daemon thread @timer = java.util.Timer.new("HTTP Output #{self.params['id']}", true) end |
#send_event(event, attempt) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/logstash/outputs/http.rb', line 245 def send_event(event, attempt) body = event_body(event) # Send the request url = event.sprintf(@url) headers = event_headers(event) # Compress the body and add appropriate header if @http_compression == true headers["Content-Encoding"] = "gzip" body = gzip(body) end # Create an async request request = client.background.send(@http_method, url, :body => body, :headers => headers) request.on_success do |response| begin if !response_success?(response) if retryable_response?(response) log_retryable_response(response) yield :retry, event, attempt else log_error_response(response, url, event) yield :failure, event, attempt end else yield :success, event, attempt end rescue => e # Shouldn't ever happen @logger.error("Unexpected error in request success!", :class => e.class.name, :message => e., :backtrace => e.backtrace) end end request.on_failure do |exception| begin will_retry = retryable_exception?(exception) log_failure("Could not fetch URL", :url => url, :method => @http_method, :body => body, :headers => headers, :message => exception., :class => exception.class.name, :backtrace => exception.backtrace, :will_retry => will_retry ) if will_retry yield :retry, event, attempt else yield :failure, event, attempt end rescue => e # Shouldn't ever happen @logger.error("Unexpected error in request failure!", :class => e.class.name, :message => e., :backtrace => e.backtrace) end end # Actually invoke the request in the background # Note: this must only be invoked after all handlers are defined, otherwise # those handlers are not guaranteed to be called! request.call end |
#send_events(events) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/logstash/outputs/http.rb', line 181 def send_events(events) successes = java.util.concurrent.atomic.AtomicInteger.new(0) failures = java.util.concurrent.atomic.AtomicInteger.new(0) retries = java.util.concurrent.atomic.AtomicInteger.new(0) pending = Queue.new events.each {|e| pending << [e, 0]} while popped = pending.pop break if popped == :done event, attempt = popped send_event(event, attempt) do |action,event,attempt| begin action = :failure if action == :retry && !@retry_failed case action when :success successes.incrementAndGet when :retry retries.incrementAndGet next_attempt = attempt+1 sleep_for = sleep_for_attempt(next_attempt) @logger.info("Retrying http request, will sleep for #{sleep_for} seconds") timer_task = RetryTimerTask.new(pending, event, next_attempt) @timer.schedule(timer_task, sleep_for*1000) when :failure failures.incrementAndGet else raise "Unknown action #{action}" end if action == :success || action == :failure if successes.get+failures.get == events.size pending << :done end end rescue => e # This should never happen unless there's a flat out bug in the code @logger.error("Error sending HTTP Request", :class => e.class.name, :message => e., :backtrace => e.backtrace) failures.incrementAndGet raise e end end end rescue => e @logger.error("Error in http output loop", :class => e.class.name, :message => e., :backtrace => e.backtrace) raise e end |
#send_json_batch(events) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/logstash/outputs/http.rb', line 141 def send_json_batch(events) attempt = 1 body = LogStash::Json.dump(events.map {|e| map_event(e) }) begin while true request = client.send(@http_method, @url, :body => body, :headers => @headers) response = request.call break if response_success?(response) if retryable_response?(response) log_retryable_response(response) sleep_for_attempt attempt attempt += 1 else log_error_response(response, url, events) end end rescue *RETRYABLE_MANTICORE_EXCEPTIONS => e logger.warn("Encountered exception during http output send, will retry after delay", :message => e., :class => e.class.name) sleep_for_attempt attempt retry end end |
#sleep_for_attempt(attempt) ⇒ Object
239 240 241 242 243 |
# File 'lib/logstash/outputs/http.rb', line 239 def sleep_for_attempt(attempt) sleep_for = attempt**2 sleep_for = sleep_for <= 60 ? sleep_for : 60 (sleep_for/2) + (rand(0..sleep_for)/2) end |