|
- <%
- ' Observability for the JSON API (spec NFR-004, FR-019): one structured access-log line per API response and
- ' in-process request metrics, exposed with database gauges by GET /api/v1/metrics (ApiHealthController.Metrics).
- '
- ' - Obs_Start is taken when this file runs (start of the request); MVC_InvokeAction sets Obs_Action to
- ' "<Controller>.<Action>", so metric labels stay bounded (no raw paths or ids).
- ' - Api_Send calls Obs_Record once per response. The log line is JSON written with console.log (stdout, so
- ' `docker logs axonasp`); 5xx go through console.error (stderr and the runtime's error.log). Each line has a
- ' timestamp/marker prefix from the runtime: extract the JSON with grep -o '{"log":.*'. No bodies, query strings
- ' or keys are logged. web.config ApiAccessLog = "false" turns the lines off (metrics are kept).
- ' - Counters live in Application (one process, reset on restart; Prometheus treats that as a counter reset).
- ' Requests whose action fails inside the dispatcher (empty 200, see AGENTS.md section 8) are not counted.
- ' Include before lib.Api.asp.
- Dim Obs_Start, Obs_Action, Obs_Done
- Obs_Start = Timer
- Obs_Action = ""
- Obs_Done = False
-
- ' Upper bounds (ms) of the latency histogram buckets; +Inf is implied.
- Const OBS_BUCKETS = "5,10,25,50,100,250,500,1000,2500,5000,10000"
-
- ' Milliseconds since the request started (Timer wraps at midnight).
- Function Obs_ElapsedMs()
- Dim d
- d = Timer - Obs_Start
- If d < 0 Then
- d = d + 86400
- End If
- Obs_ElapsedMs = Round(d * 1000, 1)
- End Function
-
- ' Adds v to an Application counter and remembers the key in a registry list. Caller holds Application.Lock.
- Sub Obs_Add(registry, key, v)
- Dim cur
- cur = Application(key)
- If IsEmpty(cur) Then
- Application(registry) = Application(registry) & key & vbLf
- cur = 0
- End If
- Application(key) = cur + v
- End Sub
-
- Sub Obs_Record(status)
- Dim ms, action, method, b, i, line, setting
- If Obs_Done Then
- Exit Sub
- End If
- Obs_Done = True
- ms = Obs_ElapsedMs()
- action = Obs_Action
- If action = "" Then
- action = "unrouted"
- End If
- method = UCase(Request.ServerVariables("REQUEST_METHOD"))
-
- b = Split(OBS_BUCKETS, ",")
- Application.Lock
- If IsEmpty(Application("obs|since")) Then
- Application("obs|since") = DateDiff("s", #1970-01-01#, Now())
- End If
- Obs_Add "obs|keys|req", "obs|req|" & action & "|" & method & "|" & status, 1
- Obs_Add "obs|keys|act", "obs|n|" & action, 1
- Application("obs|sum|" & action) = Application("obs|sum|" & action) + ms
- For i = 0 To UBound(b)
- If ms <= CDbl(b(i)) Then
- Application("obs|b|" & action & "|" & i) = Application("obs|b|" & action & "|" & i) + 1
- End If
- Next
- Application.Unlock
-
- setting = LCase("" & GetAppSetting("ApiAccessLog"))
- If setting = "false" Then
- Exit Sub
- End If
- line = "{""log"":""access"",""request_id"":" & JsonLite_Quote(Api_RequestId()) & _
- ",""method"":" & JsonLite_Quote(method) & ",""action"":" & JsonLite_Quote(action) & _
- ",""status"":" & status & ",""duration_ms"":" & JsonLite_Num(ms) & _
- ",""tenant_id"":" & Obs_JStr(Api_TenantId) & ",""principal_id"":" & Obs_JStr(Api_PrincipalId) & "}"
- If status >= 500 Then
- console.error line
- Else
- console.log line
- End If
- End Sub
-
- Function Obs_JStr(v)
- If IsEmpty(v) Or IsNull(v) Then
- Obs_JStr = "null"
- ElseIf "" & v = "" Then
- Obs_JStr = "null"
- Else
- Obs_JStr = JsonLite_Quote("" & v)
- End If
- End Function
-
- ' Prometheus label value escaping (backslash, quote, newline).
- Function Obs_Label(s)
- Obs_Label = Replace(Replace(Replace("" & s, "\", "\\"), """", "\"""), vbLf, "\n")
- End Function
-
- ' Request metrics in Prometheus text format (counters and the latency histogram).
- Function Obs_RequestMetrics()
- Dim keys, parts, i, j, out, act, b, cum, n
- out = "# HELP amk_http_requests_total API responses by action, method and status since process start." & vbLf & _
- "# TYPE amk_http_requests_total counter" & vbLf
- Application.Lock
- keys = Split("" & Application("obs|keys|req"), vbLf)
- For i = 0 To UBound(keys)
- If keys(i) <> "" Then
- parts = Split(keys(i), "|")
- out = out & "amk_http_requests_total{action=""" & Obs_Label(parts(2)) & """,method=""" & parts(3) & _
- """,status=""" & parts(4) & """} " & Application(keys(i)) & vbLf
- End If
- Next
- out = out & "# HELP amk_http_request_duration_ms API response time in milliseconds, by action." & vbLf & _
- "# TYPE amk_http_request_duration_ms histogram" & vbLf
- b = Split(OBS_BUCKETS, ",")
- keys = Split("" & Application("obs|keys|act"), vbLf)
- For i = 0 To UBound(keys)
- If keys(i) <> "" Then
- act = Mid(keys(i), Len("obs|n|") + 1)
- n = Application(keys(i))
- For j = 0 To UBound(b)
- cum = Application("obs|b|" & act & "|" & j)
- If IsEmpty(cum) Then
- cum = 0
- End If
- out = out & "amk_http_request_duration_ms_bucket{action=""" & Obs_Label(act) & """,le=""" & b(j) & """} " & cum & vbLf
- Next
- out = out & "amk_http_request_duration_ms_bucket{action=""" & Obs_Label(act) & """,le=""+Inf""} " & n & vbLf
- out = out & "amk_http_request_duration_ms_sum{action=""" & Obs_Label(act) & """} " & _
- Replace(CStr(Application("obs|sum|" & act)), ",", ".") & vbLf
- out = out & "amk_http_request_duration_ms_count{action=""" & Obs_Label(act) & """} " & n & vbLf
- End If
- Next
- out = out & "# HELP amk_process_start_time_seconds Unix time when the counters started." & vbLf & _
- "# TYPE amk_process_start_time_seconds gauge" & vbLf & _
- "amk_process_start_time_seconds " & Application("obs|since") & vbLf
- Application.Unlock
- Obs_RequestMetrics = out
- End Function
- %>
|