Skip to content

chore(logs): Add more tests #96481

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

Merged
merged 2 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/acceptance/test_explore_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

FEATURE_FLAGS = [
"organizations:ourlogs-enabled",
"organizations:ourlogs-visualize-sidebar",
"organizations:ourlogs-dashboards",
"organizations:ourlogs-alerts",
"organizations:ourlogs-infinite-scroll",
]


Expand Down
63 changes: 63 additions & 0 deletions tests/snuba/api/endpoints/test_organization_events_ourlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,66 @@ def test_pagelimit(self):
)
assert response.status_code == 400
assert response.data["detail"] == "Invalid per_page value. Must be between 1 and 9999."

def test_homepage_query(self):
"""This query matches the one made on the logs homepage so that we can be sure everything is working at least
for the initial load"""
logs = [
self.create_ourlog(
{"body": "foo"},
attributes={"sentry.observed_timestamp_nanos": str(self.ten_mins_ago.timestamp())},
timestamp=self.ten_mins_ago,
),
self.create_ourlog(
{"body": "bar"},
attributes={"sentry.observed_timestamp_nanos": str(self.nine_mins_ago.timestamp())},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Timestamp Units Mismatch in Sentry

The sentry.observed_timestamp_nanos attribute is incorrectly set to seconds. The timestamp() method returns seconds, but the attribute name implies nanoseconds. The value should be multiplied by 1,000,000,000 to convert it to nanoseconds, aligning with similar nanosecond-based timestamps in the codebase.

Locations (1)

Fix in CursorFix in Web

timestamp=self.nine_mins_ago,
),
]
self.store_ourlogs(logs)
response = self.do_request(
{
"cursor": "",
"dataset": "ourlogs",
"field": [
"sentry.item_id",
"project.id",
"trace",
"severity_number",
"severity",
"timestamp",
"tags[sentry.timestamp_precise,number]",
"sentry.observed_timestamp_nanos",
"message",
],
"per_page": 1000,
"project": self.project.id,
"query": "",
"referrer": "api.explore.logs-table",
"sort": "-timestamp",
"statsPeriod": "14d",
}
)
assert response.status_code == 200, response.content
data = response.data["data"]
meta = response.data["meta"]
assert len(data) == 2
for result, source in zip(data, reversed(logs)):
assert result == {
"sentry.item_id": UUID(bytes=bytes(reversed(source.item_id))).hex,
"project.id": self.project.id,
"trace": source.trace_id,
"severity_number": source.attributes["sentry.severity_number"].int_value,
"severity": source.attributes["sentry.severity_text"].string_value,
"timestamp": datetime.fromtimestamp(source.timestamp.seconds)
.replace(tzinfo=timezone.utc)
.isoformat(),
"tags[sentry.timestamp_precise,number]": pytest.approx(
source.attributes["sentry.timestamp_precise"].int_value
),
"sentry.observed_timestamp_nanos": source.attributes[
"sentry.observed_timestamp_nanos"
].string_value,
"message": source.attributes["sentry.body"].string_value,
}
assert meta["dataset"] == self.dataset
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from datetime import timedelta

from django.urls import reverse

from sentry.testutils.helpers.datetime import before_now
from tests.snuba.api.endpoints.test_organization_events import OrganizationEventsEndpointTestBase


class OrganizationEventsStatsOurlogsEndpointTest(OrganizationEventsEndpointTestBase):
endpoint = "sentry-api-0-organization-events-stats"

def setUp(self):
super().setUp()
self.login_as(user=self.user)
self.start = self.day_ago = before_now(days=1).replace(
hour=10, minute=0, second=0, microsecond=0
)
self.end = self.start + timedelta(hours=6)
self.two_days_ago = self.day_ago - timedelta(days=1)

self.url = reverse(
self.endpoint,
kwargs={"organization_id_or_slug": self.project.organization.slug},
)

def _do_request(self, data, url=None, features=None):
if features is None:
features = {"organizations:ourlogs": True}
features.update(self.features)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Test Setup Error: Missing Feature Initialization

The _do_request method attempts to use self.features without it being initialized in setUp, leading to an AttributeError. Additionally, the test uses the incorrect feature flag organizations:ourlogs instead of organizations:ourlogs-enabled, which will prevent the feature from being properly enabled during tests.

Locations (1)

Fix in CursorFix in Web

with self.feature(features):
return self.client.get(self.url if url is None else url, data=data, format="json")

def test_count(self):
event_counts = [6, 0, 6, 3, 0, 3]
logs = []
for hour, count in enumerate(event_counts):
logs.extend(
[
self.create_ourlog(
{"body": "foo"},
timestamp=self.start + timedelta(hours=hour, minutes=minute),
attributes={"status": {"string_value": "success"}},
)
for minute in range(count)
],
)
self.store_ourlogs(logs)

response = self._do_request(
data={
"start": self.start,
"end": self.end,
"interval": "1h",
"yAxis": "count()",
"project": self.project.id,
"dataset": "ourlogs",
},
)
assert response.status_code == 200, response.content
assert [attrs for time, attrs in response.data["data"]] == [
[{"count": count}] for count in event_counts
]

def test_zerofill(self):
response = self._do_request(
data={
"start": self.start,
"end": self.end,
"interval": "1h",
"yAxis": "count()",
"project": self.project.id,
"dataset": "ourlogs",
},
)
assert response.status_code == 200, response.content
assert [attrs for time, attrs in response.data["data"]] == [[{"count": 0}]] * 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Time Interval Test Mismatch

The test_zerofill method contains an off-by-one error, incorrectly expecting 7 intervals for a 6-hour time range with 1-hour intervals. This is inconsistent with the test_count method, which correctly expects 6 intervals for the identical time range.

Locations (1)

Fix in CursorFix in Web


def test_homepage_query(self):
"""This query matches the one made on the logs homepage so that we can be sure everything is working at least
for the initial load"""
response = self._do_request(
data={
"dataset": "ourlogs",
"excludeOther": 0,
"field": ["count(message)"],
"interval": "1h",
"orderby": "-count_message",
"partial": 1,
"per_page": 50,
"project": self.project.id,
"query": f"tags[sentry.timestamp_precise,number]:<={self.start.timestamp() * 1000000}",
"referrer": "explore.ourlogs.main-chart",
"sort": "-count_message",
"statsPeriod": "14d",
"yAxis": "count(message)",
},
)
assert response.status_code == 200, response.content
assert [attrs for time, attrs in response.data["data"]] == [[{"count": 0}]] * 338
Loading