<% ' ApiHealthController: liveness and readiness probes (spec 9, health). No authentication, no tenant data. ' ' GET /api/v1/health/live Live 200 while the process answers ' GET /api/v1/health/ready Ready 200 when the database answers (status ready, or degraded when the embedding ' provider / vector store is unreachable), else 503 ' GET /api/v1/metrics Metrics Prometheus text: request counters and latency (lib.Observability.asp) plus ' installation-wide gauges (memories, candidates, jobs, embedding index, ' retrieval and feedback totals; counts only, never content or ids). Needs ' Authorization: Bearer (environment variable of the ' container, 16+ characters); while it is unset the endpoint answers 404. Class ApiHealthController_Class Public Property Get useLayout() useLayout = False End Property Public Property Let useLayout(v) End Property Public Property Get Title() Title = "Health" End Property Public Sub Live() Api_Send 200, "{""status"":""live""}" End Sub ' 503 only when the database is down. Semantic retrieval is optional (lexical keeps working), so an unreachable ' embedding provider or vector store gives 200 with status "degraded". Counts only, no tenant data. Public Sub Ready() Dim rs, ok, emb, store, pending, failed, status, resp, st ok = False pending = 0 failed = 0 On Error Resume Next Set rs = DAL().Query("SELECT 1", Empty) If Err.Number = 0 Then ok = Not rs.EOF rs.Close End If Err.Clear If ok Then 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) If Err.Number = 0 Then If Not rs.EOF Then If Not IsNull(rs(0)) Then pending = CLng(rs(0)) failed = CLng(rs(1)) End If End If rs.Close End If Err.Clear End If On Error GoTo 0 If Not ok Then Api_Send 503, "{""status"":""unavailable"",""checks"":{""database"":""down""}}" Exit Sub End If status = "ready" If Emb_Enabled() Then resp = Emb_Http("GET", Emb_Setting("EmbeddingUrl", "http://ollama:11434") & "/api/version", "", st) If st = 200 Then emb = "up" Else emb = "down" status = "degraded" End If resp = Emb_Http("GET", Emb_Setting("VectorStoreUrl", "http://qdrant:6333") & "/readyz", "", st) If st = 200 Then store = "up" Else store = "down" status = "degraded" End If Else emb = "disabled" store = "disabled" End If Api_Send 200, "{""status"":""" & status & """,""checks"":{""database"":""up"",""embeddings"":""" & emb & """,""vector_store"":""" & store & """}" & _ ",""embedding_backlog"":{""pending"":" & pending & ",""failed"":" & failed & "}}" End Sub Public Sub Metrics() Dim sh, token, auth, out, msg Set sh = CreateObject("WScript.Shell") token = sh.ExpandEnvironmentStrings("%AMK_METRICS_TOKEN%") If token = "%AMK_METRICS_TOKEN%" Then token = "" End If If Len(token) < 16 Then Api_NotFound Exit Sub End If auth = "" & Request.ServerVariables("HTTP_AUTHORIZATION") ' Compare digests, so the time taken does not depend on how much of the token matched. If Api_Sha256(auth) <> Api_Sha256("Bearer " & token) Then Response.AddHeader "WWW-Authenticate", "Bearer" Api_Error 401, "unauthorized", "A valid metrics token is required." Exit Sub End If out = Obs_RequestMetrics() On Error Resume Next out = out & Gauge("amk_memories", "Memories by lifecycle status.", "gauge", _ "SELECT lifecycle_status, COUNT(*) FROM memories GROUP BY 1", "status") out = out & Gauge("amk_learning_candidates", "Learning candidates by state.", "gauge", _ "SELECT state, COUNT(*) FROM learning_candidates GROUP BY 1", "state") out = out & Gauge("amk_jobs", "Ingestion jobs by state.", "gauge", _ "SELECT state, COUNT(*) FROM jobs GROUP BY 1", "state") out = out & Gauge("amk_embedding_index", "Embedding index entries by status.", "gauge", _ "SELECT status, COUNT(*) FROM embedding_index GROUP BY 1", "status") out = out & Gauge("amk_retrieval_queries_total", "Recorded retrieval calls by operation and strategy.", "counter", _ "SELECT operation, strategy, COUNT(*) FROM retrieval_queries GROUP BY 1, 2", "operation,strategy") out = out & Gauge("amk_retrieval_empty_total", "Recorded retrieval calls that returned no results.", "counter", _ "SELECT operation, COUNT(*) FROM retrieval_queries WHERE result_count = 0 GROUP BY 1", "operation") out = out & Gauge("amk_retrieval_duration_ms_sum", "Total recorded retrieval time in milliseconds.", "counter", _ "SELECT operation, SUM(duration_ms) FROM retrieval_queries GROUP BY 1", "operation") out = out & Gauge("amk_feedback_total", "Usefulness feedback by rating.", "counter", _ "SELECT usefulness, COUNT(*) FROM feedback GROUP BY 1", "usefulness") If Err.Number <> 0 Then msg = Err.Description Err.Clear On Error GoTo 0 Api_Error 500, "internal_error", "Metrics query failed: " & msg Exit Sub End If On Error GoTo 0 Response.Clear Response.Status = "200 OK" Response.ContentType = "text/plain; version=0.0.4" Response.Charset = "utf-8" Response.AddHeader "X-Request-Id", Api_RequestId() Response.AddHeader "Cache-Control", "no-store" Response.Write out Obs_Record 200 End Sub ' One metric family from a grouped query: the label columns first, the value last. Private Function Gauge(name, help, kind, sql, labelNames) Dim rs, rows, names, i, r, out, labels, v names = Split(labelNames, ",") Set rs = DAL().Query(sql, Empty) rows = Empty If Not rs.EOF Then rows = rs.GetRows() End If rs.Close out = "# HELP " & name & " " & help & vbLf & "# TYPE " & name & " " & kind & vbLf If IsArray(rows) Then For r = 0 To UBound(rows, 2) labels = "" For i = 0 To UBound(names) If i > 0 Then labels = labels & "," End If labels = labels & names(i) & "=""" & Obs_Label(rows(i, r)) & """" Next v = rows(UBound(names) + 1, r) If IsNull(v) Then v = 0 End If out = out & name & "{" & labels & "} " & v & vbLf Next End If Gauge = out End Function ' Static action dispatch (AxonASP mishandles runtime-built Eval/Execute calls and shared call sites) Public Sub Invoke(actionName, args) Select Case LCase(actionName) Case "live" : Live Case "ready" : Ready Case "metrics" : Metrics Case Else Err.Raise 438, "ApiHealthController_Class", "Action '" & actionName & "' not found" End Select End Sub End Class Dim ApiHealthController_Class__Singleton Function ApiHealthController() If IsEmpty(ApiHealthController_Class__Singleton) Then Set ApiHealthController_Class__Singleton = New ApiHealthController_Class End If Set ApiHealthController = ApiHealthController_Class__Singleton End Function ' Global entry point used by MVC_InvokeAction via GetRef (one call site per controller) Sub ApiHealthController_Invoke(actionName, args) ApiHealthController().Invoke actionName, args End Sub %>