No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

143 líneas
5.9KB

  1. <%
  2. ' Observability for the JSON API (spec NFR-004, FR-019): one structured access-log line per API response and
  3. ' in-process request metrics, exposed with database gauges by GET /api/v1/metrics (ApiHealthController.Metrics).
  4. '
  5. ' - Obs_Start is taken when this file runs (start of the request); MVC_InvokeAction sets Obs_Action to
  6. ' "<Controller>.<Action>", so metric labels stay bounded (no raw paths or ids).
  7. ' - Api_Send calls Obs_Record once per response. The log line is JSON written with console.log (stdout, so
  8. ' `docker logs axonasp`); 5xx go through console.error (stderr and the runtime's error.log). Each line has a
  9. ' timestamp/marker prefix from the runtime: extract the JSON with grep -o '{"log":.*'. No bodies, query strings
  10. ' or keys are logged. web.config ApiAccessLog = "false" turns the lines off (metrics are kept).
  11. ' - Counters live in Application (one process, reset on restart; Prometheus treats that as a counter reset).
  12. ' Requests whose action fails inside the dispatcher (empty 200, see AGENTS.md section 8) are not counted.
  13. ' Include before lib.Api.asp.
  14. Dim Obs_Start, Obs_Action, Obs_Done
  15. Obs_Start = Timer
  16. Obs_Action = ""
  17. Obs_Done = False
  18. ' Upper bounds (ms) of the latency histogram buckets; +Inf is implied.
  19. Const OBS_BUCKETS = "5,10,25,50,100,250,500,1000,2500,5000,10000"
  20. ' Milliseconds since the request started (Timer wraps at midnight).
  21. Function Obs_ElapsedMs()
  22. Dim d
  23. d = Timer - Obs_Start
  24. If d < 0 Then
  25. d = d + 86400
  26. End If
  27. Obs_ElapsedMs = Round(d * 1000, 1)
  28. End Function
  29. ' Adds v to an Application counter and remembers the key in a registry list. Caller holds Application.Lock.
  30. Sub Obs_Add(registry, key, v)
  31. Dim cur
  32. cur = Application(key)
  33. If IsEmpty(cur) Then
  34. Application(registry) = Application(registry) & key & vbLf
  35. cur = 0
  36. End If
  37. Application(key) = cur + v
  38. End Sub
  39. Sub Obs_Record(status)
  40. Dim ms, action, method, b, i, line, setting
  41. If Obs_Done Then
  42. Exit Sub
  43. End If
  44. Obs_Done = True
  45. ms = Obs_ElapsedMs()
  46. action = Obs_Action
  47. If action = "" Then
  48. action = "unrouted"
  49. End If
  50. method = UCase(Request.ServerVariables("REQUEST_METHOD"))
  51. b = Split(OBS_BUCKETS, ",")
  52. Application.Lock
  53. If IsEmpty(Application("obs|since")) Then
  54. Application("obs|since") = DateDiff("s", #1970-01-01#, Now())
  55. End If
  56. Obs_Add "obs|keys|req", "obs|req|" & action & "|" & method & "|" & status, 1
  57. Obs_Add "obs|keys|act", "obs|n|" & action, 1
  58. Application("obs|sum|" & action) = Application("obs|sum|" & action) + ms
  59. For i = 0 To UBound(b)
  60. If ms <= CDbl(b(i)) Then
  61. Application("obs|b|" & action & "|" & i) = Application("obs|b|" & action & "|" & i) + 1
  62. End If
  63. Next
  64. Application.Unlock
  65. setting = LCase("" & GetAppSetting("ApiAccessLog"))
  66. If setting = "false" Then
  67. Exit Sub
  68. End If
  69. line = "{""log"":""access"",""request_id"":" & JsonLite_Quote(Api_RequestId()) & _
  70. ",""method"":" & JsonLite_Quote(method) & ",""action"":" & JsonLite_Quote(action) & _
  71. ",""status"":" & status & ",""duration_ms"":" & JsonLite_Num(ms) & _
  72. ",""tenant_id"":" & Obs_JStr(Api_TenantId) & ",""principal_id"":" & Obs_JStr(Api_PrincipalId) & "}"
  73. If status >= 500 Then
  74. console.error line
  75. Else
  76. console.log line
  77. End If
  78. End Sub
  79. Function Obs_JStr(v)
  80. If IsEmpty(v) Or IsNull(v) Then
  81. Obs_JStr = "null"
  82. ElseIf "" & v = "" Then
  83. Obs_JStr = "null"
  84. Else
  85. Obs_JStr = JsonLite_Quote("" & v)
  86. End If
  87. End Function
  88. ' Prometheus label value escaping (backslash, quote, newline).
  89. Function Obs_Label(s)
  90. Obs_Label = Replace(Replace(Replace("" & s, "\", "\\"), """", "\"""), vbLf, "\n")
  91. End Function
  92. ' Request metrics in Prometheus text format (counters and the latency histogram).
  93. Function Obs_RequestMetrics()
  94. Dim keys, parts, i, j, out, act, b, cum, n
  95. out = "# HELP amk_http_requests_total API responses by action, method and status since process start." & vbLf & _
  96. "# TYPE amk_http_requests_total counter" & vbLf
  97. Application.Lock
  98. keys = Split("" & Application("obs|keys|req"), vbLf)
  99. For i = 0 To UBound(keys)
  100. If keys(i) <> "" Then
  101. parts = Split(keys(i), "|")
  102. out = out & "amk_http_requests_total{action=""" & Obs_Label(parts(2)) & """,method=""" & parts(3) & _
  103. """,status=""" & parts(4) & """} " & Application(keys(i)) & vbLf
  104. End If
  105. Next
  106. out = out & "# HELP amk_http_request_duration_ms API response time in milliseconds, by action." & vbLf & _
  107. "# TYPE amk_http_request_duration_ms histogram" & vbLf
  108. b = Split(OBS_BUCKETS, ",")
  109. keys = Split("" & Application("obs|keys|act"), vbLf)
  110. For i = 0 To UBound(keys)
  111. If keys(i) <> "" Then
  112. act = Mid(keys(i), Len("obs|n|") + 1)
  113. n = Application(keys(i))
  114. For j = 0 To UBound(b)
  115. cum = Application("obs|b|" & act & "|" & j)
  116. If IsEmpty(cum) Then
  117. cum = 0
  118. End If
  119. out = out & "amk_http_request_duration_ms_bucket{action=""" & Obs_Label(act) & """,le=""" & b(j) & """} " & cum & vbLf
  120. Next
  121. out = out & "amk_http_request_duration_ms_bucket{action=""" & Obs_Label(act) & """,le=""+Inf""} " & n & vbLf
  122. out = out & "amk_http_request_duration_ms_sum{action=""" & Obs_Label(act) & """} " & _
  123. Replace(CStr(Application("obs|sum|" & act)), ",", ".") & vbLf
  124. out = out & "amk_http_request_duration_ms_count{action=""" & Obs_Label(act) & """} " & n & vbLf
  125. End If
  126. Next
  127. out = out & "# HELP amk_process_start_time_seconds Unix time when the counters started." & vbLf & _
  128. "# TYPE amk_process_start_time_seconds gauge" & vbLf & _
  129. "amk_process_start_time_seconds " & Application("obs|since") & vbLf
  130. Application.Unlock
  131. Obs_RequestMetrics = out
  132. End Function
  133. %>

Powered by TurnKey Linux.