boto-rb-management-ui 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +22 -0
  4. data/db/migrations/20261201000001_create_boto_monitor_instances.rb +14 -0
  5. data/db/migrations/20261201000002_create_boto_monitor_series.rb +20 -0
  6. data/db/migrations/20261201000003_create_boto_monitor_errors.rb +19 -0
  7. data/db/migrations/20261201000004_create_boto_monitor_traces.rb +19 -0
  8. data/db/migrations/20261201000005_create_boto_monitor_runtime.rb +23 -0
  9. data/db/migrations/20261201000006_create_boto_monitor_n_plus_ones.rb +17 -0
  10. data/db/migrations/20261201000007_add_browser_data_to_boto_monitor.rb +31 -0
  11. data/db/migrations/20261203000001_create_boto_monitor_streams.rb +24 -0
  12. data/db/migrations/20261203000002_add_streams_open_to_boto_monitor_instances.rb +12 -0
  13. data/lib/boto/companions/management_ui.rb +8 -0
  14. data/lib/boto/management_ui/app.rb +84 -0
  15. data/lib/boto/management_ui/app_traits.rb +34 -0
  16. data/lib/boto/management_ui/assets/monitor/api.js +36 -0
  17. data/lib/boto/management_ui/assets/monitor/charts.js +212 -0
  18. data/lib/boto/management_ui/assets/monitor/constants.js +70 -0
  19. data/lib/boto/management_ui/assets/monitor/dom.js +70 -0
  20. data/lib/boto/management_ui/assets/monitor/format.js +76 -0
  21. data/lib/boto/management_ui/assets/monitor/header.js +2 -0
  22. data/lib/boto/management_ui/assets/monitor/main.js +33 -0
  23. data/lib/boto/management_ui/assets/monitor/panes.js +109 -0
  24. data/lib/boto/management_ui/assets/monitor/runtime.js +26 -0
  25. data/lib/boto/management_ui/assets/monitor/services.js +174 -0
  26. data/lib/boto/management_ui/assets/monitor/spa.js +203 -0
  27. data/lib/boto/management_ui/assets/monitor/traces.js +88 -0
  28. data/lib/boto/management_ui/assets/panel/base.css +87 -0
  29. data/lib/boto/management_ui/assets/panel/errors.css +36 -0
  30. data/lib/boto/management_ui/assets/panel/gateway.css +3 -0
  31. data/lib/boto/management_ui/assets/panel/layout.css +63 -0
  32. data/lib/boto/management_ui/assets/panel/monitor_layout.css +77 -0
  33. data/lib/boto/management_ui/assets/panel/monitor_tables.css +97 -0
  34. data/lib/boto/management_ui/assets/panel/service_view.css +102 -0
  35. data/lib/boto/management_ui/assets/panel/services.css +55 -0
  36. data/lib/boto/management_ui/assets/panel/spa.css +41 -0
  37. data/lib/boto/management_ui/assets/panel/tables.css +67 -0
  38. data/lib/boto/management_ui/assets/panel/tabs.css +71 -0
  39. data/lib/boto/management_ui/assets/panel/traces.css +45 -0
  40. data/lib/boto/management_ui/browser_telemetry.rb +87 -0
  41. data/lib/boto/management_ui/gateway_view.rb +57 -0
  42. data/lib/boto/management_ui/health_probe.rb +70 -0
  43. data/lib/boto/management_ui/in_process_receiver.rb +17 -0
  44. data/lib/boto/management_ui/monitor/api.rb +87 -0
  45. data/lib/boto/management_ui/monitor/batch_schema/browser_call_entry.rb +26 -0
  46. data/lib/boto/management_ui/monitor/batch_schema/browser_error_entry.rb +41 -0
  47. data/lib/boto/management_ui/monitor/batch_schema/checks.rb +90 -0
  48. data/lib/boto/management_ui/monitor/batch_schema/error_entry.rb +37 -0
  49. data/lib/boto/management_ui/monitor/batch_schema/n_plus_one_entry.rb +30 -0
  50. data/lib/boto/management_ui/monitor/batch_schema/page_entry.rb +25 -0
  51. data/lib/boto/management_ui/monitor/batch_schema/runtime.rb +33 -0
  52. data/lib/boto/management_ui/monitor/batch_schema/series.rb +41 -0
  53. data/lib/boto/management_ui/monitor/batch_schema/streams.rb +46 -0
  54. data/lib/boto/management_ui/monitor/batch_schema/trace_entry.rb +45 -0
  55. data/lib/boto/management_ui/monitor/batch_schema.rb +138 -0
  56. data/lib/boto/management_ui/monitor/ingest.rb +126 -0
  57. data/lib/boto/management_ui/monitor/memory_store/error_table.rb +53 -0
  58. data/lib/boto/management_ui/monitor/memory_store/instance_table.rb +45 -0
  59. data/lib/boto/management_ui/monitor/memory_store/n_plus_one_table.rb +44 -0
  60. data/lib/boto/management_ui/monitor/memory_store/runtime_table.rb +55 -0
  61. data/lib/boto/management_ui/monitor/memory_store/series_table.rb +49 -0
  62. data/lib/boto/management_ui/monitor/memory_store/staleness.rb +20 -0
  63. data/lib/boto/management_ui/monitor/memory_store/stream_table.rb +36 -0
  64. data/lib/boto/management_ui/monitor/memory_store/trace_table.rb +41 -0
  65. data/lib/boto/management_ui/monitor/memory_store.rb +118 -0
  66. data/lib/boto/management_ui/monitor/percentile.rb +38 -0
  67. data/lib/boto/management_ui/monitor/period.rb +48 -0
  68. data/lib/boto/management_ui/monitor/queries/browser_calls.rb +56 -0
  69. data/lib/boto/management_ui/monitor/queries/browser_view.rb +91 -0
  70. data/lib/boto/management_ui/monitor/queries/charts.rb +66 -0
  71. data/lib/boto/management_ui/monitor/queries/live_instances.rb +44 -0
  72. data/lib/boto/management_ui/monitor/queries/points.rb +61 -0
  73. data/lib/boto/management_ui/monitor/queries/route_view.rb +46 -0
  74. data/lib/boto/management_ui/monitor/queries/runtime_view.rb +80 -0
  75. data/lib/boto/management_ui/monitor/queries/service_cards.rb +65 -0
  76. data/lib/boto/management_ui/monitor/queries/streams.rb +39 -0
  77. data/lib/boto/management_ui/monitor/queries/trace_tree.rb +47 -0
  78. data/lib/boto/management_ui/monitor/queries/vitals.rb +50 -0
  79. data/lib/boto/management_ui/monitor/queries.rb +171 -0
  80. data/lib/boto/management_ui/monitor/retention.rb +62 -0
  81. data/lib/boto/management_ui/monitor/schema.rb +64 -0
  82. data/lib/boto/management_ui/monitor/sql_store/error_writer.rb +62 -0
  83. data/lib/boto/management_ui/monitor/sql_store/instance_writer.rb +63 -0
  84. data/lib/boto/management_ui/monitor/sql_store/n_plus_one_writer.rb +50 -0
  85. data/lib/boto/management_ui/monitor/sql_store/rows.rb +99 -0
  86. data/lib/boto/management_ui/monitor/sql_store/series_writer.rb +52 -0
  87. data/lib/boto/management_ui/monitor/sql_store/streams_writer.rb +34 -0
  88. data/lib/boto/management_ui/monitor/sql_store.rb +139 -0
  89. data/lib/boto/management_ui/monitor/store.rb +78 -0
  90. data/lib/boto/management_ui/monitor/stream_point.rb +45 -0
  91. data/lib/boto/management_ui/panel/env_rows.rb +54 -0
  92. data/lib/boto/management_ui/panel/html_view.rb +43 -0
  93. data/lib/boto/management_ui/panel/json_view.rb +43 -0
  94. data/lib/boto/management_ui/panel/masking.rb +43 -0
  95. data/lib/boto/management_ui/panel/page.rb +41 -0
  96. data/lib/boto/management_ui/panel/service_info.rb +62 -0
  97. data/lib/boto/management_ui/panel.html.erb +207 -0
  98. data/lib/boto/management_ui/panel.rb +115 -0
  99. data/lib/boto/management_ui/password_auth.rb +26 -0
  100. data/lib/boto/management_ui/service_list.rb +59 -0
  101. data/lib/boto/management_ui/setup.rb +103 -0
  102. data/lib/boto/management_ui/spas.rb +73 -0
  103. data/lib/boto/management_ui/tabs.rb +40 -0
  104. data/lib/boto/management_ui/version.rb +7 -0
  105. data/lib/boto/management_ui.rb +15 -0
  106. metadata +203 -0
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The calls of a spa to the APIs of other services, by ref and operation: how many, the latency the browser
8
+ # saw (seconds, as the route latencies), and the share that failed (status 0 or 5xx).
9
+ class BrowserCalls
10
+ NO_RESPONSE = 0
11
+ HEAD = "HEAD"
12
+
13
+ # routes_by_ref: the route series rows of each service, by ref, to find the service side of a call.
14
+ def initialize(rows, routes_by_ref: {})
15
+ @rows = rows
16
+ @routes_by_ref = routes_by_ref
17
+ end
18
+
19
+ def call
20
+ @rows.group_by { |row| [row["ref"], row["operation"]] }
21
+ .map { |(ref, operation), rows| entry(ref, operation, rows) }
22
+ .sort_by { |entry| [-entry["calls"], entry["ref"], entry["operation"]] }
23
+ end
24
+
25
+ private
26
+
27
+ def entry(ref, operation, rows)
28
+ point = Points.total(rows)
29
+ { "ref" => ref, "operation" => operation, "calls" => point["count"],
30
+ "error_rate" => error_rate(rows, point["count"]), "service_p95" => service_p95(ref, operation) }
31
+ .merge(Points.latency(point))
32
+ end
33
+
34
+ # The p95 of the service's route series for the same operationId, summed over statuses; nil without data.
35
+ # HEAD shares the operationId of its GET but the browser never sends it, so it stays out.
36
+ def service_p95(ref, operation)
37
+ return nil if operation.nil?
38
+
39
+ rows = @routes_by_ref.fetch(ref, []).select { |row| row["operation"] == operation && row["verb"] != HEAD }
40
+ Points.latency(Points.total(rows))["p95"]
41
+ end
42
+
43
+ def error_rate(rows, calls)
44
+ return if calls.zero?
45
+
46
+ rows.select { |row| failed?(row["status"].to_i) }.sum { |row| row["count"] } / calls.to_f
47
+ end
48
+
49
+ def failed?(status)
50
+ status == NO_RESPONSE || status >= Points::SERVER_ERROR
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The pages of a spa over a period: views, vitals by route, and the charts. Rows are the "page" series, one
8
+ # per route, metric and minute, and the "views" metric counts the page views.
9
+ class BrowserView
10
+ VIEWS = "views"
11
+ SUMMARY_VITALS = %w[LCP INP CLS].freeze
12
+ CHART_VITALS = %w[LCP INP].freeze
13
+
14
+ def initialize(period:, from:, to:, rows:, errors:)
15
+ @period = period
16
+ @from = from
17
+ @to = to
18
+ @rows = rows
19
+ @errors = errors
20
+ end
21
+
22
+ def call
23
+ { "period" => @period.name, "summary" => summary, "routes" => routes, "charts" => charts }
24
+ end
25
+
26
+ private
27
+
28
+ def summary
29
+ metrics = metrics_of(@rows)
30
+ views = views_of(metrics)
31
+ { "views" => views, "views_per_second" => views / @period.seconds.to_f, "error_rate" => error_rate(views),
32
+ "vitals" => vitals(metrics, SUMMARY_VITALS) }
33
+ end
34
+
35
+ # Share of the views that had a JS error, at most 1: an error can happen without a view.
36
+ def error_rate(views)
37
+ return if views.zero?
38
+
39
+ [@errors.sum { |error| error["count"] } / views.to_f, 1.0].min
40
+ end
41
+
42
+ def routes
43
+ @rows.group_by { |row| row["route"] }
44
+ .map { |route, rows| route_entry(route, metrics_of(rows)) }
45
+ .sort_by { |entry| [-entry["views"], entry["route"]] }
46
+ end
47
+
48
+ def route_entry(route, metrics)
49
+ { "route" => route, "views" => views_of(metrics), "vitals" => vitals(metrics, Vitals::METRICS) }
50
+ end
51
+
52
+ def charts
53
+ step = @period.step
54
+ buckets = Charts.new(step: step, from: @from, to: @to)
55
+ starts = buckets.bucket_starts
56
+ by_start = @rows.group_by { |row| buckets.bucket_of(row["minute"]) }
57
+ metrics = starts.to_h { |start| [start, metrics_of(by_start.fetch(start, []))] }
58
+ { "step" => step, "views" => views_points(starts, metrics, step) }
59
+ .merge(CHART_VITALS.to_h { |metric| [metric.downcase, vital_points(starts, metrics, metric)] })
60
+ end
61
+
62
+ def views_points(starts, metrics, step)
63
+ starts.map { |start| { "at" => at(start), "per_second" => views_of(metrics[start]) / step.to_f } }
64
+ end
65
+
66
+ def vital_points(starts, metrics, metric)
67
+ starts.map do |start|
68
+ { "at" => at(start), "p75" => Vitals.entry(metric, metrics[start].fetch(metric, Points.empty))["p75"] }
69
+ end
70
+ end
71
+
72
+ def vitals(metrics, names)
73
+ names.to_h { |metric| [metric, Vitals.entry(metric, metrics.fetch(metric, Points.empty))] }
74
+ end
75
+
76
+ def metrics_of(rows)
77
+ Points.group(rows) { |row| row["metric"] }
78
+ end
79
+
80
+ def views_of(metrics)
81
+ metrics.fetch(VIEWS, Points.empty)["count"]
82
+ end
83
+
84
+ def at(start)
85
+ Queries.iso(Time.at(start))
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # Chart points: the minutes of a period grouped into buckets of `step` seconds, aligned to the epoch.
8
+ # Requests are per second over the bucket; CPU and memory are the average of its minutes.
9
+ class Charts
10
+ def initialize(step:, from:, to:)
11
+ @step = step
12
+ @from = from.to_i
13
+ @to = to.to_i
14
+ end
15
+
16
+ def build(route_rows, runtime_rows)
17
+ points = Points.group(route_rows) { |row| bucket_of(row["minute"]) }
18
+ runtime = runtime_rows.group_by { |row| bucket_of(row["minute"]) }
19
+ starts = bucket_starts
20
+ {
21
+ "step" => @step,
22
+ "requests" => starts.map { |start| requests_point(start, points.fetch(start, Points.empty)) },
23
+ "latency" => starts.map { |start| latency_point(start, points.fetch(start, Points.empty)) },
24
+ "cpu" => starts.map { |start| usage_point(start, runtime.fetch(start, []), "cores", "cpu_limit") },
25
+ "memory" => starts.map { |start| usage_point(start, runtime.fetch(start, []), "memory", "memory_limit") }
26
+ }
27
+ end
28
+
29
+ def bucket_starts
30
+ (bucket_of(@from)...@to).step(@step).to_a
31
+ end
32
+
33
+ def bucket_of(time)
34
+ seconds = time.to_i
35
+ seconds - (seconds % @step)
36
+ end
37
+
38
+ private
39
+
40
+ def requests_point(start, point)
41
+ { "at" => at(start), "per_second" => point["count"] / @step.to_f,
42
+ "errors_per_second" => point["errors"] / @step.to_f }
43
+ end
44
+
45
+ def latency_point(start, point)
46
+ { "at" => at(start) }.merge(Points.latency(point))
47
+ end
48
+
49
+ def usage_point(start, rows, usage_key, limit_key)
50
+ minutes = RuntimeView.minute_totals(rows)
51
+ { "at" => at(start), "usage" => average(minutes, usage_key), "limit" => average(minutes, limit_key) }
52
+ end
53
+
54
+ def average(minutes, key)
55
+ values = minutes.filter_map { |totals| totals[key] }
56
+ values.empty? ? nil : values.sum / values.size.to_f
57
+ end
58
+
59
+ def at(start)
60
+ Queries.iso(Time.at(start))
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The instances seen within stale_after, and the runtime rows of the minutes around now that belong to them.
8
+ class LiveInstances
9
+ # Seconds before the current minute read for the current state (latest minute and gc rates).
10
+ CURRENT_LOOKBACK = 120
11
+ SECONDS_PER_MINUTE = 60
12
+
13
+ def initialize(store:, clock:, stale_after:)
14
+ @store = store
15
+ @clock = clock
16
+ @stale_after = stale_after
17
+ end
18
+
19
+ def of(service)
20
+ only(@store.instances(service))
21
+ end
22
+
23
+ def only(infos)
24
+ cutoff = @clock.now - @stale_after
25
+ infos.select { |info| info["last_seen"] >= cutoff }
26
+ end
27
+
28
+ def runtime(service, live)
29
+ from, to = window
30
+ names = live.map { |info| info["instance"] }
31
+ @store.runtime_series(service:, from:, to:).select { |row| names.include?(row["instance"]) }
32
+ end
33
+
34
+ private
35
+
36
+ def window
37
+ minute = Period.minute_floor(@clock.now)
38
+ [Time.at(minute - CURRENT_LOOKBACK).utc, Time.at(minute + SECONDS_PER_MINUTE).utc]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # Sums route rows into points. Rows with the same minute and labels are summed here, never read as one:
8
+ # with several gateway replicas on Postgres there can be more than one row per service, minute and labels.
9
+ module Points
10
+ SERVER_ERROR = 500
11
+ SECONDS_PER_MINUTE = 60
12
+ SPARK_MINUTES = 15
13
+ QUANTILES = { "p50" => 0.5, "p95" => 0.95, "p99" => 0.99 }.freeze
14
+
15
+ module_function
16
+
17
+ def empty
18
+ { "count" => 0, "errors" => 0, "sum" => 0.0, "buckets" => Array.new(BatchSchema::BUCKET_COUNT, 0) }
19
+ end
20
+
21
+ def total(rows)
22
+ rows.each_with_object(empty) { |row, point| add(point, row) }
23
+ end
24
+
25
+ def group(rows)
26
+ rows.each_with_object({}) { |row, groups| add(groups[yield(row)] ||= empty, row) }
27
+ end
28
+
29
+ def add(point, row)
30
+ point["count"] += row["count"]
31
+ point["sum"] += row["sum"]
32
+ point["errors"] += row["count"] if row["status"].to_i >= SERVER_ERROR
33
+ point["buckets"] = point["buckets"].zip(row["buckets"]).map { |sum, bucket| sum + bucket }
34
+ point
35
+ end
36
+
37
+ def summary(point, seconds)
38
+ count = point["count"]
39
+ {
40
+ "requests" => count, "requests_per_second" => count / seconds.to_f,
41
+ "error_rate" => count.zero? ? nil : point["errors"] / count.to_f, "latency" => latency(point)
42
+ }
43
+ end
44
+
45
+ def latency(point)
46
+ QUANTILES.to_h { |name, quantile| [name, Percentile.from_buckets(point["buckets"], quantile)] }
47
+ end
48
+
49
+ # Requests per minute over the last SPARK_MINUTES minutes, the current one included.
50
+ def spark(rows, now)
51
+ per_minute = group(rows) { |row| row["minute"].to_i }
52
+ last = Period.minute_floor(now)
53
+ Array.new(SPARK_MINUTES) do |index|
54
+ per_minute.fetch(last - (SPARK_MINUTES - 1 - index) * SECONDS_PER_MINUTE, empty)["count"]
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The route list of a service: one entry per verb and route, with its points, its spark and whether it has
8
+ # an N+1 group and whether it has SSE streams in the period. Routes without a match (nil) go last.
9
+ class RouteView
10
+ SECONDS_PER_MINUTE = 60
11
+
12
+ def self.key(row)
13
+ row["route"].nil? ? [nil, nil] : [row["verb"], row["route"]]
14
+ end
15
+
16
+ # streamed: the route strings with streams in the period.
17
+ def initialize(period:, now:, flagged:, streamed: [])
18
+ @period = period
19
+ @now = now
20
+ @flagged = flagged
21
+ @streamed = streamed
22
+ end
23
+
24
+ def call(rows)
25
+ entries = rows.group_by { |row| self.class.key(row) }.map do |key, group|
26
+ entry(key, group).merge("n_plus_one" => @flagged.include?(key), "streams" => @streamed.include?(key.last))
27
+ end
28
+ entries.sort_by { |entry| [entry["route"].nil? ? 1 : 0, -entry["requests"]] }
29
+ end
30
+
31
+ private
32
+
33
+ def entry(key, rows)
34
+ verb, route = key
35
+ point = Points.total(rows)
36
+ Points.summary(point, @period.seconds).merge(
37
+ "verb" => verb, "route" => route,
38
+ "requests_per_minute" => point["count"] * SECONDS_PER_MINUTE / @period.seconds.to_f,
39
+ "spark" => Points.spark(rows, @now)
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # Runtime rows: one per instance, process and minute. The store already adds up cpu and gc_count within a
8
+ # minute, so CPU is the minute's CPU time over 60 seconds (cores) and GC is the minute's collections.
9
+ # Each instance is read at its latest complete minute, because the current minute is still filling up.
10
+ # Memory of a cgroup container counts once per instance, because every process reports the same container.
11
+ module RuntimeView
12
+ CGROUP = "cgroup"
13
+ SECONDS_PER_MINUTE = 60
14
+
15
+ module_function
16
+
17
+ def current(rows, current_minute)
18
+ rows.group_by { |row| row["instance"] }
19
+ .flat_map { |_instance, group| reference_rows(group, current_minute) }
20
+ end
21
+
22
+ def reference_rows(rows, current_minute)
23
+ complete = rows.select { |row| row["minute"].to_i < current_minute }
24
+ last_minute(complete.empty? ? rows : complete)
25
+ end
26
+
27
+ def last_minute(rows)
28
+ latest = rows.map { |row| row["minute"].to_i }.max
29
+ rows.select { |row| row["minute"].to_i == latest }
30
+ end
31
+
32
+ def minute_totals(rows)
33
+ rows.group_by { |row| row["minute"].to_i }.values.map { |group| totals(group) }
34
+ end
35
+
36
+ def totals(rows)
37
+ instances = rows.group_by { |row| row["instance"] }.values
38
+ {
39
+ "cores" => rows.sum(0.0) { |row| row["cpu"].to_f } / SECONDS_PER_MINUTE,
40
+ "cpu_limit" => total(instances.map { |group| max_of(group, "cpu_limit") }),
41
+ "memory" => instances.sum { |group| memory_of(group) },
42
+ "memory_limit" => total(instances.map { |group| max_of(group, "memory_limit") })
43
+ }
44
+ end
45
+
46
+ def instance_view(rows, info, current_minute)
47
+ reference = reference_rows(rows, current_minute)
48
+ totals(reference).merge(
49
+ "instance" => info["instance"], "workers" => reference.map { |row| row["process"] }.uniq.size,
50
+ "gc_per_minute" => gc_per_minute(reference), "pool" => info.dig("runtime", "pool"),
51
+ "in_flight" => info["in_flight"], "started_at" => Queries.iso(info["started_at"]),
52
+ "last_batch_at" => Queries.iso(info["last_seen"])
53
+ )
54
+ end
55
+
56
+ def total(values)
57
+ known = values.compact
58
+ known.empty? ? nil : known.sum
59
+ end
60
+
61
+ def max_of(group, key)
62
+ group.filter_map { |row| row[key] }.max
63
+ end
64
+
65
+ def memory_of(group)
66
+ return max_of(group, "memory").to_i if group.any? { |row| row["memory_source"] == CGROUP }
67
+
68
+ group.sum { |row| row["memory"].to_i }
69
+ end
70
+
71
+ # The rows are one minute: their gc_count values are that minute's collections, summed over processes.
72
+ def gc_per_minute(rows)
73
+ values = rows.filter_map { |row| row["gc_count"] }
74
+ values.empty? ? nil : values.sum
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The service list of the panel: one card per service with its points, its spark, its live instances and
8
+ # its current CPU and memory. A service with browser pages in the period is a spa.
9
+ class ServiceCards
10
+ SECONDS_PER_MINUTE = 60
11
+
12
+ def initialize(store:, clock:, live:)
13
+ @store = store
14
+ @clock = clock
15
+ @live = live
16
+ end
17
+
18
+ def names
19
+ @store.services.sort
20
+ end
21
+
22
+ def total(period)
23
+ Points.total(names.flat_map { |name| route_rows(name, period) })
24
+ end
25
+
26
+ def cards(period)
27
+ names.map { |name| card(name, period) }
28
+ end
29
+
30
+ private
31
+
32
+ def card(name, period)
33
+ rows = route_rows(name, period)
34
+ live = @live.of(name)
35
+ current = RuntimeView.totals(RuntimeView.current(@live.runtime(name, live), current_minute))
36
+ Points.summary(Points.total(rows), period.seconds).merge(
37
+ "name" => name, "kind" => kind_of(name, period), "health" => nil, "instances" => live.size,
38
+ "spark" => Points.spark(rows, @clock.now).map { |count| count / SECONDS_PER_MINUTE.to_f },
39
+ "cpu" => current["cores"], "cpu_limit" => current["cpu_limit"],
40
+ "memory" => current["memory"], "memory_limit" => current["memory_limit"]
41
+ )
42
+ end
43
+
44
+ # The gateway does not know the template of a service, so the browser pages tell a spa from a service.
45
+ def kind_of(name, period)
46
+ browser_series(name, period, "page").empty? ? "service" : "spa"
47
+ end
48
+
49
+ def route_rows(service, period)
50
+ browser_series(service, period, "routes")
51
+ end
52
+
53
+ def browser_series(service, period, kind)
54
+ from, to = period.window(@clock.now)
55
+ @store.series(service: service, from: from, to: to).select { |row| row["kind"] == kind }
56
+ end
57
+
58
+ def current_minute
59
+ Period.minute_floor(@clock.now)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The SSE streams of a service by route: opened and resumed, the refusals and closings by reason, the events,
8
+ # the failures, and the p50 and p95 of the stream duration. It is not the latency of the route, which ends
9
+ # when the headers are sent (proposal 0044).
10
+ class Streams
11
+ QUANTILES = %w[p50 p95].freeze
12
+
13
+ # rows: the per-minute stream rows of the period, as the store returns them.
14
+ def initialize(rows)
15
+ @rows = rows
16
+ end
17
+
18
+ def call
19
+ @rows.group_by { |row| row["route"] }
20
+ .map { |route, rows| entry(route, StreamPoint.total(rows)) }
21
+ .sort_by { |entry| [-entry["opened"], entry["route"]] }
22
+ end
23
+
24
+ private
25
+
26
+ def entry(route, point)
27
+ duration = point["duration"]
28
+ { "route" => route }.merge(point.except("duration"),
29
+ "duration" => { "count" => duration["count"] }.merge(quantiles(duration)))
30
+ end
31
+
32
+ def quantiles(duration)
33
+ Points.latency("buckets" => duration["buckets"]).slice(*QUANTILES)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The "children" of a trace: the traces of other services that its http spans called. Each child carries its
8
+ # own children, and the walk stops at MAX_DEPTH levels, so a call loop (A -> B -> A) ends.
9
+ class TraceTree
10
+ MAX_DEPTH = 5
11
+ # A browser trace calls through its fetch spans; a service trace through its http spans.
12
+ CALL_KINDS = %w[http fetch].freeze
13
+
14
+ def initialize(store:)
15
+ @store = store
16
+ end
17
+
18
+ def call(trace)
19
+ trace.merge("children" => children(trace, 1))
20
+ end
21
+
22
+ private
23
+
24
+ # depth is the level of the traces returned: the children of the root are level 1.
25
+ def children(trace, depth)
26
+ return [] if depth > MAX_DEPTH
27
+
28
+ callees(trace).map { |child| child.merge("children" => children(child, depth + 1)) }
29
+ end
30
+
31
+ def callees(trace)
32
+ ids = http_span_ids(trace)
33
+ return [] if ids.empty?
34
+
35
+ @store.traces_by_id(trace["trace_id"]).select do |other|
36
+ other["service"] != trace["service"] && ids.include?(other["parent_id"])
37
+ end
38
+ end
39
+
40
+ def http_span_ids(trace)
41
+ trace["spans"].filter_map { |span| span["id"] if CALL_KINDS.include?(span["kind"]) }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class Queries
7
+ # The web vitals of a page: the p75 of each one from its buckets, in the panel's unit (ms, and CLS as a
8
+ # plain number), and its rating by the official limits. The buckets hold seconds for the timings and the
9
+ # CLS score as it is.
10
+ module Vitals
11
+ METRICS = %w[LCP INP CLS FCP TTFB].freeze
12
+ # Good up to the first limit, needs improvement up to the second, poor above it.
13
+ LIMITS = {
14
+ "LCP" => [2500, 4000], "INP" => [200, 500], "CLS" => [0.1, 0.25], "FCP" => [1800, 3000],
15
+ "TTFB" => [800, 1800]
16
+ }.freeze
17
+ P75 = 0.75
18
+ MS_PER_SECOND = 1000
19
+
20
+ module_function
21
+
22
+ # Hash with "p75" and "rating", both nil when the point has no data.
23
+ def entry(metric, point)
24
+ value = value(metric, Percentile.from_buckets(point["buckets"], P75, bounds: bounds(metric)))
25
+ { "p75" => value, "rating" => rating(metric, value) }
26
+ end
27
+
28
+ # The agent fills the buckets with these same bounds (Boto::WebVitals, in the core).
29
+ def bounds(metric) = Boto::WebVitals.bounds(metric)
30
+
31
+ def value(metric, estimate)
32
+ return if estimate.nil?
33
+
34
+ metric == "CLS" ? estimate : estimate * MS_PER_SECOND
35
+ end
36
+
37
+ def rating(metric, value)
38
+ return if value.nil?
39
+
40
+ good, poor = LIMITS.fetch(metric)
41
+ return "good" if value <= good
42
+ return "needs_improvement" if value <= poor
43
+
44
+ "poor"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end