Class: Haproxy2Rpm::Rpm

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxy2rpm/rpm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Rpm

Returns a new instance of Rpm.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/haproxy2rpm/rpm.rb', line 7

def initialize(options = {})
  agent_options = {:log => Haproxy2Rpm.logger}
  agent_options[:app_name] = options[:app_name] if options[:app_name]
  agent_options[:env] = options[:env] if options[:env]
  NewRelic::Agent.manual_start agent_options
  @stats_engine = NewRelic::Agent.agent.stats_engine
  @queue_time_stats_engine = @stats_engine.get_stats_no_scope('WebFrontend/QueueTime')
  @routes = options[:routes] || []
  if options[:config_file]
    Haproxy2Rpm.logger.info "Reading configuration from: #{options[:config_file]}"
    content = File.open(options[:config_file]){|f| f.read }
    instance_eval(content, options[:config_file])
  end
end

Instance Attribute Details

#default_routeObject



26
27
28
# File 'lib/haproxy2rpm/rpm.rb', line 26

def default_route
  @default_route ||= '/default'
end

#queue_time_stats_engineObject

Returns the value of attribute queue_time_stats_engine.



4
5
6
# File 'lib/haproxy2rpm/rpm.rb', line 4

def queue_time_stats_engine
  @queue_time_stats_engine
end

#routesObject

Returns the value of attribute routes.



4
5
6
# File 'lib/haproxy2rpm/rpm.rb', line 4

def routes
  @routes
end

#stats_engineObject

Returns the value of attribute stats_engine.



4
5
6
# File 'lib/haproxy2rpm/rpm.rb', line 4

def stats_engine
  @stats_engine
end

Instance Method Details

#configObject



22
23
24
# File 'lib/haproxy2rpm/rpm.rb', line 22

def config
  self
end

#default_message_parserObject



39
40
41
42
43
# File 'lib/haproxy2rpm/rpm.rb', line 39

def default_message_parser
  lambda do |line|
    LineParser.new(line)
  end
end

#default_request_recorderObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/haproxy2rpm/rpm.rb', line 54

def default_request_recorder
  lambda do |request|
    rpm_number_unit = 1000.0
            
    params = {
      'metric' => "Controller#{route_for(request.http_path)}"
    }

    if request.is_error?
      params['is_error'] = true
      params['error_message'] = "#{request.uri} : Status code #{request.status_code}"
    end

    record_transaction(request.tr / rpm_number_unit, params)
    Haproxy2Rpm.logger.debug "RECORDING (transaction) #{request.http_path}: #{params.inspect}"
    result = queue_time_stats_engine.record_data_point(request.tw / rpm_number_unit)
    Haproxy2Rpm.logger.debug "RECORDING (data point): wait time #{request.tw}, #{result.inspect}"
  end
end

#message_parserObject



45
46
47
# File 'lib/haproxy2rpm/rpm.rb', line 45

def message_parser
  @message_parser ||= default_message_parser
end

#message_parser=(block) ⇒ Object



49
50
51
52
# File 'lib/haproxy2rpm/rpm.rb', line 49

def message_parser=(block)
  Haproxy2Rpm.logger.debug "Installing custom parser"
  @message_parser = block
end

#process_and_send(line) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/haproxy2rpm/rpm.rb', line 30

def process_and_send(line)
  begin
    message = message_parser.call(line)
  	request_recorder.call(message)
  rescue URI::InvalidURIError
    Haproxy2Rpm.logger.warn "Parser returned an empty message from line #{line}"
  end
end

#record_transaction(*args) ⇒ Object



83
84
85
# File 'lib/haproxy2rpm/rpm.rb', line 83

def record_transaction(*args)
  NewRelic::Agent.record_transaction(*args)
end

#request_recorderObject



74
75
76
# File 'lib/haproxy2rpm/rpm.rb', line 74

def request_recorder
  @request_recorder ||= default_request_recorder
end

#request_recorder=(block) ⇒ Object



78
79
80
81
# File 'lib/haproxy2rpm/rpm.rb', line 78

def request_recorder=(block)
  Haproxy2Rpm.logger.debug "Installing custom recorder"
  @request_recorder = block
end