Class: Appsignal::EventFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/event_formatter.rb,
lib/appsignal/event_formatter/sequel/sql_formatter.rb,
lib/appsignal/event_formatter/moped/query_formatter.rb,
lib/appsignal/event_formatter/faraday/request_formatter.rb,
lib/appsignal/event_formatter/net_http/request_formatter.rb,
lib/appsignal/event_formatter/active_record/sql_formatter.rb,
lib/appsignal/event_formatter/action_view/render_formatter.rb,
lib/appsignal/event_formatter/elastic_search/search_formatter.rb,
lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter.rb,
lib/appsignal/event_formatter/active_record/instantiation_formatter.rb

Overview

Keeps track of formatters for types event that we can use to get the title and body of an event. Formatters should inherit from this class and implement a format(payload) method which returns an array with the title and body.

When implementing a formatter remember that it cannot keep separate state per event, the same object will be called intermittently in a threaded environment. So only keep global configuration as state and pass the payload around as an argument if you need to use helper methods.

Defined Under Namespace

Modules: ActionView, ActiveRecord, ElasticSearch, Faraday, MongoRubyDriver, Moped, NetHttp, Sequel

Class Method Summary collapse

Class Method Details

.format(name, payload) ⇒ Object


57
58
59
60
# File 'lib/appsignal/event_formatter.rb', line 57

def format(name, payload)
  formatter = formatters[name]
  formatter.format(payload) unless formatter.nil?
end

.formatter_classesObject


17
18
19
# File 'lib/appsignal/event_formatter.rb', line 17

def formatter_classes
  @@formatter_classes ||= {}
end

.formattersObject


13
14
15
# File 'lib/appsignal/event_formatter.rb', line 13

def formatters
  @@formatters ||= {}
end

.initialize_formattersObject


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/appsignal/event_formatter.rb', line 40

def initialize_formatters
  formatter_classes.each do |name, formatter|
    begin
      format_method = formatter.instance_method(:format)
      if format_method && format_method.arity == 1
        formatters[name] = formatter.new
      else
        raise "#{f} does not have a format(payload) method"
      end
    rescue Exception => ex
      formatter_classes.delete(name)
      formatters.delete(name)
      Appsignal.logger.debug("'#{ex.message}' when initializing #{name} event formatter")
    end
  end
end

.register(name, formatter = self) ⇒ Object


21
22
23
# File 'lib/appsignal/event_formatter.rb', line 21

def register(name, formatter=self)
  formatter_classes[name] = formatter
end

.registered?(name, klass = nil) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
35
36
37
38
# File 'lib/appsignal/event_formatter.rb', line 32

def registered?(name, klass=nil)
  if klass
    formatter_classes[name] == klass
  else
    formatter_classes.include?(name)
  end
end

.unregister(name, formatter = self) ⇒ Object


25
26
27
28
29
30
# File 'lib/appsignal/event_formatter.rb', line 25

def unregister(name, formatter=self)
  if formatter_classes[name] == formatter
    formatter_classes.delete(name)
    formatters.delete(name)
  end
end