1from typing import Any
2
3from plain.runtime import settings
4from plain.toolbar import ToolbarItem, register_toolbar_item
5
6from .tracing import current_trace
7
8
9@register_toolbar_item
10class ConnectToolbarItem(ToolbarItem):
11 """Links the current request to its exported trace in Plain Cloud.
12
13 This module is only imported when plain.toolbar is installed (it is
14 autodiscovered by the toolbar package), so the `plain.toolbar` import
15 above is safe without a guard. The item is a button-only toolbar entry —
16 no panel — that points at the `/t/<trace_id>` short URL on the dashboard,
17 which resolves the trace back to its app and redirects.
18 """
19
20 name = "Connect"
21 button_template_name = "toolbar/connect_button.html"
22
23 def is_enabled(self) -> bool:
24 """Only show the item when connect is actively exporting."""
25 return bool(settings.CONNECT_EXPORT_ENABLED and settings.CONNECT_EXPORT_TOKEN)
26
27 def get_template_context(self) -> dict[str, Any]:
28 context = super().get_template_context()
29
30 trace = current_trace()
31 context["sampled"] = trace.sampled
32 if trace.sampled:
33 cloud_url = str(settings.CONNECT_CLOUD_URL).rstrip("/")
34 context["trace_url"] = f"{cloud_url}/t/{trace.trace_id}"
35 else:
36 context["trace_url"] = ""
37
38 return context