This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.rb
98 lines (86 loc) · 2.38 KB
/
webhook.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'sinatra'
require 'json'
require 'pp'
require 'right_aws'
AWS_ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY_ID'] or raise 'AWS_ACCESS_KEY_ID missing!'
AWS_SECRET_ACCESS_KEY = ENV['AWS_SECRET_ACCESS_KEY'] or raise 'AWS_SECRET_ACCESS_KEY missing!'
CLOUDWATCH_NAMESPACE = ENV['CLOUDWATCH_NAMESPACE'] || 'Test'
$acw = RightAws::AcwInterface.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
configure :production do
require 'newrelic_rpm'
end
get '/' do
'This is a web hook!'
end
post '/' do
payload = JSON.parse(params[:payload])
begin
handle_payload(payload)
'OK'
rescue => e
[500, "#{e.class}: #{e}"]
end
end
def handle_payload(payload)
events = payload.delete('events') or raise 'No events!'
puts "Got #{events.size} events!"
log_per_app(events)
log_per_app_and_error(events)
log_per_app_and_dyno(events)
end
def log_per_app(events)
events.group_by do |event|
event['source_name'] || 'unknown'
end.each do |app, events|
puts "#{app}: #{events.size} events."
$acw.put_metric_data({
:metric_name => "Heroku errors",
:namespace => CLOUDWATCH_NAMESPACE,
:dimensions => {:AppName => app},
:unit => :Count,
:value => events.size,
})
end
end
def log_per_app_and_error(events)
events.group_by do |event|
message = event['message']
app = event['source_name'] || 'unknown'
if message
[app, message[/^Error (\w+)/, 1] || 'other']
else
puts 'WARNING: event has no message!'
nil
end
end.each do |(app, error), events|
puts "#{app}: error #{error}: #{events.size} events."
$acw.put_metric_data({
:metric_name => "Heroku errors",
:namespace => CLOUDWATCH_NAMESPACE,
:dimensions => {:AppName => app, :ErrorCode => error},
:unit => :Count,
:value => events.size,
})
end
end
def log_per_app_and_dyno(events)
events.group_by do |event|
message = event['message']
app = event['source_name'] || 'unknown'
if message
[app, message[/ dyno=web\.(\d+) /, 1] || 'platform']
else
puts 'WARNING: event has no message!'
nil
end
end.each do |(app, dyno), events|
puts "#{app}: dyno #{dyno}: #{events.size} events."
$acw.put_metric_data({
:metric_name => "Heroku errors",
:namespace => CLOUDWATCH_NAMESPACE,
:dimensions => {:AppName => app, :Dyno => dyno},
:unit => :Count,
:value => events.size,
})
end
end