|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | from flask import Flask, request, jsonify |
3 | 3 | import json |
| 4 | +import os |
| 5 | +import httpx |
4 | 6 | from datetime import datetime |
5 | 7 |
|
6 | 8 | app = Flask(__name__) |
7 | 9 |
|
| 10 | +# IBM Cloud Logging configuration |
| 11 | +IBM_INSTANCE_ID = os.environ.get("IBM_INSTANCE_ID") |
| 12 | +CE_REGION = os.environ.get("CE_REGION", "us-south") |
| 13 | +IBM_APP_NAME = os.environ.get("CE_APP", "cos-event-trigger") |
| 14 | +IBM_SUBSYSTEM_NAME = os.environ.get("CE_PROJECT_ID", "event-processor") |
| 15 | +IBM_LOG_SEVERITY = os.environ.get("IBM_LOG_SEVERITY", "info") |
| 16 | + |
| 17 | +def get_iam_token(): |
| 18 | + ibmcloud_api_key = os.environ.get('IBMCLOUD_API_KEY') |
| 19 | + if not ibmcloud_api_key: |
| 20 | + raise ValueError("IBMCLOUD_API_KEY environment variable not found") |
| 21 | + hdrs = { 'Accept': 'application/json', 'Content-Type' : 'application/x-www-form-urlencoded' } |
| 22 | + params = { 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey', |
| 23 | + 'apikey': ibmcloud_api_key } |
| 24 | + resp = httpx.post('https://iam.cloud.ibm.com/identity/token', data = params, headers = hdrs) |
| 25 | + # raise exception if invalid status |
| 26 | + resp.raise_for_status() |
| 27 | + json_payload = resp.json() |
| 28 | + token = json_payload['access_token'] |
| 29 | + return token |
| 30 | + |
| 31 | +def send_to_ibm_logging(log_text, severity=None): |
| 32 | + """Send log to IBM Cloud Logging""" |
| 33 | + if not IBM_INSTANCE_ID: |
| 34 | + print("IBM Cloud Logging not configured. Set IBM_INSTANCE_ID and IBM_IAM_TOKEN environment variables.") |
| 35 | + return False |
| 36 | + |
| 37 | + url = f"https://{IBM_INSTANCE_ID}.ingress.{CE_REGION}.logs.cloud.ibm.com/logs/v1/singles" |
| 38 | + |
| 39 | + iam_token = get_iam_token() |
| 40 | + |
| 41 | + headers = { |
| 42 | + "Content-Type": "application/json", |
| 43 | + "Authorization": iam_token |
| 44 | + } |
| 45 | + |
| 46 | + payload = [{ |
| 47 | + "applicationName": IBM_APP_NAME, |
| 48 | + "subsystemName": IBM_SUBSYSTEM_NAME, |
| 49 | + "severity": severity or IBM_LOG_SEVERITY, |
| 50 | + "text": log_text |
| 51 | + }] |
| 52 | + |
| 53 | + try: |
| 54 | + response = httpx.post(url, headers=headers, json=payload, timeout=10.0) |
| 55 | + if response.status_code >= 200 and response.status_code < 300: |
| 56 | + print(f"Successfully sent log to IBM Cloud Logging: {response.status_code}") |
| 57 | + return True |
| 58 | + else: |
| 59 | + print(f"Failed to send log to IBM Cloud Logging: {response.status_code}, {response.text}") |
| 60 | + return False |
| 61 | + except Exception as e: |
| 62 | + print(f"Error sending log to IBM Cloud Logging: {str(e)}") |
| 63 | + return False |
| 64 | + |
8 | 65 | # Equivalent to EventStats in Go |
9 | 66 | class EventStats: |
10 | 67 | def __init__(self): |
@@ -46,6 +103,10 @@ def handle_event(): |
46 | 103 | print(f"{current_time} - Received:") |
47 | 104 | print(f"\nBody: {body}") |
48 | 105 |
|
| 106 | + # Send to IBM Cloud Logging |
| 107 | + log_message = f"COS Event: {operation} on {bucket}/{key} - {body}" |
| 108 | + send_to_ibm_logging(log_message) |
| 109 | + |
49 | 110 | return "OK" |
50 | 111 |
|
51 | 112 | if __name__ == '__main__': |
|
0 commit comments