Skip to content

Commit

Permalink
fix: Read all logs is case insensitive (#18648)
Browse files Browse the repository at this point in the history
* fix: Read all logs is case insensitive

* Update query snapshots

* Update query snapshots

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
tomasfarias and github-actions[bot] authored Nov 15, 2023
1 parent 76d085f commit 390f6c7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
48 changes: 48 additions & 0 deletions posthog/api/test/batch_exports/test_log_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,54 @@ def test_log_level_filter(batch_export, team, level):
assert results[1].batch_export_id == str(batch_export["id"])


@pytest.mark.django_db
@pytest.mark.parametrize(
"level",
[
BatchExportLogEntryLevel.INFO,
BatchExportLogEntryLevel.WARNING,
BatchExportLogEntryLevel.ERROR,
BatchExportLogEntryLevel.DEBUG,
],
)
def test_log_level_filter_with_lowercase(batch_export, team, level):
"""Test fetching a batch export log entries of a particular level."""
with freeze_time("2023-09-22 01:00:00"):
for message in ("Test log 1", "Test log 2"):
create_batch_export_log_entry(
team_id=team.pk,
batch_export_id=str(batch_export["id"]),
run_id=None,
message=message,
level=level.lower(),
)

results = []
timeout = 10
start = dt.datetime.utcnow()

while not results:
results = fetch_batch_export_log_entries(
team_id=team.pk,
batch_export_id=batch_export["id"],
level_filter=[level],
after=dt.datetime(2023, 9, 22, 0, 59, 59),
before=dt.datetime(2023, 9, 22, 1, 0, 1),
)
if (dt.datetime.utcnow() - start) > dt.timedelta(seconds=timeout):
break

results.sort(key=lambda record: record.message)

assert len(results) == 2
assert results[0].message == "Test log 1"
assert results[0].level == level
assert results[0].batch_export_id == str(batch_export["id"])
assert results[1].message == "Test log 2"
assert results[1].level == level
assert results[1].batch_export_id == str(batch_export["id"])


@pytest.mark.django_db
def test_batch_export_log_api(client, batch_export, team):
"""Test fetching batch export log entries using the API."""
Expand Down
4 changes: 2 additions & 2 deletions posthog/batch_exports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ def fetch_batch_export_log_entries(
clickhouse_where_parts.append("message ILIKE %(search)s")
clickhouse_kwargs["search"] = f"%{search}%"
if len(level_filter) > 0:
clickhouse_where_parts.append("level in %(levels)s")
clickhouse_where_parts.append("upper(level) in %(levels)s")
clickhouse_kwargs["levels"] = level_filter

clickhouse_query = f"""
SELECT team_id, log_source_id AS batch_export_id, instance_id AS run_id, timestamp, level, message FROM log_entries
SELECT team_id, log_source_id AS batch_export_id, instance_id AS run_id, timestamp, upper(level) as level, message FROM log_entries
WHERE {' AND '.join(clickhouse_where_parts)} ORDER BY timestamp DESC {f'LIMIT {limit}' if limit else ''}
"""

Expand Down

0 comments on commit 390f6c7

Please sign in to comment.