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(billing): alert #alerts-billing on community credit claim failures (#107931)

Daniel Snitkovskiy9c809ba71d62
diff --git a/terraform/us/project-2/team-billing/community-credit-alerts/alerts.tf b/terraform/us/project-2/team-billing/community-credit-alerts/alerts.tf
new file mode 100644
index 000000000000..d4d9b45e9d98
--- /dev/null
+++ b/terraform/us/project-2/team-billing/community-credit-alerts/alerts.tf
@@ -0,0 +1,85 @@
+# =============================================================================
+# Community credit claim failure alert
+# =============================================================================
+#
+# Posts every $exception from a community credit claim to Slack. The
+# #billing-error-feed only posts new or reopened error tracking issues, so
+# repeat failures of the same issue are silent there.
+#
+# Billing sets campaign_slug on the claim endpoint exception and on the
+# "Community credit was not applied after coupon claim." exception. See
+# notes/community-credits-runbook.md in PostHog/billing for how to re-grant.
+#
+# For more information, see:
+# https://registry.terraform.io/providers/PostHog/posthog/latest/docs/resources/hog_function
+# =============================================================================
+
+locals {
+ mentions = join(" ", [for id in var.billing_alert_mention_slack_user_ids : "<@${id}>"])
+}
+
+resource "posthog_hog_function" "community_credit_claim_failure" {
+ name = "Community credit claim failure to Slack"
+ description = "Post to Slack on every $exception with campaign_slug = community"
+ type = "destination"
+ enabled = true
+ hog = "let res := fetch('https://slack.com/api/chat.postMessage', {\n 'body': {\n 'channel': inputs.channel,\n 'icon_emoji': inputs.icon_emoji,\n 'username': inputs.username,\n 'blocks': inputs.blocks,\n 'text': inputs.text\n },\n 'method': 'POST',\n 'headers': {\n 'Authorization': f'Bearer {inputs.slack_workspace.access_token}',\n 'Content-Type': 'application/json'\n }\n});\n\nif (res.status != 200 or res.body.ok == false) {\n throw Error(f'Failed to post message to Slack: {res.status}: {res.body}');\n}"
+
+ inputs_json = jsonencode({
+ "text" = {
+ "value" = "${local.mentions} Community credit claim failed: {substring(event.properties.$exception_values[1], 1, 2700)} | Coupon: {event.properties.coupon_id} | Credit: {event.properties.credit_id} | Customer: {event.properties.customer_id} | Organization: {event.properties.organization_id} | Issue: {project.url}/error_tracking/{event.properties.$exception_issue_id}"
+ "templating" = "hog"
+ }
+ "blocks" = {
+ "value" = [
+ {
+ "text" = { "text" = "Community credit claim failed", "type" = "plain_text" }
+ "type" = "header"
+ },
+ {
+ "text" = {
+ # Slack rejects a section over 3,000 characters, so the exception text is cut short.
+ "text" = "${local.mentions} *{substring(event.properties.$exception_types[1], 1, 200)}*: {substring(event.properties.$exception_values[1], 1, 2700)}"
+ "type" = "mrkdwn"
+ }
+ "type" = "section"
+ },
+ {
+ "type" = "context"
+ "elements" = [{
+ "text" = "Coupon: {event.properties.coupon_id} | Credit: {event.properties.credit_id} | Customer: {event.properties.customer_id} | Organization: {event.properties.organization_id}"
+ "type" = "mrkdwn"
+ }]
+ },
+ { "type" = "divider" },
+ {
+ "type" = "actions"
+ "elements" = [{
+ "url" = "{project.url}/error_tracking/{event.properties.$exception_issue_id}"
+ "text" = { "text" = "View issue", "type" = "plain_text" }
+ "type" = "button"
+ }]
+ }
+ ]
+ "templating" = "hog"
+ }
+ "channel" = { "value" = var.billing_slack_channel_id, "templating" = "hog" }
+ "username" = { "value" = "Community credits", "templating" = "hog" }
+ "icon_emoji" = { "value" = ":hogzilla:", "templating" = "hog" }
+ "slack_workspace" = { "value" = var.billing_slack_workspace_id, "templating" = "hog" }
+ })
+
+ filters_json = jsonencode({
+ "source" = "events"
+ "events" = [{ "id" = "$exception", "type" = "events" }]
+ "properties" = [{
+ "key" = "campaign_slug"
+ "type" = "event"
+ "value" = ["community"]
+ "operator" = "exact"
+ }]
+ })
+
+ template_id = "template-slack"
+ icon_url = "/static/services/slack.png"
+}
diff --git a/terraform/us/project-2/team-billing/community-credit-alerts/terragrunt.hcl b/terraform/us/project-2/team-billing/community-credit-alerts/terragrunt.hcl
new file mode 100644
index 000000000000..acd4d5bd9476
--- /dev/null
+++ b/terraform/us/project-2/team-billing/community-credit-alerts/terragrunt.hcl
@@ -0,0 +1,29 @@
+include "root" {
+ path = find_in_parent_folders("root.hcl")
+ expose = true
+}
+
+include "region" {
+ path = "../../../terragrunt.hcl"
+ expose = true
+}
+
+include "project" {
+ path = "../../terragrunt.hcl"
+ expose = true
+}
+
+# Read team-level config for shared inputs and generate blocks
+locals {
+ team_config = read_terragrunt_config("${get_terragrunt_dir()}/../terragrunt.hcl")
+}
+
+# Generate team variables from parent config
+generate "variables_team" {
+ path = "variables_team.tf"
+ if_exists = "overwrite"
+ contents = file("${get_terragrunt_dir()}/../variables_team.tf.tpl")
+}
+
+# Merge team-level inputs
+inputs = local.team_config.inputs
diff --git a/terraform/us/project-2/team-billing/terragrunt.hcl b/terraform/us/project-2/team-billing/terragrunt.hcl
new file mode 100644
index 000000000000..6590b1f07b26
--- /dev/null
+++ b/terraform/us/project-2/team-billing/terragrunt.hcl
@@ -0,0 +1,18 @@
+# Team-level configuration for team-billing
+# This file is NOT directly runnable - it provides shared config for child modules.
+# Child modules use read_terragrunt_config() to import these settings.
+
+# Generate shared team variables in each child module
+generate "variables_team" {
+ path = "variables_team.tf"
+ if_exists = "overwrite"
+ contents = file("${get_terragrunt_dir()}/variables_team.tf.tpl")
+}
+
+# Team-level inputs shared across all child modules
+# Secret values should be stored as env vars in the Github project.
+inputs = {
+ billing_slack_channel_id = "C039XEY25ML" # #alerts-billing
+ billing_slack_workspace_id = 173069
+ billing_alert_mention_slack_user_ids = ["U0BLTRYPJ3D"]
+}
diff --git a/terraform/us/project-2/team-billing/variables_team.tf.tpl b/terraform/us/project-2/team-billing/variables_team.tf.tpl
new file mode 100644
index 000000000000..06b3404ecaab
--- /dev/null
+++ b/terraform/us/project-2/team-billing/variables_team.tf.tpl
@@ -0,0 +1,31 @@
+# =============================================================================
+# Variables for team-billing Terraform configuration
+#
+# These variables allow you to customize the configuration without hardcoding
+# sensitive or environment-specific values. Set them via environment variables:
+#
+# export TF_VAR_billing_slack_channel_id="YOUR_CHANNEL_ID"
+# ...
+#
+# Or via a terraform.tfvars file (careful with committing this in case it contains secrets!).
+#
+# Auto-generated by Terragrunt from team-level template
+# =============================================================================
+
+variable "billing_slack_channel_id" {
+ description = "Slack channel ID for alert notifications"
+ type = string
+ sensitive = true
+}
+
+variable "billing_slack_workspace_id" {
+ description = "PostHog Slack integration workspace ID"
+ type = number
+ sensitive = true
+}
+
+variable "billing_alert_mention_slack_user_ids" {
+ description = "Slack member IDs to mention in alert messages"
+ type = list(string)
+ default = []
+}