rails_mind 0.3.2 → 0.4.1
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/CHANGELOG.md +30 -0
- data/CONTRIBUTING.md +6 -7
- data/README.md +3 -4
- data/docs/compatibility.md +32 -14
- data/docs/sdk.md +141 -40
- data/lib/generators/rails_mind/install/install_generator.rb +0 -12
- data/lib/generators/rails_mind/install/templates/initializer.rb.tt +8 -1
- data/lib/rails_mind/client.rb +3 -2
- data/lib/rails_mind/configuration.rb +8 -2
- data/lib/rails_mind/context.rb +8 -2
- data/lib/rails_mind/diagnostic.rb +6 -3
- data/lib/rails_mind/integrations/rails.rb +14 -1
- data/lib/rails_mind/page_view.rb +275 -0
- data/lib/rails_mind/redactor.rb +2 -1
- data/lib/rails_mind/transport.rb +14 -0
- data/lib/rails_mind/version.rb +1 -1
- data/lib/rails_mind/visitor.rb +134 -0
- metadata +5 -5
- data/javascript/rails_mind.js +0 -49
- data/lib/rails_mind/integrations/ahoy.rb +0 -68
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
require "active_support/notifications"
|
|
2
2
|
require "active_support/inflector"
|
|
3
3
|
require "digest"
|
|
4
|
+
require_relative "../visitor"
|
|
5
|
+
require_relative "../page_view"
|
|
4
6
|
|
|
5
7
|
module RailsMind
|
|
6
8
|
module Integrations
|
|
@@ -44,7 +46,10 @@ module RailsMind
|
|
|
44
46
|
Context.with_trace do
|
|
45
47
|
previous = Thread.current[:rails_mind_queries]
|
|
46
48
|
Thread.current[:rails_mind_queries] = { count: 0, fingerprints: Hash.new(0), shapes: {} }
|
|
47
|
-
|
|
49
|
+
Visitor.start(env)
|
|
50
|
+
response = @app.call(env)
|
|
51
|
+
Visitor.finish(env, response)
|
|
52
|
+
response
|
|
48
53
|
rescue StandardError => error
|
|
49
54
|
# Capture identity before this request boundary unwinds. Object
|
|
50
55
|
# dedup still leaves other error reporters unaffected.
|
|
@@ -201,6 +206,14 @@ module RailsMind
|
|
|
201
206
|
RailsMind.client.emit(:request, "#{payload[:controller]}##{payload[:action]}", duration_ms: event.duration,
|
|
202
207
|
status: status.to_s, properties: properties)
|
|
203
208
|
end
|
|
209
|
+
subscribe("start_processing.action_controller") do |event|
|
|
210
|
+
Visitor.seed_daily(event.payload[:request])
|
|
211
|
+
end
|
|
212
|
+
subscribe("process_action.action_controller") do |event|
|
|
213
|
+
client = RailsMind.client
|
|
214
|
+
config = client.config
|
|
215
|
+
PageView.record(event.payload, client) if client.collector && config.analytics && config.page_views
|
|
216
|
+
end
|
|
204
217
|
subscribe("sql.active_record") do |event|
|
|
205
218
|
queries = Thread.current[:rails_mind_queries]
|
|
206
219
|
next unless queries && !event.payload[:cached] && !%w[SCHEMA TRANSACTION].include?(event.payload[:name])
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
require "uri"
|
|
2
|
+
require_relative "visitor"
|
|
3
|
+
|
|
4
|
+
module RailsMind
|
|
5
|
+
# One HTML page a controller rendered for a person: a successful GET, not a
|
|
6
|
+
# Turbo Frame, a prefetch, an XHR, a health check or a known bot. Built from
|
|
7
|
+
# the process_action notification, so it shares the request event's trace
|
|
8
|
+
# and the identity the app set during the action.
|
|
9
|
+
module PageView
|
|
10
|
+
# The wire contract with the hosted app. Every key survives both redactors.
|
|
11
|
+
PROPERTIES = %w[route path host referrer utm_source utm_medium utm_campaign utm_term utm_content ref
|
|
12
|
+
user_agent country visitor_id_kind].freeze
|
|
13
|
+
TAGS = %w[utm_source utm_medium utm_campaign utm_term utm_content ref].freeze
|
|
14
|
+
ROUTE_LIMIT = 500
|
|
15
|
+
PATH_LIMIT = 1_000
|
|
16
|
+
HOST_LIMIT = 255
|
|
17
|
+
REFERRER_LIMIT = 1_000
|
|
18
|
+
TAG_LIMIT = 200
|
|
19
|
+
USER_AGENT_LIMIT = 500
|
|
20
|
+
COUNTRY_HEADERS = %w[HTTP_CF_IPCOUNTRY HTTP_CLOUDFRONT_VIEWER_COUNTRY].freeze
|
|
21
|
+
# Cloudflare's unknown country and Tor.
|
|
22
|
+
NO_COUNTRY = %w[XX T1].freeze
|
|
23
|
+
PREFETCH_HEADERS = %w[HTTP_SEC_PURPOSE HTTP_X_SEC_PURPOSE].freeze
|
|
24
|
+
NO_CONTENT = [ 204, 205 ].freeze
|
|
25
|
+
# Rails' own pages, Active Storage, and the stub pages turbo-rails serves a
|
|
26
|
+
# Hotwire Native app to recede, resume or refresh.
|
|
27
|
+
FRAMEWORK_CONTROLLER = /\A(?:Rails|ActiveStorage|Turbo::Native)::/
|
|
28
|
+
FILTERED = "[FILTERED]".freeze
|
|
29
|
+
# A masked value at least this long is also masked wherever it appears in
|
|
30
|
+
# the query string; a shorter one would mask unrelated values there.
|
|
31
|
+
QUERY_MASK_MIN = 8
|
|
32
|
+
# Crawlers, link previews, LLM fetchers, monitors and bare HTTP clients. A
|
|
33
|
+
# blank user agent is not counted either. "cloudfront" is what CloudFront
|
|
34
|
+
# sends when a distribution does not forward the caller's user agent.
|
|
35
|
+
BOT = Regexp.new([
|
|
36
|
+
"bot", "crawl", "spider", "slurp", "scrap", "fetch", "monitor", "preview",
|
|
37
|
+
"headless", "phantom", "selenium", "puppeteer", "playwright",
|
|
38
|
+
"curl", "wget", "python-requests", "httpclient", "okhttp", "go-http-client", "java/",
|
|
39
|
+
"facebookexternalhit", "twitterbot", "slackbot", "discordbot", "linkedinbot", "telegrambot", "whatsapp",
|
|
40
|
+
"googlebot", "bingbot", "duckduckbot", "yandex", "baidu", "sogou", "seznam", "petalbot", "applebot",
|
|
41
|
+
"ahrefs", "semrush", "mj12", "dotbot", "sistrix", "barkrowler", "serpstat", "dataforseo", "blexbot", "exabot",
|
|
42
|
+
"gptbot", "chatgpt-user", "oai-searchbot", "claudebot", "claude-web", "anthropic", "cohere-ai",
|
|
43
|
+
"perplexitybot", "amazonbot", "bytespider", "google-extended", "meta-externalagent",
|
|
44
|
+
"cloudfront"
|
|
45
|
+
].map { |word| Regexp.escape(word) }.join("|"), Regexp::IGNORECASE)
|
|
46
|
+
|
|
47
|
+
class << self
|
|
48
|
+
def record(payload, client = RailsMind.client)
|
|
49
|
+
request, response = payload[:request], payload[:response]
|
|
50
|
+
return false unless request && response
|
|
51
|
+
config = client.config
|
|
52
|
+
return false unless counted?(payload, request, response, config)
|
|
53
|
+
properties = properties(request)
|
|
54
|
+
attributes = {}
|
|
55
|
+
settable = settable?(response)
|
|
56
|
+
if cookie_visitor?(request, settable, config)
|
|
57
|
+
properties["visitor_id_kind"] = "cookie"
|
|
58
|
+
request.set_header(Visitor::WRITE, { secure: request.ssl? }) if settable
|
|
59
|
+
else
|
|
60
|
+
properties["visitor_id_kind"] = "daily_hash"
|
|
61
|
+
attributes[:visitor_id] = Visitor.daily_id(request)
|
|
62
|
+
end
|
|
63
|
+
attributes.merge!(user_id: nil, account_id: nil) unless config.link_page_views_to_users
|
|
64
|
+
client.emit(:pageview, "#{payload[:controller]}##{payload[:action]}", properties: properties, **attributes)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def counted?(payload, request, response, config)
|
|
70
|
+
return false unless (payload[:method] || request.request_method) == "GET"
|
|
71
|
+
return false unless (200..299).cover?(payload[:status].to_i)
|
|
72
|
+
return false unless html?(payload, response)
|
|
73
|
+
# A browser asks for HTML when it loads a page; scripts, pollers and bare
|
|
74
|
+
# HTTP clients that take whatever comes back are not people reading it.
|
|
75
|
+
return false unless request.get_header("HTTP_ACCEPT").to_s.include?("text/html")
|
|
76
|
+
return false unless request.get_header("HTTP_TURBO_FRAME").to_s.empty?
|
|
77
|
+
return false if PREFETCH_HEADERS.any? { |header| request.get_header(header).to_s.downcase.include?("prefetch") }
|
|
78
|
+
return false if request.xhr?
|
|
79
|
+
return false if payload[:controller].to_s.match?(FRAMEWORK_CONTROLLER)
|
|
80
|
+
user_agent = request.user_agent.to_s
|
|
81
|
+
return false if user_agent.strip.empty? || user_agent.match?(BOT)
|
|
82
|
+
!ignored?(config, request)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Rails types a streamed template only when the response commits, as
|
|
86
|
+
# text/html like any response still untyped. An HTML request answered
|
|
87
|
+
# with content and no type yet is such a page.
|
|
88
|
+
def html?(payload, response)
|
|
89
|
+
type = response.media_type
|
|
90
|
+
return type == "text/html" if type
|
|
91
|
+
payload[:format] == :html && !NO_CONTENT.include?(payload[:status].to_i)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The cookie ID the request boundary put in context, unless the response
|
|
95
|
+
# cannot take the cookie and the visitor brought none: a fresh ID there
|
|
96
|
+
# could never be set, so that page view counts by the daily hash.
|
|
97
|
+
def cookie_visitor?(request, settable, config)
|
|
98
|
+
return false unless config.visitor_cookie
|
|
99
|
+
return false if !settable && !request.get_header(Visitor::ENV_COOKIE)
|
|
100
|
+
!Context.current[:visitor_id].to_s.empty?
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# A hook that raises has not said the page view counts.
|
|
104
|
+
def ignored?(config, request)
|
|
105
|
+
hook = config.ignore_page_view
|
|
106
|
+
hook ? hook.call(request) : false
|
|
107
|
+
rescue StandardError
|
|
108
|
+
true
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Not a response a shared cache may keep, nor one already on its way:
|
|
112
|
+
# ActionController::Live sends the headers at the first write or render,
|
|
113
|
+
# before the action ends.
|
|
114
|
+
def settable?(response)
|
|
115
|
+
return false if response.respond_to?(:committed?) && response.committed?
|
|
116
|
+
!public?(response)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def public?(response)
|
|
120
|
+
control = response.cache_control if response.respond_to?(:cache_control)
|
|
121
|
+
return true if control.is_a?(Hash) && control[:public]
|
|
122
|
+
response.get_header("Cache-Control").to_s.match?(Visitor::PUBLIC)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def properties(request)
|
|
126
|
+
values = {}
|
|
127
|
+
route = route(request)
|
|
128
|
+
values["route"] = route if route
|
|
129
|
+
values["path"] = path(request)[0, PATH_LIMIT]
|
|
130
|
+
values["host"] = request.host.to_s.downcase[0, HOST_LIMIT]
|
|
131
|
+
referrer = referrer(request.referer, request.host)
|
|
132
|
+
values["referrer"] = referrer if referrer
|
|
133
|
+
values.merge!(tags(request, secret_values(request)))
|
|
134
|
+
values["user_agent"] = request.user_agent.to_s[0, USER_AGENT_LIMIT]
|
|
135
|
+
country = country(request)
|
|
136
|
+
values["country"] = country if country
|
|
137
|
+
values
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Inside a mounted engine Rails gives the engine's own pattern, so the
|
|
141
|
+
# mount path (the request's script name) goes in front, as in the path.
|
|
142
|
+
def route(request)
|
|
143
|
+
return unless request.respond_to?(:route_uri_pattern)
|
|
144
|
+
pattern = request.route_uri_pattern.to_s.delete_suffix("(.:format)")
|
|
145
|
+
return if pattern.empty?
|
|
146
|
+
script = request.script_name.to_s
|
|
147
|
+
secrets = filtered_path_values(request)
|
|
148
|
+
script = mask(script, secrets) unless script.empty? || secrets.empty?
|
|
149
|
+
pattern = "#{script}#{pattern}".chomp("/")
|
|
150
|
+
(pattern.empty? ? "/" : pattern)[0, ROUTE_LIMIT]
|
|
151
|
+
rescue StandardError
|
|
152
|
+
nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The query string goes through the app's filter_parameters, and so do
|
|
156
|
+
# the route's own segments: a token in /passwords/:token/edit is masked
|
|
157
|
+
# like one in ?token=.
|
|
158
|
+
# If masking fails, the path is not sent at all: never the unmasked one.
|
|
159
|
+
def path(request)
|
|
160
|
+
path = request.filtered_path.to_s
|
|
161
|
+
secrets = filtered_path_values(request)
|
|
162
|
+
secrets.empty? ? path : mask(path, secrets)
|
|
163
|
+
rescue StandardError
|
|
164
|
+
FILTERED
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Each path parameter the app's filter masks, as written and as escaped,
|
|
168
|
+
# compared as bytes so a path in any encoding can be searched. Keys go to
|
|
169
|
+
# the filter as strings, as Rails' own filtered_parameters gives them.
|
|
170
|
+
def filtered_path_values(request)
|
|
171
|
+
params = request.path_parameters.except(:controller, :action, :format).transform_keys(&:to_s)
|
|
172
|
+
return [] if params.empty?
|
|
173
|
+
filtered = request.parameter_filter.filter(params)
|
|
174
|
+
params.each_with_object([]) do |(key, value), secrets|
|
|
175
|
+
next unless value.is_a?(String) && !value.empty? && filtered[key] != value
|
|
176
|
+
secrets.push(value, Rack::Utils.escape_path(value), Rack::Utils.escape(value).gsub("+", "%20"))
|
|
177
|
+
end.map(&:b).uniq
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Every route segment holding a secret goes whole (with any .format or
|
|
181
|
+
# text around it), a secret spanning segments (a glob route) goes, and a
|
|
182
|
+
# long secret repeated in the query goes too. Masking too much costs a
|
|
183
|
+
# little detail; masking too little sends a credential.
|
|
184
|
+
def mask(text, secrets)
|
|
185
|
+
return text if text.empty?
|
|
186
|
+
route_part, query = text.b.split("?", 2)
|
|
187
|
+
filtered = FILTERED.b
|
|
188
|
+
globs = secrets.select { |secret| secret.include?("/".b) }
|
|
189
|
+
globs.each { |secret| route_part = route_part.gsub(secret, filtered) }
|
|
190
|
+
pieces = (secrets + globs.flat_map { |secret| secret.split("/".b) }).reject(&:empty?).uniq
|
|
191
|
+
masked = route_part.split("/", -1).map { |segment| secret_in?(segment, pieces) ? filtered : segment }.join("/")
|
|
192
|
+
masked = "#{masked}?#{mask_query(query, secrets)}".b if query
|
|
193
|
+
masked.force_encoding(Encoding::UTF_8).scrub
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# A long secret goes wherever it appears; a query value that is, or holds
|
|
197
|
+
# as a path piece, any secret goes whole (a back link to the masked page).
|
|
198
|
+
def mask_query(query, secrets)
|
|
199
|
+
filtered = FILTERED.b
|
|
200
|
+
secrets.each { |secret| query = query.gsub(secret, filtered) if secret.bytesize >= QUERY_MASK_MIN }
|
|
201
|
+
query.split("&".b, -1).map do |pair|
|
|
202
|
+
key, value = pair.split("=".b, 2)
|
|
203
|
+
next pair unless value
|
|
204
|
+
decoded = begin
|
|
205
|
+
Rack::Utils.unescape(value).b
|
|
206
|
+
rescue StandardError
|
|
207
|
+
value
|
|
208
|
+
end
|
|
209
|
+
pieces = decoded.split("/".b)
|
|
210
|
+
secrets.any? { |secret| decoded == secret || pieces.include?(secret) } ? "#{key}=#{filtered}".b : pair
|
|
211
|
+
end.join("&".b)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# The route's masked values, for the path and the campaign tags.
|
|
215
|
+
def secret_values(request)
|
|
216
|
+
filtered_path_values(request)
|
|
217
|
+
rescue StandardError
|
|
218
|
+
[]
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def secret_in?(segment, secrets)
|
|
222
|
+
decoded = begin
|
|
223
|
+
Rack::Utils.unescape_path(segment).b
|
|
224
|
+
rescue StandardError
|
|
225
|
+
segment
|
|
226
|
+
end
|
|
227
|
+
secrets.any? { |secret| segment.include?(secret) || decoded.include?(secret) }
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Another site's scheme, host and path; its query and fragment stay home.
|
|
231
|
+
# A link inside the app sends only its origin, since its path can hold
|
|
232
|
+
# what the app masks in its own paths (a reset token, for one).
|
|
233
|
+
def referrer(value, host = nil)
|
|
234
|
+
return unless value.is_a?(String) && !value.strip.empty?
|
|
235
|
+
uri = URI.parse(value.strip)
|
|
236
|
+
return unless uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
|
|
237
|
+
origin = "#{uri.scheme.downcase}://#{uri.host.downcase}"
|
|
238
|
+
return origin if same_site?(uri.host, host)
|
|
239
|
+
"#{origin}#{uri.path}"[0, REFERRER_LIMIT]
|
|
240
|
+
rescue URI::Error
|
|
241
|
+
nil
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def same_site?(referrer_host, host)
|
|
245
|
+
return false if host.to_s.empty?
|
|
246
|
+
bare = ->(name) { name.to_s.downcase.delete_prefix("www.") }
|
|
247
|
+
bare.call(referrer_host) == bare.call(host)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# A tag the app's filter_parameters would mask in the query is not sent,
|
|
251
|
+
# nor one holding a value it masks in the route.
|
|
252
|
+
def tags(request, secrets = [])
|
|
253
|
+
query = request.GET
|
|
254
|
+
raw = TAGS.each_with_object({}) { |key, values| values[key] = query[key] if query[key].is_a?(String) }
|
|
255
|
+
filtered = request.parameter_filter.filter(raw)
|
|
256
|
+
raw.each_with_object({}) do |(key, value), tags|
|
|
257
|
+
next unless filtered[key] == value
|
|
258
|
+
next if secrets.any? { |secret| value.b.include?(secret) }
|
|
259
|
+
value = value.strip
|
|
260
|
+
tags[key] = value[0, TAG_LIMIT] unless value.empty?
|
|
261
|
+
end
|
|
262
|
+
rescue StandardError
|
|
263
|
+
{}
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def country(request)
|
|
267
|
+
COUNTRY_HEADERS.each do |header|
|
|
268
|
+
code = request.get_header(header).to_s.strip.upcase
|
|
269
|
+
return code if code.match?(/\A[A-Z]{2}\z/) && !NO_COUNTRY.include?(code)
|
|
270
|
+
end
|
|
271
|
+
nil
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
data/lib/rails_mind/redactor.rb
CHANGED
|
@@ -61,7 +61,8 @@ module RailsMind
|
|
|
61
61
|
# Userinfo runs to the last @ before the path, so an unencoded @ in a
|
|
62
62
|
# password cannot leave its tail behind.
|
|
63
63
|
USERINFO = %r{\b([a-z][a-z0-9+.\-]{0,31}://)[^\s/]*@}i
|
|
64
|
-
|
|
64
|
+
# Plain or percent-encoded, as a GET form puts an address in a query string.
|
|
65
|
+
EMAIL = /\b[A-Z0-9._%+\-]{1,128}(?:@|%40)[A-Z0-9.\-]{1,253}\.[A-Z]{2,}\b/i
|
|
65
66
|
AUTHORIZATION = /((?:proxy-)?authorization["']?\s*(?:=>|[=:])\s*(?:\[(?!FILTERED\]))?\s*["']?)(?:\[FILTERED\]|#{WHOLE_VALUE_SCHEME}|(?:#{SCHEME})?[^\s,;&"']+)/i
|
|
66
67
|
KEY_VALUE = /((?:password|secret|token|api[_-]?key|access[_-]?key|private[_-]?key|authorization)\s*(?:=>|[=:])\s*)(?:#{SCHEME})?[^\s,;&]+/i
|
|
67
68
|
PROVIDER_TOKEN = /\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_\w{22,}|(?:AKIA|ASIA)[A-Z0-9]{16}\b|[sr]k_(?:live|test)_[A-Za-z0-9]{10,}|xox[abposr]-[A-Za-z0-9-]{10,}|sk-(?:proj-|ant-)?[A-Za-z0-9_-]{20,}|eyJ[\w-]{5,512}\.[\w-]{5,}\.[\w-]*)/
|
data/lib/rails_mind/transport.rb
CHANGED
|
@@ -18,6 +18,8 @@ module RailsMind
|
|
|
18
18
|
def send_batch(json)
|
|
19
19
|
request = Net::HTTP::Post.new(@uri)
|
|
20
20
|
request.body = json
|
|
21
|
+
zone = time_zone
|
|
22
|
+
request["X-RailsMind-Time-Zone"] = zone if zone
|
|
21
23
|
perform(request)
|
|
22
24
|
end
|
|
23
25
|
|
|
@@ -29,6 +31,18 @@ module RailsMind
|
|
|
29
31
|
|
|
30
32
|
private
|
|
31
33
|
|
|
34
|
+
# The app's IANA zone (Rails' default "UTC" is Etc/UTC), so RailsMind counts
|
|
35
|
+
# days where the app's people live. Unknown until Rails has set it.
|
|
36
|
+
def time_zone
|
|
37
|
+
@time_zone ||= begin
|
|
38
|
+
zone = Time.zone_default if Time.respond_to?(:zone_default)
|
|
39
|
+
name = zone&.tzinfo&.name
|
|
40
|
+
name if name.is_a?(String) && !name.empty?
|
|
41
|
+
rescue StandardError
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
32
46
|
def perform(request)
|
|
33
47
|
Context.suppress do
|
|
34
48
|
request["Authorization"] = "Bearer #{@config.token}"
|
data/lib/rails_mind/version.rb
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
require "openssl"
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "time"
|
|
5
|
+
require "rack/utils"
|
|
6
|
+
|
|
7
|
+
module RailsMind
|
|
8
|
+
# Who is looking at a page before they sign in. By default a random ID kept in
|
|
9
|
+
# a first-party cookie; when no cookie can be set, a hash of the day, the IP
|
|
10
|
+
# and the user agent that changes every local day. The IP never leaves the app.
|
|
11
|
+
module Visitor
|
|
12
|
+
COOKIE = "_rails_mind_visitor".freeze
|
|
13
|
+
# The gem mints lowercase version 4 UUIDs. Anything else in the cookie came
|
|
14
|
+
# from somewhere else and is replaced, so hostile values never travel.
|
|
15
|
+
ID = /\A[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/
|
|
16
|
+
COOKIE_LIFETIME = 2 * 365 * 24 * 60 * 60
|
|
17
|
+
# Purpose-specific, derived from secret_key_base. Never the ingest key:
|
|
18
|
+
# RailsMind receives that with every batch and could brute-force the IPs.
|
|
19
|
+
KEY_SALT = "rails_mind visitor v1".freeze
|
|
20
|
+
|
|
21
|
+
# Rack env keys shared by the request boundary and the page-view subscriber.
|
|
22
|
+
ENV_ID = "rails_mind.visitor_id".freeze
|
|
23
|
+
ENV_COOKIE = "rails_mind.visitor_cookie".freeze
|
|
24
|
+
ENV_DAILY = "rails_mind.daily_visitor_id".freeze
|
|
25
|
+
WRITE = "rails_mind.write_visitor_cookie".freeze
|
|
26
|
+
SEED_DAILY = "rails_mind.seed_daily_visitor".freeze
|
|
27
|
+
PUBLIC = /(?:\A|[,\s])public(?:\z|[,\s=])/i
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
def mint = SecureRandom.uuid
|
|
31
|
+
|
|
32
|
+
# The first valid ID among the request's cookies of that name.
|
|
33
|
+
def from_cookie(header)
|
|
34
|
+
return unless header.is_a?(String)
|
|
35
|
+
header.split(";").each do |pair|
|
|
36
|
+
name, value = pair.split("=", 2)
|
|
37
|
+
next unless name.to_s.strip == COOKIE
|
|
38
|
+
value = value.to_s.strip
|
|
39
|
+
return value if value.match?(ID)
|
|
40
|
+
end
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def daily_hash(remote_ip, user_agent, date = today)
|
|
45
|
+
ip = remote_ip.to_s
|
|
46
|
+
secret = key
|
|
47
|
+
return if ip.empty? || secret.nil? || secret.empty?
|
|
48
|
+
OpenSSL::HMAC.hexdigest("SHA256", secret, "#{date.iso8601}\n#{ip}\n#{user_agent}")[0, 32]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# One request keeps one hash, even across local midnight.
|
|
52
|
+
def daily_id(request)
|
|
53
|
+
env = request.env
|
|
54
|
+
return env[ENV_DAILY] if env.key?(ENV_DAILY)
|
|
55
|
+
env[ENV_DAILY] = daily_hash(request.remote_ip, request.user_agent)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The local day in the app's time zone, the day RailsMind counts in.
|
|
59
|
+
def today
|
|
60
|
+
zone = Time.zone_default if Time.respond_to?(:zone_default)
|
|
61
|
+
(zone ? zone.now : Time.now.utc).to_date
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def key = @key ||= derive_key
|
|
65
|
+
|
|
66
|
+
def derive_key
|
|
67
|
+
return unless defined?(::Rails) && ::Rails.respond_to?(:application)
|
|
68
|
+
::Rails.application&.key_generator&.generate_key(KEY_SALT, 32)
|
|
69
|
+
rescue StandardError
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# At the request boundary: the visitor's cookie ID, or a fresh one for a
|
|
74
|
+
# browser navigation that may become a counted page view, goes into context
|
|
75
|
+
# so every event of the request carries it. Only such a request can set the
|
|
76
|
+
# cookie, so others without one (POSTs, JSON and API GETs, Turbo Frames,
|
|
77
|
+
# XHRs, prefetches) get no ID that would never be stored. Without the
|
|
78
|
+
# cookie, the daily hash waits for the controller, where the app's own
|
|
79
|
+
# remote IP is known.
|
|
80
|
+
def start(env)
|
|
81
|
+
client = RailsMind.client
|
|
82
|
+
config = client.config
|
|
83
|
+
return unless client.collector && config.analytics && config.page_views
|
|
84
|
+
unless config.visitor_cookie
|
|
85
|
+
env[SEED_DAILY] = true
|
|
86
|
+
return
|
|
87
|
+
end
|
|
88
|
+
incoming = from_cookie(env["HTTP_COOKIE"])
|
|
89
|
+
env[ENV_COOKIE] = incoming if incoming
|
|
90
|
+
context = Context.current
|
|
91
|
+
return unless context[:visitor_id].to_s.empty?
|
|
92
|
+
id = incoming || (mint if navigation?(env))
|
|
93
|
+
return unless id
|
|
94
|
+
env[ENV_ID] = id
|
|
95
|
+
context[:visitor_id] = id
|
|
96
|
+
rescue StandardError
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# A GET a browser makes for a whole HTML page, the only request whose
|
|
101
|
+
# response can count as a page view and set the cookie.
|
|
102
|
+
def navigation?(env)
|
|
103
|
+
return false unless env["REQUEST_METHOD"] == "GET" && env["HTTP_ACCEPT"].to_s.include?("text/html")
|
|
104
|
+
return false unless env["HTTP_TURBO_FRAME"].to_s.empty?
|
|
105
|
+
return false if env["HTTP_X_REQUESTED_WITH"].to_s.casecmp?("XMLHttpRequest")
|
|
106
|
+
%w[HTTP_SEC_PURPOSE HTTP_X_SEC_PURPOSE].none? { |header| env[header].to_s.downcase.include?("prefetch") }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# When a controller starts, after ActionDispatch::RemoteIp: the daily hash
|
|
110
|
+
# goes into context so the request's events carry the page view's visitor.
|
|
111
|
+
def seed_daily(request)
|
|
112
|
+
return unless request&.get_header(SEED_DAILY) && Context.current[:visitor_id].to_s.empty?
|
|
113
|
+
id = daily_id(request)
|
|
114
|
+
Context.current[:visitor_id] = id if id
|
|
115
|
+
rescue StandardError
|
|
116
|
+
nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# After the app: a counted page view marked the request, so the cookie is
|
|
120
|
+
# set or refreshed, unless the final response may be cached publicly.
|
|
121
|
+
def finish(env, response)
|
|
122
|
+
write = env[WRITE]
|
|
123
|
+
id = env[ENV_ID]
|
|
124
|
+
return unless write && id
|
|
125
|
+
headers = response[1]
|
|
126
|
+
return if (headers["cache-control"] || headers["Cache-Control"]).to_s.match?(PUBLIC)
|
|
127
|
+
Rack::Utils.set_cookie_header!(headers, COOKIE, value: id, path: "/", expires: Time.now + COOKIE_LIFETIME,
|
|
128
|
+
httponly: true, same_site: :lax, secure: write[:secure] == true)
|
|
129
|
+
rescue StandardError
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_mind
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- RailsMind
|
|
@@ -49,8 +49,8 @@ dependencies:
|
|
|
49
49
|
- - "<"
|
|
50
50
|
- !ruby/object:Gem::Version
|
|
51
51
|
version: '1'
|
|
52
|
-
description: The RailsMind client for Rails request, error, job, mailer,
|
|
53
|
-
telemetry, with optional
|
|
52
|
+
description: The RailsMind client for Rails page views and request, error, job, mailer,
|
|
53
|
+
and business-event telemetry, with optional Flipper and OpenTelemetry integrations.
|
|
54
54
|
executables: []
|
|
55
55
|
extensions: []
|
|
56
56
|
extra_rdoc_files: []
|
|
@@ -62,7 +62,6 @@ files:
|
|
|
62
62
|
- docs/agents.md
|
|
63
63
|
- docs/compatibility.md
|
|
64
64
|
- docs/sdk.md
|
|
65
|
-
- javascript/rails_mind.js
|
|
66
65
|
- lib/generators/rails_mind/agent/agent_generator.rb
|
|
67
66
|
- lib/generators/rails_mind/agent/templates/headers.rb.tt
|
|
68
67
|
- lib/generators/rails_mind/install/install_generator.rb
|
|
@@ -76,14 +75,15 @@ files:
|
|
|
76
75
|
- lib/rails_mind/diagnostic.rb
|
|
77
76
|
- lib/rails_mind/installation_check.rb
|
|
78
77
|
- lib/rails_mind/integrations/action_mailer.rb
|
|
79
|
-
- lib/rails_mind/integrations/ahoy.rb
|
|
80
78
|
- lib/rails_mind/integrations/open_telemetry.rb
|
|
81
79
|
- lib/rails_mind/integrations/rails.rb
|
|
80
|
+
- lib/rails_mind/page_view.rb
|
|
82
81
|
- lib/rails_mind/railtie.rb
|
|
83
82
|
- lib/rails_mind/redactor.rb
|
|
84
83
|
- lib/rails_mind/sampling.rb
|
|
85
84
|
- lib/rails_mind/transport.rb
|
|
86
85
|
- lib/rails_mind/version.rb
|
|
86
|
+
- lib/rails_mind/visitor.rb
|
|
87
87
|
- lib/tasks/rails_mind.rake
|
|
88
88
|
homepage: https://github.com/cmartyn/rails_mind
|
|
89
89
|
licenses:
|
data/javascript/rails_mind.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
// Uses the application's Ahoy endpoint/cookies. Never put the server token here.
|
|
2
|
-
// Do not run alongside another automatic pageview handler.
|
|
3
|
-
const installations = new WeakMap()
|
|
4
|
-
|
|
5
|
-
export function start({ ahoy, document: doc = document, turbo = true, pageviews = true, frames = false,
|
|
6
|
-
page = () => doc.querySelector('meta[name="rails-mind-page"]')?.content || "page" } = {}) {
|
|
7
|
-
if (!ahoy || typeof ahoy.track !== "function") throw new TypeError("Pass the existing Ahoy instance")
|
|
8
|
-
if (installations.has(doc)) return installations.get(doc)
|
|
9
|
-
const listeners = []
|
|
10
|
-
let visit = 0
|
|
11
|
-
let trackedVisit = -1
|
|
12
|
-
|
|
13
|
-
function on(name, listener) {
|
|
14
|
-
doc.addEventListener(name, listener)
|
|
15
|
-
listeners.push([name, listener])
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function trackPage() {
|
|
19
|
-
if (!pageviews || trackedVisit === visit || doc.documentElement.hasAttribute("data-turbo-preview")) return
|
|
20
|
-
trackedVisit = visit
|
|
21
|
-
ahoy.track("$pageview", { page: String(page()).slice(0, 160), navigation: turbo ? "turbo" : "document" })
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (turbo) {
|
|
25
|
-
on("turbo:visit", () => { visit += 1 })
|
|
26
|
-
on("turbo:load", trackPage)
|
|
27
|
-
} else {
|
|
28
|
-
on("DOMContentLoaded", trackPage)
|
|
29
|
-
}
|
|
30
|
-
// Supports loading the adapter after the initial page load, without duplicating
|
|
31
|
-
// a subsequent turbo:load for that same initial navigation.
|
|
32
|
-
if (doc.readyState === "complete") trackPage()
|
|
33
|
-
|
|
34
|
-
if (frames) on("turbo:frame-load", (event) => {
|
|
35
|
-
const label = event.target?.dataset?.analyticsFrame
|
|
36
|
-
if (label) ahoy.track("$frame_view", { frame: String(label).slice(0, 100), page: String(page()).slice(0, 160) })
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
const api = {
|
|
40
|
-
// The caller records a business outcome only after it actually succeeds.
|
|
41
|
-
track: (name, properties = {}) => ahoy.track(name, properties),
|
|
42
|
-
stop() {
|
|
43
|
-
listeners.forEach(([name, listener]) => doc.removeEventListener(name, listener))
|
|
44
|
-
installations.delete(doc)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
installations.set(doc, api)
|
|
48
|
-
return api
|
|
49
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
require "ahoy"
|
|
2
|
-
|
|
3
|
-
module RailsMind
|
|
4
|
-
module Integrations
|
|
5
|
-
module AhoyDelivery
|
|
6
|
-
def rails_mind_visit(data)
|
|
7
|
-
RailsMind.client.emit(:visit, "$visit", id: data[:visit_token], occurred_at: data[:started_at],
|
|
8
|
-
visitor_id: data[:visitor_token], user_id: data[:user_id]&.to_s,
|
|
9
|
-
properties: data.slice(:referring_domain, :utm_source, :utm_medium, :utm_campaign, :browser, :os, :device_type))
|
|
10
|
-
rescue StandardError
|
|
11
|
-
false
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def rails_mind_event(data)
|
|
15
|
-
RailsMind.client.emit(:event, data[:name], id: data[:event_id], occurred_at: data[:time],
|
|
16
|
-
visitor_id: ahoy&.visitor_token, user_id: data[:user_id]&.to_s,
|
|
17
|
-
properties: data.fetch(:properties, {}).merge(visit_id: data[:visit_token]))
|
|
18
|
-
rescue StandardError
|
|
19
|
-
false
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def rails_mind_authenticate(data)
|
|
23
|
-
RailsMind.client.emit(:event, "$identify", visitor_id: ahoy&.visitor_token,
|
|
24
|
-
user_id: data[:user_id]&.to_s, properties: { visit_id: data[:visit_token] })
|
|
25
|
-
rescue StandardError
|
|
26
|
-
false
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Explicit hosted-only option. Set `class Ahoy::Store < ...` in the app.
|
|
31
|
-
# This intentionally has no visit ActiveRecord model or geocoding persistence.
|
|
32
|
-
class AhoyStore < ::Ahoy::BaseStore
|
|
33
|
-
include AhoyDelivery
|
|
34
|
-
|
|
35
|
-
def track_visit(data) = rails_mind_visit(data)
|
|
36
|
-
def track_event(data) = rails_mind_event(data)
|
|
37
|
-
def authenticate(data) = rails_mind_authenticate(data)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Explicit coexistence option. Prepend once to the customer's existing Store.
|
|
41
|
-
# The existing store runs first; its arguments, exceptions and return value survive.
|
|
42
|
-
module AhoyMirror
|
|
43
|
-
include AhoyDelivery
|
|
44
|
-
|
|
45
|
-
def track_visit(data)
|
|
46
|
-
result = super
|
|
47
|
-
rails_mind_visit(data)
|
|
48
|
-
result
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def track_event(data)
|
|
52
|
-
result = super
|
|
53
|
-
rails_mind_event(data)
|
|
54
|
-
result
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def authenticate(data)
|
|
58
|
-
result = super
|
|
59
|
-
rails_mind_authenticate(data)
|
|
60
|
-
result
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def self.mirror_ahoy!(store = ::Ahoy::Store)
|
|
65
|
-
store.prepend(AhoyMirror) unless store.ancestors.include?(AhoyMirror)
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
end
|