Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-2269: tests for access log transmission to Clickhouse server #749

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
67 changes: 67 additions & 0 deletions helpers/clickhouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Simple client for Clickhouse access log storage"""

import dataclasses
import typing
from datetime import datetime
from ipaddress import IPv4Address

import clickhouse_connect

__author__ = "Tempesta Technologies, Inc."
__copyright__ = "Copyright (C) 2018-2025 Tempesta Technologies, Inc."
__license__ = "GPL2"


@dataclasses.dataclass
class ClickHouseLogRecord:
timestamp: datetime
address: IPv4Address
method: int
version: int
status: int
response_content_length: int
response_time: int
vhost: str
uri: str
referer: str
user_agent: str
dropped_events: int


class ClickHouseLogStorageClient:
symstu-tempesta marked this conversation as resolved.
Show resolved Hide resolved
def __init__(
self,
host: str = None,
username: str = None,
password: str = None,
port: int = None,
dsn: str = None,
symstu-tempesta marked this conversation as resolved.
Show resolved Hide resolved
):
self.clickhouse_client = clickhouse_connect.get_client(
symstu-tempesta marked this conversation as resolved.
Show resolved Hide resolved
host=host,
username=username,
password=password,
port=port,
dsn=dsn,
)

def log_table_exists(self) -> bool:
result = self.clickhouse_client.command("exists table access_log")
return result == 1

def delete_all(self) -> None:
self.clickhouse_client.command("delete from access_log where true")

def total_count(self) -> int:
res = self.clickhouse_client.query("select count(1) from access_log")
return res.result_rows[0][0]

def read(self) -> typing.List[ClickHouseLogRecord]:
results = self.clickhouse_client.query(
"""
select *
from access_log
order by timestamp desc
""",
)
return list(map(lambda x: ClickHouseLogRecord(*x), results.result_rows))
5 changes: 4 additions & 1 deletion helpers/dmesg.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def show(self):
print(self.log)

def log_findall(self, pattern: str):
return re.findall(pattern, self.log.decode(errors="ignore"))
if isinstance(self.log, bytes):
return re.findall(pattern, self.log.decode(errors="ignore"))

return re.findall(pattern, self.log)

def find(self, pattern: str, cond=amount_one) -> bool:
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ isort==5.12.0
black==23.1.0
inquirer
parameterized

clickhouse-connect==0.8.11
psutil~=5.9.8

# install mhddos requirements
Expand Down
Empty file added t_clickhouse/__init__.py
Empty file.
Loading