|
| 1 | +import os |
| 2 | +from dataclasses import dataclass, asdict |
| 3 | + |
| 4 | +from typing import Dict, Optional, List, Union |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +from lumigo_tracer.extension.extension_utils import get_current_bandwidth, get_extension_logger |
| 8 | +from lumigo_tracer import utils |
| 9 | +from lumigo_tracer.extension.lambda_service import LambdaService |
| 10 | +from lumigo_tracer.extension.sampler import Sampler |
| 11 | +from lumigo_tracer.utils import lumigo_safe_execute |
| 12 | + |
| 13 | +SPAN_TYPE = "extensionExecutionEnd" |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class ExtensionEvent: |
| 18 | + token: str |
| 19 | + started: int |
| 20 | + requestId: str |
| 21 | + networkBytesUsed: int |
| 22 | + cpuUsageTime: List[Dict[str, Union[float, int]]] |
| 23 | + type: str = SPAN_TYPE |
| 24 | + |
| 25 | + |
| 26 | +class LumigoExtension: |
| 27 | + def __init__(self, lambda_service: LambdaService): |
| 28 | + self.lambda_service: LambdaService = lambda_service |
| 29 | + self.sampler: Sampler = Sampler() |
| 30 | + self.start_time: Optional[datetime] = None |
| 31 | + self.request_id: Optional[str] = None |
| 32 | + self.bandwidth: Optional[int] = None |
| 33 | + |
| 34 | + def start_new_invocation(self, event: Dict[str, str]): |
| 35 | + with lumigo_safe_execute("Extension: start_new_invocation"): |
| 36 | + current_bandwidth = get_current_bandwidth() |
| 37 | + if self.request_id: |
| 38 | + self._finish_previous_invocation(current_bandwidth) |
| 39 | + self.sampler.start_sampling() |
| 40 | + self.lambda_service.ready_for_next_event() |
| 41 | + self.request_id = event.get("requestId") |
| 42 | + self.start_time = datetime.now() |
| 43 | + self.bandwidth = current_bandwidth |
| 44 | + |
| 45 | + def shutdown(self): |
| 46 | + with lumigo_safe_execute("Extension: shutdown"): |
| 47 | + current_bandwidth = get_current_bandwidth() |
| 48 | + self._finish_previous_invocation(current_bandwidth) |
| 49 | + |
| 50 | + def _finish_previous_invocation(self, current_bandwidth: Optional[int]): |
| 51 | + self.sampler.stop_sampling() |
| 52 | + token = os.environ.get(utils.LUMIGO_TOKEN_KEY) |
| 53 | + if not token: |
| 54 | + get_extension_logger().warning( |
| 55 | + f"Skip sending data: No token was found. Request id: {self.request_id}" |
| 56 | + ) |
| 57 | + return |
| 58 | + if not ( |
| 59 | + self.request_id |
| 60 | + and self.start_time # noqa: W503 |
| 61 | + and self.sampler.get_samples() # noqa: W503 |
| 62 | + and self.bandwidth # noqa: W503 |
| 63 | + and current_bandwidth # noqa: W503 |
| 64 | + ): |
| 65 | + get_extension_logger().warning("Skip sending data: unable retrieving all data") |
| 66 | + return |
| 67 | + span = ExtensionEvent( |
| 68 | + token=token, |
| 69 | + started=int(self.start_time.timestamp() * 1000), |
| 70 | + requestId=self.request_id, |
| 71 | + networkBytesUsed=current_bandwidth - self.bandwidth, |
| 72 | + cpuUsageTime=[s.dump() for s in self.sampler.get_samples()], |
| 73 | + ) |
| 74 | + utils.report_json(os.environ.get("AWS_REGION", "us-east-1"), msgs=[asdict(span)]) |
0 commit comments