Class: LogStash::Filters::Http

Inherits:
Base
  • Object
show all
Extended by:
PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter
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', 'PUT']

Instance Method Summary collapse

Constructor Details

#initialize(*params) ⇒ Http

Returns a new instance of Http.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/logstash/filters/http.rb', line 42

def initialize(*params)
  super

  @target_body ||= ecs_select[disabled: '[body]', v1: false]
  if @target_body.eql? false # user needs to specify target in ECS mode
    @logger.error missing_config_message(:target_body)
    raise LogStash::ConfigurationError.new "Wrong configuration (in ecs_compatibility mode #{ecs_compatibility.inspect})"
  end

  @target_headers ||= ecs_select[disabled: '[headers]', v1: '[@metadata][filter][http][response][headers]']
end

Instance Method Details

#filter(event) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/logstash/filters/http.rb', line 59

def filter(event)
  url_for_event = event.sprintf(@url)
  headers_sprintfed = sprintf_object(event, @headers)
  if !headers_sprintfed.key?('content-type') && !headers_sprintfed.key?('Content-Type')
    headers_sprintfed['content-type'] = @body_format == "json" ? "application/json" : "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,
                  :headers => response_headers,
                  :response => response_body)
    @tag_on_request_failure.each { |tag| event.tag(tag) }
  else
    @logger.debug? && @logger.debug('success received',
                                    :code => code, :headers => response_headers, :body => response_body)
    process_response(response_body, response_headers, event)
    filter_matched(event)
  end
end

#registerObject



54
55
56
57
# File 'lib/logstash/filters/http.rb', line 54

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