tpt_serverless 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tpt_serverless.rb +1 -0
- data/lib/tpt_serverless/sumo_log_forwarder.rb +76 -74
- data/lib/tpt_serverless/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e40167211120d2403b0f6c91770b43384ff2d9781cb0282e2fad5c34f0a1459
|
4
|
+
data.tar.gz: 790516d5026b0a26123d929ddab13424e46ce56e27eb7de1dcd05d135cd7dba5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4731b9cf978c054b73779578280757be12d8e7ecc08305bdf5e87a6b3d272ac3f6101eddd6fdf3f9d60371126b227603a6eb218156d73a05e2856a72fcf83d22
|
7
|
+
data.tar.gz: f0f048fb5a0b0f02b34c2a06e5a157b8727ae13cfdb69c8fa2a3ebced4132c939e4a3b833a7d438c800fc00830e58f31279c1b2d75086c1ba6399a5694b51711
|
data/lib/tpt_serverless.rb
CHANGED
@@ -6,7 +6,7 @@ Example of how to add this to your `serverless.yml`:
|
|
6
6
|
forwardLogs:
|
7
7
|
name: ${self:service.name}-forwardLogs-${self:provider.stage}
|
8
8
|
# NOTE: Ensure that you update the path below if you update the tpt_serverless gem
|
9
|
-
handler: /opt/ruby/2.5.0/gems/tpt_serverless-X.X.X/lib/tpt_serverless/sumo_log_forwarder.
|
9
|
+
handler: /opt/ruby/2.5.0/gems/tpt_serverless-X.X.X/lib/tpt_serverless/sumo_log_forwarder.SumoLogForwarder.handler
|
10
10
|
timeout: 10 # seconds
|
11
11
|
environment:
|
12
12
|
SUMO_ENDPOINT: ${self:custom.sumoEndpoint}
|
@@ -22,84 +22,86 @@ require 'base64'
|
|
22
22
|
|
23
23
|
TIME_REGEX = /^20\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ/
|
24
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
|
-
|
25
|
+
class SumoLogForwarder
|
26
|
+
class << self
|
27
|
+
# This handler receives CloudWatch log events, parses the events and forwards the extracted logs to
|
28
|
+
# Sumo Logic.
|
29
|
+
#
|
30
|
+
# It performs a few helpful cleanup/prep functions as well.
|
31
|
+
def handler(event: , context:)
|
32
|
+
sumo_endpoint = ENV['SUMO_ENDPOINT']
|
33
|
+
|
34
|
+
if sumo_endpoint.nil? || sumo_endpoint.strip.empty?
|
35
|
+
puts 'ERROR: SUMO_ENDPOINT is not set. Skipping log forwarding.'
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
sumo_url = URI.parse(sumo_endpoint)
|
40
|
+
raw_data = event.fetch('awslogs').fetch('data')
|
41
|
+
unzipped_data = Zlib.gunzip(Base64.decode64(raw_data))
|
42
|
+
data = JSON.parse(unzipped_data)
|
43
|
+
|
44
|
+
message_type = data.fetch('messageType')
|
45
|
+
log_group = data.fetch('logGroup')
|
46
|
+
log_stream = data.fetch('logStream')
|
47
|
+
log_events = data.fetch('logEvents')
|
48
|
+
|
49
|
+
if message_type === 'CONTROL_MESSAGE'
|
50
|
+
puts 'skipping control message'
|
51
|
+
return
|
52
|
+
end
|
53
|
+
|
54
|
+
puts "message_count=#{log_events.length}"
|
55
|
+
|
56
|
+
messages_data = extract_messages(log_events).map do |message|
|
57
|
+
# AWS replaces newlines with carriage returns in Lambda logs
|
58
|
+
message.gsub(/\r(?!\n)/, "\n")
|
59
|
+
end.join('')
|
60
|
+
|
61
|
+
send_to_sumo(sumo_url, log_group, log_stream, messages_data)
|
62
|
+
end
|
58
63
|
|
59
|
-
|
60
|
-
end
|
64
|
+
private
|
61
65
|
|
62
|
-
|
66
|
+
def extract_messages(log_events)
|
67
|
+
log_events.map do |log_event|
|
68
|
+
message = log_event.fetch('message')
|
69
|
+
timestamp = Integer(log_event.fetch('timestamp'))
|
63
70
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
71
|
+
# Ensure the message starts with a time.
|
72
|
+
# E.g. START/END/REPORT log events don't.
|
73
|
+
if TIME_REGEX !~ message
|
74
|
+
time_string = Time.at(timestamp/1000.0).utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
|
75
|
+
message = "#{time_string} #{message}"
|
76
|
+
end
|
68
77
|
|
69
|
-
|
70
|
-
|
71
|
-
if TIME_REGEX !~ message
|
72
|
-
time_string = Time.at(timestamp/1000.0).utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
|
73
|
-
message = "#{time_string} #{message}"
|
78
|
+
message
|
79
|
+
end
|
74
80
|
end
|
75
81
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
if response.code != '200'
|
103
|
-
response.error!
|
82
|
+
def send_to_sumo(sumo_url, log_group, log_stream, data)
|
83
|
+
response = Net::HTTP.start(sumo_url.host, sumo_url.port, use_ssl: sumo_url.scheme == 'https') do |http|
|
84
|
+
http.open_timeout = 3
|
85
|
+
http.read_timeout = 3
|
86
|
+
|
87
|
+
req = Net::HTTP::Post.new(
|
88
|
+
sumo_url,
|
89
|
+
{
|
90
|
+
'X-Sumo-Category' => log_group,
|
91
|
+
'X-Sumo-Client' => 'cwl-aws-lambda',
|
92
|
+
'X-Sumo-Host' => log_group,
|
93
|
+
'X-Sumo-Name' => log_stream,
|
94
|
+
}
|
95
|
+
)
|
96
|
+
|
97
|
+
req.body = data
|
98
|
+
|
99
|
+
http.request(req)
|
100
|
+
end
|
101
|
+
|
102
|
+
if response.code != '200'
|
103
|
+
response.error!
|
104
|
+
end
|
105
|
+
end
|
104
106
|
end
|
105
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tpt_serverless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TpT
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02
|
11
|
+
date: 2020-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|