Class: LogStash::Filters::JsonSchema

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

Overview

This filter will validate the message against a json schema provided in the configuration.

It adds a jsonschemafailure tag if the message does not validate

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/logstash/filters/json_schema.rb', line 25

def filter(event)
  body = event.get("message")
  begin
    if !JSON::Validator.validate(@schema, body)
      tag_as_schema_failure(event)
    end
  rescue Exception => e
    logger.error("Exception thrown while validating entry", { "schema" => @schema, "log entry" => body, "exception" => e, "trace" => e.backtrace.join(";")})
    tag_as_schema_failure(event)
  end

  # filter_matched should go in the last line of our successful code
  filter_matched(event)
end

#registerObject



20
21
22
# File 'lib/logstash/filters/json_schema.rb', line 20

def register
  # Add instance variables 
end

#tag_as_schema_failure(event) ⇒ Object

def filter



40
41
42
43
44
45
# File 'lib/logstash/filters/json_schema.rb', line 40

def tag_as_schema_failure(event)
  tags = event.get("tags")
  tags = [] if !tags
  tags.push("jsonschemafailure")
  event.set("tags", tags)
end