-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
34 lines (26 loc) · 891 Bytes
/
main.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
import base64
import json
import logging
import os
from elasticsearch import Elasticsearch
es = Elasticsearch([os.getenv('ES_HOST')])
def handle_message(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
if 'data' not in event:
logging.warn("No 'data' field in pub/sub message")
return
data = json.loads(base64.b64decode(event['data']).decode('utf-8'))
msg_type = data.get('type')
if not msg_type:
logging.warn("No 'type' field in pub/sub message")
return
if msg_type == 'LOG':
es.index(index=os.getenv('ES_INDEX'), doc_type='log',
body=data.get('payload'))
else:
logging.warn("Unknown message type '{}'".format(msg_type))
return None