Class: ProfileIt::Agent

Inherits:
Object
  • Object
show all
Includes:
Logging, Reporting
Defined in:
lib/profile_it/agent.rb,
lib/profile_it/agent/logging.rb,
lib/profile_it/agent/reporting.rb

Overview

The agent gathers performance data from a Ruby application. One Agent instance is created per-Ruby process.

Each Agent object creates a worker thread (unless monitoring is disabled or we’re forking). The worker thread wakes up every Agent#period, merges in-memory metrics w/those saved to disk, saves the merged data to disk, and sends it to the profile_it server.

Defined Under Namespace

Modules: Logging, Reporting

Constant Summary collapse

HTTP_HEADERS =

Headers passed up with all API requests.

{ "Agent-Hostname" => Socket.gethostname }
@@instance =

see self.instance

nil

Constants included from Reporting

Reporting::CA_FILE, Reporting::VERIFY_MODE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reporting

#build_http, #checkin_uri, #post, #request, #send_profile

Methods included from Logging

#apply_log_format, #init_logger, #log_level, #log_path

Constructor Details

#initialize(options = {}) ⇒ Agent

Note - this doesn’t start instruments or the worker thread. This is handled via #start as we don’t want to start the worker thread or install instrumentation if (1) disabled for this environment (2) a worker thread shouldn’t be started (when forking).



31
32
33
34
35
36
37
# File 'lib/profile_it/agent.rb', line 31

def initialize(options = {})
  @started = false
  @options ||= options
  @store = ProfileIt::Store.new
  @config = ProfileIt::Config.new(options[:config_path])
  @metric_lookup = Hash.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/profile_it/agent.rb', line 15

def config
  @config
end

#environmentObject

Returns the value of attribute environment.



16
17
18
# File 'lib/profile_it/agent.rb', line 16

def environment
  @environment
end

#log_fileObject

path to the log file



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

def log_file
  @log_file
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#metric_lookupObject

Hash used to lookup metric ids based on their name and scope



21
22
23
# File 'lib/profile_it/agent.rb', line 21

def metric_lookup
  @metric_lookup
end

#optionsObject

options passed to the agent when #start is called.



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

def options
  @options
end

#storeObject

Accessors below are for associated classes



14
15
16
# File 'lib/profile_it/agent.rb', line 14

def store
  @store
end

Class Method Details

.instance(options = {}) ⇒ Object

All access to the agent is thru this class method to ensure multiple Agent instances are not initialized per-Ruby process.



24
25
26
# File 'lib/profile_it/agent.rb', line 24

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

Instance Method Details

#gem_rootObject



72
73
74
# File 'lib/profile_it/agent.rb', line 72

def gem_root
  File.expand_path(File.join("..","..",".."), __FILE__)
end

#load_instrumentsObject

Loads the instrumention logic.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/profile_it/agent.rb', line 77

def load_instruments
  case environment.framework
  when :rails
    require File.expand_path(File.join(File.dirname(__FILE__),'instruments/rails/action_controller_instruments.rb'))
  when :rails3_or_4
    require File.expand_path(File.join(File.dirname(__FILE__),'instruments/rails3_or_4/action_controller_instruments.rb'))
  end
  require File.expand_path(File.join(File.dirname(__FILE__),'instruments/active_record_instruments.rb'))
  require File.expand_path(File.join(File.dirname(__FILE__),'instruments/net_http.rb'))
  require File.expand_path(File.join(File.dirname(__FILE__),'instruments/moped_instruments.rb'))
  require File.expand_path(File.join(File.dirname(__FILE__),'instruments/mongoid_instruments.rb'))
rescue
  logger.warn "Exception loading instruments:"
  logger.warn $!.message
  logger.warn $!.backtrace
end

#start(options = {}) ⇒ Object

This is called via ProfileIt::Agent.instance.start when ProfileIt is required in a Ruby application. It initializes the agent and starts the worker thread (if appropiate).



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

def start(options = {})
  @options.merge!(options)
  init_logger
  logger.info "Attempting to start profileit.io [#{ProfileIt::VERSION}] on [#{Socket.gethostname}] reporting to #{config.settings['host']}"
  if !config.settings['profile']
    logger.warn "Profiling isn't enabled for the [#{environment.env}] environment."
    return false
  elsif !config.settings['key']
    logger.warn "The config file doesn't specify an account key. See https://profileit.io/help for a install instructions."
    return false
  # elsif !environment.app_server
  #   logger.warn "Couldn't find a supported app server. Not starting agent."
  #   return false
  elsif started?
    logger.warn "Already started profileit.io."
    return false
  end
  @started = true
  logger.info "Starting profiling. Framework [#{environment.framework}] App Server [#{environment.app_server}]."
  start_instruments
  logger.info "ProfileIt [#{ProfileIt::VERSION}] Initialized"
end

#start_instrumentsObject

Injects instruments into the Ruby application.



95
96
97
98
# File 'lib/profile_it/agent.rb', line 95

def start_instruments
  logger.debug "Installing instrumentation"
  load_instruments
end

#started?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/profile_it/agent.rb', line 68

def started?
  @started
end