-
Notifications
You must be signed in to change notification settings - Fork 376
/
lambda_function.py
255 lines (208 loc) · 7.46 KB
/
lambda_function.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2017 Datadog, Inc.
from __future__ import print_function
import base64
import json
import urllib
import boto3
import time
import os
import socket
import ssl
import re
import StringIO
import gzip
# Parameters
# ddApiKey: Datadog API Key
ddApiKey = "<your_api_key>"
try:
ddApiKey = os.environ['DD_API_KEY']
except Exception:
pass
# metadata: Additional metadata to send with the logs
metadata = {
"ddsourcecategory": "aws",
}
host = "lambda-intake.logs.datadoghq.com"
ssl_port = 10516
cloudtrail_regex = re.compile('\d+_CloudTrail_\w{2}-\w{4,9}-\d_\d{8}T\d{4}Z.+.json.gz$', re.I)
DD_SOURCE = "ddsource"
DD_CUSTOM_TAGS = "ddtags"
def lambda_handler(event, context):
# Check prerequisites
if ddApiKey == "<your_api_key>" or ddApiKey == "":
raise Exception(
"You must configure your API key before starting this lambda function (see #Parameters section)"
)
# Attach Datadog's Socket
s = connect_to_datadog(host, ssl_port)
# Add the context to meta
if "aws" not in metadata:
metadata["aws"] = {}
aws_meta = metadata["aws"]
aws_meta["function_version"] = context.function_version
aws_meta["invoked_function_arn"] = context.invoked_function_arn
#Add custom tags here by adding new value with the following format "key1:value1, key2:value2" - might be subject to modifications
metadata[DD_CUSTOM_TAGS] = "forwardername:" + context.function_name.lower()+ ",memorysize:"+ context.memory_limit_in_mb
try:
logs = generate_logs(event)
for log in logs:
s = safe_submit_log(s, log)
except Exception as e:
print('Unexpected exception: {} for event {}'.format(str(e), event))
finally:
s.close()
def connect_to_datadog(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(s)
s.connect((host, port))
return s
def generate_logs(event):
try:
# Route to the corresponding parser
event_type = parse_event_type(event)
if event_type == "s3":
logs = s3_handler(event)
elif event_type == "awslogs":
logs = awslogs_handler(event)
elif event_type == "events":
logs = cwevent_handler(event)
except Exception as e:
# Logs through the socket the error
err_message = 'Error parsing the object. Exception: {} for event {}'.format(str(e), event)
logs = [err_message]
return logs
def safe_submit_log(s, log):
try:
send_entry(s, log)
except Exception as e:
# retry once
s = connect_to_datadog(host, ssl_port)
send_entry(s, log)
return s
# Utility functions
def parse_event_type(event):
if "Records" in event and len(event["Records"]) > 0:
if "s3" in event["Records"][0]:
return "s3"
elif "awslogs" in event:
return "awslogs"
elif "detail" in event:
return "events"
raise Exception("Event type not supported (see #Event supported section)")
# Handle S3 events
def s3_handler(event):
s3 = boto3.client('s3')
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
metadata[DD_SOURCE] = parse_event_source(event, key)
# Extract the S3 object
response = s3.get_object(Bucket=bucket, Key=key)
body = response['Body']
data = body.read()
structured_logs = []
# If the name has a .gz extension, then decompress the data
if key[-3:] == '.gz':
with gzip.GzipFile(fileobj=StringIO.StringIO(data)) as decompress_stream:
data = decompress_stream.read()
if is_cloudtrail(str(key)):
cloud_trail = json.loads(data)
for event in cloud_trail['Records']:
# Create structured object and send it
structured_line = merge_dicts(event, {"aws": {"s3": {"bucket": bucket, "key": key}}})
structured_logs.append(structured_line)
else:
# Send lines to Datadog
for line in data.splitlines():
# Create structured object and send it
structured_line = {"aws": {"s3": {"bucket": bucket, "key": key}}, "message": line}
structured_logs.append(structured_line)
return structured_logs
# Handle CloudWatch logs
def awslogs_handler(event):
# Get logs
with gzip.GzipFile(fileobj=StringIO.StringIO(base64.b64decode(event["awslogs"]["data"]))) as decompress_stream:
data = decompress_stream.read()
logs = json.loads(str(data))
#Set the source on the logs
source = logs.get("logGroup", "cloudwatch")
metadata[DD_SOURCE] = parse_event_source(event, source)
structured_logs = []
# Send lines to Datadog
for log in logs["logEvents"]:
# Create structured object and send it
structured_line = merge_dicts(log, {
"aws": {
"awslogs": {
"logGroup": logs["logGroup"],
"logStream": logs["logStream"],
"owner": logs["owner"]
}
}
})
structured_logs.append(structured_line)
return structured_logs
#Handle Cloudwatch Events
def cwevent_handler(event):
data = event
#Set the source on the log
source = data.get("source", "cloudwatch")
service = source.split(".")
if len(service)>1:
metadata[DD_SOURCE] = service[1]
else:
metadata[DD_SOURCE] = "cloudwatch"
structured_logs = []
structured_logs.append(data)
return structured_logs
def send_entry(s, log_entry):
# The log_entry can only be a string or a dict
if isinstance(log_entry, str):
log_entry = {"message": log_entry}
elif not isinstance(log_entry, dict):
raise Exception(
"Cannot send the entry as it must be either a string or a dict. Provided entry: " + str(log_entry)
)
# Merge with metadata
log_entry = merge_dicts(log_entry, metadata)
# Send to Datadog
str_entry = json.dumps(log_entry)
print(str_entry)
prefix = "%s " % ddApiKey
return s.send((prefix + str_entry + "\n").encode("UTF-8"))
def merge_dicts(a, b, path=None):
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge_dicts(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
else:
raise Exception(
'Conflict while merging metadatas and the log entry at %s' % '.'.join(path + [str(key)])
)
else:
a[key] = b[key]
return a
def is_cloudtrail(key):
match = cloudtrail_regex.search(key)
return bool(match)
def parse_event_source(event, key):
for source in ["lambda", "redshift", "cloudfront", "kinesis", "mariadb", "mysql", "apigateway", "route53", "vpc", "rds"]:
if source in key:
return source
if "elasticloadbalancing" in key:
return "elb"
if is_cloudtrail(str(key)):
return "cloudtrail"
if "awslogs" in event:
return "cloudwatch"
if "Records" in event and len(event["Records"]) > 0:
if "s3" in event["Records"][0]:
return "s3"
return "aws"