Module: Appsignal

Defined in:
lib/appsignal/rack/streaming_listener.rb,
lib/appsignal.rb,
lib/appsignal/cli.rb,
lib/appsignal/hooks.rb,
lib/appsignal/utils.rb,
lib/appsignal/config.rb,
lib/appsignal/marker.rb,
lib/appsignal/version.rb,
lib/appsignal/extension.rb,
lib/appsignal/auth_check.rb,
lib/appsignal/hooks/puma.rb,
lib/appsignal/hooks/rake.rb,
lib/appsignal/subscriber.rb,
lib/appsignal/cli/install.rb,
lib/appsignal/hooks/redis.rb,
lib/appsignal/transaction.rb,
lib/appsignal/transmitter.rb,
lib/appsignal/cli/diagnose.rb,
lib/appsignal/hooks/sequel.rb,
lib/appsignal/hooks/sidekiq.rb,
lib/appsignal/hooks/unicorn.rb,
lib/appsignal/hooks/net_http.rb,
lib/appsignal/event_formatter.rb,
lib/appsignal/hooks/celluloid.rb,
lib/appsignal/hooks/passenger.rb,
lib/appsignal/hooks/shoryuken.rb,
lib/appsignal/params_sanitizer.rb,
lib/appsignal/hooks/delayed_job.rb,
lib/appsignal/integrations/grape.rb,
lib/appsignal/integrations/resque.rb,
lib/appsignal/cli/notify_of_deploy.rb,
lib/appsignal/integrations/railtie.rb,
lib/appsignal/update_active_support.rb,
lib/appsignal/hooks/mongo_ruby_driver.rb,
lib/appsignal/js_exception_transaction.rb,
lib/appsignal/rack/js_exception_catcher.rb,
lib/appsignal/rack/rails_instrumentation.rb,
lib/appsignal/rack/generic_instrumentation.rb,
lib/appsignal/rack/sinatra_instrumentation.rb,
lib/appsignal/integrations/mongo_ruby_driver.rb,
lib/appsignal/integrations/resque_active_job.rb,
lib/appsignal/integrations/delayed_job_plugin.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/integrations/capistrano/capistrano_2_tasks.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,
ext/appsignal_extension.c

Overview

Appsignal module that tracks exceptions in Streaming rack responses.

Defined Under Namespace

Modules: Grape, Integrations, Rack, UpdateActiveSupport, Utils Classes: AuthCheck, CLI, Capistrano, Config, EventFormatter, Extension, Hooks, JSExceptionTransaction, Marker, ParamsSanitizer, ParamsSanitizerCopy, ParamsSanitizerCopyScrub, ParamsSanitizerDestructive, ParamsSanitizerDestructiveScrub, StreamWrapper, Subscriber, Transaction, Transmitter

Constant Summary collapse

VERSION =
'1.2.2'
SQL_BODY_FORMAT =
1

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.agentObject

Returns the value of attribute agent


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

def agent
  @agent
end

.configObject

Returns the value of attribute config


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

def config
  @config
end

.extension_loadedObject

Returns the value of attribute extension_loaded


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

def extension_loaded
  @extension_loaded
end

.in_memory_logObject

Returns the value of attribute in_memory_log


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

def in_memory_log
  @in_memory_log
end

.loggerObject

Returns the value of attribute logger


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

def logger
  @logger
end

.subscriberObject

Returns the value of attribute subscriber


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

def subscriber
  @subscriber
end

Class Method Details

.active?Boolean

Returns:

  • (Boolean)

247
248
249
# File 'lib/appsignal.rb', line 247

def active?
  config && config.active? && extension_loaded?
end

.add_distribution_value(key, value) ⇒ Object


204
205
206
207
208
# File 'lib/appsignal.rb', line 204

def add_distribution_value(key, value)
  Appsignal::Extension.add_distribution_value(key, value.to_f)
rescue RangeError
  Appsignal.logger.warn("Distribution value #{value} for key '#{key}' is too big")
end

.extension_loaded?Boolean

Returns:

  • (Boolean)

243
244
245
# File 'lib/appsignal.rb', line 243

def extension_loaded?
  !!@extension_loaded
end

.extensionsObject


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

def extensions
  @extensions ||= []
end

.forkedObject


80
81
82
83
84
85
86
# File 'lib/appsignal.rb', line 80

def forked
  return unless active?
  Appsignal.start_logger
  logger.debug('Forked process, resubscribing and restarting extension')
  Appsignal::Extension.start
  @subscriber.resubscribe
end

.get_server_state(key) ⇒ Object


88
89
90
# File 'lib/appsignal.rb', line 88

def get_server_state(key)
  Appsignal::Extension::get_server_state(key)
end

.increment_counter(key, value = 1) ⇒ Object


198
199
200
201
202
# File 'lib/appsignal.rb', line 198

def increment_counter(key, value=1)
  Appsignal::Extension.increment_counter(key, value)
rescue RangeError
  Appsignal.logger.warn("Counter value #{value} for key '#{key}' is too big")
end

.initialize_extensionsObject


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

def initialize_extensions
  Appsignal.logger.debug('Initializing extensions')
  extensions.each do |extension|
    Appsignal.logger.debug("Initializing #{extension}")
    extension.initializer
  end
end

.is_ignored_action?(action) ⇒ Boolean

Returns:

  • (Boolean)

256
257
258
# File 'lib/appsignal.rb', line 256

def is_ignored_action?(action)
  Appsignal.config[:ignore_actions].include?(action)
end

.is_ignored_error?(error) ⇒ Boolean Also known as: is_ignored_exception?

Returns:

  • (Boolean)

251
252
253
# File 'lib/appsignal.rb', line 251

def is_ignored_error?(error)
  Appsignal.config[:ignore_errors].include?(error.class.name)
end

.listen_for_error(&block) ⇒ Object Also known as: listen_for_exception


137
138
139
140
141
142
# File 'lib/appsignal.rb', line 137

def listen_for_error(&block)
  yield
rescue => error
  send_error(error)
  raise error
end

.log_formatterObject


218
219
220
221
222
# File 'lib/appsignal.rb', line 218

def log_formatter
    proc do |severity, datetime, progname, msg|
      "[#{datetime.strftime('%Y-%m-%dT%H:%M:%S')} (process) ##{Process.pid}][#{severity}] #{msg}\n"
    end
end

.monitor_single_transaction(name, env = {}, &block) ⇒ Object

Monitor a transaction, stop Appsignal and wait for this single transaction to be flushed.

Useful for cases such as Rake tasks and Resque-like systems where a process is forked and immediately exits after the transaction finishes.


131
132
133
134
135
# File 'lib/appsignal.rb', line 131

def monitor_single_transaction(name, env={}, &block)
  monitor_transaction(name, env, &block)
ensure
  stop
end

.monitor_transaction(name, env = {}) ⇒ Object

Wrap a transaction with appsignal monitoring.


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/appsignal.rb', line 93

def monitor_transaction(name, env={})
  unless active?
    return yield
  end

  if name.start_with?('perform_job'.freeze)
    namespace = Appsignal::Transaction::BACKGROUND_JOB
    request   = Appsignal::Transaction::GenericRequest.new(env)
  elsif name.start_with?('process_action'.freeze)
    namespace = Appsignal::Transaction::HTTP_REQUEST
    request   = ::Rack::Request.new(env)
  else
    logger.error("Unrecognized name '#{name}'") and return
  end
  transaction = Appsignal::Transaction.create(
    SecureRandom.uuid,
    namespace,
    request
  )
  begin
    ActiveSupport::Notifications.instrument(name) do
      yield
    end
  rescue => error
    transaction.set_error(error)
    raise error
  ensure
    transaction.set_http_or_background_action(request.env)
    transaction.set_http_or_background_queue_start
    Appsignal::Transaction.complete_current!
  end
end

.send_error(error, tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST) ⇒ Object Also known as: send_exception


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/appsignal.rb', line 145

def send_error(error, tags=nil, namespace=Appsignal::Transaction::HTTP_REQUEST)
  return if !active? || is_ignored_error?(error)
  unless error.is_a?(Exception)
    logger.error('Can\'t send error, given value is not an exception')
    return
  end
  transaction = Appsignal::Transaction.new(
    SecureRandom.uuid,
    namespace,
    Appsignal::Transaction::GenericRequest.new({})
  )
  transaction.set_tags(tags) if tags
  transaction.set_error(error)
  transaction.complete
end

.set_error(exception) ⇒ Object Also known as: set_exception, add_exception


162
163
164
165
166
167
168
# File 'lib/appsignal.rb', line 162

def set_error(exception)
  return if !active? ||
            Appsignal::Transaction.current.nil? ||
            exception.nil? ||
            is_ignored_error?(exception)
  Appsignal::Transaction.current.set_error(exception)
end

.set_gauge(key, value) ⇒ Object


180
181
182
183
184
# File 'lib/appsignal.rb', line 180

def set_gauge(key, value)
  Appsignal::Extension.set_gauge(key, value.to_f)
rescue RangeError
  Appsignal.logger.warn("Gauge value #{value} for key '#{key}' is too big")
end

.set_host_gauge(key, value) ⇒ Object


186
187
188
189
190
# File 'lib/appsignal.rb', line 186

def set_host_gauge(key, value)
  Appsignal::Extension.set_host_gauge(key, value.to_f)
rescue RangeError
  Appsignal.logger.warn("Host gauge value #{value} for key '#{key}' is too big")
end

.set_process_gauge(key, value) ⇒ Object


192
193
194
195
196
# File 'lib/appsignal.rb', line 192

def set_process_gauge(key, value)
  Appsignal::Extension.set_process_gauge(key, value.to_f)
rescue RangeError
  Appsignal.logger.warn("Process gauge value #{value} for key '#{key}' is too big")
end

.startObject


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/appsignal.rb', line 32

def start
  unless extension_loaded?
    logger.info('Not starting appsignal, extension is not loaded')
    return
  else
    logger.debug('Starting appsignal')
  end

  unless @config
    @config = Config.new(
      Dir.pwd,
      ENV['APPSIGNAL_APP_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV']
    )
  end

  if config.valid?
    if config[:debug]
      logger.level = Logger::DEBUG
    else
      logger.level = Logger::INFO
    end
    if config.active?
      logger.info("Starting AppSignal #{Appsignal::VERSION} (#{$0}, Ruby #{RUBY_VERSION}, #{RUBY_PLATFORM})")
      config.write_to_environment
      Appsignal::Extension.start
      Appsignal::Hooks.load_hooks
      Appsignal::EventFormatter.initialize_formatters
      initialize_extensions
      Appsignal::Extension.install_allocation_event_hook if config[:enable_allocation_tracking]
      Appsignal::Extension.install_gc_event_hooks if config[:enable_gc_instrumentation]
      @subscriber = Appsignal::Subscriber.new
    else
      logger.info("Not starting, not active for #{config.env}")
    end
  else
    logger.error('Not starting, no valid config for this environment')
  end
end

.start_logger(path_arg = nil) ⇒ Object


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/appsignal.rb', line 224

def start_logger(path_arg=nil)
  path = Appsignal.config ? Appsignal.config.log_file_path : nil
  if path && !ENV['DYNO']
    @logger = Logger.new(path)
    @logger.formatter = log_formatter
  else
    @logger = Logger.new($stdout)
    @logger.formatter = lambda do |severity, datetime, progname, msg|
      "appsignal: #{msg}\n"
    end
  end
  @logger.level = Logger::INFO
  @logger << @in_memory_log.string if @in_memory_log

  if path_arg
    @logger.info('Setting the path in start_logger has no effect anymore, set it in the config instead')
  end
end

.stop(called_by = nil) ⇒ Object


71
72
73
74
75
76
77
78
# File 'lib/appsignal.rb', line 71

def stop(called_by=nil)
  if called_by
    logger.debug("Stopping appsignal (#{called_by})")
  else
    logger.debug('Stopping appsignal')
  end
  Appsignal::Extension.stop
end

.tag_request(params = {}) ⇒ Object Also known as: tag_job


172
173
174
175
176
177
# File 'lib/appsignal.rb', line 172

def tag_request(params={})
  return unless active?
  transaction = Appsignal::Transaction.current
  return false unless transaction
  transaction.set_tags(params)
end

.without_instrumentationObject

Convenience method for skipping instrumentations around a block of code.

Since:

  • 0.8.7


263
264
265
266
267
268
# File 'lib/appsignal.rb', line 263

def without_instrumentation
  Appsignal::Transaction.current.pause! if Appsignal::Transaction.current
  yield
ensure
  Appsignal::Transaction.current.resume! if Appsignal::Transaction.current
end