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,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "rack/utils"
5
+
6
+ module Boto
7
+ module ManagementUi
8
+ module Monitor
9
+ # Rack app of the monitor's JSON endpoints. The gateway mounts it under /_boto/monitor and passes PATH_INFO
10
+ # relative to that prefix, such as "/services/pedidos/routes.json".
11
+ class Api
12
+ SERVICE = "[a-z][a-z0-9_]*"
13
+ TRACE_ID = "\\h{32}"
14
+ JSON_TYPE = "application/json"
15
+ READ_METHODS = %w[GET HEAD].freeze
16
+ DEFAULT_PERIOD = "1h"
17
+ ROUTES = {
18
+ %r{\A/solution\.json\z} => :solution,
19
+ %r{\A/services\.json\z} => :services,
20
+ %r{\A/services/(?<service>#{SERVICE})/routes\.json\z} => :routes,
21
+ %r{\A/services/(?<service>#{SERVICE})/charts\.json\z} => :charts,
22
+ %r{\A/services/(?<service>#{SERVICE})/errors\.json\z} => :errors,
23
+ %r{\A/services/(?<service>#{SERVICE})/n_plus_ones\.json\z} => :n_plus_ones,
24
+ %r{\A/services/(?<service>#{SERVICE})/traces\.json\z} => :traces,
25
+ %r{\A/services/(?<service>#{SERVICE})/traces/(?<trace_id>#{TRACE_ID})\.json\z} => :trace,
26
+ %r{\A/services/(?<service>#{SERVICE})/runtime\.json\z} => :runtime,
27
+ %r{\A/services/(?<service>#{SERVICE})/pages\.json\z} => :pages,
28
+ %r{\A/services/(?<service>#{SERVICE})/browser_errors\.json\z} => :browser_errors,
29
+ %r{\A/services/(?<service>#{SERVICE})/browser_calls\.json\z} => :browser_calls,
30
+ %r{\A/services/(?<service>#{SERVICE})/streams\.json\z} => :streams
31
+ }.freeze
32
+
33
+ def initialize(queries:)
34
+ @queries = queries
35
+ end
36
+
37
+ def call(env)
38
+ return method_not_allowed(env) unless READ_METHODS.include?(env["REQUEST_METHOD"])
39
+
40
+ name, captures = match(env["PATH_INFO"].to_s)
41
+ return error(env, 404, "not found") unless name
42
+
43
+ period = period_name(env)
44
+ return error(env, 400, "period must be one of #{Period::SECONDS.keys.join(", ")}") unless period
45
+
46
+ data = @queries.public_send(name, **arguments(name, captures, period))
47
+ data.nil? ? error(env, 404, "not found") : reply(env, 200, data)
48
+ end
49
+
50
+ private
51
+
52
+ def method_not_allowed(env)
53
+ reply(env, 405, { "error" => "method not allowed" }, "allow" => READ_METHODS.join(", "))
54
+ end
55
+
56
+ def error(env, status, message)
57
+ reply(env, status, { "error" => message })
58
+ end
59
+
60
+ def match(path)
61
+ ROUTES.each do |pattern, name|
62
+ found = pattern.match(path)
63
+ return [name, found.named_captures.transform_keys(&:to_sym)] if found
64
+ end
65
+ nil
66
+ end
67
+
68
+ def period_name(env)
69
+ name = Rack::Utils.parse_query(env["QUERY_STRING"].to_s).fetch("period", DEFAULT_PERIOD)
70
+ name if Period.valid?(name)
71
+ end
72
+
73
+ # Passes only the keywords the query method takes; `period` is ignored by the ones that do not use it.
74
+ def arguments(name, captures, period)
75
+ keywords = @queries.method(name).parameters.map(&:last)
76
+ captures.merge(period: period).slice(*keywords)
77
+ end
78
+
79
+ def reply(env, status, body, headers = {})
80
+ text = JSON.generate(body)
81
+ head = { "content-type" => JSON_TYPE, "cache-control" => "no-store", "content-length" => text.bytesize.to_s }
82
+ [status, head.merge(headers), env["REQUEST_METHOD"] == "HEAD" ? [] : [text]]
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # One call of the browser to an operation of another project, checked against the limits of the format.
8
+ module BrowserCallEntry
9
+ extend Checks
10
+
11
+ def self.call(item)
12
+ data = stringify(item, "browser_call").slice(*BROWSER_CALL_KEYS)
13
+ data.merge(
14
+ "ref" => text(data["ref"], "browser_call ref", max: MAX_SERVICE_LENGTH),
15
+ "operation" => text(data["operation"], "browser_call operation", max: MAX_PATH_LENGTH),
16
+ "status" => status(data["status"], "browser_call status"),
17
+ "count" => count(data["count"], "browser_call count"),
18
+ "sum" => number(data["sum"], "browser_call sum"),
19
+ "buckets" => Series.buckets(data["buckets"], "browser_call")
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # One error of the browser in a batch: like an error of the service, plus its route and browsers.
8
+ module BrowserErrorEntry
9
+ extend Checks
10
+
11
+ def self.call(item)
12
+ data = stringify(item, "browser_error").slice(*BROWSER_ERROR_KEYS)
13
+ data.merge(
14
+ "fingerprint" => text(data["fingerprint"], "browser_error fingerprint", max: MAX_FINGERPRINT_LENGTH),
15
+ "class" => optional_text(data["class"], "browser_error class", max: MAX_CLASS_LENGTH),
16
+ "message" => optional_text(data["message"], "browser_error message", max: MAX_MESSAGE_LENGTH),
17
+ "location" => optional_text(data["location"], "browser_error location", max: MAX_PATH_LENGTH),
18
+ "route" => optional_text(data["route"], "browser_error route", max: MAX_PATH_LENGTH),
19
+ "count" => count(data["count"], "browser_error count"),
20
+ "first_at" => timestamp(data["first_at"], "browser_error first_at"),
21
+ "last_at" => timestamp(data["last_at"], "browser_error last_at"),
22
+ "backtrace" => ErrorEntry.backtrace(data["backtrace"]),
23
+ "browsers" => browsers(data["browsers"])
24
+ )
25
+ end
26
+
27
+ # Browser name and version (such as "Chrome 131") to how many errors came from it.
28
+ def self.browsers(value)
29
+ valid = value.is_a?(Hash) && value.size <= MAX_BROWSERS
30
+ raise Invalid, "browser_error browsers must be an object with up to #{MAX_BROWSERS} browsers" unless valid
31
+
32
+ value.to_h do |browser, total|
33
+ [text(browser, "browser_error browser", max: MAX_BROWSER_LENGTH),
34
+ count(total, "browser_error browser count")]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ module Monitor
8
+ module BatchSchema
9
+ # Primitive checks shared by the batch validators. Each one returns the value or raises Invalid.
10
+ module Checks
11
+ module_function
12
+
13
+ def stringify(value, name)
14
+ raise Invalid, "#{name} must be an object" unless value.is_a?(Hash)
15
+
16
+ value.transform_keys(&:to_s)
17
+ end
18
+
19
+ def count(value, name)
20
+ raise Invalid, "#{name} must be an Integer >= 0" unless value.is_a?(Integer) && value >= 0
21
+
22
+ value
23
+ end
24
+
25
+ def number(value, name)
26
+ raise Invalid, "#{name} must be a number >= 0" unless value.is_a?(Numeric) && value >= 0
27
+
28
+ value
29
+ end
30
+
31
+ def text(value, name, max:)
32
+ valid = value.is_a?(String) && value.length.between?(1, max)
33
+ raise Invalid, "#{name} must be a String of 1 to #{max} characters" unless valid
34
+
35
+ value
36
+ end
37
+
38
+ def optional_text(value, name, max:)
39
+ value.nil? ? nil : text(value, name, max: max)
40
+ end
41
+
42
+ def hex(value, name, length:)
43
+ valid = value.is_a?(String) && value.match?(/\A\h{#{length}}\z/)
44
+ raise Invalid, "#{name} must be #{length} hexadecimal characters" unless valid
45
+
46
+ value
47
+ end
48
+
49
+ def optional_hex(value, name, length:)
50
+ value.nil? ? nil : hex(value, name, length: length)
51
+ end
52
+
53
+ def status(value, name)
54
+ valid = value.nil? || (value.is_a?(Integer) && STATUS_RANGE.cover?(value))
55
+ raise Invalid, "#{name} must be an HTTP status from 100 to 599 or null" unless valid
56
+
57
+ value
58
+ end
59
+
60
+ def one_of(value, name, options)
61
+ raise Invalid, "#{name} must be one of #{options.join(", ")}" unless options.include?(value)
62
+
63
+ value
64
+ end
65
+
66
+ # Lists come with a cap, so one batch cannot make the store grow without limit.
67
+ def list(value, name, max: nil)
68
+ raise Invalid, "#{name} must be a list" unless value.is_a?(Array)
69
+ raise Invalid, "#{name} has more than #{max} items" if max && value.size > max
70
+
71
+ value
72
+ end
73
+
74
+ def timestamp(value, name)
75
+ Time.iso8601(value)
76
+ value
77
+ rescue ArgumentError, TypeError
78
+ raise Invalid, "#{name} must be an ISO 8601 time"
79
+ end
80
+
81
+ def optional_type(value, name, type)
82
+ raise Invalid, "#{name} must be a #{type} or null" unless value.nil? || value.is_a?(type)
83
+
84
+ value
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # One error of a batch: its known fields, checked against the limits of the format.
8
+ module ErrorEntry
9
+ extend Checks
10
+
11
+ def self.call(item)
12
+ data = stringify(item, "error").slice(*ERROR_KEYS)
13
+ data.merge(
14
+ "fingerprint" => text(data["fingerprint"], "error fingerprint", max: MAX_FINGERPRINT_LENGTH),
15
+ "class" => optional_text(data["class"], "error class", max: MAX_CLASS_LENGTH),
16
+ "message" => optional_text(data["message"], "error message", max: MAX_MESSAGE_LENGTH),
17
+ "location" => optional_text(data["location"], "error location", max: MAX_PATH_LENGTH),
18
+ "verb" => optional_text(data["verb"], "error verb", max: MAX_VERB_LENGTH),
19
+ "route" => optional_text(data["route"], "error route", max: MAX_PATH_LENGTH),
20
+ "count" => count(data["count"], "error count"),
21
+ "first_at" => timestamp(data["first_at"], "error first_at"),
22
+ "last_at" => timestamp(data["last_at"], "error last_at"),
23
+ "backtrace" => backtrace(data["backtrace"]),
24
+ "trace_id" => optional_hex(data["trace_id"], "error trace_id", length: TRACE_ID_LENGTH)
25
+ )
26
+ end
27
+
28
+ def self.backtrace(value)
29
+ list(value, "error backtrace", max: MAX_BACKTRACE).map do |line|
30
+ text(line, "error backtrace line", max: MAX_BACKTRACE_LINE)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # One N+1 group of a batch: a route and a normalized query, checked against the limits of the format.
8
+ module NPlusOneEntry
9
+ extend Checks
10
+
11
+ def self.call(item)
12
+ data = stringify(item, "n_plus_one").slice(*N_PLUS_ONE_KEYS)
13
+ data.merge(
14
+ "fingerprint" => text(data["fingerprint"], "n_plus_one fingerprint",
15
+ max: MAX_N_PLUS_ONE_FINGERPRINT_LENGTH),
16
+ "verb" => optional_text(data["verb"], "n_plus_one verb", max: MAX_VERB_LENGTH),
17
+ "route" => optional_text(data["route"], "n_plus_one route", max: MAX_PATH_LENGTH),
18
+ "sql" => text(data["sql"], "n_plus_one sql", max: MAX_SQL_LENGTH),
19
+ "count" => count(data["count"], "n_plus_one count"),
20
+ "max_repeats" => count(data["max_repeats"], "n_plus_one max_repeats"),
21
+ "first_at" => timestamp(data["first_at"], "n_plus_one first_at"),
22
+ "last_at" => timestamp(data["last_at"], "n_plus_one last_at"),
23
+ "trace_id" => optional_hex(data["trace_id"], "n_plus_one trace_id", length: TRACE_ID_LENGTH)
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # One browser series of a batch: a route and a metric, checked against the limits of the format.
8
+ module PageEntry
9
+ extend Checks
10
+
11
+ def self.call(item)
12
+ data = stringify(item, "page").slice(*PAGE_KEYS)
13
+ data.merge(
14
+ "route" => text(data["route"], "page route", max: MAX_PATH_LENGTH),
15
+ "metric" => one_of(data["metric"], "page metric", PAGE_METRICS),
16
+ "count" => count(data["count"], "page count"),
17
+ "sum" => number(data["sum"], "page sum"),
18
+ "buckets" => Series.buckets(data["buckets"], "page")
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # The runtime of the process that sent the batch. Every field is optional, and null is allowed.
8
+ module Runtime
9
+ extend Checks
10
+
11
+ def self.call(value)
12
+ stringify(value, "runtime").slice(*RUNTIME_KEYS, "memory_source", "pool")
13
+ .to_h { |key, item| [key, field(key, item)] }
14
+ end
15
+
16
+ def self.field(key, item)
17
+ case key
18
+ when "memory_source" then optional_text(item, key, max: MAX_MEMORY_SOURCE_LENGTH)
19
+ when "pool" then pool(item)
20
+ else optional_type(item, key, Numeric)
21
+ end
22
+ end
23
+
24
+ def self.pool(value)
25
+ return nil if value.nil?
26
+
27
+ stringify(value, "pool").slice(*POOL_KEYS).to_h { |key, item| [key, optional_type(item, key, Numeric)] }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # The routes and operations of a batch: one entry per label combination of a minute.
8
+ module Series
9
+ extend Checks
10
+
11
+ def self.call(value, kind)
12
+ list(value, kind).map { |item| entry(item, kind) }
13
+ end
14
+
15
+ def self.entry(item, kind)
16
+ data = stringify(item, kind)
17
+ labels = SERIES_LABELS.fetch(kind).to_h { |label| [label, label_value(label, data[label], kind)] }
18
+ labels.merge(
19
+ "count" => count(data["count"], "#{kind} count"),
20
+ "sum" => number(data["sum"], "#{kind} sum"),
21
+ "buckets" => buckets(data["buckets"], kind)
22
+ )
23
+ end
24
+
25
+ def self.label_value(label, value, kind)
26
+ return status(value, "#{kind} status") if label == "status"
27
+
28
+ optional_text(value, "#{kind} #{label}", max: LABEL_LIMITS.fetch(label))
29
+ end
30
+
31
+ def self.buckets(value, name)
32
+ valid = value.is_a?(Array) && value.size == BUCKET_COUNT
33
+ raise Invalid, "#{name} buckets must have #{BUCKET_COUNT} counts" unless valid
34
+
35
+ value.each { |bucket| count(bucket, "#{name} bucket") }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # The SSE streams of a batch: the open gauge of the process and one entry per route, with its deltas.
8
+ # Optional as a whole: an app without the stream observer sends no "streams" key, which is nil here.
9
+ module Streams
10
+ extend Checks
11
+
12
+ def self.call(value)
13
+ return nil if value.nil?
14
+
15
+ data = stringify(value, "streams")
16
+ { "open" => count(data["open"], "streams open"),
17
+ "routes" => list(data["routes"], "streams routes", max: MAX_STREAM_ROUTES).map { |item| route(item) } }
18
+ end
19
+
20
+ def self.route(item)
21
+ data = stringify(item, "streams route")
22
+ { "route" => text(data["route"], "streams route", max: MAX_PATH_LENGTH) }
23
+ .merge(counters(data), "rejected" => reasons(data["rejected"], "rejected", STREAM_REJECTIONS),
24
+ "closed" => reasons(data["closed"], "closed", STREAM_CLOSINGS), "duration" => duration(data["duration"]))
25
+ end
26
+
27
+ def self.counters(data)
28
+ STREAM_COUNTERS.to_h { |key| [key, count(data[key], "streams #{key}")] }
29
+ end
30
+
31
+ def self.reasons(value, name, keys)
32
+ data = stringify(value, "streams #{name}")
33
+ keys.to_h { |key| [key, count(data[key], "streams #{name} #{key}")] }
34
+ end
35
+
36
+ def self.duration(value)
37
+ data = stringify(value, "streams duration")
38
+ { "count" => count(data["count"], "streams duration count"),
39
+ "sum" => number(data["sum"], "streams duration sum"),
40
+ "buckets" => Series.buckets(data["buckets"], "streams duration") }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ module BatchSchema
7
+ # One trace of a batch, with its spans, checked against the limits of the format.
8
+ module TraceEntry
9
+ extend Checks
10
+
11
+ def self.call(item)
12
+ data = stringify(item, "trace").slice(*TRACE_KEYS)
13
+ data.merge(
14
+ "trace_id" => hex(data["trace_id"], "trace trace_id", length: TRACE_ID_LENGTH),
15
+ "parent_id" => optional_hex(data["parent_id"], "trace parent_id", length: PARENT_ID_LENGTH),
16
+ "verb" => optional_text(data["verb"], "trace verb", max: MAX_VERB_LENGTH),
17
+ "route" => optional_text(data["route"], "trace route", max: MAX_PATH_LENGTH),
18
+ "path" => optional_text(data["path"], "trace path", max: MAX_PATH_LENGTH),
19
+ "status" => status(data["status"], "trace status"),
20
+ "started_at" => timestamp(data["started_at"], "trace started_at"),
21
+ "duration" => number(data["duration"], "trace duration"),
22
+ "reason" => one_of(data["reason"], "trace reason", REASONS),
23
+ "spans" => spans(data["spans"])
24
+ )
25
+ end
26
+
27
+ def self.spans(value)
28
+ list(value, "trace spans", max: MAX_SPANS).map { |item| span(item) }
29
+ end
30
+
31
+ def self.span(item)
32
+ data = stringify(item, "span").slice(*SPAN_KEYS)
33
+ data.merge(
34
+ "id" => optional_hex(data["id"], "span id", length: PARENT_ID_LENGTH),
35
+ "kind" => one_of(data["kind"], "span kind", SPAN_KINDS),
36
+ "name" => text(data["name"], "span name", max: MAX_SPAN_NAME_LENGTH),
37
+ "start" => number(data["start"], "span start"),
38
+ "duration" => number(data["duration"], "span duration")
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ module Monitor
8
+ # Validates a batch sent by a service and returns a normalized copy: only the known keys, with checked types.
9
+ module BatchSchema
10
+ extend Checks
11
+
12
+ class Invalid < StandardError; end
13
+
14
+ VERSION = 1
15
+ SERVICE_FORMAT = /\A[a-z][a-z0-9_]*\z/
16
+ MAX_SERVICE_LENGTH = 128
17
+ MAX_INSTANCE_LENGTH = 128
18
+ MAX_BOTO_LENGTH = 32
19
+ MAX_TRACES = 200
20
+ MAX_ERRORS = 100
21
+ MAX_SPANS = 200
22
+ MAX_BACKTRACE = 50
23
+ MAX_BACKTRACE_LINE = 512
24
+ MAX_VERB_LENGTH = 16
25
+ MAX_PATH_LENGTH = 512
26
+ MAX_RESULT_LENGTH = 64
27
+ MAX_OPERATION_LENGTH = 256
28
+ MAX_MESSAGE_LENGTH = 1000
29
+ MAX_CLASS_LENGTH = 256
30
+ MAX_FINGERPRINT_LENGTH = 600
31
+ MAX_SPAN_NAME_LENGTH = 2000
32
+ MAX_SQL_LENGTH = 1000
33
+ MAX_N_PLUS_ONES = 50
34
+ MAX_N_PLUS_ONE_FINGERPRINT_LENGTH = MAX_VERB_LENGTH + MAX_PATH_LENGTH + MAX_SQL_LENGTH + 2
35
+ MAX_PAGES = 200
36
+ MAX_BROWSER_CALLS = 200
37
+ MAX_BROWSER_ERRORS = 100
38
+ MAX_BROWSERS = 20
39
+ MAX_BROWSER_LENGTH = 64
40
+ MAX_MEMORY_SOURCE_LENGTH = 64
41
+ TRACE_ID_LENGTH = 32
42
+ PARENT_ID_LENGTH = 16
43
+ STATUS_RANGE = (100..599)
44
+ REASONS = %w[sample error slow all parent n_plus_one].freeze
45
+ SPAN_KINDS = %w[plugin operation sql http browser fetch].freeze
46
+ PAGE_METRICS = %w[LCP INP CLS FCP TTFB views].freeze
47
+ BUCKET_COUNT = 15
48
+ SERIES_LABELS = {
49
+ "routes" => %w[verb route status operation], "operations" => %w[name result],
50
+ "page" => %w[route metric], "browser_call" => %w[ref operation status]
51
+ }.freeze
52
+ # Batch key to the kind its entries are stored under in the series.
53
+ SERIES_KINDS = { "routes" => "routes", "operations" => "operations", "pages" => "page",
54
+ "browser_calls" => "browser_call" }.freeze
55
+ # Batch key to the source its errors are stored under.
56
+ ERROR_SOURCES = { "errors" => "server", "browser_errors" => "browser" }.freeze
57
+ LABEL_LIMITS = {
58
+ "verb" => MAX_VERB_LENGTH, "route" => MAX_PATH_LENGTH, "name" => MAX_PATH_LENGTH, "result" => MAX_RESULT_LENGTH,
59
+ "operation" => MAX_OPERATION_LENGTH
60
+ }.freeze
61
+ ERROR_KEYS = %w[fingerprint class message location verb route count first_at last_at backtrace trace_id].freeze
62
+ N_PLUS_ONE_KEYS = %w[fingerprint verb route sql count max_repeats first_at last_at trace_id].freeze
63
+ PAGE_KEYS = %w[route metric count sum buckets].freeze
64
+ BROWSER_CALL_KEYS = %w[ref operation status count sum buckets].freeze
65
+ BROWSER_ERROR_KEYS = %w[
66
+ fingerprint class message location route count first_at last_at backtrace browsers
67
+ ].freeze
68
+ TRACE_KEYS = %w[trace_id parent_id verb route path status started_at duration reason spans].freeze
69
+ SPAN_KEYS = %w[id kind name start duration].freeze
70
+ RUNTIME_KEYS = %w[cpu memory memory_limit cpu_limit gc_count dropped].freeze
71
+ POOL_KEYS = %w[size in_use].freeze
72
+ # Runtime fields the agent sends as deltas of the batch interval: the rows of a minute add them up.
73
+ SUMMED_RUNTIME = %w[cpu gc_count dropped].freeze
74
+ MAX_STREAM_ROUTES = 200
75
+ # The fixed reasons of the stream observer: a reason the gateway does not know is left out of the batch.
76
+ STREAM_COUNTERS = %w[opened resumed events_sent serialization_failures write_failures overflows].freeze
77
+ STREAM_REJECTIONS = %w[max_connections max_per_client draining pubsub].freeze
78
+ STREAM_CLOSINGS = %w[end client max_duration shutdown overflow error pubsub].freeze
79
+
80
+ class << self
81
+ def validate(batch)
82
+ data = stringify(batch, "batch")
83
+ {
84
+ "version" => version(data["version"]),
85
+ "service" => service(data["service"]),
86
+ "instance" => text(data["instance"], "instance", max: MAX_INSTANCE_LENGTH),
87
+ "boto" => text(data["boto"], "boto", max: MAX_BOTO_LENGTH),
88
+ "process" => count(data["process"], "process"),
89
+ "started_at" => timestamp(data["started_at"], "started_at"),
90
+ "from" => timestamp(data["from"], "from"),
91
+ "to" => timestamp(data["to"], "to"),
92
+ "in_flight" => count(data["in_flight"], "in_flight"),
93
+ "routes" => Series.call(data["routes"], "routes"),
94
+ "operations" => Series.call(data["operations"], "operations"),
95
+ "errors" => list(data["errors"], "errors", max: MAX_ERRORS).map { |item| ErrorEntry.call(item) },
96
+ "n_plus_ones" => optional_list(data["n_plus_ones"], "n_plus_ones", max: MAX_N_PLUS_ONES) do |item|
97
+ NPlusOneEntry.call(item)
98
+ end,
99
+ "pages" => optional_list(data["pages"], "pages", max: MAX_PAGES) { |item| PageEntry.call(item) },
100
+ "browser_calls" => optional_list(data["browser_calls"], "browser_calls", max: MAX_BROWSER_CALLS) do |item|
101
+ BrowserCallEntry.call(item)
102
+ end,
103
+ "browser_errors" => optional_list(data["browser_errors"], "browser_errors",
104
+ max: MAX_BROWSER_ERRORS) do |item|
105
+ BrowserErrorEntry.call(item)
106
+ end,
107
+ "streams" => Streams.call(data["streams"]),
108
+ "traces" => list(data["traces"], "traces", max: MAX_TRACES).map { |item| TraceEntry.call(item) },
109
+ "runtime" => Runtime.call(data["runtime"])
110
+ }
111
+ end
112
+
113
+ private
114
+
115
+ # Optional keys, added after the first batches: null or missing means none.
116
+ def optional_list(value, name, max:, &entry)
117
+ return [] if value.nil?
118
+
119
+ list(value, name, max: max).map(&entry)
120
+ end
121
+
122
+ def version(value)
123
+ raise Invalid, "version must be #{VERSION}" unless value.is_a?(Integer) && value == VERSION
124
+
125
+ value
126
+ end
127
+
128
+ def service(value)
129
+ valid = value.is_a?(String) && value.length <= MAX_SERVICE_LENGTH && SERVICE_FORMAT.match?(value)
130
+ raise Invalid, "service must match #{SERVICE_FORMAT.source}, up to #{MAX_SERVICE_LENGTH} characters" unless valid
131
+
132
+ value
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end