monitoring.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. const modelLowTpsQuery = (product: "go" | "zen") => {
  107. const filters = [
  108. { column: "model", op: "exists" },
  109. { column: "event_type", op: "=", value: "completions" },
  110. { column: "user_agent", op: "contains", value: "opencode" },
  111. { column: "isGoTier", op: "=", value: product === "go" ? "true" : "false" },
  112. { column: "status", op: ">=", value: "200" },
  113. { column: "status", op: "<", value: "400" },
  114. { column: "tps.output", op: "exists" },
  115. ]
  116. return honeycomb.getQuerySpecificationOutput({
  117. breakdowns: ["model"],
  118. calculations: [
  119. { op: "COUNT", name: "TOTAL", filterCombination: "AND", filters },
  120. {
  121. op: "P50",
  122. name: "TPS",
  123. column: "tps.output",
  124. filterCombination: "AND",
  125. filters,
  126. },
  127. ],
  128. formulas: [{ name: "LOW_TPS", expression: "IF(GTE($TOTAL, 100), $TPS, 999)" }],
  129. timeRange: 1800,
  130. }).json
  131. }
  132. new honeycomb.Trigger("IncreasedModelHttpErrorsGo", {
  133. name: "Increased Model HTTP Errors [Go]",
  134. description,
  135. queryJson: modelHttpErrorsQuery("go"),
  136. alertType: "on_change",
  137. frequency: 300,
  138. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  139. recipients: [
  140. {
  141. id: webhookRecipient.id,
  142. notificationDetails: [
  143. {
  144. variables: [{ name: "type", value: "model_http_errors" }],
  145. },
  146. ],
  147. },
  148. ],
  149. })
  150. new honeycomb.Trigger("IncreasedModelHttpErrorsZen", {
  151. name: "Increased Model HTTP Errors [Zen]",
  152. description,
  153. queryJson: modelHttpErrorsQuery("zen"),
  154. alertType: "on_change",
  155. frequency: 300,
  156. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  157. recipients: [
  158. {
  159. id: webhookRecipient.id,
  160. notificationDetails: [
  161. {
  162. variables: [{ name: "type", value: "model_http_errors" }],
  163. },
  164. ],
  165. },
  166. ],
  167. })
  168. new honeycomb.Trigger("LowModelTpsGo", {
  169. name: "Low Model TPS [Go]",
  170. description,
  171. queryJson: modelLowTpsQuery("go"),
  172. alertType: "on_change",
  173. frequency: 600,
  174. thresholds: [{ op: "<=", value: 10, exceededLimit: 1 }],
  175. recipients: [
  176. {
  177. id: webhookRecipient.id,
  178. notificationDetails: [
  179. {
  180. variables: [{ name: "type", value: "model_low_tps" }],
  181. },
  182. ],
  183. },
  184. ],
  185. })
  186. new honeycomb.Trigger("LowModelTpsZen", {
  187. name: "Low Model TPS [Zen]",
  188. description,
  189. queryJson: modelLowTpsQuery("zen"),
  190. alertType: "on_change",
  191. frequency: 600,
  192. thresholds: [{ op: "<=", value: 10, exceededLimit: 1 }],
  193. recipients: [
  194. {
  195. id: webhookRecipient.id,
  196. notificationDetails: [
  197. {
  198. variables: [{ name: "type", value: "model_low_tps" }],
  199. },
  200. ],
  201. },
  202. ],
  203. })
  204. new honeycomb.Trigger("IncreasedProviderHttpErrorsGo", {
  205. name: "Increased Provider HTTP Errors [Go]",
  206. description,
  207. queryJson: providerHttpErrorsQuery("go"),
  208. alertType: "on_change",
  209. frequency: 300,
  210. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  211. recipients: [
  212. {
  213. id: webhookRecipient.id,
  214. notificationDetails: [
  215. {
  216. variables: [{ name: "type", value: "provider_http_errors" }],
  217. },
  218. ],
  219. },
  220. ],
  221. })
  222. new honeycomb.Trigger("IncreasedProviderHttpErrorsZen", {
  223. name: "Increased Provider HTTP Errors [Zen]",
  224. description,
  225. queryJson: providerHttpErrorsQuery("zen"),
  226. alertType: "on_change",
  227. frequency: 300,
  228. thresholds: [{ op: ">=", value: 0.7, exceededLimit: 1 }],
  229. recipients: [
  230. {
  231. id: webhookRecipient.id,
  232. notificationDetails: [
  233. {
  234. variables: [{ name: "type", value: "provider_http_errors" }],
  235. },
  236. ],
  237. },
  238. ],
  239. })
  240. new honeycomb.Trigger("IncreasedFreeTierRequests", {
  241. name: "Increased Free Tier Requests",
  242. description,
  243. queryJson: honeycomb.getQuerySpecificationOutput({
  244. calculations: [{ op: "COUNT" }],
  245. filters: [
  246. { column: "event_type", op: "=", value: "completions" },
  247. { column: "user_agent", op: "contains", value: "opencode" },
  248. { column: "isFreeTier", op: "=", value: "true" },
  249. ],
  250. timeRange: 3600,
  251. }).json,
  252. alertType: "on_change",
  253. frequency: 900,
  254. thresholds: [{ op: ">=", value: 50, exceededLimit: 1 }],
  255. baselineDetails: [{ type: "percentage", offsetMinutes: 1440 }],
  256. recipients: [
  257. {
  258. id: webhookRecipient.id,
  259. notificationDetails: [
  260. {
  261. variables: [{ name: "type", value: "custom" }],
  262. },
  263. ],
  264. },
  265. ],
  266. })