Skip to content

Commit

Permalink
allow using cpu_warning event for arbitrary event logging with custom…
Browse files Browse the repository at this point in the history
… text and timestamp
  • Loading branch information
cyberw committed Apr 28, 2022
1 parent 3f6572e commit 4007f1c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions locust_plugins/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ def dbcursor(self):
pass
raise

def on_cpu_warning(self, environment: locust.env.Environment, cpu_usage):
def on_cpu_warning(self, environment: locust.env.Environment, cpu_usage, message=None, timestamp=None, **kwargs):
# passing a custom message & timestamp to the event is a haxx to allow using this event for reporting generic events
if not timestamp:
timestamp = datetime.now(timezone.utc).isoformat()
if not message:
message = f"{self._testplan} High CPU usage ({cpu_usage}%)"
with self.dbcursor() as cur:
cur.execute(
"INSERT INTO events (time, text) VALUES (%s, %s)",
(datetime.now(timezone.utc).isoformat(), f"{self._testplan} High CPU usage ({cpu_usage}%)"),
)
cur.execute("INSERT INTO events (time, text) VALUES (%s, %s)", (timestamp, message))

def on_start(self, environment: locust.env.Environment):
try:
Expand Down

0 comments on commit 4007f1c

Please sign in to comment.