Class: LogStash::Filters::ApiHammerRequest

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/api_hammer_request.rb

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/logstash/filters/api_hammer_request.rb', line 16

def filter(event)
  # discard the request status line for humans - always followed by json which we'll parse 
  col = /[\e\[\dm]*/.source
  #               begin  direction     status        method      path             time      end
  human_request = [/\A/, /[<>]/, /\s/, /\d+/, / : /, /\w+/, / /, /[^\e]+/, / @ /, /[^\e]+/, /\z/].map(&:source).join(col)
  event.cancel if event[@source] =~ /#{human_request}/

  begin
    parsed_message = JSON.parse(event[@source])
    if @consume
      # replace the source with a brief human-readable message
      bound = parsed_message['bound']
      dir = role == 'inbound' ? '<' : role == 'outbound' ? '>' : '*'
      status = parsed_message['response'] && parsed_message['response']['status']
      request_method = parsed_message['request'] && parsed_message['request']['method']
      request_uri = parsed_message['request'] && parsed_message['request']['uri']
      now_s = Time.now.strftime('%Y-%m-%d %H:%M:%S %Z')
      event[@source] = "#{dir} #{status} : #{request_method} #{request_uri} @ #{now_s}"
    end
  rescue JSON::ParserError
    nil
  end

  if parsed_message
    if parsed_message.is_a?(Hash)
      event.to_hash.update(parsed_message)
      if parsed_message['processing'].is_a?(Hash) && parsed_message['processing']['began_at'].is_a?(Integer)
        event['@timestamp'] = Time.at(parsed_message['processing']['began_at']).utc
      end
    else
      event['parsed_message'] = parsed_message
    end
  end
end

#registerObject



12
13
# File 'lib/logstash/filters/api_hammer_request.rb', line 12

def register
end