|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import zlib |
| 6 | +from typing import List |
| 7 | + |
| 8 | +import requests |
| 9 | + |
| 10 | +# AWS Lambda function for reading structured analytics events off of a Kinesis stream and sending them to Tinybird |
| 11 | + |
| 12 | +log = logging.getLogger() |
| 13 | + |
| 14 | + |
| 15 | +def extract_analytics_events(lambda_event) -> List[str]: |
| 16 | + """ |
| 17 | + Extracts analytics events from a lambda event invocation payload. |
| 18 | + :param lambda_event: The event payload sent to the Lambda function at its execution. This payload is expected to |
| 19 | + contain Kinesis records, which in turn contain CloudWatch log messages (base64 encoded and |
| 20 | + gzipped). |
| 21 | + :return: A list of serialized analytics events, each with a newline at the end. |
| 22 | + """ |
| 23 | + analytics_events = [] |
| 24 | + for record in lambda_event["Records"]: |
| 25 | + record_data = record["kinesis"]["data"] |
| 26 | + log_payload = json.loads( |
| 27 | + zlib.decompress(base64.b64decode(record_data), 16 + zlib.MAX_WBITS) |
| 28 | + ) |
| 29 | + for log_event in log_payload["logEvents"]: |
| 30 | + log_message = log_event["message"] |
| 31 | + analytics_events.append( |
| 32 | + log_message + "\n" if not log_message.endswith("\n") else log_message |
| 33 | + ) |
| 34 | + return analytics_events |
| 35 | + |
| 36 | + |
| 37 | +def load_events_to_tinybird( |
| 38 | + analytics_events: List[str], tinybird_url: str, tinybird_auth_token: str |
| 39 | +): |
| 40 | + """ |
| 41 | + Loads analytics events into a Tinybird datasource. |
| 42 | + :param analytics_events: A list of serialized, JSON-encoded analytics events. Each event is expected to end with a |
| 43 | + newline delimiter. |
| 44 | + :param tinybird_url: Complete Tinybird URL to POST events to (via the 'events' API) |
| 45 | + :param tinybird_auth_token: HTTP bearer token for Tinybird |
| 46 | + :raises RuntimeError: If Tinybird returns a non 2XX HTTP response code |
| 47 | + """ |
| 48 | + headers = {"Authorization": f"Bearer {tinybird_auth_token}"} |
| 49 | + body = "".join(analytics_events) |
| 50 | + response = requests.post(tinybird_url, data=body, headers=headers) |
| 51 | + if response.status_code > 299: |
| 52 | + log.error( |
| 53 | + f"failed to load events to Tinybird with status code {response.status_code}: {response.text}" |
| 54 | + ) |
| 55 | + raise RuntimeError("failed to load events to Tinybird") |
| 56 | + log.info(response.text) |
| 57 | + |
| 58 | + |
| 59 | +def handler(event, context): |
| 60 | + tinybird_url = os.getenv("TINYBIRD_URL") |
| 61 | + if tinybird_url is None: |
| 62 | + raise EnvironmentError("env var TINYBIRD_URL not set") |
| 63 | + tinybird_auth_token = os.getenv("TINYBIRD_AUTH_TOKEN") |
| 64 | + if tinybird_auth_token is None: |
| 65 | + raise EnvironmentError("env var TINYBIRD_AUTH_TOKEN not set") |
| 66 | + |
| 67 | + analytics_events = extract_analytics_events(event) |
| 68 | + load_events_to_tinybird(analytics_events, tinybird_url, tinybird_auth_token) |
0 commit comments