Skip to content

Commit

Permalink
Publish CloudWatch metrics for processed and failed jobs
Browse files Browse the repository at this point in the history
This should allow us to display these values on the dashbaord (#5).
  • Loading branch information
jagthedrummer committed Jun 30, 2021
1 parent f25be59 commit 6981337
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/funktor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "funktor/version"
require 'funktor/aws/sqs/event'
require 'funktor/aws/sqs/record'
require 'funktor/counter'
require 'funktor/job'
require 'funktor/worker'
require 'funktor/middleware_chain'
Expand Down
7 changes: 7 additions & 0 deletions lib/funktor/active_job_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
module Funktor
class ActiveJobHandler

def initialize
@failed_counter = Funktor::Counter.new('failed')
@processed_counter = Funktor::Counter.new('processed')
end

def call(event:, context:)
event = Funktor::Aws::Sqs::Event.new(event)
puts "event.jobs.count = #{event.jobs.count}"
Expand All @@ -20,10 +25,12 @@ def dispatch(job)
Funktor.active_job_handler_middleware.invoke(job) do
job.execute
end
@processed_counter.incr(job)
# rescue Funktor::Job::InvalidJsonError # TODO Make this work
rescue Exception => e
puts "Error during processing: #{$!}"
puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
@failed_counter.incr(job)
attempt_retry_or_bail(job)
end
end
Expand Down
42 changes: 42 additions & 0 deletions lib/funktor/counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Funktor
class Counter
attr_accessor :dimension

def initialize(dimension)
@dimension = dimension
end

def incr(job)
put_metric_to_stdout(job)
end

def put_metric_to_stdout(job)
puts Funktor.dump_json(metric_hash(job))
end

def metric_hash(job)
{
"_aws": {
"Timestamp": Time.now.strftime('%s%3N').to_i,
"CloudWatchMetrics": [
{
"Namespace": ENV['FUNKTOR_APP_NAME'],
"Dimensions": [["WorkerClassName"]],
"Metrics": [ # CPU, Memory, Duration, etc...
{
"Name": dimension,
"Unit": "Count"
}
]
}
]
},
"WorkerClassName": job.worker_class_name,
"#{dimension}": 1
#"count": value,
#"requestId": "989ffbf8-9ace-4817-a57c-e4dd734019ee"
}
#data[key] = value
end
end
end

0 comments on commit 6981337

Please sign in to comment.