monitoring.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { SECRET } from "./secret"
  2. import { domain } from "./stage"
  3. const webhookRecipient = new honeycomb.WebhookRecipient("DiscordAlerts", {
  4. name: $app.stage === "production" ? "Discord Alerts" : `Discord Alerts (${$app.stage})`,
  5. url: `https://${domain}/honeycomb/webhook`,
  6. secret: SECRET.HoneycombWebhookSecret.result,
  7. templates: [
  8. {
  9. type: "trigger",
  10. body: `{
  11. "url": {{ .Result.URL | quote }},
  12. "type": {{ .Vars.type | quote }},
  13. "name": {{ .Name | quote }},
  14. "status": {{ .Alert.Status | quote }},
  15. "isTest": {{ .Alert.IsTest }},
  16. "groups": {{ .Result.GroupsTriggered | toJson }}
  17. }`,
  18. },
  19. ],
  20. variables: [
  21. {
  22. name: "type",
  23. },
  24. ],
  25. })
  26. const modelHttpErrorsQuery = (product: "go" | "zen") => {
  27. const filters = [
  28. { column: "model", op: "exists" },
  29. { column: "event_type", op: "=", value: "completions" },
  30. { column: "user_agent", op: "contains", value: "opencode" },
  31. { column: "isGoTier", op: "=", value: product === "go" ? "true" : "false" },
  32. ]
  33. return honeycomb.getQuerySpecificationOutput({
  34. breakdowns: ["model"],
  35. calculatedFields: [
  36. {
  37. name: "is_failed_http_status",
  38. expression: product === "go"
  39. ? `IF(AND(GTE($status, "400"), NOT(EQUALS($status, "401")), NOT(EQUALS($status, "429"))), 1, 0)`
  40. : `IF(AND(GTE($status, "400"), NOT(EQUALS($status, "401"))), 1, 0)`,
  41. },
  42. ],
  43. calculations: [
  44. { op: "COUNT", name: "TOTAL", filterCombination: "AND", filters },
  45. { op: "SUM", name: "FAILED", column: "is_failed_http_status", filterCombination: "AND", filters },
  46. ],
  47. formulas: [{ name: "ERROR", expression: "IF(GTE($TOTAL, 100), DIV($FAILED, $TOTAL), 0)" }],
  48. timeRange: 900,
  49. }).json
  50. }
  51. const providerHttpErrorsQuery = (product: "go" | "zen") => {
  52. const filters = [
  53. { column: "provider", op: "exists" },
  54. { column: "user_agent", op: "contains", value: "opencode" },
  55. { column: "isGoTier", op: "=", value: product === "go" ? "true" : "false" },
  56. ]
  57. return honeycomb.getQuerySpecificationOutput({
  58. breakdowns: ["provider"],
  59. calculatedFields: [
  60. {
  61. name: "is_success_http_status",
  62. expression: `IF(AND(GTE($status, "200"), LT($status, "400")), 1, 0)`,
  63. },
  64. {
  65. name: "is_failed_provider_http_status",
  66. expression: `IF(GT($llm.error.code, "400"), 1, 0)`,
  67. },
  68. ],
  69. calculations: [
  70. {
  71. op: "SUM",
  72. name: "SUCCESS",
  73. column: "is_success_http_status",
  74. filterCombination: "AND",
  75. filters: [...filters, { column: "event_type", op: "=", value: "completions" }],
  76. },
  77. {
  78. op: "SUM",
  79. name: "FAILED",
  80. column: "is_failed_provider_http_status",
  81. filterCombination: "AND",
  82. filters: [...filters, { column: "event_type", op: "=", value: "llm.error" }],
  83. },
  84. ],
  85. formulas: [
  86. { name: "ERROR", expression: "IF(GTE(SUM($SUCCESS, $FAILED), 50), DIV($FAILED, SUM($SUCCESS, $FAILED)), 0)" },
  87. ],
  88. timeRange: 900,
  89. }).json
  90. }
  91. const description = "Managed by SST (Don't edit in Honeycomb UI)"
  92. new honeycomb.Trigger("IncreasedModelHttpErrorsGo", {
  93. name: "Increased Model HTTP Errors [Go]",
  94. description,
  95. queryJson: modelHttpErrorsQuery("go"),
  96. alertType: "on_change",
  97. frequency: 300,
  98. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  99. recipients: [
  100. {
  101. id: webhookRecipient.id,
  102. notificationDetails: [
  103. {
  104. variables: [{ name: "type", value: "model_http_errors" }],
  105. },
  106. ],
  107. },
  108. ],
  109. })
  110. new honeycomb.Trigger("IncreasedModelHttpErrorsZen", {
  111. name: "Increased Model HTTP Errors [Zen]",
  112. description,
  113. queryJson: modelHttpErrorsQuery("zen"),
  114. alertType: "on_change",
  115. frequency: 300,
  116. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  117. recipients: [
  118. {
  119. id: webhookRecipient.id,
  120. notificationDetails: [
  121. {
  122. variables: [{ name: "type", value: "model_http_errors" }],
  123. },
  124. ],
  125. },
  126. ],
  127. })
  128. new honeycomb.Trigger("IncreasedProviderHttpErrorsGo", {
  129. name: "Increased Provider HTTP Errors [Go]",
  130. description,
  131. queryJson: providerHttpErrorsQuery("go"),
  132. alertType: "on_change",
  133. frequency: 300,
  134. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  135. recipients: [
  136. {
  137. id: webhookRecipient.id,
  138. notificationDetails: [
  139. {
  140. variables: [{ name: "type", value: "provider_http_errors" }],
  141. },
  142. ],
  143. },
  144. ],
  145. })
  146. new honeycomb.Trigger("IncreasedProviderHttpErrorsZen", {
  147. name: "Increased Provider HTTP Errors [Zen]",
  148. description,
  149. queryJson: providerHttpErrorsQuery("zen"),
  150. alertType: "on_change",
  151. frequency: 300,
  152. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  153. recipients: [
  154. {
  155. id: webhookRecipient.id,
  156. notificationDetails: [
  157. {
  158. variables: [{ name: "type", value: "provider_http_errors" }],
  159. },
  160. ],
  161. },
  162. ],
  163. })
  164. new honeycomb.Trigger("IncreasedFreeTierRequests", {
  165. name: "Increased Free Tier Requests",
  166. description,
  167. queryJson: honeycomb.getQuerySpecificationOutput({
  168. calculations: [{ op: "COUNT" }],
  169. filters: [
  170. { column: "event_type", op: "=", value: "completions" },
  171. { column: "user_agent", op: "contains", value: "opencode" },
  172. { column: "isFreeTier", op: "=", value: "true" },
  173. ],
  174. timeRange: 3600,
  175. }).json,
  176. alertType: "on_change",
  177. frequency: 900,
  178. thresholds: [{ op: ">=", value: 50, exceededLimit: 1 }],
  179. baselineDetails: [{ type: "percentage", offsetMinutes: 1440 }],
  180. recipients: [
  181. {
  182. id: webhookRecipient.id,
  183. notificationDetails: [
  184. {
  185. variables: [{ name: "type", value: "custom" }],
  186. },
  187. ],
  188. },
  189. ],
  190. })