test_organization_events_span_indexed.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. import uuid
  2. import pytest
  3. from tests.snuba.api.endpoints.test_organization_events import OrganizationEventsEndpointTestBase
  4. class OrganizationEventsSpanIndexedEndpointTest(OrganizationEventsEndpointTestBase):
  5. is_eap = False
  6. """Test the indexed spans dataset.
  7. To run this locally you may need to set the ENABLE_SPANS_CONSUMER flag to True in Snuba.
  8. A way to do this is
  9. 1. run: `sentry devservices down snuba`
  10. 2. clone snuba locally
  11. 3. run: `export ENABLE_SPANS_CONSUMER=True`
  12. 4. run snuba
  13. At this point tests should work locally
  14. Once span ingestion is on by default this will no longer need to be done
  15. """
  16. @property
  17. def dataset(self):
  18. if self.is_eap:
  19. return "spans"
  20. else:
  21. return "spansIndexed"
  22. def setUp(self):
  23. super().setUp()
  24. self.features = {
  25. "organizations:starfish-view": True,
  26. }
  27. @pytest.mark.querybuilder
  28. def test_simple(self):
  29. self.store_spans(
  30. [
  31. self.create_span(
  32. {"description": "foo", "sentry_tags": {"status": "success"}},
  33. start_ts=self.ten_mins_ago,
  34. ),
  35. self.create_span(
  36. {"description": "bar", "sentry_tags": {"status": "invalid_argument"}},
  37. start_ts=self.ten_mins_ago,
  38. ),
  39. ],
  40. is_eap=self.is_eap,
  41. )
  42. response = self.do_request(
  43. {
  44. "field": ["span.status", "description", "count()"],
  45. "query": "",
  46. "orderby": "description",
  47. "project": self.project.id,
  48. "dataset": self.dataset,
  49. }
  50. )
  51. assert response.status_code == 200, response.content
  52. data = response.data["data"]
  53. meta = response.data["meta"]
  54. assert len(data) == 2
  55. assert data == [
  56. {
  57. "span.status": "invalid_argument",
  58. "description": "bar",
  59. "count()": 1,
  60. },
  61. {
  62. "span.status": "ok",
  63. "description": "foo",
  64. "count()": 1,
  65. },
  66. ]
  67. assert meta["dataset"] == self.dataset
  68. def test_id_fields(self):
  69. self.store_spans(
  70. [
  71. self.create_span(
  72. {"description": "foo", "sentry_tags": {"status": "success"}},
  73. start_ts=self.ten_mins_ago,
  74. ),
  75. self.create_span(
  76. {"description": "bar", "sentry_tags": {"status": "invalid_argument"}},
  77. start_ts=self.ten_mins_ago,
  78. ),
  79. ],
  80. is_eap=self.is_eap,
  81. )
  82. response = self.do_request(
  83. {
  84. "field": ["id", "span_id"],
  85. "query": "",
  86. "orderby": "id",
  87. "project": self.project.id,
  88. "dataset": self.dataset,
  89. }
  90. )
  91. assert response.status_code == 200, response.content
  92. data = response.data["data"]
  93. meta = response.data["meta"]
  94. assert len(data) == 2
  95. for obj in data:
  96. assert obj["id"] == obj["span_id"]
  97. assert meta["dataset"] == self.dataset
  98. def test_sentry_tags_vs_tags(self):
  99. self.store_spans(
  100. [
  101. self.create_span(
  102. {"sentry_tags": {"transaction.method": "foo"}}, start_ts=self.ten_mins_ago
  103. ),
  104. ],
  105. is_eap=self.is_eap,
  106. )
  107. response = self.do_request(
  108. {
  109. "field": ["transaction.method", "count()"],
  110. "query": "",
  111. "orderby": "count()",
  112. "project": self.project.id,
  113. "dataset": self.dataset,
  114. }
  115. )
  116. assert response.status_code == 200, response.content
  117. data = response.data["data"]
  118. meta = response.data["meta"]
  119. assert len(data) == 1
  120. assert data[0]["transaction.method"] == "foo"
  121. assert meta["dataset"] == self.dataset
  122. def test_sentry_tags_syntax(self):
  123. self.store_spans(
  124. [
  125. self.create_span(
  126. {"sentry_tags": {"transaction.method": "foo"}}, start_ts=self.ten_mins_ago
  127. ),
  128. ],
  129. is_eap=self.is_eap,
  130. )
  131. response = self.do_request(
  132. {
  133. "field": ["sentry_tags[transaction.method]", "count()"],
  134. "query": "",
  135. "orderby": "count()",
  136. "project": self.project.id,
  137. "dataset": self.dataset,
  138. }
  139. )
  140. assert response.status_code == 200, response.content
  141. data = response.data["data"]
  142. meta = response.data["meta"]
  143. assert len(data) == 1
  144. assert data[0]["sentry_tags[transaction.method]"] == "foo"
  145. assert meta["dataset"] == self.dataset
  146. def test_module_alias(self):
  147. # Delegates `span.module` to `sentry_tags[category]`. Maps `"db.redis"` spans to the `"cache"` module
  148. self.store_spans(
  149. [
  150. self.create_span(
  151. {
  152. "op": "db.redis",
  153. "description": "EXEC *",
  154. "sentry_tags": {
  155. "description": "EXEC *",
  156. "category": "db",
  157. "op": "db.redis",
  158. "transaction": "/app/index",
  159. },
  160. },
  161. start_ts=self.ten_mins_ago,
  162. ),
  163. ],
  164. is_eap=self.is_eap,
  165. )
  166. response = self.do_request(
  167. {
  168. "field": ["span.module", "span.description"],
  169. "query": "span.module:cache",
  170. "project": self.project.id,
  171. "dataset": self.dataset,
  172. }
  173. )
  174. assert response.status_code == 200, response.content
  175. data = response.data["data"]
  176. meta = response.data["meta"]
  177. assert len(data) == 1
  178. assert data[0]["span.module"] == "cache"
  179. assert data[0]["span.description"] == "EXEC *"
  180. assert meta["dataset"] == self.dataset
  181. def test_device_class_filter_unknown(self):
  182. self.store_spans(
  183. [
  184. self.create_span({"sentry_tags": {"device.class": ""}}, start_ts=self.ten_mins_ago),
  185. ],
  186. is_eap=self.is_eap,
  187. )
  188. response = self.do_request(
  189. {
  190. "field": ["device.class", "count()"],
  191. "query": "device.class:Unknown",
  192. "orderby": "count()",
  193. "project": self.project.id,
  194. "dataset": self.dataset,
  195. }
  196. )
  197. assert response.status_code == 200, response.content
  198. data = response.data["data"]
  199. meta = response.data["meta"]
  200. assert len(data) == 1
  201. assert data[0]["device.class"] == "Unknown"
  202. assert meta["dataset"] == self.dataset
  203. def test_network_span(self):
  204. self.store_spans(
  205. [
  206. self.create_span(
  207. {
  208. "sentry_tags": {
  209. "action": "GET",
  210. "category": "http",
  211. "description": "GET https://*.resource.com",
  212. "domain": "*.resource.com",
  213. "op": "http.client",
  214. "status_code": "200",
  215. "transaction": "/api/0/data/",
  216. "transaction.method": "GET",
  217. "transaction.op": "http.server",
  218. }
  219. },
  220. start_ts=self.ten_mins_ago,
  221. ),
  222. ],
  223. is_eap=self.is_eap,
  224. )
  225. response = self.do_request(
  226. {
  227. "field": ["span.op", "span.status_code"],
  228. "query": "span.module:http span.status_code:200",
  229. "project": self.project.id,
  230. "dataset": self.dataset,
  231. }
  232. )
  233. assert response.status_code == 200, response.content
  234. data = response.data["data"]
  235. meta = response.data["meta"]
  236. assert len(data) == 1
  237. assert data[0]["span.op"] == "http.client"
  238. assert data[0]["span.status_code"] == "200"
  239. assert meta["dataset"] == self.dataset
  240. def test_other_category_span(self):
  241. self.store_spans(
  242. [
  243. self.create_span(
  244. {
  245. "sentry_tags": {
  246. "action": "GET",
  247. "category": "alternative",
  248. "description": "GET https://*.resource.com",
  249. "domain": "*.resource.com",
  250. "op": "alternative",
  251. "status_code": "200",
  252. "transaction": "/api/0/data/",
  253. "transaction.method": "GET",
  254. "transaction.op": "http.server",
  255. }
  256. },
  257. start_ts=self.ten_mins_ago,
  258. ),
  259. ],
  260. is_eap=self.is_eap,
  261. )
  262. response = self.do_request(
  263. {
  264. "field": ["span.op", "span.status_code"],
  265. "query": "span.module:other span.status_code:200",
  266. "project": self.project.id,
  267. "dataset": self.dataset,
  268. }
  269. )
  270. assert response.status_code == 200, response.content
  271. data = response.data["data"]
  272. meta = response.data["meta"]
  273. assert len(data) == 1
  274. assert data[0]["span.op"] == "alternative"
  275. assert data[0]["span.status_code"] == "200"
  276. assert meta["dataset"] == self.dataset
  277. def test_inp_span(self):
  278. replay_id = uuid.uuid4().hex
  279. self.store_spans(
  280. [
  281. self.create_span(
  282. {
  283. "sentry_tags": {
  284. "replay_id": replay_id,
  285. "browser.name": "Chrome",
  286. "transaction": "/pageloads/",
  287. }
  288. },
  289. start_ts=self.ten_mins_ago,
  290. ),
  291. ],
  292. is_eap=self.is_eap,
  293. )
  294. response = self.do_request(
  295. {
  296. "field": ["replay.id", "browser.name", "origin.transaction", "count()"],
  297. "query": f"replay.id:{replay_id} AND browser.name:Chrome AND origin.transaction:/pageloads/",
  298. "orderby": "count()",
  299. "project": self.project.id,
  300. "dataset": self.dataset,
  301. }
  302. )
  303. assert response.status_code == 200, response.content
  304. data = response.data["data"]
  305. meta = response.data["meta"]
  306. assert len(data) == 1
  307. assert data[0]["replay.id"] == replay_id
  308. assert data[0]["browser.name"] == "Chrome"
  309. assert data[0]["origin.transaction"] == "/pageloads/"
  310. assert meta["dataset"] == self.dataset
  311. def test_id_filtering(self):
  312. span = self.create_span({"description": "foo"}, start_ts=self.ten_mins_ago)
  313. self.store_span(span, is_eap=self.is_eap)
  314. response = self.do_request(
  315. {
  316. "field": ["description", "count()"],
  317. "query": f"id:{span['span_id']}",
  318. "orderby": "description",
  319. "project": self.project.id,
  320. "dataset": self.dataset,
  321. }
  322. )
  323. assert response.status_code == 200, response.content
  324. data = response.data["data"]
  325. meta = response.data["meta"]
  326. assert len(data) == 1
  327. assert data[0]["description"] == "foo"
  328. assert meta["dataset"] == self.dataset
  329. response = self.do_request(
  330. {
  331. "field": ["description", "count()"],
  332. "query": f"transaction.id:{span['event_id']}",
  333. "orderby": "description",
  334. "project": self.project.id,
  335. "dataset": self.dataset,
  336. }
  337. )
  338. assert response.status_code == 200, response.content
  339. data = response.data["data"]
  340. meta = response.data["meta"]
  341. assert len(data) == 1
  342. assert data[0]["description"] == "foo"
  343. assert meta["dataset"] == self.dataset
  344. def test_span_op_casing(self):
  345. self.store_spans(
  346. [
  347. self.create_span(
  348. {
  349. "sentry_tags": {
  350. "replay_id": "abc123",
  351. "browser.name": "Chrome",
  352. "transaction": "/pageloads/",
  353. "op": "this is a transaction",
  354. }
  355. },
  356. start_ts=self.ten_mins_ago,
  357. ),
  358. ],
  359. is_eap=self.is_eap,
  360. )
  361. response = self.do_request(
  362. {
  363. "field": ["span.op", "count()"],
  364. "query": 'span.op:"ThIs Is a TraNSActiON"',
  365. "orderby": "count()",
  366. "project": self.project.id,
  367. "dataset": self.dataset,
  368. }
  369. )
  370. assert response.status_code == 200, response.content
  371. data = response.data["data"]
  372. meta = response.data["meta"]
  373. assert len(data) == 1
  374. assert data[0]["span.op"] == "this is a transaction"
  375. assert meta["dataset"] == self.dataset
  376. def test_queue_span(self):
  377. self.store_spans(
  378. [
  379. self.create_span(
  380. {
  381. "measurements": {
  382. "messaging.message.body.size": {"value": 1024, "unit": "byte"},
  383. "messaging.message.receive.latency": {
  384. "value": 1000,
  385. "unit": "millisecond",
  386. },
  387. "messaging.message.retry.count": {"value": 2, "unit": "none"},
  388. },
  389. "sentry_tags": {
  390. "transaction": "queue-processor",
  391. "messaging.destination.name": "events",
  392. "messaging.message.id": "abc123",
  393. "trace.status": "ok",
  394. },
  395. },
  396. start_ts=self.ten_mins_ago,
  397. ),
  398. ],
  399. is_eap=self.is_eap,
  400. )
  401. response = self.do_request(
  402. {
  403. "field": [
  404. "transaction",
  405. "messaging.destination.name",
  406. "messaging.message.id",
  407. "measurements.messaging.message.receive.latency",
  408. "measurements.messaging.message.body.size",
  409. "measurements.messaging.message.retry.count",
  410. "trace.status",
  411. "count()",
  412. ],
  413. "query": 'messaging.destination.name:"events"',
  414. "orderby": "count()",
  415. "project": self.project.id,
  416. "dataset": self.dataset,
  417. }
  418. )
  419. assert response.status_code == 200, response.content
  420. data = response.data["data"]
  421. meta = response.data["meta"]
  422. assert len(data) == 1
  423. assert data[0]["transaction"] == "queue-processor"
  424. assert data[0]["messaging.destination.name"] == "events"
  425. assert data[0]["messaging.message.id"] == "abc123"
  426. assert data[0]["trace.status"] == "ok"
  427. assert data[0]["measurements.messaging.message.receive.latency"] == 1000
  428. assert data[0]["measurements.messaging.message.body.size"] == 1024
  429. assert data[0]["measurements.messaging.message.retry.count"] == 2
  430. assert meta["dataset"] == self.dataset
  431. def test_tag_wildcards(self):
  432. self.store_spans(
  433. [
  434. self.create_span(
  435. {"description": "foo", "tags": {"foo": "BaR"}},
  436. start_ts=self.ten_mins_ago,
  437. ),
  438. self.create_span(
  439. {"description": "qux", "tags": {"foo": "QuX"}},
  440. start_ts=self.ten_mins_ago,
  441. ),
  442. ],
  443. is_eap=self.is_eap,
  444. )
  445. for query in [
  446. "foo:b*",
  447. "foo:*r",
  448. "foo:*a*",
  449. "foo:b*r",
  450. ]:
  451. response = self.do_request(
  452. {
  453. "field": ["foo", "count()"],
  454. "query": query,
  455. "project": self.project.id,
  456. "dataset": self.dataset,
  457. }
  458. )
  459. assert response.status_code == 200, response.content
  460. assert response.data["data"] == [{"foo": "BaR", "count()": 1}]
  461. def test_query_for_missing_tag(self):
  462. self.store_spans(
  463. [
  464. self.create_span(
  465. {"description": "foo"},
  466. start_ts=self.ten_mins_ago,
  467. ),
  468. self.create_span(
  469. {"description": "qux", "tags": {"foo": "bar"}},
  470. start_ts=self.ten_mins_ago,
  471. ),
  472. ],
  473. is_eap=self.is_eap,
  474. )
  475. response = self.do_request(
  476. {
  477. "field": ["foo", "count()"],
  478. "query": 'foo:""',
  479. "project": self.project.id,
  480. "dataset": self.dataset,
  481. }
  482. )
  483. assert response.status_code == 200, response.content
  484. assert response.data["data"] == [{"foo": "", "count()": 1}]
  485. @pytest.mark.xfail(
  486. reason="Snuba is not stable for the EAP dataset, xfailing since its prone to failure"
  487. )
  488. class OrganizationEventsEAPSpanEndpointTest(OrganizationEventsSpanIndexedEndpointTest):
  489. is_eap = True
  490. def test_simple(self):
  491. self.store_spans(
  492. [
  493. self.create_span(
  494. {"description": "foo", "sentry_tags": {"status": "success"}},
  495. start_ts=self.ten_mins_ago,
  496. ),
  497. self.create_span(
  498. {"description": "bar", "sentry_tags": {"status": "invalid_argument"}},
  499. start_ts=self.ten_mins_ago,
  500. ),
  501. ],
  502. is_eap=self.is_eap,
  503. )
  504. response = self.do_request(
  505. {
  506. "field": ["span.status", "description", "count()"],
  507. "query": "",
  508. "orderby": "description",
  509. "project": self.project.id,
  510. "dataset": self.dataset,
  511. }
  512. )
  513. assert response.status_code == 200, response.content
  514. data = response.data["data"]
  515. meta = response.data["meta"]
  516. assert len(data) == 2
  517. assert data == [
  518. {
  519. "span.status": "invalid_argument",
  520. "description": "bar",
  521. "count()": 1,
  522. },
  523. {
  524. "span.status": "success",
  525. "description": "foo",
  526. "count()": 1,
  527. },
  528. ]
  529. assert meta["dataset"] == self.dataset
  530. @pytest.mark.xfail(reason="event_id isn't being written to the new table")
  531. def test_id_filtering(self):
  532. super().test_id_filtering()
  533. def test_span_duration(self):
  534. self.store_spans(
  535. [
  536. self.create_span(
  537. {"description": "foo", "sentry_tags": {"status": "success"}},
  538. start_ts=self.ten_mins_ago,
  539. ),
  540. self.create_span(
  541. {"description": "bar", "sentry_tags": {"status": "invalid_argument"}},
  542. start_ts=self.ten_mins_ago,
  543. ),
  544. ],
  545. is_eap=self.is_eap,
  546. )
  547. response = self.do_request(
  548. {
  549. "field": ["span.duration", "description"],
  550. "query": "",
  551. "orderby": "description",
  552. "project": self.project.id,
  553. "dataset": self.dataset,
  554. }
  555. )
  556. assert response.status_code == 200, response.content
  557. data = response.data["data"]
  558. meta = response.data["meta"]
  559. assert len(data) == 2
  560. assert data == [
  561. {
  562. "span.duration": 1000.0,
  563. "description": "bar",
  564. },
  565. {
  566. "span.duration": 1000.0,
  567. "description": "foo",
  568. },
  569. ]
  570. assert meta["dataset"] == self.dataset
  571. def test_extrapolation_smoke(self):
  572. """This is a hack, we just want to make sure nothing errors from using the weighted functions"""
  573. for function in [
  574. "count_weighted()",
  575. "sum_weighted(span.duration)",
  576. "avg_weighted(span.duration)",
  577. "percentile_weighted(span.duration, 0.23)",
  578. "p50_weighted()",
  579. "p75_weighted()",
  580. "p90_weighted()",
  581. "p95_weighted()",
  582. "p99_weighted()",
  583. "p100_weighted()",
  584. "min_weighted(span.duration)",
  585. "max_weighted(span.duration)",
  586. ]:
  587. response = self.do_request(
  588. {
  589. "field": ["description", function],
  590. "query": "",
  591. "orderby": "description",
  592. "project": self.project.id,
  593. "dataset": self.dataset,
  594. }
  595. )
  596. assert response.status_code == 200, f"error: {response.content}\naggregate: {function}"
  597. def test_numeric_attr_without_space(self):
  598. self.store_spans(
  599. [
  600. self.create_span(
  601. {
  602. "description": "foo",
  603. "sentry_tags": {"status": "success"},
  604. "tags": {"foo": "five"},
  605. },
  606. measurements={"foo": {"value": 5}},
  607. start_ts=self.ten_mins_ago,
  608. ),
  609. ],
  610. is_eap=self.is_eap,
  611. )
  612. response = self.do_request(
  613. {
  614. "field": ["description", "tags[foo,number]", "tags[foo,string]", "tags[foo]"],
  615. "query": "",
  616. "orderby": "description",
  617. "project": self.project.id,
  618. "dataset": self.dataset,
  619. }
  620. )
  621. assert response.status_code == 200, response.content
  622. assert len(response.data["data"]) == 1
  623. data = response.data["data"]
  624. assert data[0]["tags[foo,number]"] == 5
  625. assert data[0]["tags[foo,string]"] == "five"
  626. assert data[0]["tags[foo]"] == "five"
  627. def test_numeric_attr_with_spaces(self):
  628. self.store_spans(
  629. [
  630. self.create_span(
  631. {
  632. "description": "foo",
  633. "sentry_tags": {"status": "success"},
  634. "tags": {"foo": "five"},
  635. },
  636. measurements={"foo": {"value": 5}},
  637. start_ts=self.ten_mins_ago,
  638. ),
  639. ],
  640. is_eap=self.is_eap,
  641. )
  642. response = self.do_request(
  643. {
  644. "field": ["description", "tags[foo, number]", "tags[foo, string]", "tags[foo]"],
  645. "query": "",
  646. "orderby": "description",
  647. "project": self.project.id,
  648. "dataset": self.dataset,
  649. }
  650. )
  651. assert response.status_code == 200, response.content
  652. assert len(response.data["data"]) == 1
  653. data = response.data["data"]
  654. assert data[0]["tags[foo, number]"] == 5
  655. assert data[0]["tags[foo, string]"] == "five"
  656. assert data[0]["tags[foo]"] == "five"
  657. def test_numeric_attr_filtering(self):
  658. self.store_spans(
  659. [
  660. self.create_span(
  661. {
  662. "description": "foo",
  663. "sentry_tags": {"status": "success"},
  664. "tags": {"foo": "five"},
  665. },
  666. measurements={"foo": {"value": 5}},
  667. start_ts=self.ten_mins_ago,
  668. ),
  669. self.create_span(
  670. {"description": "bar", "sentry_tags": {"status": "success", "foo": "five"}},
  671. measurements={"foo": {"value": 8}},
  672. start_ts=self.ten_mins_ago,
  673. ),
  674. ],
  675. is_eap=self.is_eap,
  676. )
  677. response = self.do_request(
  678. {
  679. "field": ["description", "tags[foo,number]"],
  680. "query": "tags[foo,number]:5",
  681. "orderby": "description",
  682. "project": self.project.id,
  683. "dataset": self.dataset,
  684. }
  685. )
  686. assert response.status_code == 200, response.content
  687. assert len(response.data["data"]) == 1
  688. data = response.data["data"]
  689. assert data[0]["tags[foo,number]"] == 5
  690. assert data[0]["description"] == "foo"
  691. def test_long_attr_name(self):
  692. response = self.do_request(
  693. {
  694. "field": ["description", "z" * 201],
  695. "query": "",
  696. "orderby": "description",
  697. "project": self.project.id,
  698. "dataset": self.dataset,
  699. }
  700. )
  701. assert response.status_code == 400, response.content
  702. assert "Is Too Long" in response.data["detail"].title()
  703. def test_numeric_attr_orderby(self):
  704. self.store_spans(
  705. [
  706. self.create_span(
  707. {
  708. "description": "baz",
  709. "sentry_tags": {"status": "success"},
  710. "tags": {"foo": "five"},
  711. },
  712. measurements={"foo": {"value": 71}},
  713. start_ts=self.ten_mins_ago,
  714. ),
  715. self.create_span(
  716. {
  717. "description": "foo",
  718. "sentry_tags": {"status": "success"},
  719. "tags": {"foo": "five"},
  720. },
  721. measurements={"foo": {"value": 5}},
  722. start_ts=self.ten_mins_ago,
  723. ),
  724. self.create_span(
  725. {
  726. "description": "bar",
  727. "sentry_tags": {"status": "success"},
  728. "tags": {"foo": "five"},
  729. },
  730. measurements={"foo": {"value": 8}},
  731. start_ts=self.ten_mins_ago,
  732. ),
  733. ],
  734. is_eap=self.is_eap,
  735. )
  736. response = self.do_request(
  737. {
  738. "field": ["description", "tags[foo,number]"],
  739. "query": "",
  740. "orderby": ["tags[foo,number]"],
  741. "project": self.project.id,
  742. "dataset": self.dataset,
  743. }
  744. )
  745. assert response.status_code == 200, response.content
  746. assert len(response.data["data"]) == 3
  747. data = response.data["data"]
  748. assert data[0]["tags[foo,number]"] == 5
  749. assert data[0]["description"] == "foo"
  750. assert data[1]["tags[foo,number]"] == 8
  751. assert data[1]["description"] == "bar"
  752. assert data[2]["tags[foo,number]"] == 71
  753. assert data[2]["description"] == "baz"
  754. def test_aggregate_numeric_attr(self):
  755. self.store_spans(
  756. [
  757. self.create_span(
  758. {
  759. "description": "foo",
  760. "sentry_tags": {"status": "success"},
  761. "tags": {"bar": "bar1"},
  762. },
  763. start_ts=self.ten_mins_ago,
  764. ),
  765. self.create_span(
  766. {
  767. "description": "foo",
  768. "sentry_tags": {"status": "success"},
  769. "tags": {"bar": "bar2"},
  770. },
  771. measurements={"foo": {"value": 5}},
  772. start_ts=self.ten_mins_ago,
  773. ),
  774. ],
  775. is_eap=self.is_eap,
  776. )
  777. response = self.do_request(
  778. {
  779. "field": [
  780. "description",
  781. "count_unique(bar)",
  782. "count_unique(tags[bar])",
  783. "count_unique(tags[bar,string])",
  784. "count()",
  785. "count(span.duration)",
  786. "count(tags[foo, number])",
  787. "sum(tags[foo,number])",
  788. "avg(tags[foo,number])",
  789. "p50(tags[foo,number])",
  790. "p75(tags[foo,number])",
  791. "p95(tags[foo,number])",
  792. "p99(tags[foo,number])",
  793. "p100(tags[foo,number])",
  794. "min(tags[foo,number])",
  795. "max(tags[foo,number])",
  796. ],
  797. "query": "",
  798. "orderby": "description",
  799. "project": self.project.id,
  800. "dataset": self.dataset,
  801. }
  802. )
  803. assert response.status_code == 200, response.content
  804. assert len(response.data["data"]) == 1
  805. data = response.data["data"]
  806. assert data[0] == {
  807. "description": "foo",
  808. "count_unique(bar)": 2,
  809. "count_unique(tags[bar])": 2,
  810. "count_unique(tags[bar,string])": 2,
  811. "count()": 2,
  812. "count(span.duration)": 2,
  813. "count(tags[foo, number])": 1,
  814. "sum(tags[foo,number])": 5.0,
  815. "avg(tags[foo,number])": 5.0,
  816. "p50(tags[foo,number])": 5.0,
  817. "p75(tags[foo,number])": 5.0,
  818. "p95(tags[foo,number])": 5.0,
  819. "p99(tags[foo,number])": 5.0,
  820. "p100(tags[foo,number])": 5.0,
  821. "min(tags[foo,number])": 5.0,
  822. "max(tags[foo,number])": 5.0,
  823. }
  824. def test_margin_of_error(self):
  825. total_samples = 10
  826. in_group = 5
  827. spans = []
  828. for _ in range(in_group):
  829. spans.append(
  830. self.create_span(
  831. {
  832. "description": "foo",
  833. "sentry_tags": {"status": "success"},
  834. "measurements": {"client_sample_rate": {"value": 0.00001}},
  835. },
  836. start_ts=self.ten_mins_ago,
  837. )
  838. )
  839. for _ in range(total_samples - in_group):
  840. spans.append(
  841. self.create_span(
  842. {
  843. "description": "bar",
  844. "sentry_tags": {"status": "success"},
  845. "measurements": {"client_sample_rate": {"value": 0.00001}},
  846. },
  847. )
  848. )
  849. self.store_spans(
  850. spans,
  851. is_eap=self.is_eap,
  852. )
  853. response = self.do_request(
  854. {
  855. "field": [
  856. "margin_of_error()",
  857. "lower_count_limit()",
  858. "upper_count_limit()",
  859. "count_weighted()",
  860. ],
  861. "query": "description:foo",
  862. "project": self.project.id,
  863. "dataset": self.dataset,
  864. }
  865. )
  866. assert response.status_code == 200, response.content
  867. assert len(response.data["data"]) == 1
  868. data = response.data["data"][0]
  869. margin_of_error = data["margin_of_error()"]
  870. lower_limit = data["lower_count_limit()"]
  871. upper_limit = data["upper_count_limit()"]
  872. extrapolated = data["count_weighted()"]
  873. assert margin_of_error == pytest.approx(0.306, rel=1e-1)
  874. # How to read this; these results mean that the extrapolated count is
  875. # 500k, with a lower estimated bound of ~200k, and an upper bound of 800k
  876. assert lower_limit == pytest.approx(190_000, abs=5000)
  877. assert extrapolated == pytest.approx(500_000)
  878. assert upper_limit == pytest.approx(810_000, abs=5000)