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,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "rack/utils"
5
+ require "stringio"
6
+ require "zlib"
7
+
8
+ module Boto
9
+ module ManagementUi
10
+ module Monitor
11
+ # Rack app for POST /ingest. Checks the token, the size and the shape of a batch before the store gets it.
12
+ # token: nil turns authentication off; that is only meant for development, in the same process.
13
+ # public: false refuses (403) a batch forwarded for a public address: the ingest belongs on the internal network,
14
+ # and the one token of the solution must not travel through a public ingress.
15
+ class Ingest
16
+ class TooLarge < StandardError; end
17
+
18
+ DEFAULT_MAX_BYTES = 1_048_576
19
+ DEFAULT_QUEUE_LIMIT = 64
20
+ READ_CHUNK = 16_384
21
+ JSON_TYPE = "application/json"
22
+ BEARER_SCHEME = "Bearer"
23
+ CHALLENGE = 'Bearer realm="boto-monitor"'
24
+ FORWARDED_FOR = /for="?\[?([^\]";,\s]+)/i
25
+ PUBLIC_MESSAGE = "ingest reached through a public proxy; keep it on the internal network " \
26
+ "(or set BOTO_MONITOR_PUBLIC_INGEST=1 on the management ui)"
27
+
28
+ def initialize(store:, token:, max_bytes: DEFAULT_MAX_BYTES, queue_limit: DEFAULT_QUEUE_LIMIT, clock: Time,
29
+ public: false)
30
+ @store = store
31
+ @public = public
32
+ @token = token
33
+ @max_bytes = max_bytes
34
+ @queue_limit = queue_limit
35
+ @clock = clock
36
+ @mutex = Mutex.new
37
+ @in_flight = 0
38
+ end
39
+
40
+ def call(env)
41
+ return error(403, PUBLIC_MESSAGE) unless @public || internal?(env)
42
+ return error(401, "unauthorized", "www-authenticate" => CHALLENGE) unless authorized?(env)
43
+ return error(405, "method not allowed", "allow" => "POST") unless env["REQUEST_METHOD"] == "POST"
44
+
45
+ with_slot { receive(env) }
46
+ end
47
+
48
+ private
49
+
50
+ def authorized?(env)
51
+ return true if @token.nil?
52
+
53
+ scheme, credential = env["HTTP_AUTHORIZATION"].to_s.split(" ", 2)
54
+ scheme == BEARER_SCHEME && !credential.nil? && Rack::Utils.secure_compare(credential, @token)
55
+ end
56
+
57
+ # Every address a proxy forwarded for (X-Forwarded-For and Forwarded) is private; true without either header.
58
+ def internal?(env)
59
+ forwarded = env["HTTP_X_FORWARDED_FOR"].to_s.split(",")
60
+ forwarded += env["HTTP_FORWARDED"].to_s.scan(FORWARDED_FOR).flatten
61
+ forwarded.all? { |address| Boto::ClientIp.private?(address) }
62
+ end
63
+
64
+ def receive(env)
65
+ batch = BatchSchema.validate(JSON.parse(body(env)))
66
+ @store.ingest(batch, received_at: @clock.now)
67
+ [202, {}, []]
68
+ rescue TooLarge
69
+ error(413, "payload too large")
70
+ rescue BatchSchema::Invalid => failure
71
+ error(400, failure.message)
72
+ rescue JSON::ParserError, Zlib::Error
73
+ error(400, "body is not valid JSON or gzip")
74
+ end
75
+
76
+ def body(env)
77
+ raw = read_limited(env["rack.input"])
78
+ text = gzip?(env) ? Zlib::GzipReader.wrap(StringIO.new(raw)) { |gzip| read_limited(gzip) } : raw
79
+ text.force_encoding(Encoding::UTF_8)
80
+ end
81
+
82
+ def gzip?(env)
83
+ env["HTTP_CONTENT_ENCODING"].to_s.strip.casecmp?("gzip")
84
+ end
85
+
86
+ # Reads in chunks and stops at the limit, so a big or compressed-bomb body never fills memory.
87
+ def read_limited(io)
88
+ data = String.new
89
+ while (chunk = io.read(READ_CHUNK))
90
+ data << chunk
91
+ raise TooLarge if data.bytesize > @max_bytes
92
+ end
93
+ data
94
+ end
95
+
96
+ # Refuses the batch with 429 when too many are already in flight; the counter is shared by threads and fibers.
97
+ def with_slot
98
+ return error(429, "too many batches in flight", "retry-after" => "1") unless admit
99
+
100
+ begin
101
+ yield
102
+ ensure
103
+ release
104
+ end
105
+ end
106
+
107
+ def admit
108
+ @mutex.synchronize do
109
+ next false if @in_flight >= @queue_limit
110
+
111
+ @in_flight += 1
112
+ true
113
+ end
114
+ end
115
+
116
+ def release
117
+ @mutex.synchronize { @in_flight -= 1 }
118
+ end
119
+
120
+ def error(status, message, headers = {})
121
+ [status, { "content-type" => JSON_TYPE }.merge(headers), [JSON.generate("error" => message)]]
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class MemoryStore
7
+ # Errors by service, keyed by source and fingerprint. Delete and set moves the updated one to the end, so
8
+ # the cap drops the oldest.
9
+ class ErrorTable
10
+ include Staleness
11
+
12
+ def initialize(max_errors:)
13
+ @max_errors = max_errors
14
+ @errors = {}
15
+ end
16
+
17
+ def add(service, errors, source)
18
+ known = @errors.fetch(service, {})
19
+ errors.each { |error| remember(known, error.merge("source" => source)) }
20
+ @errors[service] = known.to_a.last(@max_errors).to_h
21
+ end
22
+
23
+ def get(service, source:)
24
+ @errors.fetch(service, {}).values.select { |error| error["source"] == source }
25
+ end
26
+
27
+ def prune(cutoff)
28
+ @errors.each_value { |known| known.delete_if { |_fingerprint, error| stale?(error["last_at"], cutoff) } }
29
+ end
30
+
31
+ private
32
+
33
+ def remember(known, error)
34
+ key = [error["source"], error["fingerprint"]]
35
+ previous = known.delete(key)
36
+ known[key] = previous ? merge(previous, error) : error
37
+ end
38
+
39
+ def merge(previous, error)
40
+ merged = error.merge("count" => previous["count"] + error["count"], "first_at" => previous["first_at"])
41
+ return merged unless error.key?("browsers")
42
+
43
+ merged.merge("browsers" => sum_browsers(previous["browsers"], error["browsers"]))
44
+ end
45
+
46
+ def sum_browsers(previous, incoming)
47
+ previous.merge(incoming) { |_browser, old, new| old + new }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ module Monitor
8
+ class MemoryStore
9
+ # Instances by service and name, with the last batch seen from each one.
10
+ class InstanceTable
11
+ def initialize
12
+ @instances = {}
13
+ end
14
+
15
+ def record(service, batch, received_at)
16
+ instances = (@instances[service] ||= {})
17
+ instances[batch["instance"]] = {
18
+ "last_seen" => received_at, "started_at" => Time.iso8601(batch["started_at"]),
19
+ "process" => batch["process"],
20
+ "in_flight" => batch["in_flight"], "runtime" => batch["runtime"],
21
+ "streams_open" => batch.dig("streams", "open")
22
+ }
23
+ end
24
+
25
+ def services
26
+ @instances.keys
27
+ end
28
+
29
+ def list(service)
30
+ @instances.fetch(service, {}).map { |name, info| info.merge("instance" => name) }
31
+ end
32
+
33
+ def runtime(service)
34
+ @instances.fetch(service, {}).transform_values { |info| info["runtime"] }
35
+ end
36
+
37
+ def prune(cutoff)
38
+ @instances.each_value { |instances| instances.delete_if { |_name, info| info["last_seen"] < cutoff } }
39
+ @instances.delete_if { |_service, instances| instances.empty? }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class MemoryStore
7
+ # N+1 groups by service, one per fingerprint: the count adds up, the biggest max_repeats wins, and first_at
8
+ # stays.
9
+ class NPlusOneTable
10
+ include Staleness
11
+
12
+ def initialize
13
+ @groups = {}
14
+ end
15
+
16
+ def add(service, entries)
17
+ known = (@groups[service] ||= {})
18
+ entries.each do |entry|
19
+ previous = known[entry["fingerprint"]]
20
+ known[entry["fingerprint"]] = previous ? merge(previous, entry) : entry
21
+ end
22
+ end
23
+
24
+ def get(service)
25
+ @groups.fetch(service, {}).values
26
+ end
27
+
28
+ def prune(cutoff)
29
+ @groups.each_value { |known| known.delete_if { |_fingerprint, entry| stale?(entry["last_at"], cutoff) } }
30
+ end
31
+
32
+ private
33
+
34
+ def merge(previous, entry)
35
+ entry.merge(
36
+ "count" => previous["count"] + entry["count"],
37
+ "max_repeats" => [previous["max_repeats"], entry["max_repeats"]].max, "first_at" => previous["first_at"]
38
+ )
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ module Monitor
8
+ class MemoryStore
9
+ # Runtime rows by service, minute, instance and process. Like the SQL runtime table: summed fields add up
10
+ # within the minute, the others keep the last batch.
11
+ class RuntimeTable
12
+ def initialize
13
+ @rows = {}
14
+ end
15
+
16
+ def add(service, minute, batch)
17
+ key = [service, minute, batch["instance"], batch["process"]]
18
+ incoming = row(minute, batch)
19
+ @rows[key] = @rows.key?(key) ? sum(@rows[key], incoming) : incoming
20
+ end
21
+
22
+ def rows(service:, from:, to:)
23
+ @rows.filter_map do |(name, minute, _instance, _process), row|
24
+ row if name == service && minute >= from.to_i && minute < to.to_i
25
+ end.sort_by { |row| row["minute"] }
26
+ end
27
+
28
+ def prune(cutoff)
29
+ @rows.delete_if { |(_name, minute, _instance, _process), _row| minute < cutoff.to_i }
30
+ end
31
+
32
+ private
33
+
34
+ def row(minute, batch)
35
+ { "minute" => Time.at(minute).utc, "instance" => batch["instance"], "process" => batch["process"],
36
+ "in_flight" => batch["in_flight"] }.merge(batch["runtime"])
37
+ end
38
+
39
+ def sum(previous, incoming)
40
+ previous.merge(incoming) do |column, old, new|
41
+ BatchSchema::SUMMED_RUNTIME.include?(column) ? add_values(old, new) : new
42
+ end
43
+ end
44
+
45
+ def add_values(old, new)
46
+ return new if old.nil?
47
+ return old if new.nil?
48
+
49
+ old + new
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class MemoryStore
7
+ # Series points by service, minute, kind and labels. A batch adds into the point of its key, so the
8
+ # instances of a service sum up.
9
+ class SeriesTable
10
+ def initialize
11
+ @points = {}
12
+ end
13
+
14
+ def add(service, minute, batch)
15
+ BatchSchema::SERIES_KINDS.each do |key, kind|
16
+ labels = BatchSchema::SERIES_LABELS.fetch(kind)
17
+ batch[key].each { |entry| merge_point(service, minute, kind, entry, labels) }
18
+ end
19
+ end
20
+
21
+ def rows(service:, from:, to:)
22
+ @points.filter_map do |(name, minute, kind, labels), point|
23
+ next unless name == service && minute >= from.to_i && minute < to.to_i
24
+
25
+ { "minute" => Time.at(minute).utc, "kind" => kind }.merge(labels, point)
26
+ end.sort_by { |row| row["minute"] }
27
+ end
28
+
29
+ def prune(cutoff)
30
+ @points.delete_if { |(_name, minute, _kind, _labels), _point| minute < cutoff.to_i }
31
+ end
32
+
33
+ private
34
+
35
+ def merge_point(service, minute, kind, entry, labels)
36
+ point = (@points[[service, minute, kind, entry.slice(*labels)]] ||= empty_point)
37
+ point["count"] += entry["count"]
38
+ point["sum"] += entry["sum"]
39
+ point["buckets"] = point["buckets"].zip(entry["buckets"]).map { |total, bucket| total + bucket }
40
+ end
41
+
42
+ def empty_point
43
+ { "count" => 0, "sum" => 0, "buckets" => Array.new(BatchSchema::BUCKET_COUNT, 0) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ module Monitor
8
+ class MemoryStore
9
+ # Whether a stored ISO 8601 time is older than the window's cutoff.
10
+ module Staleness
11
+ module_function
12
+
13
+ def stale?(time, cutoff)
14
+ Time.iso8601(time) < cutoff
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class MemoryStore
7
+ # Stream points by service, minute and route. A batch adds into the point of its key, so the instances of a
8
+ # service sum up, as the SQL store does.
9
+ class StreamTable
10
+ def initialize
11
+ @points = {}
12
+ end
13
+
14
+ def add(service, minute, streams)
15
+ streams["routes"].each do |entry|
16
+ key = [service, minute, entry["route"]]
17
+ @points[key] = StreamPoint.add(@points.fetch(key) { StreamPoint.empty }, entry)
18
+ end
19
+ end
20
+
21
+ def rows(service:, from:, to:)
22
+ @points.filter_map do |(name, minute, route), point|
23
+ next unless name == service && minute >= from.to_i && minute < to.to_i
24
+
25
+ { "minute" => Time.at(minute).utc, "route" => route }.merge(point)
26
+ end.sort_by { |row| row["minute"] }
27
+ end
28
+
29
+ def prune(cutoff)
30
+ @points.delete_if { |(_name, minute, _route), _point| minute < cutoff.to_i }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ class MemoryStore
7
+ # Traces by service, oldest first, capped to the newest max_traces.
8
+ class TraceTable
9
+ include Staleness
10
+
11
+ def initialize(max_traces:)
12
+ @max_traces = max_traces
13
+ @traces = {}
14
+ end
15
+
16
+ def add(service, traces)
17
+ @traces[service] = (@traces.fetch(service, []) + traces).last(@max_traces)
18
+ end
19
+
20
+ def recent(service, limit:)
21
+ @traces.fetch(service, []).last(limit).reverse
22
+ end
23
+
24
+ def find(service, trace_id)
25
+ @traces.fetch(service, []).find { |trace| trace["trace_id"] == trace_id }
26
+ end
27
+
28
+ def by_id(trace_id)
29
+ @traces.flat_map do |service, traces|
30
+ traces.select { |trace| trace["trace_id"] == trace_id }.map { |trace| trace.merge("service" => service) }
31
+ end
32
+ end
33
+
34
+ def prune(cutoff)
35
+ @traces.transform_values! { |traces| traces.reject { |trace| stale?(trace["started_at"], cutoff) } }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ module Monitor
8
+ # In-memory Store for development. Keeps only the last window and sums series across instances.
9
+ class MemoryStore
10
+ include Store
11
+
12
+ DEFAULT_WINDOW = 3600
13
+ DEFAULT_MAX_TRACES = 500
14
+ DEFAULT_MAX_ERRORS = 200
15
+ SECONDS_PER_MINUTE = 60
16
+
17
+ def initialize(window: DEFAULT_WINDOW, max_traces: DEFAULT_MAX_TRACES, max_errors: DEFAULT_MAX_ERRORS,
18
+ clock: Time)
19
+ @window = window
20
+ @clock = clock
21
+ @mutex = Mutex.new
22
+ @series = SeriesTable.new
23
+ @runtime = RuntimeTable.new
24
+ @streams = StreamTable.new
25
+ @traces = TraceTable.new(max_traces:)
26
+ @errors = ErrorTable.new(max_errors:)
27
+ @n_plus_ones = NPlusOneTable.new
28
+ @instances = InstanceTable.new
29
+ end
30
+
31
+ def ingest(batch, received_at:)
32
+ @mutex.synchronize do
33
+ prune(@clock.now)
34
+ service = batch["service"]
35
+ minute = minute_of(batch["to"])
36
+ @series.add(service, minute, batch)
37
+ @runtime.add(service, minute, batch)
38
+ @streams.add(service, minute, batch["streams"]) if batch["streams"]
39
+ @traces.add(service, batch["traces"])
40
+ BatchSchema::ERROR_SOURCES.each { |key, source| @errors.add(service, batch[key], source) }
41
+ @n_plus_ones.add(service, batch["n_plus_ones"])
42
+ @instances.record(service, batch, received_at)
43
+ end
44
+ nil
45
+ end
46
+
47
+ def services
48
+ read { @instances.services }
49
+ end
50
+
51
+ def instances(service)
52
+ read { @instances.list(service) }
53
+ end
54
+
55
+ def series(service:, from:, to:)
56
+ read { @series.rows(service:, from:, to:) }
57
+ end
58
+
59
+ def runtime_series(service:, from:, to:)
60
+ read { @runtime.rows(service:, from:, to:) }
61
+ end
62
+
63
+ def streams(service:, from:, to:)
64
+ read { @streams.rows(service:, from:, to:) }
65
+ end
66
+
67
+ def errors(service, source: DEFAULT_ERROR_SOURCE)
68
+ read { @errors.get(service, source:) }
69
+ end
70
+
71
+ def n_plus_ones(service)
72
+ read { @n_plus_ones.get(service) }
73
+ end
74
+
75
+ def traces_by_id(trace_id)
76
+ read { @traces.by_id(trace_id) }
77
+ end
78
+
79
+ def traces(service, limit:)
80
+ read { @traces.recent(service, limit:) }
81
+ end
82
+
83
+ def trace(service, trace_id)
84
+ read { @traces.find(service, trace_id) }
85
+ end
86
+
87
+ def runtime(service)
88
+ read { @instances.runtime(service) }
89
+ end
90
+
91
+ private
92
+
93
+ def read
94
+ @mutex.synchronize do
95
+ prune(@clock.now)
96
+ yield
97
+ end
98
+ end
99
+
100
+ def prune(now)
101
+ cutoff = now - @window
102
+ @series.prune(cutoff)
103
+ @runtime.prune(cutoff)
104
+ @streams.prune(cutoff)
105
+ @traces.prune(cutoff)
106
+ @errors.prune(cutoff)
107
+ @n_plus_ones.prune(cutoff)
108
+ @instances.prune(cutoff)
109
+ end
110
+
111
+ def minute_of(time)
112
+ seconds = Time.iso8601(time).to_i
113
+ seconds - (seconds % SECONDS_PER_MINUTE)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ # Quantiles estimated from the latency buckets of a series: linear inside the bucket where the rank falls.
7
+ module Percentile
8
+ # Upper bounds in seconds; the bucket after the last one holds everything above it.
9
+ BOUNDS = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10].freeze
10
+
11
+ module_function
12
+
13
+ # Returns nil without data. The overflow bucket cannot be interpolated, so it answers the last bound.
14
+ def from_buckets(buckets, quantile, bounds: BOUNDS)
15
+ total = buckets.sum
16
+ return nil if total.zero?
17
+
18
+ rank = quantile * total
19
+ seen = 0
20
+ buckets.each_with_index do |count, index|
21
+ seen += count
22
+ next if count.zero? || seen < rank
23
+
24
+ return value_in(index, (rank - (seen - count)) / count.to_f, bounds)
25
+ end
26
+ bounds.last
27
+ end
28
+
29
+ def value_in(index, fraction, bounds)
30
+ return bounds.last if index == bounds.size
31
+
32
+ lower = index.zero? ? 0.0 : bounds[index - 1]
33
+ lower + fraction * (bounds[index] - lower)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ # A period of the panel: how far back the queries look, and how wide each point of a chart is.
7
+ class Period
8
+ SECONDS = { "15m" => 900, "1h" => 3_600, "24h" => 86_400, "7d" => 604_800 }.freeze
9
+ STEPS = [60, 300, 3_600].freeze
10
+ MAX_POINTS = 300
11
+ SECONDS_PER_MINUTE = 60
12
+
13
+ attr_reader :name, :seconds
14
+
15
+ def self.valid?(name)
16
+ SECONDS.key?(name)
17
+ end
18
+
19
+ def self.parse(name)
20
+ raise ArgumentError, "period must be one of #{SECONDS.keys.join(", ")}" unless valid?(name)
21
+
22
+ new(name, SECONDS.fetch(name))
23
+ end
24
+
25
+ def self.minute_floor(time)
26
+ seconds = time.to_i
27
+ seconds - (seconds % SECONDS_PER_MINUTE)
28
+ end
29
+
30
+ def initialize(name, seconds)
31
+ @name = name
32
+ @seconds = seconds
33
+ end
34
+
35
+ # Seconds per chart point: the smallest step that keeps a chart within MAX_POINTS points.
36
+ def step
37
+ STEPS.find { |candidate| seconds / candidate <= MAX_POINTS }
38
+ end
39
+
40
+ # [from, to) as Times, where `to` is the end of the current minute.
41
+ def window(now)
42
+ to = self.class.minute_floor(now) + SECONDS_PER_MINUTE
43
+ [Time.at(to - seconds).utc, Time.at(to).utc]
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end