|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import html |
| 4 | +import logging |
| 5 | +import re |
| 6 | +from collections.abc import Mapping |
| 7 | +from typing import Any |
| 8 | +from urllib.parse import urlparse |
| 9 | + |
| 10 | +from django.http.request import QueryDict |
| 11 | + |
| 12 | +from sentry import analytics, features |
| 13 | +from sentry.api import client |
| 14 | +from sentry.charts import backend as charts |
| 15 | +from sentry.charts.types import ChartType |
| 16 | +from sentry.integrations.messaging.metrics import ( |
| 17 | + MessagingInteractionEvent, |
| 18 | + MessagingInteractionType, |
| 19 | +) |
| 20 | +from sentry.integrations.models.integration import Integration |
| 21 | +from sentry.integrations.services.integration import RpcIntegration, integration_service |
| 22 | +from sentry.integrations.slack.analytics import SlackIntegrationChartUnfurl |
| 23 | +from sentry.integrations.slack.message_builder.discover import SlackDiscoverMessageBuilder |
| 24 | +from sentry.integrations.slack.spec import SlackMessagingSpec |
| 25 | +from sentry.integrations.slack.unfurl.types import Handler, UnfurlableUrl, UnfurledUrl |
| 26 | +from sentry.models.apikey import ApiKey |
| 27 | +from sentry.models.organization import Organization |
| 28 | +from sentry.snuba.referrer import Referrer |
| 29 | +from sentry.users.models.user import User |
| 30 | +from sentry.users.services.user import RpcUser |
| 31 | +from sentry.utils import json |
| 32 | + |
| 33 | +_logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | +DEFAULT_PERIOD = "14d" |
| 36 | +DEFAULT_Y_AXIS = "count(span.duration)" |
| 37 | + |
| 38 | +# All `multiPlotType: line` fields in /static/app/utils/discover/fields.tsx |
| 39 | +LINE_PLOT_FIELDS = { |
| 40 | + "count_unique", |
| 41 | + "min", |
| 42 | + "max", |
| 43 | + "p50", |
| 44 | + "p75", |
| 45 | + "p90", |
| 46 | + "p95", |
| 47 | + "p99", |
| 48 | + "p100", |
| 49 | + "percentile", |
| 50 | + "avg", |
| 51 | +} |
| 52 | + |
| 53 | +TOP_N = 5 |
| 54 | + |
| 55 | + |
| 56 | +def _serialize_single_series(series: dict[str, Any]) -> dict[str, Any]: |
| 57 | + """Convert a single TimeSeries into events-stats format.""" |
| 58 | + values = series.get("values", []) |
| 59 | + data = [] |
| 60 | + for row in values: |
| 61 | + # events-timeseries uses milliseconds, events-stats uses seconds |
| 62 | + timestamp = int(row["timestamp"] / 1000) |
| 63 | + data.append((timestamp, [{"count": row.get("value", 0)}])) |
| 64 | + |
| 65 | + start = int(values[0]["timestamp"] / 1000) if values else 0 |
| 66 | + end = int(values[-1]["timestamp"] / 1000) if values else 0 |
| 67 | + |
| 68 | + return { |
| 69 | + "data": data, |
| 70 | + "start": start, |
| 71 | + "end": end, |
| 72 | + "isMetricsData": False, |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +def timeseries_to_chart_data( |
| 77 | + resp_data: dict[str, Any], y_axis: str, has_groups: bool = False |
| 78 | +) -> dict[str, Any]: |
| 79 | + """ |
| 80 | + Converts an events-timeseries StatsResponse into the events-stats format |
| 81 | + that Chartcuterie expects. |
| 82 | +
|
| 83 | + For single series: |
| 84 | + {"data": [(timestamp_sec, [{"count": N}]), ...], "start": sec, "end": sec} |
| 85 | +
|
| 86 | + For top events (grouped): |
| 87 | + {"group_label": {"data": [...], "order": N, ...}, ...} |
| 88 | + """ |
| 89 | + time_series = resp_data.get("timeSeries", []) |
| 90 | + matching = [ts for ts in time_series if ts.get("yAxis") == y_axis] |
| 91 | + |
| 92 | + if not matching: |
| 93 | + return {"data": [], "start": 0, "end": 0, "isMetricsData": False} |
| 94 | + |
| 95 | + if has_groups: |
| 96 | + # Top events: return dict keyed by group label |
| 97 | + result = {} |
| 98 | + for i, ts in enumerate(matching): |
| 99 | + group_by = ts.get("groupBy", []) |
| 100 | + label = ",".join(str(g.get("value", "")) for g in group_by) if group_by else str(i) |
| 101 | + series_data = _serialize_single_series(ts) |
| 102 | + series_data["order"] = ts.get("meta", {}).get("order", i) |
| 103 | + result[label] = series_data |
| 104 | + return result |
| 105 | + |
| 106 | + return _serialize_single_series(matching[0]) |
| 107 | + |
| 108 | + |
| 109 | +def unfurl_explore( |
| 110 | + integration: Integration | RpcIntegration, |
| 111 | + links: list[UnfurlableUrl], |
| 112 | + user: User | RpcUser | None = None, |
| 113 | +) -> UnfurledUrl: |
| 114 | + with MessagingInteractionEvent( |
| 115 | + MessagingInteractionType.UNFURL_EXPLORE, SlackMessagingSpec(), user=user |
| 116 | + ).capture() as lifecycle: |
| 117 | + lifecycle.add_extras({"integration_id": integration.id}) |
| 118 | + return _unfurl_explore(integration, links, user) |
| 119 | + |
| 120 | + |
| 121 | +def _unfurl_explore( |
| 122 | + integration: Integration | RpcIntegration, |
| 123 | + links: list[UnfurlableUrl], |
| 124 | + user: User | RpcUser | None = None, |
| 125 | +) -> UnfurledUrl: |
| 126 | + org_integrations = integration_service.get_organization_integrations( |
| 127 | + integration_id=integration.id |
| 128 | + ) |
| 129 | + organizations = Organization.objects.filter( |
| 130 | + id__in=[oi.organization_id for oi in org_integrations] |
| 131 | + ) |
| 132 | + orgs_by_slug = {org.slug: org for org in organizations} |
| 133 | + |
| 134 | + # Check if any org has the feature flag enabled before doing any work |
| 135 | + enabled_orgs = { |
| 136 | + slug: org |
| 137 | + for slug, org in orgs_by_slug.items() |
| 138 | + if features.has("organizations:data-browsing-widget-unfurl", org, actor=user) |
| 139 | + } |
| 140 | + if not enabled_orgs: |
| 141 | + return {} |
| 142 | + |
| 143 | + unfurls = {} |
| 144 | + |
| 145 | + for link in links: |
| 146 | + org_slug = link.args["org_slug"] |
| 147 | + org = enabled_orgs.get(org_slug) |
| 148 | + |
| 149 | + if not org: |
| 150 | + continue |
| 151 | + |
| 152 | + params = link.args["query"] |
| 153 | + |
| 154 | + y_axes = params.getlist("yAxis") |
| 155 | + if not y_axes: |
| 156 | + y_axes = [DEFAULT_Y_AXIS] |
| 157 | + params.setlist("yAxis", y_axes) |
| 158 | + |
| 159 | + group_bys = params.getlist("groupBy") |
| 160 | + |
| 161 | + # Only one yAxis is charted; multiple charts per unfurl not yet supported. |
| 162 | + if group_bys: |
| 163 | + aggregate_fn = y_axes[-1].split("(")[0] |
| 164 | + if aggregate_fn in LINE_PLOT_FIELDS: |
| 165 | + style = ChartType.SLACK_DISCOVER_TOP5_PERIOD_LINE |
| 166 | + else: |
| 167 | + style = ChartType.SLACK_DISCOVER_TOP5_PERIOD |
| 168 | + params.setlist("topEvents", [str(TOP_N)]) |
| 169 | + else: |
| 170 | + style = ChartType.SLACK_DISCOVER_TOTAL_PERIOD |
| 171 | + |
| 172 | + if not params.get("statsPeriod") and not params.get("start"): |
| 173 | + params["statsPeriod"] = DEFAULT_PERIOD |
| 174 | + |
| 175 | + params["dataset"] = "spans" |
| 176 | + params["referrer"] = Referrer.EXPLORE_SLACK_UNFURL.value |
| 177 | + |
| 178 | + try: |
| 179 | + resp = client.get( |
| 180 | + auth=ApiKey(organization_id=org.id, scope_list=["org:read"]), |
| 181 | + user=user, |
| 182 | + path=f"/organizations/{org_slug}/events-timeseries/", |
| 183 | + params=params, |
| 184 | + ) |
| 185 | + except Exception: |
| 186 | + _logger.warning("Failed to load events-timeseries for explore unfurl") |
| 187 | + continue |
| 188 | + |
| 189 | + # QueryDict.items() sends only the last value per key to the API, |
| 190 | + # so we must match that by charting the last yAxis |
| 191 | + y_axis = y_axes[-1] |
| 192 | + stats = timeseries_to_chart_data(resp.data, y_axis, has_groups=bool(group_bys)) |
| 193 | + chart_data = {"seriesName": y_axis, "stats": stats} |
| 194 | + |
| 195 | + try: |
| 196 | + url = charts.generate_chart(style, chart_data) |
| 197 | + except RuntimeError: |
| 198 | + _logger.warning("Failed to generate chart for explore unfurl") |
| 199 | + continue |
| 200 | + |
| 201 | + unfurls[link.url] = SlackDiscoverMessageBuilder( |
| 202 | + title="Explore Traces", |
| 203 | + chart_url=url, |
| 204 | + ).build() |
| 205 | + |
| 206 | + first_org_integration = org_integrations[0] if len(org_integrations) > 0 else None |
| 207 | + if first_org_integration is not None and hasattr(first_org_integration, "id"): |
| 208 | + analytics.record( |
| 209 | + SlackIntegrationChartUnfurl( |
| 210 | + organization_id=first_org_integration.organization_id, |
| 211 | + user_id=user.id if user else None, |
| 212 | + unfurls_count=len(unfurls), |
| 213 | + ) |
| 214 | + ) |
| 215 | + |
| 216 | + return unfurls |
| 217 | + |
| 218 | + |
| 219 | +def map_explore_query_args(url: str, args: Mapping[str, str | None]) -> Mapping[str, Any]: |
| 220 | + """ |
| 221 | + Extracts explore arguments from the explore link's query string. |
| 222 | + Parses aggregateField JSON params to extract yAxes and groupBy. |
| 223 | + """ |
| 224 | + # Slack uses HTML escaped ampersands in its Event Links |
| 225 | + url = html.unescape(url) |
| 226 | + parsed_url = urlparse(url) |
| 227 | + raw_query = QueryDict(parsed_url.query) |
| 228 | + |
| 229 | + # Parse aggregateField JSON params |
| 230 | + aggregate_fields = raw_query.getlist("aggregateField") |
| 231 | + y_axes: list[str] = [] |
| 232 | + group_bys: list[str] = [] |
| 233 | + for field_json in aggregate_fields: |
| 234 | + try: |
| 235 | + parsed = json.loads(field_json) |
| 236 | + if "yAxes" in parsed and isinstance(parsed["yAxes"], list): |
| 237 | + y_axes.extend(parsed["yAxes"]) |
| 238 | + if "groupBy" in parsed and parsed["groupBy"]: |
| 239 | + group_bys.append(parsed["groupBy"]) |
| 240 | + except (json.JSONDecodeError, TypeError): |
| 241 | + continue |
| 242 | + |
| 243 | + if not y_axes: |
| 244 | + y_axes = [DEFAULT_Y_AXIS] |
| 245 | + |
| 246 | + # Build query params |
| 247 | + query = QueryDict(mutable=True) |
| 248 | + query.setlist("yAxis", y_axes) |
| 249 | + |
| 250 | + if group_bys: |
| 251 | + query.setlist("groupBy", group_bys) |
| 252 | + |
| 253 | + # Copy standard params |
| 254 | + for param in ("project", "statsPeriod", "start", "end", "query", "environment"): |
| 255 | + values = raw_query.getlist(param) |
| 256 | + if values: |
| 257 | + query.setlist(param, values) |
| 258 | + |
| 259 | + return dict(**args, query=query) |
| 260 | + |
| 261 | + |
| 262 | +explore_traces_link_regex = re.compile( |
| 263 | + r"^https?\://(?#url_prefix)[^/]+/organizations/(?P<org_slug>[^/]+)/explore/traces/" |
| 264 | +) |
| 265 | + |
| 266 | +customer_domain_explore_traces_link_regex = re.compile( |
| 267 | + r"^https?\://(?P<org_slug>[^.]+?)\.(?#url_prefix)[^/]+/explore/traces/" |
| 268 | +) |
| 269 | + |
| 270 | +explore_handler = Handler( |
| 271 | + fn=unfurl_explore, |
| 272 | + matcher=[ |
| 273 | + explore_traces_link_regex, |
| 274 | + customer_domain_explore_traces_link_regex, |
| 275 | + ], |
| 276 | + arg_mapper=map_explore_query_args, |
| 277 | +) |
0 commit comments