-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.rb
executable file
·68 lines (56 loc) · 1.54 KB
/
lambda_function.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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Copyright 2020 Scribd, Inc.
require 'logger'
require 'date'
require 'dogapi'
require_relative 'lib/slowlog_check'
LOGGER = Logger.new($stdout)
LOGGER.level = Logger::INFO
def event_time
# DateTime because Time does not natively parse AWS CloudWatch Event time
DateTime.rfc3339(@event.fetch('time', DateTime.now.rfc3339))
end
def log_context
LOGGER.debug('## ENVIRONMENT VARIABLES')
LOGGER.debug(ENV.to_a)
LOGGER.debug('## EVENT')
LOGGER.debug(@event)
LOGGER.info "Event time: #{event_time}."
end
def lambda_handler(event: {}, context: {})
@event = event
log_context
ssm_path = ENV.fetch('SSM_PATH', false)
if ssm_path
require 'aws-sdk-ssm'
resp = Aws::SSM::Client.new.get_parameters_by_path(
path: "/#{ssm_path}/",
recursive: true,
with_decryption: true
)
resp.parameters.each do |parameter|
name = File.basename(parameter.name)
LOGGER.info "Setting parameter: #{name} from SSM."
ENV[name] = parameter.value
end
end
unless defined? @slowlog_check
@slowlog_check = SlowlogCheck.new(
ddog: Dogapi::Client.new(
ENV.fetch('DATADOG_API_KEY'),
ENV.fetch('DATADOG_APP_KEY')
),
redis: {
host: ENV.fetch('REDIS_HOST')
},
namespace: ENV.fetch('NAMESPACE'),
env: ENV.fetch('ENV'),
metricname: ENV.fetch('METRICNAME', 'elasticache.slowlog')
)
@slowlog_check.update_metadatas
end
@slowlog_check.ship_slowlogs
nil
end
lambda_handler if __FILE__ == $PROGRAM_NAME