Class: HeimdallApm::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/heimdall_apm/agent.rb

Overview

Main entry point for HeimdallApm. Only one instance is created per ruby process, and it manages the lifecycle of the monitoring

Constant Summary collapse

DEFAULT_PUSH_INTERVAL =
60
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Agent

Returns a new instance of Agent.



21
22
23
24
25
26
# File 'lib/heimdall_apm/agent.rb', line 21

def initialize(opts)
  @options            = opts
  @context            = ::HeimdallApm::AgentContext.new
  @background_thread  = nil
  @stopped            = false
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



19
20
21
# File 'lib/heimdall_apm/agent.rb', line 19

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/heimdall_apm/agent.rb', line 17

def options
  @options
end

Class Method Details

.instance(opts = {}) ⇒ Object



13
14
15
# File 'lib/heimdall_apm/agent.rb', line 13

def self.instance(opts = {})
  @@instance ||= self.new(opts)
end

Instance Method Details

#install(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/heimdall_apm/agent.rb', line 28

def install(options = {})
  context.config = ::HeimdallApm::Config.new

  if context.interactive?
    HeimdallApm.logger.info 'Preventing agent to start in interactive mode'
    return
  end

  if defined?(Sidekiq) && Sidekiq.server?
    # TODO: handle custom instrumentation disabling
    HeimdallApm.logger.info 'Preventing agent to start in sidekiq server'
    return
  end

  start(options)
end

#start(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/heimdall_apm/agent.rb', line 45

def start(options = {})
  return unless context.config.value('enabled')

  # TODO: use instruments manager
  require 'heimdall_apm/instruments/active_record'      if defined?(ActiveRecord)
  require 'heimdall_apm/instruments/action_controller'  if defined?(ActionController)
  require 'heimdall_apm/instruments/elasticsearch'      if defined?(Elasticsearch)

  if (options[:app])
    require 'heimdall_apm/instruments/middleware'
    # TODO: make the position configurable
    options[:app].config.middleware.insert_after Rack::Cors, HeimdallApm::Instruments::Middleware
  end

  # TODO: handle platform/webserver that don't handle this correctly
  at_exit { stop }

  context.started!
  @background_thread = Thread.new { background_run }
end

#stopObject



66
67
68
69
70
71
72
73
74
# File 'lib/heimdall_apm/agent.rb', line 66

def stop
  HeimdallApm.logger.info 'Stopping agent...'
  @stopped = true
  context.stopped!
  if @background_thread.alive?
    @background_thread.wakeup
    @background_thread.join
  end
end