boto-rb-telemetry 0.28.0 → 0.30.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.
- checksums.yaml +4 -4
- data/lib/boto/plugins/telemetry.rb +65 -5
- data/lib/boto/telemetry/agent.rb +7 -2
- data/lib/boto/telemetry/batch.rb +9 -2
- data/lib/boto/telemetry/browser/call_trace.rb +35 -0
- data/lib/boto/telemetry/browser/collector.rb +123 -0
- data/lib/boto/telemetry/browser/error_groups.rb +92 -0
- data/lib/boto/telemetry/browser/operations.rb +29 -0
- data/lib/boto/telemetry/browser/payload/reader.rb +158 -0
- data/lib/boto/telemetry/browser/payload.rb +55 -0
- data/lib/boto/telemetry/browser/rate_window.rb +63 -0
- data/lib/boto/telemetry/browser/receiver.rb +76 -0
- data/lib/boto/telemetry/browser/scrubber.rb +34 -0
- data/lib/boto/telemetry/collector.rb +11 -5
- data/lib/boto/telemetry/histogram.rb +5 -3
- data/lib/boto/telemetry/local_sender.rb +15 -3
- data/lib/boto/telemetry/options.rb +33 -0
- data/lib/boto/telemetry/version.rb +1 -1
- metadata +12 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 643337ec509bfe1f7a826fe287425a330055bba169f48a37251cd23139c7bf1e
|
|
4
|
+
data.tar.gz: 64a9b127f6d0385f9f24cc55d7d9218bf5ced2b041894a5accc992ed77e47f08
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6e3f5c77d813a7e5038039f71cbdd83a7258893337e65f40b49bddb4927da6accfcb8f8ff6862ca72c10ce234be1fb77f21d226351bb05300c6a8b66c01ee9b4
|
|
7
|
+
data.tar.gz: e992f71c9be4730aa61b8d49bb990a7a6713ae5654fafac0159738c002fc4f2f8465f273fb23875e36c1be9cb9ee36c4108b162188553acc874093a21d3b21f8
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
3
5
|
module Boto
|
|
4
6
|
module Plugins
|
|
5
7
|
# The monitor agent of the project: counts each request into series, keeps the traces the sampler asks for, and
|
|
6
8
|
# ships the batches to the gateway (BOTO_MONITOR_URL, or the gateway of the same process in development). With
|
|
7
9
|
# log: true it also writes one JSON line per request. With nowhere to ship and no log, requests go straight through.
|
|
10
|
+
# With browser: on, POST <base>_boto/rum takes the batches of the browsers (see Boto::Telemetry::Browser).
|
|
8
11
|
class Telemetry < Boto::Plugin
|
|
9
12
|
scope :global
|
|
10
13
|
|
|
@@ -16,16 +19,24 @@ module Boto
|
|
|
16
19
|
QUERY_KEY = "QUERY_STRING"
|
|
17
20
|
STATUS_INDEX = 0
|
|
18
21
|
AGENT = Boto::Telemetry::Agent
|
|
22
|
+
RUM_PATH = "/_boto/rum"
|
|
23
|
+
RUM_LIMIT = 60
|
|
24
|
+
RUM_PERIOD = 60
|
|
25
|
+
RUM_SCOPE = "rum"
|
|
26
|
+
RATE_LIMIT_PLUGIN = :rate_limit
|
|
27
|
+
OPERATIONS_FILE = %w[dist .boto operations.json].freeze
|
|
19
28
|
|
|
20
29
|
# The running agent, nil without a delivery. Tests flush the batches through it.
|
|
21
30
|
attr_reader :agent
|
|
22
31
|
|
|
23
32
|
def attach(app)
|
|
24
|
-
@routes = app.endpoints.map { |endpoint| app.route_for(endpoint) }
|
|
33
|
+
@routes = app.endpoints.map { |endpoint| [*app.route_for(endpoint), Boto::OpenAPI.operation_id(endpoint)] }
|
|
25
34
|
@title = app.title
|
|
35
|
+
@rate_limit = rate_limit_of(app)
|
|
26
36
|
end
|
|
27
37
|
|
|
28
38
|
def call(env, app)
|
|
39
|
+
return @rum.call(env) if @rum && env[AGENT::PATH_KEY] == RUM_PATH
|
|
29
40
|
return app.call(env) unless @recording
|
|
30
41
|
|
|
31
42
|
started = monotonic_now
|
|
@@ -40,6 +51,7 @@ module Boto
|
|
|
40
51
|
@started = true
|
|
41
52
|
@agent = build_agent
|
|
42
53
|
@recording = @log || !@agent.nil?
|
|
54
|
+
@rum = build_receiver
|
|
43
55
|
@agent&.start
|
|
44
56
|
end
|
|
45
57
|
|
|
@@ -49,6 +61,7 @@ module Boto
|
|
|
49
61
|
|
|
50
62
|
@started = false
|
|
51
63
|
@recording = false
|
|
64
|
+
@rum = nil
|
|
52
65
|
@agent&.stop
|
|
53
66
|
@agent = nil
|
|
54
67
|
end
|
|
@@ -56,25 +69,37 @@ module Boto
|
|
|
56
69
|
# Called by the app for each plugin of its chains: the plugins inside this one get a span per request.
|
|
57
70
|
def wrap_plugin(plugin, app) = Boto::Telemetry::PluginSpans.wrap(plugin, app)
|
|
58
71
|
|
|
72
|
+
# The <meta> the spa reads for the browser side, as JSON; nil when browser: is off. base is the URL prefix the
|
|
73
|
+
# browser sees ("/" for the spa at the root).
|
|
74
|
+
def browser_meta(base:)
|
|
75
|
+
return nil unless @browser
|
|
76
|
+
|
|
77
|
+
JSON.generate(endpoint: "#{base}_boto/rum", sample: @browser[:sample], traceSample: @browser[:trace_sample])
|
|
78
|
+
end
|
|
79
|
+
|
|
59
80
|
private
|
|
60
81
|
|
|
61
82
|
# transport: and output: are for tests and the benchmark. transport receives each batch (a Hash) instead of the
|
|
62
83
|
# gateway; output receives the request log lines.
|
|
63
84
|
def configure(filter: [], log: true, sample: DEFAULT_SAMPLE, slow: DEFAULT_SLOW, http: true, propagate: :all,
|
|
64
85
|
n_plus_one: Boto::Telemetry::NPlusOnes::DEFAULT_THRESHOLD, transport: nil, output: $stdout,
|
|
65
|
-
**options)
|
|
86
|
+
browser: nil, **options)
|
|
66
87
|
reject_options(options)
|
|
67
88
|
@log = boolean(:log, log)
|
|
68
89
|
@n_plus_one = threshold(:n_plus_one, n_plus_one)
|
|
69
90
|
@outbound = outbound_policy(http, propagate)
|
|
70
91
|
@output = output
|
|
71
|
-
|
|
72
|
-
@request_log = Boto::Telemetry::RequestLog.new(filter:
|
|
92
|
+
@filter = Boto::Telemetry::Filter.new(extra: list(:filter, filter))
|
|
93
|
+
@request_log = Boto::Telemetry::RequestLog.new(filter: @filter)
|
|
73
94
|
@sampler = Boto::Telemetry::Sampler.new(sample:, slow:, all: development?)
|
|
74
95
|
@destination = Boto::Telemetry::Destination.new(env:, env_name:, transport:)
|
|
75
96
|
@project = File.basename(project_dir.to_s)
|
|
97
|
+
@browser = browser_settings(browser, development: development?)
|
|
98
|
+
@operations = @browser ? Boto::Telemetry::Browser::Operations.read(operations_path) : nil
|
|
76
99
|
@routes = []
|
|
77
100
|
@title = nil
|
|
101
|
+
@rate_limit = nil
|
|
102
|
+
@rum = nil
|
|
78
103
|
@started = false
|
|
79
104
|
@recording = false
|
|
80
105
|
end
|
|
@@ -103,10 +128,45 @@ module Boto
|
|
|
103
128
|
AGENT.new(
|
|
104
129
|
service: @project, routes: @routes, sampler: @sampler, delivery:, project_dir: project_dir.to_s,
|
|
105
130
|
boto_version: Boto::VERSION, pool: Boto::Telemetry::DatabasePool.new(@title), outbound: @outbound,
|
|
106
|
-
n_plus_one: @n_plus_one
|
|
131
|
+
n_plus_one: @n_plus_one, browser_routes: @browser&.fetch(:routes)
|
|
107
132
|
)
|
|
108
133
|
end
|
|
109
134
|
|
|
135
|
+
# Only with an agent to ship to: without a monitor the browser has nowhere to go, so the path is not served.
|
|
136
|
+
def build_receiver
|
|
137
|
+
return nil unless @agent&.browser
|
|
138
|
+
|
|
139
|
+
Boto::Telemetry::Browser::Receiver.new(
|
|
140
|
+
collector: @agent.browser, operations: @operations, filter: @filter, limiter: rum_limiter,
|
|
141
|
+
client_ip: rum_client_ip
|
|
142
|
+
)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The IP rule of the project's :rate_limit, so X-Forwarded-For counts only from its trusted proxies. Without
|
|
146
|
+
# one, the private networks are trusted: an ingress inside the network forwards the visitor, and a visitor that
|
|
147
|
+
# reaches the server straight from outside cannot pick its IP.
|
|
148
|
+
def rum_client_ip
|
|
149
|
+
rule = @rate_limit.client_ip if @rate_limit.respond_to?(:client_ip)
|
|
150
|
+
rule || Boto::ClientIp.new(trusted_proxies: Boto::ClientIp::PRIVATE_NETWORKS)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The store of the project's :rate_limit when there is one, so the counts sit with the others; else in memory.
|
|
154
|
+
def rum_limiter
|
|
155
|
+
return @rate_limit.endpoint_limiter(limit: RUM_LIMIT, period: RUM_PERIOD, scope: RUM_SCOPE) if @rate_limit
|
|
156
|
+
|
|
157
|
+
Boto::Telemetry::Browser::RateWindow.new(limit: RUM_LIMIT, period: RUM_PERIOD)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def rate_limit_of(app)
|
|
161
|
+
return nil unless app.respond_to?(:plugins)
|
|
162
|
+
|
|
163
|
+
app.plugins.find { |plugin| plugin.class.plugin_name == RATE_LIMIT_PLUGIN }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def operations_path
|
|
167
|
+
project_dir && File.join(project_dir.to_s, *OPERATIONS_FILE)
|
|
168
|
+
end
|
|
169
|
+
|
|
110
170
|
def write_log(env, status, started)
|
|
111
171
|
trace = env[AGENT::TRACE_KEY]
|
|
112
172
|
@output.write(@request_log.line(
|
data/lib/boto/telemetry/agent.rb
CHANGED
|
@@ -5,6 +5,9 @@ module Boto
|
|
|
5
5
|
# The monitor of one project in one process: counts each request into its series, keeps the traces the sampler
|
|
6
6
|
# asks for, groups the errors and hands the batches to a delivery. The plugin builds it at start, after the fork.
|
|
7
7
|
class Agent
|
|
8
|
+
# nil unless the project has browser: on. The RUM receiver records into it.
|
|
9
|
+
attr_reader :browser
|
|
10
|
+
|
|
8
11
|
METHOD_KEY = "REQUEST_METHOD"
|
|
9
12
|
PATH_KEY = "PATH_INFO"
|
|
10
13
|
ROUTE_KEY = "boto.route"
|
|
@@ -16,8 +19,9 @@ module Boto
|
|
|
16
19
|
|
|
17
20
|
# outbound is the HTTP policy of the project (see HttpSpans); nil means the outgoing calls are not traced.
|
|
18
21
|
# n_plus_one is the repeats of one statement that make a request an N+1 (false turns the count off).
|
|
22
|
+
# browser_routes is the route limit of the browser series; nil leaves the browser side off (see Browser::Collector).
|
|
19
23
|
def initialize(service:, routes:, sampler:, delivery:, project_dir:, boto_version:, pool:, outbound: nil,
|
|
20
|
-
n_plus_one: NPlusOnes::DEFAULT_THRESHOLD)
|
|
24
|
+
n_plus_one: NPlusOnes::DEFAULT_THRESHOLD, browser_routes: nil)
|
|
21
25
|
@service = service
|
|
22
26
|
@sampler = sampler
|
|
23
27
|
@delivery = delivery
|
|
@@ -27,9 +31,10 @@ module Boto
|
|
|
27
31
|
@errors = Errors.new(project_dir:, clock: Batch::WALL_CLOCK)
|
|
28
32
|
@n_plus_ones = NPlusOnes.new(clock: Batch::WALL_CLOCK)
|
|
29
33
|
@traces = TraceBuffer.new
|
|
34
|
+
@browser = Browser::Collector.new(routes: browser_routes, traces: @traces) if browser_routes
|
|
30
35
|
@batch = Batch.new(service:, process: Process.pid, boto_version:, started_at: Time.now,
|
|
31
36
|
collector: @collector, traces: @traces, errors: @errors, n_plus_ones: @n_plus_ones,
|
|
32
|
-
runtime: Runtime.new(pool:))
|
|
37
|
+
runtime: Runtime.new(pool:), browser: @browser)
|
|
33
38
|
@built = false
|
|
34
39
|
@skip_last_batch = false
|
|
35
40
|
end
|
data/lib/boto/telemetry/batch.rb
CHANGED
|
@@ -12,8 +12,9 @@ module Boto
|
|
|
12
12
|
FORMAT_VERSION = 1
|
|
13
13
|
WALL_CLOCK = -> { Time.now }
|
|
14
14
|
|
|
15
|
+
# browser is the Browser::Collector of a project with browser: on; without it the batch has no browser keys.
|
|
15
16
|
def initialize(service:, process:, boto_version:, started_at:, collector:, traces:, errors:, n_plus_ones:,
|
|
16
|
-
runtime:, instance: Socket.gethostname, clock: WALL_CLOCK)
|
|
17
|
+
runtime:, browser: nil, instance: Socket.gethostname, clock: WALL_CLOCK)
|
|
17
18
|
@service = service
|
|
18
19
|
@instance = instance
|
|
19
20
|
@process = process
|
|
@@ -24,6 +25,7 @@ module Boto
|
|
|
24
25
|
@errors = errors
|
|
25
26
|
@n_plus_ones = n_plus_ones
|
|
26
27
|
@runtime = runtime
|
|
28
|
+
@browser = browser
|
|
27
29
|
@clock = clock
|
|
28
30
|
@from = started_at
|
|
29
31
|
end
|
|
@@ -31,18 +33,23 @@ module Boto
|
|
|
31
33
|
def build
|
|
32
34
|
to = @clock.call
|
|
33
35
|
# Read the counts before the drains, which reset them.
|
|
34
|
-
dropped = @traces.take_dropped + @errors.dropped + @n_plus_ones.dropped
|
|
36
|
+
dropped = @traces.take_dropped + @errors.dropped + @n_plus_ones.dropped + browser_dropped
|
|
35
37
|
series = @collector.drain
|
|
36
38
|
batch = header(to).merge(series).merge(
|
|
37
39
|
errors: @errors.drain, n_plus_ones: @n_plus_ones.drain, traces: @traces.drain,
|
|
38
40
|
runtime: @runtime.read.merge(dropped:)
|
|
39
41
|
)
|
|
42
|
+
batch = batch.merge(@browser.drain) if @browser
|
|
40
43
|
@from = to
|
|
41
44
|
batch
|
|
42
45
|
end
|
|
43
46
|
|
|
44
47
|
private
|
|
45
48
|
|
|
49
|
+
def browser_dropped
|
|
50
|
+
@browser ? @browser.take_dropped : 0
|
|
51
|
+
end
|
|
52
|
+
|
|
46
53
|
def header(to)
|
|
47
54
|
{
|
|
48
55
|
version: FORMAT_VERSION, service: @service, instance: @instance, process: @process,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Telemetry
|
|
7
|
+
module Browser
|
|
8
|
+
# The trace of one sampled browser call, in the trace format of the batch: a `browser` span for the page and a
|
|
9
|
+
# `fetch` span whose id is the span of the traceparent the client sent. The service's trace has that id as its
|
|
10
|
+
# parent, so the gateway nests it under the fetch.
|
|
11
|
+
module CallTrace
|
|
12
|
+
MS = 1000.0
|
|
13
|
+
REASON = Sampler::REASON_SAMPLE
|
|
14
|
+
BROWSER_KIND = "browser"
|
|
15
|
+
FETCH_KIND = "fetch"
|
|
16
|
+
ROOT_START = 0.0
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def build(call, route)
|
|
21
|
+
duration = call.ms / MS
|
|
22
|
+
{
|
|
23
|
+
trace_id: call.trace.id, parent_id: nil, verb: nil, route:, path: route, status: call.status,
|
|
24
|
+
started_at: Time.at(call.trace.start / MS).getutc.iso8601, duration:, reason: REASON,
|
|
25
|
+
spans: [span(BROWSER_KIND, route, duration, nil), span(FETCH_KIND, "#{call.ref}.#{call.op}", duration, call.trace.span)]
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def span(kind, name, duration, id)
|
|
30
|
+
Span.new(kind:, name:, start: ROOT_START, duration:, id:).to_h
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Telemetry
|
|
5
|
+
module Browser
|
|
6
|
+
# Keeps what the browsers post, as series of one worker: the pages by route (views and the Web Vitals), the calls
|
|
7
|
+
# to the APIs by ref, operation and status, and the JS errors grouped. Drain hands the batch over and starts over.
|
|
8
|
+
#
|
|
9
|
+
# Units: timings are recorded in seconds (the ms are divided by 1000) and the CLS as it is, its score. The
|
|
10
|
+
# buckets and the sum of each vital go out in those units. The sum of the calls goes out in ms.
|
|
11
|
+
class Collector
|
|
12
|
+
VITALS = %w[LCP INP CLS FCP TTFB].freeze
|
|
13
|
+
CLS = "CLS"
|
|
14
|
+
VIEWS = "views"
|
|
15
|
+
OTHER_ROUTE = "(other)"
|
|
16
|
+
MS = 1000.0
|
|
17
|
+
EMPTY_BUCKETS = Array.new(Histogram::BOUNDARIES.size + 1, 0).freeze
|
|
18
|
+
|
|
19
|
+
Page = Struct.new(:views, :vitals)
|
|
20
|
+
|
|
21
|
+
def initialize(routes:, traces:, clock: Batch::WALL_CLOCK)
|
|
22
|
+
@routes_limit = routes
|
|
23
|
+
@traces = traces
|
|
24
|
+
@errors = ErrorGroups.new(clock:)
|
|
25
|
+
@pages = {}
|
|
26
|
+
@calls = {}
|
|
27
|
+
@dropped = 0
|
|
28
|
+
@lock = Thread::Mutex.new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def record(payload)
|
|
32
|
+
@lock.synchronize do
|
|
33
|
+
label = route_label(payload.route)
|
|
34
|
+
page = page_for(label)
|
|
35
|
+
payload.events.each { |event| record_event(event, label, payload.browser, page) }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Counts a request the receiver refused for its volume.
|
|
40
|
+
def count_dropped
|
|
41
|
+
@lock.synchronize { @dropped += 1 }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def take_dropped
|
|
45
|
+
@lock.synchronize do
|
|
46
|
+
taken = @dropped + @errors.take_dropped
|
|
47
|
+
@dropped = 0
|
|
48
|
+
taken
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def drain
|
|
53
|
+
@lock.synchronize do
|
|
54
|
+
batch = { pages: page_rows, browser_calls: call_rows, browser_errors: @errors.drain }
|
|
55
|
+
@pages = {}
|
|
56
|
+
@calls = {}
|
|
57
|
+
batch
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def record_event(event, label, browser, page)
|
|
64
|
+
case event
|
|
65
|
+
when Payload::View then page.views += 1
|
|
66
|
+
when Payload::Vital then record_vital(page, event)
|
|
67
|
+
when Payload::Call then record_call(event, label)
|
|
68
|
+
when Payload::JsError then @errors.record(event, route: label, browser:)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Past the limit a new route joins the "(other)" series, so the cardinality stays bounded.
|
|
73
|
+
def route_label(route)
|
|
74
|
+
return route if @pages.key?(route) || @pages.size < @routes_limit
|
|
75
|
+
|
|
76
|
+
OTHER_ROUTE
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def page_for(label)
|
|
80
|
+
@pages[label] ||= Page.new(0, VITALS.to_h { |metric| [metric, vital_histogram(metric)] })
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def vital_histogram(metric)
|
|
84
|
+
Histogram.new(Boto::WebVitals.bounds(metric))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def record_vital(page, vital)
|
|
88
|
+
seconds = vital.name == CLS ? vital.value : vital.value / MS
|
|
89
|
+
page.vitals.fetch(vital.name).record(seconds)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def record_call(call, label)
|
|
93
|
+
(@calls[[call.ref, call.op, call.status]] ||= Histogram.new).record(call.ms / MS)
|
|
94
|
+
@traces.push(CallTrace.build(call, label)) if call.trace
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def page_rows
|
|
98
|
+
@pages.flat_map do |route, page|
|
|
99
|
+
rows = page.vitals.filter_map { |metric, histogram| page_row(route, metric, histogram) }
|
|
100
|
+
page.views.zero? ? rows : [views_row(route, page.views), *rows]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def page_row(route, metric, histogram)
|
|
105
|
+
return if histogram.empty?
|
|
106
|
+
|
|
107
|
+
{ route:, metric:, count: histogram.count, sum: histogram.sum, buckets: histogram.buckets.dup }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def views_row(route, views)
|
|
111
|
+
{ route:, metric: VIEWS, count: views, sum: 0, buckets: EMPTY_BUCKETS.dup }
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def call_rows
|
|
115
|
+
@calls.map do |(ref, operation, status), histogram|
|
|
116
|
+
{ ref:, operation:, status:, count: histogram.count, sum: (histogram.sum * MS).round(3),
|
|
117
|
+
buckets: histogram.buckets.dup }
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Telemetry
|
|
7
|
+
module Browser
|
|
8
|
+
# Groups the JS errors of a batch by fingerprint (class, message with the numbers folded, file:line of the source),
|
|
9
|
+
# so a repeated failure is one entry with a count. Called under the collector's lock.
|
|
10
|
+
class ErrorGroups
|
|
11
|
+
LIMIT = 50
|
|
12
|
+
MAX_MESSAGE = 500
|
|
13
|
+
MAX_BROWSERS = 20
|
|
14
|
+
DIGITS = /\d+/
|
|
15
|
+
FOLDED_DIGITS = "N"
|
|
16
|
+
# shortcut: the column is dropped from the location, so errors on one line group together.
|
|
17
|
+
LOCATION = /\A(.*?):(\d+)(?::\d+)?\z/
|
|
18
|
+
|
|
19
|
+
attr_reader :dropped
|
|
20
|
+
|
|
21
|
+
def initialize(clock:, limit: LIMIT)
|
|
22
|
+
@clock = clock
|
|
23
|
+
@limit = limit
|
|
24
|
+
@groups = {}
|
|
25
|
+
@dropped = 0
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def record(error, route:, browser:)
|
|
29
|
+
location = location_of(error.source)
|
|
30
|
+
key = fingerprint(error, location)
|
|
31
|
+
now = timestamp
|
|
32
|
+
group = @groups[key] || open_group(key, error, location, route, now)
|
|
33
|
+
return @dropped += 1 unless group
|
|
34
|
+
|
|
35
|
+
group[:count] += 1
|
|
36
|
+
group[:last_at] = now
|
|
37
|
+
count_browser(group[:browsers], browser)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns the groups in batch format and starts the next batch empty.
|
|
41
|
+
def drain
|
|
42
|
+
groups = @groups.values
|
|
43
|
+
@groups = {}
|
|
44
|
+
groups
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def take_dropped
|
|
48
|
+
taken = @dropped
|
|
49
|
+
@dropped = 0
|
|
50
|
+
taken
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def fingerprint(error, location)
|
|
56
|
+
folded = error.message.to_s.gsub(DIGITS, FOLDED_DIGITS)
|
|
57
|
+
"#{error.kind}@#{location}: #{folded}"[0, MAX_MESSAGE]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def open_group(key, error, location, route, now)
|
|
61
|
+
return nil if @groups.size >= @limit
|
|
62
|
+
|
|
63
|
+
@groups[key] = {
|
|
64
|
+
fingerprint: key, class: error.kind, message: error.message.to_s[0, MAX_MESSAGE], location:, route:,
|
|
65
|
+
count: 0, first_at: now, last_at: now, backtrace: backtrace_of(error), browsers: {}
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def count_browser(browsers, browser)
|
|
70
|
+
return if !browsers.key?(browser) && browsers.size >= MAX_BROWSERS
|
|
71
|
+
|
|
72
|
+
browsers[browser] = browsers.fetch(browser, 0) + 1
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def backtrace_of(error)
|
|
76
|
+
error.stack ? error.stack.lines.map(&:chomp) : []
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def location_of(source)
|
|
80
|
+
return "" if source.nil? || source.empty?
|
|
81
|
+
|
|
82
|
+
match = LOCATION.match(source)
|
|
83
|
+
match ? "#{match[1]}:#{match[2]}" : source
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def timestamp
|
|
87
|
+
@clock.call.getutc.iso8601
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "set"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Telemetry
|
|
8
|
+
module Browser
|
|
9
|
+
# The "ref.operation" names the client knows, written by the build of the spa. Read once at boot; a missing file
|
|
10
|
+
# means none, so every call the browser reports is dropped.
|
|
11
|
+
module Operations
|
|
12
|
+
MESSAGE = "telemetry: dist/.boto/operations.json must be a JSON Array of \"ref.operation\" Strings"
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def read(path)
|
|
17
|
+
return Set.new unless path && File.file?(path)
|
|
18
|
+
|
|
19
|
+
list = JSON.parse(File.read(path))
|
|
20
|
+
raise ArgumentError, MESSAGE unless list.is_a?(Array) && list.all?(String)
|
|
21
|
+
|
|
22
|
+
Set.new(list)
|
|
23
|
+
rescue JSON::ParserError
|
|
24
|
+
raise ArgumentError, MESSAGE
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Telemetry
|
|
5
|
+
module Browser
|
|
6
|
+
class Payload
|
|
7
|
+
# Reads one body. Holds the known operations and the :telemetry filter the texts go through.
|
|
8
|
+
class Reader
|
|
9
|
+
def initialize(operations:, filter:)
|
|
10
|
+
@operations = operations
|
|
11
|
+
@scrubber = Scrubber.new(filter: filter)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def read(body)
|
|
15
|
+
data = decode(body)
|
|
16
|
+
raise Invalid, "unsupported version" unless data["v"] == VERSION
|
|
17
|
+
|
|
18
|
+
Payload.new(route: route(data["route"]), browser: browser(data["browser"]), events: events(data["events"]))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def decode(body)
|
|
24
|
+
raise Invalid, "body must be a String" unless body.is_a?(String)
|
|
25
|
+
raise Invalid, "body larger than #{MAX_BYTES} bytes" if body.bytesize > MAX_BYTES
|
|
26
|
+
|
|
27
|
+
data = JSON.parse(body)
|
|
28
|
+
raise Invalid, "body must be a JSON object" unless data.is_a?(Hash)
|
|
29
|
+
|
|
30
|
+
data
|
|
31
|
+
rescue JSON::ParserError
|
|
32
|
+
raise Invalid, "body is not valid JSON"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Only the path is kept: the query and the fragment never leave the browser.
|
|
36
|
+
def route(value)
|
|
37
|
+
raise Invalid, "route must be a String" unless value.is_a?(String)
|
|
38
|
+
|
|
39
|
+
text(value.split(ROUTE_CUT, 2).first.to_s, MAX_ROUTE_LENGTH, "route")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def browser(value)
|
|
43
|
+
return OTHER_BROWSER unless value.is_a?(String)
|
|
44
|
+
|
|
45
|
+
match = BROWSER_PATTERN.match(value)
|
|
46
|
+
return "#{match[1]} #{match[2].to_i}" if match
|
|
47
|
+
|
|
48
|
+
from_user_agent(value)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def from_user_agent(agent)
|
|
52
|
+
USER_AGENT_PATTERNS.each do |family, pattern|
|
|
53
|
+
match = pattern.match(agent)
|
|
54
|
+
return "#{family} #{match[1].to_i}" if match
|
|
55
|
+
end
|
|
56
|
+
OTHER_BROWSER
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def events(value)
|
|
60
|
+
raise Invalid, "events must be an Array" unless value.is_a?(Array)
|
|
61
|
+
raise Invalid, "more than #{MAX_EVENTS} events" if value.size > MAX_EVENTS
|
|
62
|
+
|
|
63
|
+
value.filter_map { |item| event(item) }.freeze
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def event(data)
|
|
67
|
+
raise Invalid, "event must be an object" unless data.is_a?(Hash)
|
|
68
|
+
|
|
69
|
+
case data["t"]
|
|
70
|
+
when "vital" then vital_event(data)
|
|
71
|
+
when "call" then call_event(data)
|
|
72
|
+
when "error" then error_event(data)
|
|
73
|
+
when "view" then View.new
|
|
74
|
+
else raise Invalid, "unknown event type"
|
|
75
|
+
end
|
|
76
|
+
rescue Invalid
|
|
77
|
+
nil
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def vital_event(data)
|
|
81
|
+
raise Invalid, "unknown vital" unless VITAL_LIMITS.key?(data["name"])
|
|
82
|
+
|
|
83
|
+
Vital.new(name: data["name"], value: within(data["value"], 0..VITAL_LIMITS.fetch(data["name"]), "vital"))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def call_event(data)
|
|
87
|
+
ref = text(data["ref"], MAX_REF_LENGTH, "ref")
|
|
88
|
+
op = text(data["op"], MAX_OP_LENGTH, "op")
|
|
89
|
+
raise Invalid, "unknown operation" unless @operations.include?("#{ref}.#{op}")
|
|
90
|
+
|
|
91
|
+
Call.new(ref: ref, op: op, status: status(data["status"]),
|
|
92
|
+
ms: within(data["ms"], 0..MAX_MS, "ms"), trace: trace(data["trace"]))
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def error_event(data)
|
|
96
|
+
JsError.new(
|
|
97
|
+
kind: text(data["kind"], MAX_KIND_LENGTH, "kind"),
|
|
98
|
+
message: @scrubber.call(text(data["message"] || "", MAX_MESSAGE_LENGTH, "message", min: 0)),
|
|
99
|
+
source: text(data["source"] || "", MAX_SOURCE_LENGTH, "source", min: 0),
|
|
100
|
+
stack: stack(data["stack"]),
|
|
101
|
+
unhandled: data["unhandled"] == true
|
|
102
|
+
)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# A broken trace is removed, but the call it belongs to is kept.
|
|
106
|
+
def trace(data)
|
|
107
|
+
return nil unless data.is_a?(Hash)
|
|
108
|
+
|
|
109
|
+
Trace.new(id: hex(data["id"], TRACE_ID_PATTERN, "trace id"),
|
|
110
|
+
span: hex(data["span"], SPAN_ID_PATTERN, "trace span"),
|
|
111
|
+
start: whole(data["start"]))
|
|
112
|
+
rescue Invalid
|
|
113
|
+
nil
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Truncates first, so the scrubber only works on the lines that are kept.
|
|
117
|
+
def stack(value)
|
|
118
|
+
return nil if value.nil?
|
|
119
|
+
raise Invalid, "stack must be a String" unless value.is_a?(String)
|
|
120
|
+
|
|
121
|
+
@scrubber.call(value.lines.first(MAX_STACK_LINES).join)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def status(value)
|
|
125
|
+
raise Invalid, "status out of range" unless value.is_a?(Integer) && STATUS_RANGE.cover?(value)
|
|
126
|
+
|
|
127
|
+
value
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def within(value, range, name)
|
|
131
|
+
raise Invalid, "#{name} out of range" unless value.is_a?(Numeric) && range.cover?(value)
|
|
132
|
+
|
|
133
|
+
value
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def whole(value)
|
|
137
|
+
raise Invalid, "trace start must be an Integer >= 0" unless value.is_a?(Integer) && value >= 0
|
|
138
|
+
|
|
139
|
+
value
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def hex(value, pattern, name)
|
|
143
|
+
raise Invalid, "#{name} must be hexadecimal" unless value.is_a?(String) && value.match?(pattern)
|
|
144
|
+
|
|
145
|
+
value
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def text(value, max, name, min: 1)
|
|
149
|
+
valid = value.is_a?(String) && value.length.between?(min, max)
|
|
150
|
+
raise Invalid, "#{name} must be a String of #{min} to #{max} characters" unless valid
|
|
151
|
+
|
|
152
|
+
value
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Telemetry
|
|
7
|
+
module Browser
|
|
8
|
+
# The batch the browser posts to the RUM endpoint, validated and cleaned. Pure: no IO, no clock.
|
|
9
|
+
# Bad top-level data raises Invalid; a bad event is dropped alone, so one odd event keeps the batch.
|
|
10
|
+
class Payload < Data.define(:route, :browser, :events)
|
|
11
|
+
class Invalid < StandardError; end
|
|
12
|
+
|
|
13
|
+
Vital = Data.define(:name, :value)
|
|
14
|
+
Call = Data.define(:ref, :op, :status, :ms, :trace)
|
|
15
|
+
Trace = Data.define(:id, :span, :start)
|
|
16
|
+
JsError = Data.define(:kind, :message, :source, :stack, :unhandled)
|
|
17
|
+
View = Data.define
|
|
18
|
+
|
|
19
|
+
VERSION = 1
|
|
20
|
+
MAX_BYTES = 64 * 1024
|
|
21
|
+
MAX_EVENTS = 200
|
|
22
|
+
MAX_ROUTE_LENGTH = 512
|
|
23
|
+
MAX_REF_LENGTH = 128
|
|
24
|
+
MAX_OP_LENGTH = 128
|
|
25
|
+
MAX_KIND_LENGTH = 256
|
|
26
|
+
MAX_MESSAGE_LENGTH = 1000
|
|
27
|
+
MAX_SOURCE_LENGTH = 512
|
|
28
|
+
MAX_STACK_LINES = 50
|
|
29
|
+
MAX_MS = 60_000
|
|
30
|
+
MAX_CLS = 10
|
|
31
|
+
VITAL_LIMITS = { "LCP" => MAX_MS, "INP" => MAX_MS, "FCP" => MAX_MS, "TTFB" => MAX_MS, "CLS" => MAX_CLS }.freeze
|
|
32
|
+
STATUS_RANGE = (100..599)
|
|
33
|
+
TRACE_ID_PATTERN = /\A\h{32}\z/
|
|
34
|
+
SPAN_ID_PATTERN = /\A\h{16}\z/
|
|
35
|
+
ROUTE_CUT = /[?#]/
|
|
36
|
+
BROWSER_PATTERN = /\A(Chrome|Edge|Firefox|Safari|Opera|Samsung Internet) (\d{1,4})\b/
|
|
37
|
+
# The agent sends navigator.userAgent: only the family and the major version are kept. Order matters, since
|
|
38
|
+
# Edge, Opera and Samsung Internet also carry "Chrome/", and Chrome carries "Safari/".
|
|
39
|
+
USER_AGENT_PATTERNS = [
|
|
40
|
+
["Edge", %r{\bEdg(?:e|A|iOS)?/(\d{1,4})}],
|
|
41
|
+
["Opera", %r{\b(?:OPR|Opera)/(\d{1,4})}],
|
|
42
|
+
["Samsung Internet", %r{\bSamsungBrowser/(\d{1,4})}],
|
|
43
|
+
["Firefox", %r{\b(?:Firefox|FxiOS)/(\d{1,4})}],
|
|
44
|
+
["Chrome", %r{\b(?:Chrome|CriOS)/(\d{1,4})}],
|
|
45
|
+
["Safari", %r{\bVersion/(\d{1,4})[.\d]* (?:Mobile/\S+ )?Safari/}]
|
|
46
|
+
].freeze
|
|
47
|
+
OTHER_BROWSER = "Other"
|
|
48
|
+
|
|
49
|
+
def self.parse(body, operations:, filter:)
|
|
50
|
+
Reader.new(operations: operations, filter: filter).read(body)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Telemetry
|
|
5
|
+
module Browser
|
|
6
|
+
# In-process fixed-window limit for the RUM endpoint, used when the project has no :rate_limit. Same contract as
|
|
7
|
+
# the store of :rate_limit (check! raises TooManyRequests past the limit). Keeps counters for one window only and
|
|
8
|
+
# at most max_keys keys, evicting the oldest, so a flood of addresses cannot grow the memory.
|
|
9
|
+
class RateWindow
|
|
10
|
+
DEFAULT_LIMIT = 60
|
|
11
|
+
DEFAULT_PERIOD = 60
|
|
12
|
+
DEFAULT_MAX_KEYS = 10_000
|
|
13
|
+
MIN_RESET_SECONDS = 1
|
|
14
|
+
MONOTONIC = -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
15
|
+
|
|
16
|
+
def initialize(limit: DEFAULT_LIMIT, period: DEFAULT_PERIOD, max_keys: DEFAULT_MAX_KEYS, clock: MONOTONIC)
|
|
17
|
+
@limit = limit
|
|
18
|
+
@period = period
|
|
19
|
+
@max_keys = max_keys
|
|
20
|
+
@clock = clock
|
|
21
|
+
@window = nil
|
|
22
|
+
@counts = {}
|
|
23
|
+
@mutex = Thread::Mutex.new
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def check!(key)
|
|
27
|
+
count, reset_in = increment(key)
|
|
28
|
+
raise Boto::Error::TooManyRequests.new(reset_in) if count > @limit
|
|
29
|
+
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
# Returns [count, reset_in] for the key in the current window.
|
|
36
|
+
def increment(key)
|
|
37
|
+
now = @clock.call
|
|
38
|
+
window = (now / @period).floor
|
|
39
|
+
@mutex.synchronize do
|
|
40
|
+
@counts = {} unless window == @window
|
|
41
|
+
@window = window
|
|
42
|
+
[count_request(key), reset_in(now, window)]
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Hash keeps insertion order, so shift drops the oldest key in O(1).
|
|
47
|
+
def count_request(key)
|
|
48
|
+
if @counts.key?(key)
|
|
49
|
+
@counts[key] += 1
|
|
50
|
+
else
|
|
51
|
+
@counts.shift if @counts.size >= @max_keys
|
|
52
|
+
@counts[key] = 1
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def reset_in(now, window)
|
|
57
|
+
window_end = (window + 1) * @period
|
|
58
|
+
[(window_end - now).ceil, MIN_RESET_SECONDS].max
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/media_type"
|
|
4
|
+
require "rack/request"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Telemetry
|
|
8
|
+
module Browser
|
|
9
|
+
# The Rack app of POST <base>_boto/rum. Lets in only same-origin posts of the two content types the beacon uses,
|
|
10
|
+
# limits each client address, and hands the valid batches to the collector. Errors go out as the core's problems.
|
|
11
|
+
class Receiver
|
|
12
|
+
POST = "POST"
|
|
13
|
+
METHOD_KEY = "REQUEST_METHOD"
|
|
14
|
+
CONTENT_TYPE_KEY = "CONTENT_TYPE"
|
|
15
|
+
CONTENT_LENGTH_KEY = "CONTENT_LENGTH"
|
|
16
|
+
RACK_INPUT_KEY = "rack.input"
|
|
17
|
+
ORIGIN_KEY = "HTTP_ORIGIN"
|
|
18
|
+
FETCH_SITE_KEY = "HTTP_SEC_FETCH_SITE"
|
|
19
|
+
SAME_ORIGIN = "same-origin"
|
|
20
|
+
CONTENT_TYPES = %w[text/plain application/json].freeze
|
|
21
|
+
NO_CONTENT = 204
|
|
22
|
+
|
|
23
|
+
def initialize(collector:, operations:, filter:, limiter:, client_ip:)
|
|
24
|
+
@collector = collector
|
|
25
|
+
@operations = operations
|
|
26
|
+
@filter = filter
|
|
27
|
+
@limiter = limiter
|
|
28
|
+
@client_ip = client_ip
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def call(env)
|
|
32
|
+
check_request!(env)
|
|
33
|
+
@collector.record(Payload.parse(read_body(env), operations: @operations, filter: @filter))
|
|
34
|
+
[NO_CONTENT, {}, []]
|
|
35
|
+
rescue Boto::Error => error
|
|
36
|
+
error.to_response
|
|
37
|
+
rescue Payload::Invalid => invalid
|
|
38
|
+
Boto::Error::BadRequest.new(invalid.message).to_response
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def check_request!(env)
|
|
44
|
+
raise Boto::Error::MethodNotAllowed.new([POST]) unless env[METHOD_KEY] == POST
|
|
45
|
+
raise Boto::Error::Forbidden.new unless same_origin?(env)
|
|
46
|
+
raise Boto::Error::UnsupportedMediaType.new unless CONTENT_TYPES.include?(Rack::MediaType.type(env[CONTENT_TYPE_KEY]))
|
|
47
|
+
|
|
48
|
+
admit!(env)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Same origin when the browser says so, or when Origin is the scheme and host of this request.
|
|
52
|
+
def same_origin?(env)
|
|
53
|
+
env[FETCH_SITE_KEY] == SAME_ORIGIN || env[ORIGIN_KEY] == Rack::Request.new(env).base_url
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# A refused request still counts as dropped, so the monitor shows how much the limit turned away.
|
|
57
|
+
def admit!(env)
|
|
58
|
+
@limiter.check!(@client_ip.call(env))
|
|
59
|
+
rescue Boto::Error::TooManyRequests
|
|
60
|
+
@collector.count_dropped
|
|
61
|
+
raise
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def read_body(env)
|
|
65
|
+
raise Boto::Error::PayloadTooLarge.new if env[CONTENT_LENGTH_KEY].to_i > Payload::MAX_BYTES
|
|
66
|
+
|
|
67
|
+
input = env[RACK_INPUT_KEY]
|
|
68
|
+
body = (input&.read(Payload::MAX_BYTES + 1) || String.new).force_encoding(Encoding::UTF_8)
|
|
69
|
+
raise Boto::Error::PayloadTooLarge.new if body.bytesize > Payload::MAX_BYTES
|
|
70
|
+
|
|
71
|
+
body
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Telemetry
|
|
5
|
+
module Browser
|
|
6
|
+
# Masks what must not leave the browser in an error message or stack: sensitive query pairs,
|
|
7
|
+
# e-mails and long digit runs. Works line by line, so a masked value never swallows the next line.
|
|
8
|
+
class Scrubber
|
|
9
|
+
# Bounded repetitions keep the match linear on hostile input.
|
|
10
|
+
EMAIL_PATTERN = /[\w.%+-]{1,64}@[A-Za-z0-9-]{1,63}(?:\.[A-Za-z0-9-]{1,63})+/
|
|
11
|
+
# 8 or more digits, optionally split by a space, dot or hyphen (cards, CPF). Short runs like 1:48211 stay.
|
|
12
|
+
LONG_NUMBER_PATTERN = /\d(?:[ .-]?\d){7,}/
|
|
13
|
+
EMAIL_MASK = "[email]"
|
|
14
|
+
NUMBER_MASK = "[number]"
|
|
15
|
+
LINE_SEPARATOR = "\n"
|
|
16
|
+
|
|
17
|
+
def initialize(filter:)
|
|
18
|
+
@filter = filter
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def call(text)
|
|
22
|
+
text.split(LINE_SEPARATOR, -1).map { |line| scrub_line(line) }.join(LINE_SEPARATOR)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def scrub_line(line)
|
|
28
|
+
masked = @filter.query(line).gsub(EMAIL_PATTERN, EMAIL_MASK)
|
|
29
|
+
masked.gsub(LONG_NUMBER_PATTERN, NUMBER_MASK)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -20,20 +20,22 @@ module Boto
|
|
|
20
20
|
|
|
21
21
|
def initialize(routes:)
|
|
22
22
|
@requests = {}
|
|
23
|
+
@route_operations = {}
|
|
23
24
|
@request_series = []
|
|
24
25
|
@operations = {}
|
|
25
26
|
@operation_series = []
|
|
26
27
|
@in_flight = 0
|
|
27
28
|
@drain_lock = Thread::Mutex.new
|
|
28
29
|
(KNOWN_METHODS + [OTHER_METHOD]).each { |verb| @requests[verb] = { nil => {} } }
|
|
29
|
-
routes.each { |verb, route| add_route(verb, route) }
|
|
30
|
+
routes.each { |verb, route, operation| add_route(verb, route, operation) }
|
|
30
31
|
end
|
|
31
32
|
|
|
32
33
|
def record_request(verb, route, status, seconds)
|
|
33
34
|
verb = known_verb(verb)
|
|
34
35
|
route = nil unless @requests[verb].key?(route)
|
|
35
36
|
statuses = @requests[verb][route]
|
|
36
|
-
series = statuses[status] ||= new_series(@request_series,
|
|
37
|
+
series = statuses[status] ||= new_series(@request_series,
|
|
38
|
+
{ verb:, route:, status:, operation: @route_operations[[verb, route]] })
|
|
37
39
|
series.histogram.record(seconds)
|
|
38
40
|
end
|
|
39
41
|
|
|
@@ -63,10 +65,14 @@ module Boto
|
|
|
63
65
|
|
|
64
66
|
private
|
|
65
67
|
|
|
66
|
-
# The core answers HEAD from the GET endpoint, so each GET route also gets a HEAD series.
|
|
67
|
-
def add_route(verb, route)
|
|
68
|
+
# The core answers HEAD from the GET endpoint, so each GET route also gets a HEAD series, with the same operation.
|
|
69
|
+
def add_route(verb, route, operation)
|
|
68
70
|
@requests[known_verb(verb)][route] ||= {}
|
|
69
|
-
@
|
|
71
|
+
@route_operations[[known_verb(verb), route]] = operation
|
|
72
|
+
return unless verb == GET
|
|
73
|
+
|
|
74
|
+
@requests[HEAD][route] ||= {}
|
|
75
|
+
@route_operations[[HEAD, route]] = operation
|
|
70
76
|
end
|
|
71
77
|
|
|
72
78
|
def known_verb(verb)
|
|
@@ -8,10 +8,12 @@ module Boto
|
|
|
8
8
|
|
|
9
9
|
attr_reader :count, :sum, :buckets
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
# boundaries: the upper limits of the buckets, ascending; one more bucket holds everything above the last.
|
|
12
|
+
def initialize(boundaries = BOUNDARIES)
|
|
13
|
+
@boundaries = boundaries
|
|
12
14
|
@count = 0
|
|
13
15
|
@sum = 0.0
|
|
14
|
-
@buckets = Array.new(
|
|
16
|
+
@buckets = Array.new(boundaries.size + 1, 0)
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def record(seconds)
|
|
@@ -49,7 +51,7 @@ module Boto
|
|
|
49
51
|
|
|
50
52
|
# The last bucket takes everything above the highest boundary.
|
|
51
53
|
def bucket_index(seconds)
|
|
52
|
-
|
|
54
|
+
@boundaries.bsearch_index { |limit| limit >= seconds } || @boundaries.size
|
|
53
55
|
end
|
|
54
56
|
end
|
|
55
57
|
end
|
|
@@ -6,12 +6,15 @@ module Boto
|
|
|
6
6
|
# test sink. Like Sender, it runs one background thread (started after the fork) and stop sends the last batch.
|
|
7
7
|
class LocalSender
|
|
8
8
|
STOP_TIMEOUT = 5
|
|
9
|
+
FAILED_MESSAGE = "boto telemetry: a batch was not delivered (%<error>s: %<message>s)"
|
|
9
10
|
|
|
10
11
|
attr_reader :thread
|
|
11
12
|
|
|
12
|
-
def initialize(interval:, transport:)
|
|
13
|
+
def initialize(interval:, transport:, err: $stderr)
|
|
13
14
|
@interval = interval
|
|
14
15
|
@transport = transport
|
|
16
|
+
@err = err
|
|
17
|
+
@last_failure = nil
|
|
15
18
|
@wakeup = Thread::Queue.new
|
|
16
19
|
@lock = Thread::Mutex.new
|
|
17
20
|
@stopping = false
|
|
@@ -39,13 +42,22 @@ module Boto
|
|
|
39
42
|
batch = @build_batch.call
|
|
40
43
|
@transport.call(batch) if batch
|
|
41
44
|
end
|
|
42
|
-
rescue StandardError
|
|
45
|
+
rescue StandardError => error
|
|
43
46
|
# The loop must keep running; the next batch carries the data, so a failed one is not retried.
|
|
44
|
-
|
|
47
|
+
report(error)
|
|
45
48
|
end
|
|
46
49
|
|
|
47
50
|
private
|
|
48
51
|
|
|
52
|
+
# A batch the receiver refuses (an invalid one, say) would otherwise vanish; each distinct failure is told once.
|
|
53
|
+
def report(error)
|
|
54
|
+
failure = [error.class, error.message]
|
|
55
|
+
return if failure == @last_failure
|
|
56
|
+
|
|
57
|
+
@last_failure = failure
|
|
58
|
+
@err.puts format(FAILED_MESSAGE, error: error.class, message: error.message)
|
|
59
|
+
end
|
|
60
|
+
|
|
49
61
|
def run
|
|
50
62
|
until @stopping
|
|
51
63
|
@wakeup.pop(timeout: @interval)
|
|
@@ -5,6 +5,11 @@ module Boto
|
|
|
5
5
|
# Checks the :telemetry plugin options when the plugin is built, so a wrong type fails at boot.
|
|
6
6
|
module Options
|
|
7
7
|
PROPAGATIONS = [HttpSpans::ALL, HttpSpans::NONE].freeze
|
|
8
|
+
BROWSER_KEYS = %i[sample trace_sample routes].freeze
|
|
9
|
+
DEVELOPMENT_SAMPLE = 1.0
|
|
10
|
+
PRODUCTION_SAMPLE = 0.1
|
|
11
|
+
DEFAULT_TRACE_SAMPLE = 0.01
|
|
12
|
+
DEFAULT_ROUTES = 200
|
|
8
13
|
|
|
9
14
|
module_function
|
|
10
15
|
|
|
@@ -31,6 +36,34 @@ module Boto
|
|
|
31
36
|
|
|
32
37
|
raise ArgumentError, "telemetry: #{name} must be :all, :none or an Array of hosts, got #{value.inspect}"
|
|
33
38
|
end
|
|
39
|
+
|
|
40
|
+
# nil (or false) turns the browser side off. The sample default depends on the environment: every page in
|
|
41
|
+
# development, a tenth elsewhere.
|
|
42
|
+
def browser_settings(value, development:)
|
|
43
|
+
return nil if value.nil? || value == false
|
|
44
|
+
raise ArgumentError, "telemetry: browser must be a Hash, got #{value.inspect}" unless value.is_a?(Hash)
|
|
45
|
+
|
|
46
|
+
unknown = value.keys - BROWSER_KEYS
|
|
47
|
+
raise ArgumentError, "telemetry: unknown browser option #{unknown.first.inspect}" unless unknown.empty?
|
|
48
|
+
|
|
49
|
+
{
|
|
50
|
+
sample: fraction(:sample, value.fetch(:sample, development ? DEVELOPMENT_SAMPLE : PRODUCTION_SAMPLE)),
|
|
51
|
+
trace_sample: fraction(:trace_sample, value.fetch(:trace_sample, DEFAULT_TRACE_SAMPLE)),
|
|
52
|
+
routes: positive_integer(:routes, value.fetch(:routes, DEFAULT_ROUTES))
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def fraction(name, value)
|
|
57
|
+
return value.to_f if value.is_a?(Numeric) && value.between?(0, 1)
|
|
58
|
+
|
|
59
|
+
raise ArgumentError, "telemetry: browser #{name} must be a number between 0 and 1, got #{value.inspect}"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def positive_integer(name, value)
|
|
63
|
+
return value if value.is_a?(Integer) && value.positive?
|
|
64
|
+
|
|
65
|
+
raise ArgumentError, "telemetry: browser #{name} must be a positive Integer, got #{value.inspect}"
|
|
66
|
+
end
|
|
34
67
|
end
|
|
35
68
|
end
|
|
36
69
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: boto-rb-telemetry
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.30.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Evo Tech Builder
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - '='
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.
|
|
18
|
+
version: 0.30.0
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - '='
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 0.
|
|
25
|
+
version: 0.30.0
|
|
26
26
|
executables: []
|
|
27
27
|
extensions: []
|
|
28
28
|
extra_rdoc_files: []
|
|
@@ -32,6 +32,15 @@ files:
|
|
|
32
32
|
- lib/boto/telemetry.rb
|
|
33
33
|
- lib/boto/telemetry/agent.rb
|
|
34
34
|
- lib/boto/telemetry/batch.rb
|
|
35
|
+
- lib/boto/telemetry/browser/call_trace.rb
|
|
36
|
+
- lib/boto/telemetry/browser/collector.rb
|
|
37
|
+
- lib/boto/telemetry/browser/error_groups.rb
|
|
38
|
+
- lib/boto/telemetry/browser/operations.rb
|
|
39
|
+
- lib/boto/telemetry/browser/payload.rb
|
|
40
|
+
- lib/boto/telemetry/browser/payload/reader.rb
|
|
41
|
+
- lib/boto/telemetry/browser/rate_window.rb
|
|
42
|
+
- lib/boto/telemetry/browser/receiver.rb
|
|
43
|
+
- lib/boto/telemetry/browser/scrubber.rb
|
|
35
44
|
- lib/boto/telemetry/collector.rb
|
|
36
45
|
- lib/boto/telemetry/current.rb
|
|
37
46
|
- lib/boto/telemetry/database_pool.rb
|