posthog-rails 3.9.0 → 3.9.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/lib/posthog/rails/active_job.rb +7 -1
- data/lib/posthog/rails/capture_exceptions.rb +6 -1
- data/lib/posthog/rails/configuration.rb +16 -10
- data/lib/posthog/rails/error_subscriber.rb +10 -2
- data/lib/posthog/rails/parameter_filter.rb +4 -0
- data/lib/posthog/rails/railtie.rb +41 -2
- data/lib/posthog/rails/request_context.rb +5 -0
- data/lib/posthog/rails/request_metadata.rb +4 -0
- data/lib/posthog/rails/rescued_exception_interceptor.rb +7 -2
- data/lib/posthog/rails/tracing_headers.rb +7 -0
- data/lib/posthog/rails.rb +12 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 792f6e0ca6b259928ec0704d585e4887fbd4839837b7d49c08fc47b7eeeeeaaf
|
|
4
|
+
data.tar.gz: bc72fab471755cd32695bfdc919aa5cc99559c626504af061e89b80ad98dfd33
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f860543aeca28abfd50a110fa76fd2eed17a86e8c933cbbb0a0fdcdf4002d11bf400d2fb41d5680fa7587de749cad9514e71ad2a0855f1050827fb3da5d7a71b
|
|
7
|
+
data.tar.gz: afb7cb589db961516dea558424cdbe71ff9e20a004a0b944eba6f0aa54fdcb5a4be92acd0dfe6ce88abc84e35a63ca6567ecea4b5fd662018940b05b595aa27b
|
|
@@ -13,7 +13,12 @@ module PostHog
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
module ClassMethods
|
|
16
|
-
# DSL for defining how to extract distinct_id from job arguments
|
|
16
|
+
# DSL for defining how to extract distinct_id from job arguments.
|
|
17
|
+
#
|
|
18
|
+
# @param proc [Proc, nil] Callable that receives the job's perform arguments.
|
|
19
|
+
# @yield The block receives the job's perform arguments.
|
|
20
|
+
# @return [Proc, nil] The configured extractor.
|
|
21
|
+
#
|
|
17
22
|
# Example:
|
|
18
23
|
# class MyJob < ApplicationJob
|
|
19
24
|
# posthog_distinct_id ->(user, arg1, arg2) { user.id }
|
|
@@ -25,6 +30,7 @@ module PostHog
|
|
|
25
30
|
@posthog_distinct_id_proc = proc || block
|
|
26
31
|
end
|
|
27
32
|
|
|
33
|
+
# @return [Proc, nil] The configured distinct_id extractor.
|
|
28
34
|
def posthog_distinct_id_proc
|
|
29
35
|
@posthog_distinct_id_proc
|
|
30
36
|
end
|
|
@@ -4,14 +4,19 @@ require 'posthog/rails/parameter_filter'
|
|
|
4
4
|
|
|
5
5
|
module PostHog
|
|
6
6
|
module Rails
|
|
7
|
-
# Middleware that captures exceptions and sends them to PostHog
|
|
7
|
+
# Middleware that captures exceptions and sends them to PostHog.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
8
10
|
class CaptureExceptions
|
|
9
11
|
include ParameterFilter
|
|
10
12
|
|
|
13
|
+
# @param app [#call] Rack application.
|
|
11
14
|
def initialize(app)
|
|
12
15
|
@app = app
|
|
13
16
|
end
|
|
14
17
|
|
|
18
|
+
# @param env [Hash] Rack environment.
|
|
19
|
+
# @return [Array] Rack response.
|
|
15
20
|
def call(env)
|
|
16
21
|
# Signal that we're in a web request context
|
|
17
22
|
# ErrorSubscriber will skip capture for web requests to avoid duplicates
|
|
@@ -3,31 +3,33 @@
|
|
|
3
3
|
module PostHog
|
|
4
4
|
module Rails
|
|
5
5
|
class Configuration
|
|
6
|
-
# Whether to automatically capture exceptions from Rails
|
|
6
|
+
# @return [Boolean] Whether to automatically capture exceptions from Rails. Defaults to false.
|
|
7
7
|
attr_accessor :auto_capture_exceptions
|
|
8
8
|
|
|
9
|
-
# Whether to capture exceptions that Rails rescues (e.g., with rescue_from)
|
|
9
|
+
# @return [Boolean] Whether to capture exceptions that Rails rescues (e.g., with rescue_from). Defaults to false.
|
|
10
10
|
attr_accessor :report_rescued_exceptions
|
|
11
11
|
|
|
12
|
-
# Whether to automatically instrument ActiveJob
|
|
12
|
+
# @return [Boolean] Whether to automatically instrument ActiveJob. Defaults to false.
|
|
13
13
|
attr_accessor :auto_instrument_active_job
|
|
14
14
|
|
|
15
|
-
#
|
|
15
|
+
# @return [Array<String>] Exception class names to ignore in addition to the defaults.
|
|
16
16
|
attr_accessor :excluded_exceptions
|
|
17
17
|
|
|
18
|
-
# Whether to use PostHog tracing headers for request-scoped identity/session context
|
|
18
|
+
# @return [Boolean] Whether to use PostHog tracing headers for request-scoped identity/session context.
|
|
19
|
+
# Defaults to true.
|
|
19
20
|
attr_accessor :use_tracing_headers
|
|
20
21
|
|
|
21
|
-
# Whether to capture the current user context in exceptions
|
|
22
|
+
# @return [Boolean] Whether to capture the current user context in exceptions. Defaults to true.
|
|
22
23
|
attr_accessor :capture_user_context
|
|
23
24
|
|
|
24
|
-
# Method name to call on controller to get user
|
|
25
|
+
# @return [Symbol] Method name to call on controller to get the current user. Defaults to :current_user.
|
|
25
26
|
attr_accessor :current_user_method
|
|
26
27
|
|
|
27
|
-
# Method name to call on user object to get distinct_id
|
|
28
|
-
#
|
|
28
|
+
# @return [Symbol, nil] Method name to call on the user object to get distinct_id. When nil, tries:
|
|
29
|
+
# posthog_distinct_id, distinct_id, id, pk, uuid in order.
|
|
29
30
|
attr_accessor :user_id_method
|
|
30
31
|
|
|
32
|
+
# @return [PostHog::Rails::Configuration]
|
|
31
33
|
def initialize
|
|
32
34
|
@auto_capture_exceptions = false
|
|
33
35
|
@report_rescued_exceptions = false
|
|
@@ -39,7 +41,9 @@ module PostHog
|
|
|
39
41
|
@user_id_method = nil
|
|
40
42
|
end
|
|
41
43
|
|
|
42
|
-
# Default exceptions that Rails apps typically don't want to track
|
|
44
|
+
# Default exceptions that Rails apps typically don't want to track.
|
|
45
|
+
#
|
|
46
|
+
# @return [Array<String>]
|
|
43
47
|
def default_excluded_exceptions
|
|
44
48
|
[
|
|
45
49
|
'AbstractController::ActionNotFound',
|
|
@@ -58,6 +62,8 @@ module PostHog
|
|
|
58
62
|
]
|
|
59
63
|
end
|
|
60
64
|
|
|
65
|
+
# @param exception [Exception] The exception to check.
|
|
66
|
+
# @return [Boolean] Whether the exception should be captured.
|
|
61
67
|
def should_capture_exception?(exception)
|
|
62
68
|
exception_name = exception.class.name
|
|
63
69
|
!all_excluded_exceptions.include?(exception_name)
|
|
@@ -4,11 +4,19 @@ require 'posthog/rails/parameter_filter'
|
|
|
4
4
|
|
|
5
5
|
module PostHog
|
|
6
6
|
module Rails
|
|
7
|
-
# Rails 7.0+ error reporter integration
|
|
8
|
-
# This integrates with Rails.error.handle and Rails.error.record
|
|
7
|
+
# Rails 7.0+ error reporter integration.
|
|
8
|
+
# This integrates with Rails.error.handle and Rails.error.record.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
9
11
|
class ErrorSubscriber
|
|
10
12
|
include ParameterFilter
|
|
11
13
|
|
|
14
|
+
# @param error [Exception] Error reported by Rails.
|
|
15
|
+
# @param handled [Boolean]
|
|
16
|
+
# @param severity [Symbol, String]
|
|
17
|
+
# @param context [Hash]
|
|
18
|
+
# @param source [String, nil]
|
|
19
|
+
# @return [void]
|
|
12
20
|
def report(error, handled:, severity:, context:, source: nil)
|
|
13
21
|
return unless PostHog::Rails.config&.auto_capture_exceptions
|
|
14
22
|
return unless PostHog::Rails.config&.should_capture_exception?(error)
|
|
@@ -9,6 +9,8 @@ module PostHog
|
|
|
9
9
|
# It automatically detects the correct Rails parameter filtering API based on
|
|
10
10
|
# the Rails version.
|
|
11
11
|
#
|
|
12
|
+
# @api private
|
|
13
|
+
#
|
|
12
14
|
# @example Usage in a class
|
|
13
15
|
# class MyClass
|
|
14
16
|
# include PostHog::Rails::ParameterFilter
|
|
@@ -24,10 +26,12 @@ module PostHog
|
|
|
24
26
|
MAX_DEPTH = 10
|
|
25
27
|
|
|
26
28
|
if ::Rails.version.to_f >= 6.0
|
|
29
|
+
# @return [Class] Rails parameter filter backend.
|
|
27
30
|
def self.backend
|
|
28
31
|
ActiveSupport::ParameterFilter
|
|
29
32
|
end
|
|
30
33
|
else
|
|
34
|
+
# @return [Class] Rails parameter filter backend.
|
|
31
35
|
def self.backend
|
|
32
36
|
ActionDispatch::Http::ParameterFilter
|
|
33
37
|
end
|
|
@@ -21,7 +21,11 @@ module PostHog
|
|
|
21
21
|
get_all_flags
|
|
22
22
|
].freeze
|
|
23
23
|
|
|
24
|
-
# Initialize PostHog client
|
|
24
|
+
# Initialize the singleton PostHog client used by Rails delegators.
|
|
25
|
+
#
|
|
26
|
+
# @param options [Hash] Core {PostHog::Client} options.
|
|
27
|
+
# @yieldparam config [PostHog::Rails::InitConfig] Block-based core SDK configuration.
|
|
28
|
+
# @return [PostHog::Client]
|
|
25
29
|
def init(options = {})
|
|
26
30
|
# If block given, yield to configuration
|
|
27
31
|
if block_given?
|
|
@@ -42,11 +46,14 @@ module PostHog
|
|
|
42
46
|
end
|
|
43
47
|
end
|
|
44
48
|
|
|
49
|
+
# @return [Boolean] Whether {PostHog.init} has created a client.
|
|
45
50
|
def initialized?
|
|
46
51
|
!@client.nil?
|
|
47
52
|
end
|
|
48
53
|
|
|
49
|
-
# Fallback for any client methods not explicitly defined
|
|
54
|
+
# Fallback for any client methods not explicitly defined.
|
|
55
|
+
#
|
|
56
|
+
# @api private
|
|
50
57
|
# rubocop:disable Lint/RedundantSafeNavigation
|
|
51
58
|
def method_missing(method_name, ...)
|
|
52
59
|
if client&.respond_to?(method_name)
|
|
@@ -57,6 +64,7 @@ module PostHog
|
|
|
57
64
|
end
|
|
58
65
|
end
|
|
59
66
|
|
|
67
|
+
# @api private
|
|
60
68
|
def respond_to_missing?(method_name, include_private = false)
|
|
61
69
|
client&.respond_to?(method_name) || super
|
|
62
70
|
end
|
|
@@ -118,6 +126,8 @@ module PostHog
|
|
|
118
126
|
at_exit { PostHog.client&.shutdown if PostHog.initialized? }
|
|
119
127
|
end
|
|
120
128
|
|
|
129
|
+
# @api private
|
|
130
|
+
# @return [void]
|
|
121
131
|
def insert_middleware_after(app, target, middleware)
|
|
122
132
|
# During initialization, app.config.middleware is a MiddlewareStackProxy
|
|
123
133
|
# which only supports recording operations (insert_after, use, etc.)
|
|
@@ -125,6 +135,8 @@ module PostHog
|
|
|
125
135
|
app.config.middleware.insert_after(target, middleware)
|
|
126
136
|
end
|
|
127
137
|
|
|
138
|
+
# @api private
|
|
139
|
+
# @return [void]
|
|
128
140
|
def insert_middleware_before(app, target, middleware)
|
|
129
141
|
# During initialization, app.config.middleware is a MiddlewareStackProxy
|
|
130
142
|
# which only supports recording operations (insert_before, use, etc.)
|
|
@@ -132,6 +144,8 @@ module PostHog
|
|
|
132
144
|
app.config.middleware.insert_before(target, middleware)
|
|
133
145
|
end
|
|
134
146
|
|
|
147
|
+
# @api private
|
|
148
|
+
# @return [void]
|
|
135
149
|
def self.register_error_subscriber
|
|
136
150
|
return unless PostHog::Rails.config&.auto_capture_exceptions
|
|
137
151
|
|
|
@@ -142,6 +156,8 @@ module PostHog
|
|
|
142
156
|
PostHog::Logging.logger.warn("Backtrace: #{e.backtrace&.first(5)&.join("\n")}")
|
|
143
157
|
end
|
|
144
158
|
|
|
159
|
+
# @api private
|
|
160
|
+
# @return [Boolean]
|
|
145
161
|
def self.rails_version_above_7?
|
|
146
162
|
::Rails.version.to_f >= 7.0
|
|
147
163
|
end
|
|
@@ -149,51 +165,74 @@ module PostHog
|
|
|
149
165
|
|
|
150
166
|
# Configuration wrapper for the init block
|
|
151
167
|
class InitConfig
|
|
168
|
+
# @param base_options [Hash] Initial core SDK options.
|
|
152
169
|
def initialize(base_options = {})
|
|
153
170
|
@base_options = base_options
|
|
154
171
|
end
|
|
155
172
|
|
|
156
173
|
# Core PostHog options
|
|
174
|
+
#
|
|
175
|
+
# @param value [String]
|
|
176
|
+
# @return [String]
|
|
157
177
|
def api_key=(value)
|
|
158
178
|
@base_options[:api_key] = value
|
|
159
179
|
end
|
|
160
180
|
|
|
181
|
+
# @param value [String, nil]
|
|
182
|
+
# @return [String, nil]
|
|
161
183
|
def personal_api_key=(value)
|
|
162
184
|
@base_options[:personal_api_key] = value
|
|
163
185
|
end
|
|
164
186
|
|
|
187
|
+
# @param value [String]
|
|
188
|
+
# @return [String]
|
|
165
189
|
def host=(value)
|
|
166
190
|
@base_options[:host] = value
|
|
167
191
|
end
|
|
168
192
|
|
|
193
|
+
# @param value [Integer]
|
|
194
|
+
# @return [Integer]
|
|
169
195
|
def max_queue_size=(value)
|
|
170
196
|
@base_options[:max_queue_size] = value
|
|
171
197
|
end
|
|
172
198
|
|
|
199
|
+
# @param value [Boolean]
|
|
200
|
+
# @return [Boolean]
|
|
173
201
|
def test_mode=(value)
|
|
174
202
|
@base_options[:test_mode] = value
|
|
175
203
|
end
|
|
176
204
|
|
|
205
|
+
# @param value [Boolean]
|
|
206
|
+
# @return [Boolean]
|
|
177
207
|
def sync_mode=(value)
|
|
178
208
|
@base_options[:sync_mode] = value
|
|
179
209
|
end
|
|
180
210
|
|
|
211
|
+
# @param value [Proc]
|
|
212
|
+
# @return [Proc]
|
|
181
213
|
def on_error=(value)
|
|
182
214
|
@base_options[:on_error] = value
|
|
183
215
|
end
|
|
184
216
|
|
|
217
|
+
# @param value [Integer]
|
|
218
|
+
# @return [Integer]
|
|
185
219
|
def feature_flags_polling_interval=(value)
|
|
186
220
|
@base_options[:feature_flags_polling_interval] = value
|
|
187
221
|
end
|
|
188
222
|
|
|
223
|
+
# @param value [Integer]
|
|
224
|
+
# @return [Integer]
|
|
189
225
|
def feature_flag_request_timeout_seconds=(value)
|
|
190
226
|
@base_options[:feature_flag_request_timeout_seconds] = value
|
|
191
227
|
end
|
|
192
228
|
|
|
229
|
+
# @param value [Proc]
|
|
230
|
+
# @return [Proc]
|
|
193
231
|
def before_send=(value)
|
|
194
232
|
@base_options[:before_send] = value
|
|
195
233
|
end
|
|
196
234
|
|
|
235
|
+
# @return [Hash] Core SDK options suitable for {PostHog::Client.new}.
|
|
197
236
|
def to_client_options
|
|
198
237
|
@base_options
|
|
199
238
|
end
|
|
@@ -7,11 +7,16 @@ require 'posthog/rails/request_metadata'
|
|
|
7
7
|
module PostHog
|
|
8
8
|
module Rails
|
|
9
9
|
# Rack middleware that creates a request-local PostHog context from tracing headers.
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
10
12
|
class RequestContext
|
|
13
|
+
# @param app [#call] Rack application.
|
|
11
14
|
def initialize(app)
|
|
12
15
|
@app = app
|
|
13
16
|
end
|
|
14
17
|
|
|
18
|
+
# @param env [Hash] Rack environment.
|
|
19
|
+
# @return [Array] Rack response.
|
|
15
20
|
def call(env)
|
|
16
21
|
request = build_request(env)
|
|
17
22
|
|
|
@@ -5,9 +5,13 @@ require 'posthog/rails/tracing_headers'
|
|
|
5
5
|
module PostHog
|
|
6
6
|
module Rails
|
|
7
7
|
# Internal helpers for extracting request metadata owned by RequestContext.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
8
10
|
module RequestMetadata
|
|
9
11
|
module_function
|
|
10
12
|
|
|
13
|
+
# @param request [Object] Rack or Rails request object.
|
|
14
|
+
# @return [Hash] Event properties extracted from the request.
|
|
11
15
|
def extract(request)
|
|
12
16
|
properties = {}
|
|
13
17
|
add_property(properties, '$current_url', current_url(request))
|
|
@@ -2,14 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
module PostHog
|
|
4
4
|
module Rails
|
|
5
|
-
# Middleware that intercepts exceptions that are rescued by Rails
|
|
5
|
+
# Middleware that intercepts exceptions that are rescued by Rails.
|
|
6
6
|
# This middleware runs before ShowExceptions and captures the exception
|
|
7
|
-
# so we can report it even if Rails rescues it
|
|
7
|
+
# so we can report it even if Rails rescues it.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
8
10
|
class RescuedExceptionInterceptor
|
|
11
|
+
# @param app [#call] Rack application.
|
|
9
12
|
def initialize(app)
|
|
10
13
|
@app = app
|
|
11
14
|
end
|
|
12
15
|
|
|
16
|
+
# @param env [Hash] Rack environment.
|
|
17
|
+
# @return [Array] Rack response.
|
|
13
18
|
def call(env)
|
|
14
19
|
@app.call(env)
|
|
15
20
|
rescue StandardError => e
|
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
module PostHog
|
|
4
4
|
module Rails
|
|
5
5
|
# Helpers for extracting and sanitizing PostHog tracing headers from Rack/Rails requests.
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
6
8
|
module TracingHeaders
|
|
7
9
|
MAX_HEADER_VALUE_LENGTH = 1000
|
|
8
10
|
CONTROL_CHARACTERS = /[[:cntrl:]]/
|
|
9
11
|
|
|
10
12
|
module_function
|
|
11
13
|
|
|
14
|
+
# @param value [Object]
|
|
15
|
+
# @return [String, nil]
|
|
12
16
|
def sanitize_header_value(value)
|
|
13
17
|
return nil unless value.is_a?(String)
|
|
14
18
|
|
|
@@ -18,6 +22,9 @@ module PostHog
|
|
|
18
22
|
sanitized[0, MAX_HEADER_VALUE_LENGTH]
|
|
19
23
|
end
|
|
20
24
|
|
|
25
|
+
# @param request_or_env [Object, Hash] Rack request, Rails request, or Rack env hash.
|
|
26
|
+
# @param header_name [String]
|
|
27
|
+
# @return [String, nil]
|
|
21
28
|
def extract_header(request_or_env, header_name)
|
|
22
29
|
candidates = header_candidates(header_name)
|
|
23
30
|
|
data/lib/posthog/rails.rb
CHANGED
|
@@ -18,29 +18,41 @@ module PostHog
|
|
|
18
18
|
IN_WEB_REQUEST_KEY = :posthog_in_web_request
|
|
19
19
|
|
|
20
20
|
class << self
|
|
21
|
+
# @return [PostHog::Rails::Configuration] Rails integration configuration.
|
|
21
22
|
def config
|
|
22
23
|
@config ||= Configuration.new
|
|
23
24
|
end
|
|
24
25
|
|
|
26
|
+
# @param config [PostHog::Rails::Configuration] Rails integration configuration.
|
|
25
27
|
attr_writer :config
|
|
26
28
|
|
|
29
|
+
# Configure Rails integration options.
|
|
30
|
+
#
|
|
31
|
+
# @yieldparam config [PostHog::Rails::Configuration]
|
|
32
|
+
# @return [void]
|
|
27
33
|
def configure
|
|
28
34
|
yield config if block_given?
|
|
29
35
|
end
|
|
30
36
|
|
|
31
37
|
# Mark that we're in a web request context
|
|
32
38
|
# CaptureExceptions middleware will handle exception capture
|
|
39
|
+
# @api private
|
|
40
|
+
# @return [void]
|
|
33
41
|
def enter_web_request
|
|
34
42
|
Thread.current[IN_WEB_REQUEST_KEY] = true
|
|
35
43
|
end
|
|
36
44
|
|
|
37
45
|
# Clear web request context (called at end of request)
|
|
46
|
+
# @api private
|
|
47
|
+
# @return [void]
|
|
38
48
|
def exit_web_request
|
|
39
49
|
Thread.current[IN_WEB_REQUEST_KEY] = false
|
|
40
50
|
end
|
|
41
51
|
|
|
42
52
|
# Check if we're currently in a web request context
|
|
43
53
|
# Used by ErrorSubscriber to avoid duplicate captures
|
|
54
|
+
# @api private
|
|
55
|
+
# @return [Boolean]
|
|
44
56
|
def in_web_request?
|
|
45
57
|
Thread.current[IN_WEB_REQUEST_KEY] == true
|
|
46
58
|
end
|