|
| 1 | +from collections import defaultdict |
| 2 | +from concurrent.futures import ThreadPoolExecutor |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from rest_framework.request import Request |
| 6 | +from rest_framework.response import Response |
| 7 | +from sentry_protos.snuba.v1.endpoint_trace_item_stats_pb2 import ( |
| 8 | + AttributeDistributionsRequest, |
| 9 | + StatsType, |
| 10 | + TraceItemStatsRequest, |
| 11 | +) |
| 12 | + |
| 13 | +from sentry import features |
| 14 | +from sentry.api.api_owners import ApiOwner |
| 15 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 16 | +from sentry.api.base import region_silo_endpoint |
| 17 | +from sentry.api.bases import NoProjects, OrganizationEventsV2EndpointBase |
| 18 | +from sentry.models.organization import Organization |
| 19 | +from sentry.search.eap.resolver import SearchResolver |
| 20 | +from sentry.search.eap.spans.definitions import SPAN_DEFINITIONS |
| 21 | +from sentry.search.eap.types import SearchResolverConfig, SupportedTraceItemType |
| 22 | +from sentry.search.eap.utils import translate_internal_to_public_alias |
| 23 | +from sentry.seer.workflows.compare import keyed_rrf_score |
| 24 | +from sentry.snuba.referrer import Referrer |
| 25 | +from sentry.snuba.spans_rpc import run_table_query |
| 26 | +from sentry.utils.snuba_rpc import trace_item_stats_rpc |
| 27 | + |
| 28 | +_query_thread_pool = ThreadPoolExecutor(max_workers=4) |
| 29 | + |
| 30 | + |
| 31 | +@region_silo_endpoint |
| 32 | +class OrganizationTraceItemsAttributesRankedEndpoint(OrganizationEventsV2EndpointBase): |
| 33 | + publish_status = { |
| 34 | + "GET": ApiPublishStatus.PRIVATE, |
| 35 | + } |
| 36 | + owner = ApiOwner.PERFORMANCE |
| 37 | + |
| 38 | + def get(self, request: Request, organization: Organization) -> Response: |
| 39 | + |
| 40 | + if not features.has( |
| 41 | + "organizations:performance-spans-suspect-attributes", organization, actor=request.user |
| 42 | + ): |
| 43 | + return Response(status=404) |
| 44 | + |
| 45 | + try: |
| 46 | + snuba_params = self.get_snuba_params(request, organization) |
| 47 | + except NoProjects: |
| 48 | + return Response({"rankedAttributes": []}) |
| 49 | + |
| 50 | + resolver = SearchResolver( |
| 51 | + params=snuba_params, config=SearchResolverConfig(), definitions=SPAN_DEFINITIONS |
| 52 | + ) |
| 53 | + |
| 54 | + meta = resolver.resolve_meta(referrer=Referrer.API_SPANS_FREQUENCY_STATS_RPC.value) |
| 55 | + query_1 = request.GET.get("query_1", "") |
| 56 | + query_2 = request.GET.get("query_2", "") |
| 57 | + |
| 58 | + if query_1 == query_2: |
| 59 | + return Response({"rankedAttributes": []}) |
| 60 | + |
| 61 | + cohort_1, _, _ = resolver.resolve_query(query_1) |
| 62 | + cohort_1_request = TraceItemStatsRequest( |
| 63 | + filter=cohort_1, |
| 64 | + meta=meta, |
| 65 | + stats_types=[ |
| 66 | + StatsType( |
| 67 | + attribute_distributions=AttributeDistributionsRequest( |
| 68 | + max_buckets=100, |
| 69 | + ) |
| 70 | + ) |
| 71 | + ], |
| 72 | + ) |
| 73 | + |
| 74 | + cohort_2, _, _ = resolver.resolve_query(query_2) |
| 75 | + cohort_2_request = TraceItemStatsRequest( |
| 76 | + filter=cohort_2, |
| 77 | + meta=meta, |
| 78 | + stats_types=[ |
| 79 | + StatsType( |
| 80 | + attribute_distributions=AttributeDistributionsRequest( |
| 81 | + max_buckets=100, |
| 82 | + ) |
| 83 | + ) |
| 84 | + ], |
| 85 | + ) |
| 86 | + |
| 87 | + cohort_1_future = _query_thread_pool.submit( |
| 88 | + trace_item_stats_rpc, |
| 89 | + cohort_1_request, |
| 90 | + ) |
| 91 | + totals_1_future = _query_thread_pool.submit( |
| 92 | + run_table_query, |
| 93 | + snuba_params, |
| 94 | + query_1, |
| 95 | + ["count(span.duration)"], |
| 96 | + None, |
| 97 | + config=SearchResolverConfig(use_aggregate_conditions=False), |
| 98 | + offset=0, |
| 99 | + limit=1, |
| 100 | + sampling_mode=snuba_params.sampling_mode, |
| 101 | + referrer=Referrer.API_SPAN_SAMPLE_GET_SPAN_DATA.value, |
| 102 | + ) |
| 103 | + |
| 104 | + cohort_2_future = _query_thread_pool.submit( |
| 105 | + trace_item_stats_rpc, |
| 106 | + cohort_2_request, |
| 107 | + ) |
| 108 | + |
| 109 | + totals_2_future = _query_thread_pool.submit( |
| 110 | + run_table_query, |
| 111 | + snuba_params, |
| 112 | + query_2, |
| 113 | + ["count(span.duration)"], |
| 114 | + None, |
| 115 | + config=SearchResolverConfig(use_aggregate_conditions=False), |
| 116 | + offset=0, |
| 117 | + limit=1, |
| 118 | + sampling_mode=snuba_params.sampling_mode, |
| 119 | + referrer=Referrer.API_SPAN_SAMPLE_GET_SPAN_DATA.value, |
| 120 | + ) |
| 121 | + |
| 122 | + cohort_1_data = cohort_1_future.result() |
| 123 | + cohort_2_data = cohort_2_future.result() |
| 124 | + totals_1_result = totals_1_future.result() |
| 125 | + totals_2_result = totals_2_future.result() |
| 126 | + |
| 127 | + cohort_1_distribution = [] |
| 128 | + cohort_1_distribution_map = defaultdict(list) |
| 129 | + for attribute in cohort_1_data.results[0].attribute_distributions.attributes: |
| 130 | + for bucket in attribute.buckets: |
| 131 | + cohort_1_distribution.append((attribute.attribute_name, bucket.label, bucket.value)) |
| 132 | + cohort_1_distribution_map[attribute.attribute_name].append( |
| 133 | + {"label": bucket.label, "value": bucket.value} |
| 134 | + ) |
| 135 | + |
| 136 | + cohort_2_distribution = [] |
| 137 | + cohort_2_distribution_map = defaultdict(list) |
| 138 | + for attribute in cohort_2_data.results[0].attribute_distributions.attributes: |
| 139 | + for bucket in attribute.buckets: |
| 140 | + cohort_2_distribution.append((attribute.attribute_name, bucket.label, bucket.value)) |
| 141 | + cohort_2_distribution_map[attribute.attribute_name].append( |
| 142 | + {"label": bucket.label, "value": bucket.value} |
| 143 | + ) |
| 144 | + |
| 145 | + scored_attrs = keyed_rrf_score( |
| 146 | + cohort_1_distribution, |
| 147 | + cohort_2_distribution, |
| 148 | + totals_1_result["data"][0]["count(span.duration)"], |
| 149 | + totals_2_result["data"][0]["count(span.duration)"], |
| 150 | + ) |
| 151 | + |
| 152 | + ranked_distribution: dict[str, list[dict[str, Any]]] = {"rankedAttributes": []} |
| 153 | + for attr, _ in scored_attrs: |
| 154 | + distribution = { |
| 155 | + "attributeName": translate_internal_to_public_alias( |
| 156 | + attr, "string", SupportedTraceItemType.SPANS |
| 157 | + ) |
| 158 | + or attr, |
| 159 | + "cohort1": cohort_1_distribution_map.get(attr), |
| 160 | + "cohort2": cohort_2_distribution_map.get(attr), |
| 161 | + } |
| 162 | + ranked_distribution["rankedAttributes"].append(distribution) |
| 163 | + |
| 164 | + return Response(ranked_distribution) |
0 commit comments