monitoring.ts 6.3 KB

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