Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(egress): label slack calls by the flow that makes them (#105814)

Julian Bez854c5395af26
diff --git a/ee/tasks/subscriptions/slack_subscriptions.py b/ee/tasks/subscriptions/slack_subscriptions.py
index ef11567e11e1..efe5812950bf 100644
--- a/ee/tasks/subscriptions/slack_subscriptions.py
+++ b/ee/tasks/subscriptions/slack_subscriptions.py
@@ -339,7 +339,7 @@ def send_slack_message_with_integration(
is_new_subscription,
integration=integration,
)
- slack_integration = SlackIntegration(integration)
+ slack_integration = SlackIntegration(integration, source="subscriptions")
# Send main message
message_res = slack_integration.client.chat_postMessage(
@@ -401,7 +401,7 @@ async def deliver_slack_message_data(
message_data: SlackMessage,
) -> SlackDeliveryResult:
# shared send path: callers build the SlackMessage; retry + partial-failure handling are shared
- slack_integration = SlackIntegration(integration)
+ slack_integration = SlackIntegration(integration, source="subscriptions")
async with aiohttp.ClientSession(trust_env=True) as slack_session:
async_client = slack_integration.async_client(session=slack_session)
@@ -454,7 +454,7 @@ async def deliver_slack_message_data(
async def deliver_slack_gallery(
integration: Integration, subscription: Subscription, gallery: SlackGallery
) -> SlackDeliveryResult:
- slack_integration = SlackIntegration(integration)
+ slack_integration = SlackIntegration(integration, source="subscriptions")
async with aiohttp.ClientSession(trust_env=True) as slack_session:
async_client = slack_integration.async_client(session=slack_session)
if not gallery.file_uploads:
diff --git a/posthog/egress/slack/README.md b/posthog/egress/slack/README.md
index 985c2c4baae9..c50353494529 100644
--- a/posthog/egress/slack/README.md
+++ b/posthog/egress/slack/README.md
@@ -17,6 +17,7 @@ With no single budget to draw from, a proactive limiter would guess, so callers
No lanes, because nothing is gated.
- Slack SDK calls use `SlackWebClient` or `SlackAsyncWebClient` from `client.py` and `async_client.py`. A retry handler records every HTTP attempt, including each retry.
+- `SlackIntegration(integration, source=...)` sets the `source` label on the clients it builds. A flow that posts on its own, such as an alert, a digest, or a subscription, passes its own name, so its calls and 429s are separate from other callers. A caller that passes no name records as `integration`.
- Direct HTTP calls, such as file downloads in `posthog/temporal/ai/slack_app/attachments.py`, use `slack_request`.
- OAuth code exchanges in `posthog/models/integration/oauth.py` and MCP Store token exchanges and refreshes record their responses with `record_slack_api_response`.
diff --git a/posthog/models/integration/slack.py b/posthog/models/integration/slack.py
index d99562fa9420..16bff822548d 100644
--- a/posthog/models/integration/slack.py
+++ b/posthog/models/integration/slack.py
@@ -86,17 +86,20 @@ SLACK_MEMBERS_MAX_REQUESTS = 10
class SlackIntegration:
integration: model.Integration
- def __init__(self, integration: model.Integration) -> None:
+ def __init__(self, integration: model.Integration, *, source: str = "integration") -> None:
if integration.kind not in SLACK_INTEGRATION_KINDS:
raise Exception("SlackIntegration init called with Integration with wrong 'kind'")
self.integration = integration
+ # The egress `source` label. A flow that posts on its own names itself here, so its Slack
+ # calls and 429s can be told apart from every other caller of the same integration.
+ self.source = source
@property
def client(self) -> WebClient:
return WebClient(
self.integration.sensitive_config["access_token"],
- source="integration",
+ source=self.source,
workspace_id=self.integration.integration_id,
app_id="posthog",
)
@@ -108,7 +111,7 @@ class SlackIntegration:
return SlackAsyncWebClient(
self.integration.sensitive_config["access_token"],
- source="integration",
+ source=self.source,
workspace_id=self.integration.integration_id,
app_id="posthog",
session=session,
diff --git a/posthog/team_notifications/slack.py b/posthog/team_notifications/slack.py
index fce09a27f9e5..86ae1056d102 100644
--- a/posthog/team_notifications/slack.py
+++ b/posthog/team_notifications/slack.py
@@ -110,7 +110,7 @@ def is_shared_channel(channel: dict) -> bool:
return any(channel.get(flag) for flag in SHARED_CHANNEL_FLAGS)
-def fetch_channel_map(integration: Integration) -> dict[str, SlackChannel]:
+def fetch_channel_map(integration: Integration, *, source: str) -> dict[str, SlackChannel]:
"""Public channel name -> channel, for one Slack integration.
Private channels are skipped: listing them needs a real authed Slack user, and this runs from a
@@ -120,7 +120,7 @@ def fetch_channel_map(integration: Integration) -> dict[str, SlackChannel]:
"""
return {
channel["name"]: SlackChannel(channel_id=channel["id"], shared=is_shared_channel(channel))
- for channel in SlackIntegration(integration).list_public_channels()
+ for channel in SlackIntegration(integration, source=source).list_public_channels()
}
diff --git a/posthog/temporal/ai_observability/eval_reports/delivery.py b/posthog/temporal/ai_observability/eval_reports/delivery.py
index 3071da3d6fa9..2e5d50ec6772 100644
--- a/posthog/temporal/ai_observability/eval_reports/delivery.py
+++ b/posthog/temporal/ai_observability/eval_reports/delivery.py
@@ -393,7 +393,7 @@ def deliver_slack_report(
integration = Integration.objects.select_related("team__organization").get(
id=integration_id, team_id=team_id, kind="slack"
)
- client = SlackIntegration(integration).client
+ client = SlackIntegration(integration, source="eval_reports").client
# Main message: header + context + metrics grid + first section (if any)
blocks: list[dict] = [
diff --git a/products/customer_analytics/backend/logic/event_stream_destination.py b/products/customer_analytics/backend/logic/event_stream_destination.py
index c5705247fded..71eaaa07659b 100644
--- a/products/customer_analytics/backend/logic/event_stream_destination.py
+++ b/products/customer_analytics/backend/logic/event_stream_destination.py
@@ -185,7 +185,7 @@ def send_test_slack_message(*, team_id: int, stream_id: str, user: "User") -> st
raise EventStreamTestMessageError("The stream's Slack workspace is no longer connected.")
try:
- SlackIntegration(integration).client.chat_postMessage(
+ SlackIntegration(integration, source="customer_analytics_event_stream").client.chat_postMessage(
channel=stream.slack_channel_id,
text=(
":wave: This is a test message from PostHog Customer analytics — "
diff --git a/products/error_tracking/backend/temporal/alerts/delivery.py b/products/error_tracking/backend/temporal/alerts/delivery.py
index b66b0a102e90..66b6db527517 100644
--- a/products/error_tracking/backend/temporal/alerts/delivery.py
+++ b/products/error_tracking/backend/temporal/alerts/delivery.py
@@ -623,4 +623,4 @@ def _slack_client(destination: ErrorTrackingAlertDestination) -> WebClient | Non
integration = destination.integration
if integration is None or integration.kind != Integration.IntegrationKind.SLACK:
return None
- return SlackIntegration(integration).client
+ return SlackIntegration(integration, source="error_tracking_alerts").client
diff --git a/products/signals/backend/scout_harness/slack_delivery.py b/products/signals/backend/scout_harness/slack_delivery.py
index bbdc9e99c09d..eeb72130e26a 100644
--- a/products/signals/backend/scout_harness/slack_delivery.py
+++ b/products/signals/backend/scout_harness/slack_delivery.py
@@ -280,7 +280,7 @@ def post_scout_emission_to_slack(
blocks, fallback = build_scout_slack_message(emission)
blocks.extend(_scout_invite_footer(integration, scout_team_id=emission.team_id, channel_id=channel_id))
- slack = SlackIntegration(integration)
+ slack = SlackIntegration(integration, source="signals_scout")
client = slack.client
try:
_ensure_dm_recipient_eligible(slack, channel_id)
@@ -715,7 +715,7 @@ def post_scout_report_to_slack(
integration_id=integration_id,
project_id=report.team.project_id,
)
- slack = SlackIntegration(integration)
+ slack = SlackIntegration(integration, source="signals_scout")
client = slack.client
lead_blocks = [
*messages.lead_blocks,
diff --git a/products/signals/backend/slack_inbox_notifications.py b/products/signals/backend/slack_inbox_notifications.py
index eab058366a46..ee90f73d80b7 100644
--- a/products/signals/backend/slack_inbox_notifications.py
+++ b/products/signals/backend/slack_inbox_notifications.py
@@ -675,7 +675,7 @@ def _deliver_route_notification(
}
delivered = False
try:
- slack = SlackIntegration(route.integration)
+ slack = SlackIntegration(route.integration, source="signals_inbox")
if route.is_direct_message and slack.get_user_by_id(channel_id) is None:
# A member reachable when the target was saved can since have left or become a guest.
logger.warning("Skipping signals inbox-item Slack DM to an ineligible member", extra=log_context)
diff --git a/products/signals/backend/tasks.py b/products/signals/backend/tasks.py
index c616ee7fcddf..edfa9255fc50 100644
--- a/products/signals/backend/tasks.py
+++ b/products/signals/backend/tasks.py
@@ -293,7 +293,7 @@ def deliver_scout_slack_thread_replies(
"""Continue a rate-limited report thread without holding or retrying the lead-message worker."""
team = Team.objects.only("project_id").get(id=team_id)
integration = _slack_integration_for_project(integration_id=integration_id, project_id=team.project_id)
- slack = SlackIntegration(integration)
+ slack = SlackIntegration(integration, source="signals_scout")
channel_id = _slack_channel_id(channel)
def _schedule_retry(
diff --git a/products/stamphog/backend/logic/channel_resolution.py b/products/stamphog/backend/logic/channel_resolution.py
index 4d6353f28055..5c46efed4662 100644
--- a/products/stamphog/backend/logic/channel_resolution.py
+++ b/products/stamphog/backend/logic/channel_resolution.py
@@ -207,7 +207,7 @@ def build_routing_context(team_id: int) -> RoutingContext | None:
declared_repo_channel[repo_config.repository] = routing.declared_channel
try:
- channels_by_name = fetch_channel_map(integration)
+ channels_by_name = fetch_channel_map(integration, source="stamphog")
except Exception as e:
raise RoutingUnavailable(f"could not list Slack channels for team {team_id}: {e}") from e
diff --git a/products/stamphog/backend/logic/slack_digest.py b/products/stamphog/backend/logic/slack_digest.py
index 1c3376d3f0b5..ad7a765c6bcc 100644
--- a/products/stamphog/backend/logic/slack_digest.py
+++ b/products/stamphog/backend/logic/slack_digest.py
@@ -194,7 +194,7 @@ def post_digest_details(team_id: int, destination: Destination, summary: DigestS
return
try:
post_message(
- SlackIntegration(integration),
+ SlackIntegration(integration, source="stamphog"),
destination.channel_id,
_detail_blocks(summary),
_build_fallback_text(summary),
@@ -220,7 +220,7 @@ def post_digest_lead(team_id: int, destination: Destination, summary: DigestSumm
try:
return post_with_join(
- SlackIntegration(integration),
+ SlackIntegration(integration, source="stamphog"),
destination.channel_id,
_lead_blocks(summary),
_lead_text(summary),
diff --git a/products/stamphog/backend/tests/fakes.py b/products/stamphog/backend/tests/fakes.py
index c08208768e8b..a94871651d60 100644
--- a/products/stamphog/backend/tests/fakes.py
+++ b/products/stamphog/backend/tests/fakes.py
@@ -492,7 +492,7 @@ class FakeSlackIntegration:
# the digest is expected to swallow.
fail_thread_replies: bool = False
- def __init__(self, integration: Any) -> None:
+ def __init__(self, integration: Any, *, source: str = "integration") -> None:
self.integration = integration
@property
diff --git a/products/tasks/backend/loop_notifications.py b/products/tasks/backend/loop_notifications.py
index 45562412b012..e14b14fd7fc0 100644
--- a/products/tasks/backend/loop_notifications.py
+++ b/products/tasks/backend/loop_notifications.py
@@ -196,7 +196,7 @@ def _send_slack(
report = payload.get("report")
slack_body = str(report) if report else body
text = _truncate(f"*{_escape_slack_mrkdwn(title)}*\n{_escape_slack_mrkdwn(slack_body)}", _SLACK_BODY_MAX_CHARS)
- SlackIntegration(integration).client.chat_postMessage(
+ SlackIntegration(integration, source="loop_notifications").client.chat_postMessage(
channel=channel, text=text, unfurl_links=False, unfurl_media=False
)
except SlackApiError as e:
diff --git a/products/visual_review/backend/logic/debt_digest.py b/products/visual_review/backend/logic/debt_digest.py
index 244318a4fcbf..d9f9b97a3b18 100644
--- a/products/visual_review/backend/logic/debt_digest.py
+++ b/products/visual_review/backend/logic/debt_digest.py
@@ -877,7 +877,7 @@ def send_debt_digest(repo: Repo, mode: str) -> list[str]:
if integration is None:
logger.info("visual_review.debt_digest_no_slack_integration", team_id=repo.team_id)
return []
- channels_by_name = fetch_channel_map(integration)
+ channels_by_name = fetch_channel_map(integration, source="visual_review")
rendered: list[str] = []
for post in posts:
@@ -928,7 +928,7 @@ def _send_one(
if delivery is None:
return ""
- slack = SlackIntegration(integration)
+ slack = SlackIntegration(integration, source="visual_review")
try:
thread_ts = post_with_join(
slack, delivery.channel_id, post.lead.blocks, post.lead.text, channel_name=delivery.channel_name