Skip to content

Commit

Permalink
Support of cloudwatch metrics - Removed the need of Service Context c…
Browse files Browse the repository at this point in the history
…lass - Addressing reviewing comments
  • Loading branch information
srininara committed Oct 13, 2023
1 parent 21629f6 commit 9ab631e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 36 deletions.
4 changes: 2 additions & 2 deletions examples/cloudwatch_listener_ex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from locust import events
from locust_plugins.listeners.cloudwatch import CloudwatchAdapter, ServiceContext
from locust_plugins.listeners.cloudwatch import CloudwatchAdapter


@events.init.add_listener
def on_locust_init(environment, **_kwargs):
CloudwatchAdapter(environment, ServiceContext("MyExampleService", "perf"))
CloudwatchAdapter(environment, "MyExampleService", "perf")
36 changes: 7 additions & 29 deletions locust_plugins/listeners/cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,6 @@ def api_id(self):
return f"{self.request_type}-{self.name}"


class ServiceContext:
"""
A class which hold the contextual information of a test. Which service the test is conducted on.
Which environment (perf, staging etc.) it is running on. Helps to derive the namespace to which
the metrics have to be written in cloudwatch.
"""

def __init__(self, service, environment="perf"):
self._service = service
self._environment = environment

def environment(self):
return self._environment

def service(self):
return self._service

def name(self):
return f"{self._environment}-{self._service}"

def metrics_namespace(self):
return f"{self._environment}/{self._service}/loadtests"


class CloudwatchAdapter:
"""
The primary class which does the actual work for pushing metrics to cloudwatch using the provided
Expand All @@ -177,20 +153,24 @@ class CloudwatchAdapter:
REQUESTS_BATCH_SIZE = 10
CLOUDWATCH_METRICS_BATCH_SIZE = 15 # Keeping it conservative not to breach the CW limit

def __init__(self, locust_env, service_context, cloudwatch=None):
def __init__(self, locust_env, service_name, environment="perf", cloudwatch=None):
if cloudwatch is None:
self.cloudwatch = boto3.client("cloudwatch")
else:
self.cloudwatch = cloudwatch
self.locust_env = locust_env
self.service_context = service_context
self._service = service_name
self._environment = environment
# The queue that hold the metrics locally.
self.request_results_q = queue.Queue(100)
events = self.locust_env.events
events.test_start.add_listener(self.on_test_start)
events.request.add_listener(self.on_request)
events.test_stop.add_listener(self.on_test_stop)

def metrics_namespace(self):
return f"{self._environment}/{self._service}/loadtests"

def on_test_start(self, environment, **kwargs):
log.info("Begin setup")

Expand Down Expand Up @@ -249,9 +229,7 @@ def _post_to_cloudwatch(self, cw_metrics_batch):
cw_metrics_batch = cw_metrics_batch[CloudwatchAdapter.CLOUDWATCH_METRICS_BATCH_SIZE :]
if len(current_batch) > 0:
try:
self.cloudwatch.put_metric_data(
Namespace=self.service_context.metrics_namespace(), MetricData=current_batch
)
self.cloudwatch.put_metric_data(Namespace=self.metrics_namespace(), MetricData=current_batch)
log.debug(f"Posted {len(current_batch)} metrics to cloudwatch")
except Exception as e:
print(str(e))
7 changes: 2 additions & 5 deletions test/test_cloudwatch_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from locust.env import Environment
from locust.stats import stats_printer, stats_history
from locust.log import setup_logging
from locust_plugins.listeners.cloudwatch import CloudwatchAdapter, ServiceContext
from locust_plugins.listeners.cloudwatch import CloudwatchAdapter
from unittest import TestCase


Expand All @@ -25,12 +25,9 @@ def put_metric_data(self, Namespace, MetricData):
cw = CloudwatchMock()

def on_locust_init(environment, **_kwargs):
CloudwatchAdapter(cw, environment, _service_context())
CloudwatchAdapter(environment, "MyExampleService", "perf", cw)


def _service_context():
return ServiceContext("MyExampleService", "perf")

events.init.add_listener(on_locust_init)

class User(HttpUser):
Expand Down

0 comments on commit 9ab631e

Please sign in to comment.