adapto 0.1.3__tar.gz → 0.1.4__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {adapto-0.1.3 → adapto-0.1.4}/PKG-INFO +1 -1
- adapto-0.1.4/adapto/aws_lambda_monitor.py +25 -0
- adapto-0.1.4/adapto/aws_lambda_scaler.py +48 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto.egg-info/PKG-INFO +1 -1
- {adapto-0.1.3 → adapto-0.1.4}/adapto.egg-info/SOURCES.txt +2 -0
- {adapto-0.1.3 → adapto-0.1.4}/setup.py +1 -1
- {adapto-0.1.3 → adapto-0.1.4}/LICENSE +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/README.md +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto/__init__.py +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto/predictor.py +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto/scaler.py +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto/utils.py +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto.egg-info/dependency_links.txt +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto.egg-info/requires.txt +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/adapto.egg-info/top_level.txt +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/setup.cfg +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/tests/test_predictor.py +0 -0
- {adapto-0.1.3 → adapto-0.1.4}/tests/test_scaler.py +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}")
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|