Class: LogStash::Filters::Http

Inherits:
Base
  • Object
show all
Includes:
PluginMixins::HttpClient
Defined in:
lib/logstash/filters/http.rb

Overview

Logstash HTTP Filter This filter calls a defined URL and saves the answer into a specified field.

Constant Summary collapse

VALID_VERBS =
['GET', 'HEAD', 'PATCH', 'DELETE', 'POST']

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/logstash/filters/http.rb', line 37

def filter(event)
  url_for_event = event.sprintf(@url)
  headers_sprintfed = sprintf_object(event, @headers)
  if body_format == "json"
    headers_sprintfed["content-type"] = "application/json"
  else
    headers_sprintfed["content-type"] = "text/plain"
  end
  query_sprintfed = sprintf_object(event, @query)
  body_sprintfed = sprintf_object(event, @body)
  # we don't need to serialize strings and numbers
  if @body_format == "json" && body_sprintfed.kind_of?(Enumerable)
    body_sprintfed = LogStash::Json.dump(body_sprintfed)
  end

  options = { :headers => headers_sprintfed, :query => query_sprintfed, :body => body_sprintfed }

  @logger.debug? && @logger.debug('processing request', :url => url_for_event, :headers => headers_sprintfed, :query => query_sprintfed)
  client_error = nil

  begin
    code, response_headers, response_body = request_http(@verb, url_for_event, options)
  rescue => e
    client_error = e
  end

  if client_error
    @logger.error('error during HTTP request',
                  :url => url_for_event, :body => body_sprintfed,
                  :client_error => client_error.message)
    @tag_on_request_failure.each { |tag| event.tag(tag) }
  elsif !code.between?(200, 299)
    @logger.error('error during HTTP request',
                  :url => url_for_event, :code => code,
                  :response => response_body)
    @tag_on_request_failure.each { |tag| event.tag(tag) }
  else
    @logger.debug? && @logger.debug('success received',
                                    :code => code, :body => response_body)
    process_response(response_body, response_headers, event)
    filter_matched(event)
  end
end

#registerObject



32
33
34
35
# File 'lib/logstash/filters/http.rb', line 32

def register
  # nothing to see here
  @verb = verb.downcase
end