Skip to content

Commit

Permalink
Timescale listener: Save url from request event to database
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberw committed Nov 2, 2021
1 parent 44dc2cf commit 9b61411
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions locust_plugins/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def write_samples_to_db(self, samples):
with self._conn.cursor() as cur:
psycopg2.extras.execute_values(
cur,
"""INSERT INTO request(time,run_id,greenlet_id,loadgen,name,request_type,response_time,success,testplan,response_length,exception,pid,context) VALUES %s""",
"""INSERT INTO request(time,run_id,greenlet_id,loadgen,name,request_type,response_time,success,testplan,response_length,exception,pid,url,context) VALUES %s""",
samples,
template="(%(time)s, %(run_id)s, %(greenlet_id)s, %(loadgen)s, %(name)s, %(request_type)s, %(response_time)s, %(success)s, %(testplan)s, %(response_length)s, %(exception)s, %(pid)s, %(context)s)",
template="(%(time)s, %(run_id)s, %(greenlet_id)s, %(loadgen)s, %(name)s, %(request_type)s, %(response_time)s, %(success)s, %(testplan)s, %(response_length)s, %(exception)s, %(pid)s, %(url)s, %(context)s)",
)
except psycopg2.Error as error:
logging.error("Failed to write samples to Postgresql timescale database: " + repr(error))
Expand All @@ -166,7 +166,16 @@ def quitting(self, **_kwargs):
self.exit()

def on_request(
self, request_type, name, response_time, response_length, exception, context, start_time=None, **_kwargs
self,
request_type,
name,
response_time,
response_length,
exception,
context,
start_time=None,
url=None,
**_kwargs,
):
success = 0 if exception else 1
if start_time:
Expand All @@ -185,6 +194,7 @@ def on_request(
"request_type": request_type,
"response_time": response_time,
"success": success,
"url": url[0:255] if url else None,
"testplan": self._testplan,
"pid": self._pid,
"context": psycopg2.extras.Json(context, safe_serialize),
Expand Down
3 changes: 2 additions & 1 deletion locust_plugins/timescale_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ CREATE TABLE public.request (
success smallint NOT NULL,
testplan character varying(30) NOT NULL,
pid integer,
context jsonb
context jsonb,
url character varying(255)
);


Expand Down

0 comments on commit 9b61411

Please sign in to comment.