123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- import pytest
- from django.urls import reverse
- from sentry.search.events import constants
- from sentry.testutils.cases import MetricsEnhancedPerformanceTestCase
- from sentry.testutils.helpers.datetime import before_now
- from sentry.testutils.silo import region_silo_test
- pytestmark = pytest.mark.sentry_metrics
- class OrganizationEventsMetricsEnhancedPerformanceEndpointTest(MetricsEnhancedPerformanceTestCase):
- viewname = "sentry-api-0-organization-events"
- # Poor intentionally omitted for test_measurement_rating_that_does_not_exist
- METRIC_STRINGS = [
- "foo_transaction",
- "bar_transaction",
- ]
- def setUp(self):
- super().setUp()
- self.min_ago = before_now(minutes=1)
- self.six_min_ago = before_now(minutes=6)
- self.three_days_ago = before_now(days=3)
- self.features = {
- "organizations:starfish-view": True,
- }
- def do_request(self, query, features=None):
- if features is None:
- features = {"organizations:discover-basic": True}
- features.update(self.features)
- self.login_as(user=self.user)
- url = reverse(
- self.viewname,
- kwargs={"organization_slug": self.organization.slug},
- )
- with self.feature(features):
- return self.client.get(url, query, format="json")
- def test_p50_with_no_data(self):
- response = self.do_request(
- {
- "field": ["p50()"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["p50()"] == 0
- assert meta["dataset"] == "spansMetrics"
- def test_count(self):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.three_days_ago,
- )
- response = self.do_request(
- {
- "field": ["count()"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "7d",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["count()"] == 1
- assert meta["dataset"] == "spansMetrics"
- def test_count_unique(self):
- self.store_span_metric(
- 1,
- "user",
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 2,
- "user",
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["count_unique(user)"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["count_unique(user)"] == 2
- assert meta["dataset"] == "spansMetrics"
- def test_sum(self):
- self.store_span_metric(
- 321,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 99,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["sum(span.self_time)"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["sum(span.self_time)"] == 420
- assert meta["dataset"] == "spansMetrics"
- def test_percentile(self):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["percentile(span.self_time, 0.95)"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["percentile(span.self_time, 0.95)"] == 1
- assert meta["dataset"] == "spansMetrics"
- def test_fixed_percentile_functions(self):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- for function in ["p50()", "p75()", "p95()", "p99()", "p100()"]:
- response = self.do_request(
- {
- "field": [function],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0][function] == 1, function
- assert meta["dataset"] == "spansMetrics", function
- assert meta["fields"][function] == "duration", function
- def test_fixed_percentile_functions_with_duration(self):
- self.store_span_metric(
- 1,
- internal_metric=constants.SPAN_METRICS_MAP["span.duration"],
- timestamp=self.min_ago,
- )
- for function in [
- "p50(span.duration)",
- "p75(span.duration)",
- "p95(span.duration)",
- "p99(span.duration)",
- "p100(span.duration)",
- ]:
- response = self.do_request(
- {
- "field": [function],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1, function
- assert data[0][function] == 1, function
- assert meta["dataset"] == "spansMetrics", function
- assert meta["fields"][function] == "duration", function
- def test_avg(self):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["avg()"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["avg()"] == 1
- assert meta["dataset"] == "spansMetrics"
- def test_eps(self):
- for _ in range(6):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["eps()", "sps()"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["eps()"] == 0.01
- assert data[0]["sps()"] == 0.01
- assert meta["fields"]["eps()"] == "rate"
- assert meta["fields"]["sps()"] == "rate"
- assert meta["units"]["eps()"] == "1/second"
- assert meta["units"]["sps()"] == "1/second"
- assert meta["dataset"] == "spansMetrics"
- def test_epm(self):
- for _ in range(6):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["epm()", "spm()"],
- "query": "",
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["epm()"] == 0.6
- assert data[0]["spm()"] == 0.6
- assert meta["fields"]["epm()"] == "rate"
- assert meta["fields"]["spm()"] == "rate"
- assert meta["units"]["epm()"] == "1/minute"
- assert meta["units"]["spm()"] == "1/minute"
- assert meta["dataset"] == "spansMetrics"
- def test_time_spent_percentage(self):
- for _ in range(4):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 1,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 1,
- tags={"transaction": "bar_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["transaction", "time_spent_percentage()"],
- "query": "",
- "orderby": ["-time_spent_percentage()"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 2
- assert data[0]["time_spent_percentage()"] == 0.8
- assert data[0]["transaction"] == "foo_transaction"
- assert data[1]["time_spent_percentage()"] == 0.2
- assert data[1]["transaction"] == "bar_transaction"
- assert meta["dataset"] == "spansMetrics"
- def test_time_spent_percentage_local(self):
- response = self.do_request(
- {
- "field": ["time_spent_percentage(local)"],
- "query": "",
- "orderby": ["-time_spent_percentage(local)"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["time_spent_percentage(local)"] is None
- assert meta["dataset"] == "spansMetrics"
- def test_http_error_rate_and_count(self):
- for _ in range(4):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- tags={"span.status_code": "500"},
- timestamp=self.min_ago,
- )
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- tags={"span.status_code": "200"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["http_error_count()", "http_error_rate()"],
- "query": "",
- "orderby": ["-http_error_rate()"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["http_error_rate()"] == 0.8
- assert meta["dataset"] == "spansMetrics"
- assert meta["fields"]["http_error_count()"] == "integer"
- assert meta["fields"]["http_error_rate()"] == "percentage"
- def test_use_self_time_light(self):
- self.store_span_metric(
- 100,
- internal_metric=constants.SELF_TIME_LIGHT,
- tags={"transaction": "foo_transaction"},
- timestamp=self.min_ago,
- )
- response = self.do_request(
- {
- "field": ["p50(span.self_time)"],
- # Should be 0 since its filtering on transaction
- "query": "transaction:foo_transaction",
- "orderby": ["-p50(span.self_time)"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["p50(span.self_time)"] == 0
- assert meta["dataset"] == "spansMetrics"
- assert meta["fields"]["p50(span.self_time)"] == "duration"
- response = self.do_request(
- {
- # Should be 0 since it has a transaction column
- "field": ["transaction", "p50(span.self_time)"],
- "query": "",
- "orderby": ["-p50(span.self_time)"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 0
- response = self.do_request(
- {
- "field": ["p50(span.self_time)"],
- # Should be 100 since its not filtering on transaction
- "query": "",
- "orderby": ["-p50(span.self_time)"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["p50(span.self_time)"] == 100
- assert meta["dataset"] == "spansMetrics"
- assert meta["fields"]["p50(span.self_time)"] == "duration"
- def test_span_module(self):
- self.store_span_metric(
- 1,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.six_min_ago,
- tags={"span.category": "http", "span.description": "f"},
- )
- self.store_span_metric(
- 3,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.six_min_ago,
- tags={"span.category": "db", "span.description": "e"},
- )
- self.store_span_metric(
- 5,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.six_min_ago,
- tags={"span.category": "foobar", "span.description": "d"},
- )
- self.store_span_metric(
- 7,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.six_min_ago,
- tags={"span.category": "cache", "span.description": "c"},
- )
- self.store_span_metric(
- 9,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.six_min_ago,
- tags={"span.category": "db", "span.op": "db.redis", "span.description": "b"},
- )
- self.store_span_metric(
- 11,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.six_min_ago,
- tags={"span.category": "db", "span.op": "db.sql.room", "span.description": "a"},
- )
- response = self.do_request(
- {
- "field": ["span.module", "span.description", "p50(span.self_time)"],
- "query": "",
- "orderby": ["-p50(span.self_time)"],
- "project": self.project.id,
- "dataset": "spansMetrics",
- "statsPeriod": "10m",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 6
- assert data[0]["p50(span.self_time)"] == 11
- assert data[0]["span.module"] == "other"
- assert data[0]["span.description"] == "a"
- assert data[1]["p50(span.self_time)"] == 9
- assert data[1]["span.module"] == "cache"
- assert data[1]["span.description"] == "b"
- assert data[2]["p50(span.self_time)"] == 7
- assert data[2]["span.module"] == "cache"
- assert data[2]["span.description"] == "c"
- assert data[3]["p50(span.self_time)"] == 5
- assert data[3]["span.module"] == "other"
- assert data[3]["span.description"] == "d"
- assert data[4]["p50(span.self_time)"] == 3
- assert data[4]["span.module"] == "db"
- assert data[4]["span.description"] == "e"
- assert data[5]["p50(span.self_time)"] == 1
- assert data[5]["span.module"] == "http"
- assert data[5]["span.description"] == "f"
- assert meta["dataset"] == "spansMetrics"
- assert meta["fields"]["p50(span.self_time)"] == "duration"
- def test_tag_search(self):
- self.store_span_metric(
- 321,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- tags={"span.description": "foo"},
- )
- self.store_span_metric(
- 99,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- tags={"span.description": "bar"},
- )
- response = self.do_request(
- {
- "field": ["sum(span.self_time)"],
- "query": "span.description:bar",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["sum(span.self_time)"] == 99
- assert meta["dataset"] == "spansMetrics"
- def test_free_text_search(self):
- self.store_span_metric(
- 321,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- tags={"span.description": "foo"},
- )
- self.store_span_metric(
- 99,
- internal_metric=constants.SELF_TIME_LIGHT,
- timestamp=self.min_ago,
- tags={"span.description": "bar"},
- )
- response = self.do_request(
- {
- "field": ["sum(span.self_time)"],
- "query": "foo",
- "project": self.project.id,
- "dataset": "spansMetrics",
- }
- )
- assert response.status_code == 200, response.content
- data = response.data["data"]
- meta = response.data["meta"]
- assert len(data) == 1
- assert data[0]["sum(span.self_time)"] == 321
- assert meta["dataset"] == "spansMetrics"
- @region_silo_test
- class OrganizationEventsMetricsEnhancedPerformanceEndpointTestWithMetricLayer(
- OrganizationEventsMetricsEnhancedPerformanceEndpointTest
- ):
- def setUp(self):
- super().setUp()
- self.features["organizations:use-metrics-layer"] = True
- @pytest.mark.xfail(reason="Not implemented")
- def test_time_spent_percentage(self):
- super().test_time_spent_percentage()
- @pytest.mark.xfail(reason="Not implemented")
- def test_time_spent_percentage_local(self):
- super().test_time_spent_percentage_local()
- @pytest.mark.xfail(reason="Not implemented")
- def test_http_error_rate_and_count(self):
- super().test_http_error_rate_and_count()
- @pytest.mark.xfail(reason="Cannot group by function 'if'")
- def test_span_module(self):
- super().test_span_module()
- @pytest.mark.xfail(reason="Cannot search by tags")
- def test_tag_search(self):
- super().test_tag_search()
- @pytest.mark.xfail(reason="Cannot search by tags")
- def test_free_text_search(self):
- super().test_free_text_search()
|