adapto 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- adapto/aws_lambda_monitor.py +25 -0
- adapto/aws_lambda_scaler.py +48 -0
- {adapto-0.1.3.dist-info → adapto-0.1.5.dist-info}/METADATA +1 -1
- adapto-0.1.5.dist-info/RECORD +11 -0
- {adapto-0.1.3.dist-info → adapto-0.1.5.dist-info}/WHEEL +1 -1
- adapto-0.1.3.dist-info/RECORD +0 -9
- {adapto-0.1.3.dist-info → adapto-0.1.5.dist-info}/LICENSE +0 -0
- {adapto-0.1.3.dist-info → adapto-0.1.5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
import time
|
2
|
+
import logging
|
3
|
+
from utils.aws_client import get_aws_client
|
4
|
+
|
5
|
+
class AWSLambdaMonitor:
|
6
|
+
def __init__(self, function_name):
|
7
|
+
self.client = get_aws_client("cloudwatch")
|
8
|
+
self.function_name = function_name
|
9
|
+
|
10
|
+
def get_execution_time(self):
|
11
|
+
"""Fetches the average execution time of the AWS Lambda function."""
|
12
|
+
try:
|
13
|
+
response = self.client.get_metric_statistics(
|
14
|
+
Namespace="AWS/Lambda",
|
15
|
+
MetricName="Duration",
|
16
|
+
Dimensions=[{"Name": "FunctionName", "Value": self.function_name}],
|
17
|
+
StartTime=time.time() - 3600,
|
18
|
+
EndTime=time.time(),
|
19
|
+
Period=300,
|
20
|
+
Statistics=["Average"]
|
21
|
+
)
|
22
|
+
return response["Datapoints"][-1]["Average"] if response["Datapoints"] else None
|
23
|
+
except Exception as e:
|
24
|
+
logging.error(f"Error fetching Lambda execution time: {e}")
|
25
|
+
return None
|
@@ -0,0 +1,48 @@
|
|
1
|
+
import logging
|
2
|
+
from utils.aws_client import get_aws_client
|
3
|
+
from adapto.config.settings import EXECUTION_TIME_THRESHOLD, LAMBDA_MEMORY_STEP, MIN_MEMORY, MAX_MEMORY
|
4
|
+
from adapto.aws_lambda_monitor import AWSLambdaMonitor
|
5
|
+
|
6
|
+
|
7
|
+
class AWSLambdaScaler:
|
8
|
+
def __init__(self, function_name):
|
9
|
+
self.client = get_aws_client("lambda")
|
10
|
+
self.monitor = AWSLambdaMonitor(function_name)
|
11
|
+
self.function_name = function_name
|
12
|
+
|
13
|
+
def get_current_memory(self):
|
14
|
+
"""Retrieves the current memory allocation of the Lambda function."""
|
15
|
+
try:
|
16
|
+
response = self.client.get_function_configuration(FunctionName=self.function_name)
|
17
|
+
return response["MemorySize"]
|
18
|
+
except Exception as e:
|
19
|
+
logging.error(f"Error fetching Lambda memory configuration: {e}")
|
20
|
+
return None
|
21
|
+
|
22
|
+
def scale_lambda_memory(self):
|
23
|
+
"""Adjusts Lambda memory allocation based on execution time."""
|
24
|
+
execution_time = self.monitor.get_execution_time()
|
25
|
+
if execution_time is None:
|
26
|
+
return
|
27
|
+
|
28
|
+
current_memory = self.get_current_memory()
|
29
|
+
if current_memory is None:
|
30
|
+
return
|
31
|
+
|
32
|
+
if execution_time > EXECUTION_TIME_THRESHOLD and current_memory < MAX_MEMORY:
|
33
|
+
new_memory = min(current_memory + LAMBDA_MEMORY_STEP, MAX_MEMORY)
|
34
|
+
self.update_lambda_memory(new_memory)
|
35
|
+
elif execution_time < EXECUTION_TIME_THRESHOLD * 0.6 and current_memory > MIN_MEMORY:
|
36
|
+
new_memory = max(current_memory - LAMBDA_MEMORY_STEP, MIN_MEMORY)
|
37
|
+
self.update_lambda_memory(new_memory)
|
38
|
+
|
39
|
+
def update_lambda_memory(self, new_memory):
|
40
|
+
"""Updates the Lambda memory configuration."""
|
41
|
+
try:
|
42
|
+
self.client.update_function_configuration(
|
43
|
+
FunctionName=self.function_name,
|
44
|
+
MemorySize=new_memory
|
45
|
+
)
|
46
|
+
logging.info(f"Updated Lambda {self.function_name} memory to {new_memory}MB")
|
47
|
+
except Exception as e:
|
48
|
+
logging.error(f"Error updating Lambda memory: {e}")
|
@@ -0,0 +1,11 @@
|
|
1
|
+
adapto/__init__.py,sha256=8SWysOKRVNsD3cOXLOfIdVZx6MxzIqGKEh9Z26QuCyM,30
|
2
|
+
adapto/aws_lambda_monitor.py,sha256=XGv4QHe1DlZ4SmHvVZX_6KWOkoUoKGxC6W91-H1PAZs,959
|
3
|
+
adapto/aws_lambda_scaler.py,sha256=r2oV2cvwJG9oqd1mFiGfv8Lmwpzp38BJ_v6Q7S5pPh4,2045
|
4
|
+
adapto/predictor.py,sha256=ze-cMHdf290o3_9D-PQ7yQ8eMz1Utv3E3qTHRUFQiAI,842
|
5
|
+
adapto/scaler.py,sha256=BqAs7rlIbrK4kkeBQzz3CgLHwpKWjhyQCciACqbFqTY,5654
|
6
|
+
adapto/utils.py,sha256=6TgDvowlXL__v-OkLSTZF12ZOEqEKCLuNv1LmL70eAY,140
|
7
|
+
adapto-0.1.5.dist-info/LICENSE,sha256=B73NNEiUy-CCs4zrOPlOWR39PfoG4zKtK97OnNctjvg,1070
|
8
|
+
adapto-0.1.5.dist-info/METADATA,sha256=vvubcyABKCsBPtXN96zSpVtTIWbzfikAT3k_YMebmZ8,609
|
9
|
+
adapto-0.1.5.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
|
10
|
+
adapto-0.1.5.dist-info/top_level.txt,sha256=IYsgAr6fnEC1R1ztYmK2ZpFNNGV0qRaf3Tvf-R8j0cM,7
|
11
|
+
adapto-0.1.5.dist-info/RECORD,,
|
adapto-0.1.3.dist-info/RECORD
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
adapto/__init__.py,sha256=8SWysOKRVNsD3cOXLOfIdVZx6MxzIqGKEh9Z26QuCyM,30
|
2
|
-
adapto/predictor.py,sha256=ze-cMHdf290o3_9D-PQ7yQ8eMz1Utv3E3qTHRUFQiAI,842
|
3
|
-
adapto/scaler.py,sha256=BqAs7rlIbrK4kkeBQzz3CgLHwpKWjhyQCciACqbFqTY,5654
|
4
|
-
adapto/utils.py,sha256=6TgDvowlXL__v-OkLSTZF12ZOEqEKCLuNv1LmL70eAY,140
|
5
|
-
adapto-0.1.3.dist-info/LICENSE,sha256=B73NNEiUy-CCs4zrOPlOWR39PfoG4zKtK97OnNctjvg,1070
|
6
|
-
adapto-0.1.3.dist-info/METADATA,sha256=I09ftZmmqq2srMzQ2-ITC8hBIspuVym29oI9dZXYWjQ,609
|
7
|
-
adapto-0.1.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
8
|
-
adapto-0.1.3.dist-info/top_level.txt,sha256=IYsgAr6fnEC1R1ztYmK2ZpFNNGV0qRaf3Tvf-R8j0cM,7
|
9
|
-
adapto-0.1.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|