Class: Binnacle::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/binnacle/configuration.rb

Constant Summary collapse

DEFAULT_IGNORED_EXCEPTIONS =
[
  'ActiveRecord::RecordNotFound',
  'ActionController::RoutingError',
  'ActionController::InvalidAuthenticityToken',
  'CGI::Session::CookieStore::TamperedWithCookie',
  'ActionController::UnknownHttpMethod',
  'ActionController::UnknownAction',
  'AbstractController::ActionNotFound',
  'Mongoid::Errors::DocumentNotFound',
  'ActionController::UnknownFormat',
  'Sinatra::NotFound'
].map(&:freeze).freeze
DEFAULT_PORT =
'8080'
DEFAULT_BACKTRACE_FILTERS =
[
  lambda { |line| line.gsub(/^\.\//, "") },
  lambda { |line|
    Gem.path.each{ |path| line.sub!(/#{path}/, "[GEM_ROOT]") unless path.to_s.strip.empty? } if defined?(Gem)
    line
  },
  lambda { |line| line if line !~ %r{lib/binnacle} }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/binnacle/configuration.rb', line 80

def initialize
  if ENV['BINNACLE_ENDPOINT']
    self.endpoint    ||= ENV['BINNACLE_ENDPOINT'].include?(',') ? ENV['BINNACLE_ENDPOINT'].split(',') : ENV['BINNACLE_ENDPOINT']
  else
    if self.endpoint !~ /[^[:space:]]/
      self.endpoint = (1..6).to_a.sample(3).map { |n| "api#{n}.binnacle-api.io" }
    end
  end

  self.logging_channel ||= ENV['BINNACLE_APP_LOG_CHANNEL']
  self.error_channel   ||= ENV['BINNACLE_APP_ERR_CHANNEL']
  self.api_key     ||= ENV['BINNACLE_API_KEY']
  self.api_secret  ||= ENV['BINNACLE_API_SECRET']
  self.intercept_rails_logging = Configuration.set_boolean_flag_for(ENV['BINNACLE_RAILS_LOG'])
  self.rails_verbose_logging = Configuration.set_boolean_flag_for(ENV['BINNACLE_RAILS_LOG_VERBOSE'])
  self.report_exceptions = Configuration.set_boolean_flag_for(ENV['BINNACLE_REPORT_EXCEPTIONS'])
  self.ignored_exceptions ||= ENV['BINNACLE_IGNORED_EXCEPTIONS'] ? DEFAULT_IGNORED_EXCEPTIONS + ENV['BINNACLE_IGNORED_EXCEPTIONS'].split(',') : DEFAULT_IGNORED_EXCEPTIONS
  self.ignore_cascade_pass     ||= true
  self.asynch_logging = Configuration.set_boolean_flag_for(ENV['BINNACLE_RAILS_LOG_ASYNCH'], true)
  @encrypted = Configuration.set_boolean_flag_for(ENV['BINNACLE_ENCRYPTED'], true)

  self.url_whitelist_patterns ||= ENV['BINNACLE_HTTP_LOGGING_WHITELIST'] ? ENV['BINNACLE_HTTP_LOGGING_WHITELIST'].split(',') : []
  self.url_blacklist_patterns ||= ENV['BINNACLE_HTTP_LOGGING_BLACKLIST'] ? ENV['BINNACLE_HTTP_LOGGING_BLACKLIST'].split(',') : []
  @url_whitelist_pattern = /.*/
  @url_blacklist_pattern = nil

  self.log_binnacle_signals = Configuration.set_boolean_flag_for(ENV['BINNACLE_LOG_SIGNALS'])

  prepare!
end

Instance Attribute Details

#api_keyObject

An approved publisher API key for the App (BINNACLE_API_KEY)



41
42
43
# File 'lib/binnacle/configuration.rb', line 41

def api_key
  @api_key
end

#api_secretObject

The API secret for the given API key (BINNACLE_API_SECRET)



44
45
46
# File 'lib/binnacle/configuration.rb', line 44

def api_secret
  @api_secret
end

#asynch_loggingObject

Whether to log asynchronoushly via the Ruby logger



66
67
68
# File 'lib/binnacle/configuration.rb', line 66

def asynch_logging
  @asynch_logging
end

#encryptedObject

Whether to make the requests over HTTPS, default is HTTPS



63
64
65
# File 'lib/binnacle/configuration.rb', line 63

def encrypted
  @encrypted
end

#endpointObject

The Binnacle Endpoint (BINNACLE_ENDPOINT) single IP or Array of IPs



29
30
31
# File 'lib/binnacle/configuration.rb', line 29

def endpoint
  @endpoint
end

#error_channelObject

The application error Binnacle Channel (BINNACLE_APP_ERR_CHANNEL)



38
39
40
# File 'lib/binnacle/configuration.rb', line 38

def error_channel
  @error_channel
end

#ignore_actions(actions) ⇒ Object

Set conditions for events that should be ignored

Currently supported formats are:

- A single string representing a controller action, e.g. 'users#sign_in'
- An array of strings representing controller actions
- An object that responds to call with an event argument and returns
  true if the event should be ignored.

The action ignores are given to ‘ignore_actions’. The callable ignores are given to ‘ignore’. Both methods can be called multiple times, which just adds more ignore conditions to a list that is checked before logging.



254
255
256
257
258
259
# File 'lib/binnacle/configuration.rb', line 254

def ignore_actions(actions)
  ignore(lamba do |event|
    params = event.payload[:params]
    Array(actions).include?("#{params['controller']}##{params['action']}")
  end)
end

#ignore_cascade_passObject

Whether to skip reporting exceptions where the headers is set to ‘pass’. In Rails typically it means route was not found (404 error).



60
61
62
# File 'lib/binnacle/configuration.rb', line 60

def ignore_cascade_pass
  @ignore_cascade_pass
end

#ignored_exceptionsObject

Exceptions that do not get reported to Binnacle (BINNACLE_IGNORED_EXCEPTIONS)



56
57
58
# File 'lib/binnacle/configuration.rb', line 56

def ignored_exceptions
  @ignored_exceptions
end

#intercept_rails_loggingObject

Whether to redirect rails logging to Binnacle (BINNACLE_RAILS_LOG)



47
48
49
# File 'lib/binnacle/configuration.rb', line 47

def intercept_rails_logging
  @intercept_rails_logging
end

#log_binnacle_signalsObject

Returns the value of attribute log_binnacle_signals.



77
78
79
# File 'lib/binnacle/configuration.rb', line 77

def log_binnacle_signals
  @log_binnacle_signals
end

#logging_channelObject

The application logger Binnacle Channel (BINNACLE_APP_LOG_CHANNEL)



35
36
37
# File 'lib/binnacle/configuration.rb', line 35

def logging_channel
  @logging_channel
end

#portObject

The Binnacle Endpoint PORT (BINNACLE_PORT), defaults to 8080 if not encrypted



32
33
34
# File 'lib/binnacle/configuration.rb', line 32

def port
  @port
end

#rails_verbose_loggingObject

Whether to pipe Rails logs as they are (verbose as shit) or just grab action_controller events and single line them (BINNACLE_RAILS_LOG_VERBOSE)



50
51
52
# File 'lib/binnacle/configuration.rb', line 50

def rails_verbose_logging
  @rails_verbose_logging
end

#report_exceptionsObject

Whether to report exceptions to Binnacle (BINNACLE_REPORT_EXCEPTIONS)



53
54
55
# File 'lib/binnacle/configuration.rb', line 53

def report_exceptions
  @report_exceptions
end

#url_blacklist_patternObject (readonly)

Returns the value of attribute url_blacklist_pattern.



75
76
77
# File 'lib/binnacle/configuration.rb', line 75

def url_blacklist_pattern
  @url_blacklist_pattern
end

#url_blacklist_patternsObject

Returns the value of attribute url_blacklist_patterns.



73
74
75
# File 'lib/binnacle/configuration.rb', line 73

def url_blacklist_patterns
  @url_blacklist_patterns
end

#url_whitelist_patternObject (readonly)

Returns the value of attribute url_whitelist_pattern.



74
75
76
# File 'lib/binnacle/configuration.rb', line 74

def url_whitelist_pattern
  @url_whitelist_pattern
end

#url_whitelist_patternsObject

HTTP outgoing logging options



72
73
74
# File 'lib/binnacle/configuration.rb', line 72

def url_whitelist_patterns
  @url_whitelist_patterns
end

Class Method Details

.set_boolean_flag_for(value, default = false) ⇒ Object



234
235
236
# File 'lib/binnacle/configuration.rb', line 234

def self.set_boolean_flag_for(value, default = false)
  !value.nil? ? value.downcase == 'true' : default
end

Instance Method Details

#build_url(ip_or_host) ⇒ Object



160
161
162
# File 'lib/binnacle/configuration.rb', line 160

def build_url(ip_or_host)
  ["#{protocol}://#{ip_or_host}", port].compact.join(":")
end

#can_setup_logger?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/binnacle/configuration.rb', line 136

def can_setup_logger?
  !self.logging_channel.nil?
end

#encrypted?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/binnacle/configuration.rb', line 156

def encrypted?
  self.encrypted
end

#ignore(test) ⇒ Object



265
266
267
# File 'lib/binnacle/configuration.rb', line 265

def ignore(test)
  ignore_tests.push(test) if test
end

#ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/binnacle/configuration.rb', line 273

def ignore?(event)
  ignore_tests.any? { |ignore_test| ignore_test.call(event) }
end

#ignore_cascade_pass?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/binnacle/configuration.rb', line 152

def ignore_cascade_pass?
  self.ignore_cascade_pass
end

#ignore_nothingObject



269
270
271
# File 'lib/binnacle/configuration.rb', line 269

def ignore_nothing
  @ignore_tests = []
end

#ignore_testsObject



261
262
263
# File 'lib/binnacle/configuration.rb', line 261

def ignore_tests
  @ignore_tests ||= []
end

#intercept_rails_logging?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/binnacle/configuration.rb', line 140

def intercept_rails_logging?
  self.intercept_rails_logging && !self.logging_channel.nil?
end

#prepare!Object



111
112
113
114
115
116
# File 'lib/binnacle/configuration.rb', line 111

def prepare!
  set_default_port
  set_urls
  set_blacklist_patterns
  set_whitelist_patterns
end

#protocolObject



164
165
166
# File 'lib/binnacle/configuration.rb', line 164

def protocol
  self.encrypted? ? 'https' : 'http'
end

#rails_verbose_logging?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/binnacle/configuration.rb', line 144

def rails_verbose_logging?
  self.rails_verbose_logging
end

#ready?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/binnacle/configuration.rb', line 128

def ready?
  urls? && self.api_key && self.api_secret
end

#set_blacklist_patternsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/binnacle/configuration.rb', line 174

def set_blacklist_patterns
  blacklist_patterns = []

  # don't log binnacle's posts
  unless self.log_binnacle_signals
    if @urls.is_a?(Array)
      @urls.each do |url|
        blacklist_patterns << /#{url}/ if url
      end
    elsif @urls
      blacklist_patterns << /#{@urls}/
    end
  end

  self.url_blacklist_patterns.each do |pattern|
    blacklist_patterns << pattern
  end

  unless blacklist_patterns.empty?
    @url_blacklist_pattern = Regexp.union(blacklist_patterns)
  end
end

#set_default_portObject



238
239
240
# File 'lib/binnacle/configuration.rb', line 238

def set_default_port
  self.port ||= ENV['BINNACLE_PORT'] || (self.encrypted? ? nil : DEFAULT_PORT)
end

#set_urlsObject



168
169
170
171
172
# File 'lib/binnacle/configuration.rb', line 168

def set_urls
  if self.endpoint && (self.endpoint.is_a?(Array) ? !self.endpoint.empty? : (!self.endpoint !~ /[^[:space:]]/))
    @urls = self.endpoint.is_a?(Array) ? self.endpoint.map { |ep| build_url(ep) } : build_url(endpoint)
  end
end

#set_whitelist_patternsObject



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/binnacle/configuration.rb', line 197

def set_whitelist_patterns
  whitelist_patterns = []

  self.url_whitelist_patterns.each do |pattern|
    whitelist_patterns << pattern
  end

  unless whitelist_patterns.empty?
    @whitelist_pattern = Regexp.union(whitelist_patterns)
  end
end

#to_sObject



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/binnacle/configuration.rb', line 220

def to_s
  [ :endpoint,
    :logging_channel,
    :error_channel,
    :api_key,
    :api_secret,
    :intercept_rails_logging,
    :report_exceptions,
    :ignore_cascade_pass,
    :encrypted,
    :asynch_logging
  ].map { |m| "#{m}: #{self.send(m)}" }.join(', ')
end

#trap?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/binnacle/configuration.rb', line 148

def trap?
  !self.report_exceptions.nil? && !self.error_channel.nil?
end

#urlObject



118
119
120
121
122
# File 'lib/binnacle/configuration.rb', line 118

def url
  if @urls
    @urls.is_a?(Array) ? @urls.sample : @urls
  end
end

#urlsObject



124
125
126
# File 'lib/binnacle/configuration.rb', line 124

def urls
  @urls
end

#urls?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/binnacle/configuration.rb', line 132

def urls?
  (!@urls.nil? && !@urls.is_a?(Array)) || (@urls.is_a?(Array) && !@urls.empty?)
end