Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

197 řádky
8.3KB

  1. <%
  2. ' ApiHealthController: liveness and readiness probes (spec 9, health). No authentication, no tenant data.
  3. '
  4. ' GET /api/v1/health/live Live 200 while the process answers
  5. ' GET /api/v1/health/ready Ready 200 when the database answers (status ready, or degraded when the embedding
  6. ' provider / vector store is unreachable), else 503
  7. ' GET /api/v1/metrics Metrics Prometheus text: request counters and latency (lib.Observability.asp) plus
  8. ' installation-wide gauges (memories, candidates, jobs, embedding index,
  9. ' retrieval and feedback totals; counts only, never content or ids). Needs
  10. ' Authorization: Bearer <AMK_METRICS_TOKEN> (environment variable of the
  11. ' container, 16+ characters); while it is unset the endpoint answers 404.
  12. Class ApiHealthController_Class
  13. Public Property Get useLayout()
  14. useLayout = False
  15. End Property
  16. Public Property Let useLayout(v)
  17. End Property
  18. Public Property Get Title()
  19. Title = "Health"
  20. End Property
  21. Public Sub Live()
  22. Api_Send 200, "{""status"":""live""}"
  23. End Sub
  24. ' 503 only when the database is down. Semantic retrieval is optional (lexical keeps working), so an unreachable
  25. ' embedding provider or vector store gives 200 with status "degraded". Counts only, no tenant data.
  26. Public Sub Ready()
  27. Dim rs, ok, emb, store, pending, failed, status, resp, st
  28. ok = False
  29. pending = 0
  30. failed = 0
  31. On Error Resume Next
  32. Set rs = DAL().Query("SELECT 1", Empty)
  33. If Err.Number = 0 Then
  34. ok = Not rs.EOF
  35. rs.Close
  36. End If
  37. Err.Clear
  38. If ok Then
  39. Set rs = DAL().Query("SELECT SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END), SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) FROM embedding_index", Empty)
  40. If Err.Number = 0 Then
  41. If Not rs.EOF Then
  42. If Not IsNull(rs(0)) Then
  43. pending = CLng(rs(0))
  44. failed = CLng(rs(1))
  45. End If
  46. End If
  47. rs.Close
  48. End If
  49. Err.Clear
  50. End If
  51. On Error GoTo 0
  52. If Not ok Then
  53. Api_Send 503, "{""status"":""unavailable"",""checks"":{""database"":""down""}}"
  54. Exit Sub
  55. End If
  56. status = "ready"
  57. If Emb_Enabled() Then
  58. resp = Emb_Http("GET", Emb_Setting("EmbeddingUrl", "http://ollama:11434") & "/api/version", "", st)
  59. If st = 200 Then
  60. emb = "up"
  61. Else
  62. emb = "down"
  63. status = "degraded"
  64. End If
  65. resp = Emb_Http("GET", Emb_Setting("VectorStoreUrl", "http://qdrant:6333") & "/readyz", "", st)
  66. If st = 200 Then
  67. store = "up"
  68. Else
  69. store = "down"
  70. status = "degraded"
  71. End If
  72. Else
  73. emb = "disabled"
  74. store = "disabled"
  75. End If
  76. Api_Send 200, "{""status"":""" & status & """,""checks"":{""database"":""up"",""embeddings"":""" & emb & """,""vector_store"":""" & store & """}" & _
  77. ",""embedding_backlog"":{""pending"":" & pending & ",""failed"":" & failed & "}}"
  78. End Sub
  79. Public Sub Metrics()
  80. Dim sh, token, auth, out, msg
  81. Set sh = CreateObject("WScript.Shell")
  82. token = sh.ExpandEnvironmentStrings("%AMK_METRICS_TOKEN%")
  83. If token = "%AMK_METRICS_TOKEN%" Then
  84. token = ""
  85. End If
  86. If Len(token) < 16 Then
  87. Api_NotFound
  88. Exit Sub
  89. End If
  90. auth = "" & Request.ServerVariables("HTTP_AUTHORIZATION")
  91. ' Compare digests, so the time taken does not depend on how much of the token matched.
  92. If Api_Sha256(auth) <> Api_Sha256("Bearer " & token) Then
  93. Response.AddHeader "WWW-Authenticate", "Bearer"
  94. Api_Error 401, "unauthorized", "A valid metrics token is required."
  95. Exit Sub
  96. End If
  97. out = Obs_RequestMetrics()
  98. On Error Resume Next
  99. out = out & Gauge("amk_memories", "Memories by lifecycle status.", "gauge", _
  100. "SELECT lifecycle_status, COUNT(*) FROM memories GROUP BY 1", "status")
  101. out = out & Gauge("amk_learning_candidates", "Learning candidates by state.", "gauge", _
  102. "SELECT state, COUNT(*) FROM learning_candidates GROUP BY 1", "state")
  103. out = out & Gauge("amk_jobs", "Ingestion jobs by state.", "gauge", _
  104. "SELECT state, COUNT(*) FROM jobs GROUP BY 1", "state")
  105. out = out & Gauge("amk_embedding_index", "Embedding index entries by status.", "gauge", _
  106. "SELECT status, COUNT(*) FROM embedding_index GROUP BY 1", "status")
  107. out = out & Gauge("amk_retrieval_queries_total", "Recorded retrieval calls by operation and strategy.", "counter", _
  108. "SELECT operation, strategy, COUNT(*) FROM retrieval_queries GROUP BY 1, 2", "operation,strategy")
  109. out = out & Gauge("amk_retrieval_empty_total", "Recorded retrieval calls that returned no results.", "counter", _
  110. "SELECT operation, COUNT(*) FROM retrieval_queries WHERE result_count = 0 GROUP BY 1", "operation")
  111. out = out & Gauge("amk_retrieval_duration_ms_sum", "Total recorded retrieval time in milliseconds.", "counter", _
  112. "SELECT operation, SUM(duration_ms) FROM retrieval_queries GROUP BY 1", "operation")
  113. out = out & Gauge("amk_feedback_total", "Usefulness feedback by rating.", "counter", _
  114. "SELECT usefulness, COUNT(*) FROM feedback GROUP BY 1", "usefulness")
  115. If Err.Number <> 0 Then
  116. msg = Err.Description
  117. Err.Clear
  118. On Error GoTo 0
  119. Api_Error 500, "internal_error", "Metrics query failed: " & msg
  120. Exit Sub
  121. End If
  122. On Error GoTo 0
  123. Response.Clear
  124. Response.Status = "200 OK"
  125. Response.ContentType = "text/plain; version=0.0.4"
  126. Response.Charset = "utf-8"
  127. Response.AddHeader "X-Request-Id", Api_RequestId()
  128. Response.AddHeader "Cache-Control", "no-store"
  129. Response.Write out
  130. Obs_Record 200
  131. End Sub
  132. ' One metric family from a grouped query: the label columns first, the value last.
  133. Private Function Gauge(name, help, kind, sql, labelNames)
  134. Dim rs, rows, names, i, r, out, labels, v
  135. names = Split(labelNames, ",")
  136. Set rs = DAL().Query(sql, Empty)
  137. rows = Empty
  138. If Not rs.EOF Then
  139. rows = rs.GetRows()
  140. End If
  141. rs.Close
  142. out = "# HELP " & name & " " & help & vbLf & "# TYPE " & name & " " & kind & vbLf
  143. If IsArray(rows) Then
  144. For r = 0 To UBound(rows, 2)
  145. labels = ""
  146. For i = 0 To UBound(names)
  147. If i > 0 Then
  148. labels = labels & ","
  149. End If
  150. labels = labels & names(i) & "=""" & Obs_Label(rows(i, r)) & """"
  151. Next
  152. v = rows(UBound(names) + 1, r)
  153. If IsNull(v) Then
  154. v = 0
  155. End If
  156. out = out & name & "{" & labels & "} " & v & vbLf
  157. Next
  158. End If
  159. Gauge = out
  160. End Function
  161. ' Static action dispatch (AxonASP mishandles runtime-built Eval/Execute calls and shared call sites)
  162. Public Sub Invoke(actionName, args)
  163. Select Case LCase(actionName)
  164. Case "live" : Live
  165. Case "ready" : Ready
  166. Case "metrics" : Metrics
  167. Case Else
  168. Err.Raise 438, "ApiHealthController_Class", "Action '" & actionName & "' not found"
  169. End Select
  170. End Sub
  171. End Class
  172. Dim ApiHealthController_Class__Singleton
  173. Function ApiHealthController()
  174. If IsEmpty(ApiHealthController_Class__Singleton) Then
  175. Set ApiHealthController_Class__Singleton = New ApiHealthController_Class
  176. End If
  177. Set ApiHealthController = ApiHealthController_Class__Singleton
  178. End Function
  179. ' Global entry point used by MVC_InvokeAction via GetRef (one call site per controller)
  180. Sub ApiHealthController_Invoke(actionName, args)
  181. ApiHealthController().Invoke actionName, args
  182. End Sub
  183. %>

Powered by TurnKey Linux.