-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws_asyncio.py
63 lines (53 loc) · 1.82 KB
/
aws_asyncio.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
import asyncio
import time
import datetime
import uuid
import boto3
import json
LOG = get_logger(__name__)
def firehose_client(region_name="us-east-1"):
"""Kinesis Firehose client"""
firehose_conn = boto3.client("firehose", region_name=region_name)
extra_msg = {"region_name": region_name, "aws_service": "firehose"}
LOG.info("firehose connection initiated", extra=extra_msg)
return firehose_conn
async def put_record(data,
client,
delivery_stream_name="test-firehose-nomad-no-lambda"):
"""
See this:
http://boto3.readthedocs.io/en/latest/reference/services/
firehose.html#Firehose.Client.put_record
"""
extra_msg = {"aws_service": "firehose"}
LOG.info(f"Pushing record to firehose: {data}", extra=extra_msg)
response = client.put_record(
DeliveryStreamName=delivery_stream_name,
Record={
'Data': data
}
)
return response
def gen_uuid_events():
"""Creates a time stamped UUID based event"""
current_time = 'test-{date:%Y-%m-%d %H:%M:%S}'.format(date=datetime.datetime.now())
event_id = str(uuid.uuid4())
event = {event_id:current_time}
return json.dumps(event)
def send_async_firehose_events(count=100):
"""Async sends events to firehose"""
start = time.time()
client = firehose_client()
extra_msg = {"aws_service": "firehose"}
loop = asyncio.get_event_loop()
tasks = []
LOG.info(f"sending aysnc events TOTAL {count}",extra=extra_msg)
num = 0
for _ in range(count):
tasks.append(asyncio.ensure_future(put_record(gen_uuid_events(), client)))
LOG.info(f"sending aysnc events: COUNT {num}/{count}")
num +=1
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
end = time.time()
LOG.info("Total time: {}".format(end - start))