Skip to content

Commit

Permalink
make send_metrics class method
Browse files Browse the repository at this point in the history
  • Loading branch information
shifi committed Jan 22, 2013
1 parent ef2a4f9 commit 7f5eebf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/airbrake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def build_lookup_hash_for(exception, options = {})

private

at_exit { Airbrake::Metrics.send_metrics if defined?(Airbrake::Metrics) }

def send_notice(notice)
if configuration.public?
if configuration.async?
Expand Down
27 changes: 10 additions & 17 deletions lib/airbrake/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ class Metrics

def initialize(app)
@app = app
@start_time = Time.now
@@start_time = Time.now
@@all_requests = []
@hash = {}
@@hash = {}
@@duration_of_requests = 0
@exceptions = Airbrake.configuration.exceptions
end

def call(env)

# every hour send data to Airbrake
if (Time.now - @start_time) >= 3600
send_metrics
if (Time.now - @@start_time) >= 3600
Metrics.send_metrics
end

time = Time.now.getutc.strftime("%Y-%m-%d at %H:%M UTC")

# clear hash params if new minute
clear_hash_params unless @hash.has_key?(time)
clear_hash_params unless @@hash.has_key?(time)

@@all_requests << ::Rack::Request.new(env)

Expand All @@ -41,13 +41,12 @@ def call(env)

# TODO: track duration time separate for exceptions

@hash[time] = { "app_request_total_count" => @@all_requests.length,
@@hash[time] = {"app_request_total_count" => @@all_requests.length,
"app_request_error_count" => @exceptions.length,
"app_request_min_time" => "#{@@min_response_time.to_i}[us]",
"app_request_avg_time" => "#{@@average_response_time.to_f.round(3)}[us]",
"app_request_max_time" => "#{@@max_response_time.to_i}[us]",
"app_request_total_time" => "#{@@duration_of_requests.to_i}[us]"}
pp @hash
end

[status, headers.merge("Content-Type" => "text"), [body]]
Expand All @@ -56,7 +55,7 @@ def call(env)
end

def body
body = "#{@hash}"
body = "#{@@hash}"
end

def clear_hash_params
Expand All @@ -68,16 +67,10 @@ def clear_hash_params
@@average_response_time = nil
end

def send_metrics
def self.send_metrics
# TODO send hash to Airbrake
@hash = {}
@start_time = Time.now
@@hash = {}
@@start_time = Time.now
end

at_exit do
# I can't call this method inside this block
send_metrics
end

end
end

0 comments on commit 7f5eebf

Please sign in to comment.