posthog-rails 3.19.6 → 3.20.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ef5e8bb473e8401bd4d7d7221ab6ffe3ae99b123542479742647956fcf573e4
4
- data.tar.gz: e1d8569b80ebbf9d60b4bff0062c3af648af2018230411b86553dc1309c11d97
3
+ metadata.gz: c40fa412f1db146f214ae8d0bea09266ab699f321c18f47ba8c60dd413e9a080
4
+ data.tar.gz: e1480a1a2fefd4b6e86ada5f90d91231251c4127650ec5783314c64d97bc0f8b
5
5
  SHA512:
6
- metadata.gz: 2133f560847bfad8161f96c2e0af04909e3f9eb2e981c20bc39235425a7ea6188a0bb7a86023043a0178ee056cc9809670bb37674d0c382aa44c5a90c7cf1a5c
7
- data.tar.gz: c98c753d3c73004cb5b17593bdb0433e6874b1a2075fa702de6ee254042e643235b864a95e1427b6dbae9e38159fe42e29e3b1a72548f979779a9c79232ec00e
6
+ metadata.gz: da97f13898259211df03a678953dc1e4ca70bb52f12c8f6e4f4561146d6c1ed524f1e7389a01835c837e322e87a9c6e24eaad7913474385f024d6342ada6783c
7
+ data.tar.gz: d6b3870337f9f6a2ec4ad54e730c34a8f4f2bcae718e080370c6e214b2ca2208d8bc7e1213f0b2c8ddab05abbd66b2f5140a3de8c8aad40e0951596f4d4148ec
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'posthog/rails/parameter_filter'
4
+ require 'posthog/internal/context'
5
+ require 'posthog/rails/tracing_headers'
4
6
 
5
7
  module PostHog
6
8
  module Rails
@@ -10,6 +12,7 @@ module PostHog
10
12
 
11
13
  def self.prepended(base)
12
14
  base.extend(ClassMethods)
15
+ base.around_perform :with_posthog_job_context, prepend: true if base.respond_to?(:around_perform)
13
16
  end
14
17
 
15
18
  module ClassMethods
@@ -36,21 +39,119 @@ module PostHog
36
39
  end
37
40
  end
38
41
 
39
- def perform_now
42
+ # @api private
43
+ def enqueue(...)
44
+ posthog_job_context if PostHog::Rails.config&.auto_instrument_active_job
40
45
  super
41
- rescue StandardError => e
42
- # Capture the exception with job context
43
- capture_job_exception(e)
44
- raise
46
+ end
47
+
48
+ # @api private
49
+ def serialize
50
+ data = super
51
+ return data unless PostHog::Rails.config&.auto_instrument_active_job
52
+
53
+ data.merge('posthog_context' => posthog_job_context.dup)
54
+ end
55
+
56
+ # @api private
57
+ def deserialize(data)
58
+ super.tap { @posthog_job_context = copy_posthog_ids(data['posthog_context']) }
59
+ end
60
+
61
+ # @api private
62
+ def rescue_with_handler(...)
63
+ return super unless PostHog::Rails.config&.auto_instrument_active_job
64
+
65
+ Internal::Context.with_context(
66
+ @posthog_execution_context || posthog_job_context, fresh: true, enrich_current_span: false
67
+ ) do
68
+ enrich_posthog_job_span
69
+ super
70
+ end
71
+ end
72
+
73
+ def perform_now
74
+ return super unless PostHog::Rails.config&.auto_instrument_active_job
75
+
76
+ posthog_job_context
77
+ @posthog_execution_context = nil
78
+ Internal::Context.with_context({}, fresh: true, owner: PostHog.client, enrich_current_span: false) do
79
+ super
80
+ rescue StandardError => e
81
+ # Capture the exception with job context
82
+ Internal::Context.with_context(
83
+ @posthog_execution_context || @posthog_job_context, fresh: true, enrich_current_span: false
84
+ ) do
85
+ enrich_posthog_job_span
86
+ capture_job_exception(e)
87
+ end
88
+ raise
89
+ end
45
90
  end
46
91
 
47
92
  private
48
93
 
94
+ def copy_posthog_ids(data)
95
+ data = {} unless data.is_a?(Hash)
96
+ %w[distinct_id session_id].each_with_object({}) do |key, result|
97
+ value = data[key]
98
+ value = value.to_s if key == 'distinct_id' && (value.is_a?(Integer) || (value.is_a?(Float) && value.finite?))
99
+ value = TracingHeaders.sanitize_header_value(value)
100
+ result[key] = value.freeze if value
101
+ end.freeze
102
+ end
103
+
104
+ def posthog_job_context
105
+ @posthog_job_context ||= begin
106
+ context = Internal::Context.current
107
+ copy_posthog_ids('distinct_id' => context&.distinct_id, 'session_id' => context&.session_id)
108
+ end
109
+ end
110
+
111
+ def with_posthog_job_context(&block)
112
+ return yield unless PostHog::Rails.config&.auto_instrument_active_job
113
+
114
+ distinct_id = posthog_job_distinct_id
115
+ @posthog_execution_context = if distinct_id.nil?
116
+ posthog_job_context
117
+ else
118
+ posthog_job_context.merge('distinct_id' => distinct_id).freeze
119
+ end
120
+ Internal::Context.with_context(@posthog_execution_context, fresh: true, enrich_current_span: false) do
121
+ enrich_posthog_job_span
122
+ block.call
123
+ end
124
+ end
125
+
126
+ def enrich_posthog_job_span
127
+ return unless defined?(::OpenTelemetry::Trace)
128
+
129
+ span = ::OpenTelemetry::Trace.current_span
130
+ return unless span.recording? && span.respond_to?(:attributes)
131
+
132
+ attributes = span.attributes || {}
133
+ return unless attributes['messaging.system'] == 'active_job' && attributes['messaging.message.id'] == job_id
134
+
135
+ Internal::SpanContext.apply(span)
136
+ rescue StandardError
137
+ nil
138
+ end
139
+
140
+ def posthog_job_distinct_id
141
+ extract_distinct_id_from_job
142
+ rescue StandardError
143
+ nil
144
+ end
145
+
49
146
  def capture_job_exception(exception)
50
147
  return unless PostHog::Rails.config&.auto_instrument_active_job
51
148
 
52
149
  # Build distinct_id from job arguments if possible
53
- distinct_id = extract_distinct_id_from_job
150
+ distinct_id = if @posthog_execution_context
151
+ @posthog_execution_context['distinct_id']
152
+ else
153
+ extract_distinct_id_from_job
154
+ end
54
155
 
55
156
  properties = {
56
157
  '$exception_source' => 'active_job',
@@ -70,7 +171,6 @@ module PostHog
70
171
  properties,
71
172
  mechanism: { 'type' => 'active_job', 'handled' => false }
72
173
  )
73
- PostHog::Rails.mark_active_job_exception_captured(exception)
74
174
  rescue StandardError => e
75
175
  # Don't let PostHog errors break job processing
76
176
  PostHog::Logging.logger.error("Failed to capture job exception: #{e.message}")
@@ -18,10 +18,6 @@ module PostHog
18
18
  # @param env [Hash] Rack environment.
19
19
  # @return [Array] Rack response.
20
20
  def call(env)
21
- # Signal that we're in a web request context
22
- # ErrorSubscriber will skip capture for web requests to avoid duplicates
23
- PostHog::Rails.enter_web_request
24
-
25
21
  response = @app.call(env)
26
22
  env['posthog.response_status_code'] = response_status(response)
27
23
 
@@ -35,8 +31,6 @@ module PostHog
35
31
  # Capture unhandled exceptions
36
32
  capture_exception(e, env) if should_capture?(e)
37
33
  raise
38
- ensure
39
- PostHog::Rails.exit_web_request
40
34
  end
41
35
 
42
36
  private
@@ -60,13 +54,12 @@ module PostHog
60
54
  distinct_id = extract_distinct_id(env)
61
55
  additional_properties = build_properties(request, env)
62
56
 
63
- captured = PostHog.capture_exception(
57
+ PostHog.capture_exception(
64
58
  exception,
65
59
  distinct_id,
66
60
  additional_properties,
67
61
  mechanism: { 'type' => 'rails', 'handled' => false }
68
62
  )
69
- PostHog::Rails.mark_web_exception_captured(exception) if captured
70
63
  rescue StandardError => e
71
64
  PostHog::Logging.logger.error("Failed to capture exception: #{e.message}")
72
65
  PostHog::Logging.logger.error("Backtrace: #{e.backtrace&.first(5)&.join("\n")}")
@@ -20,15 +20,6 @@ module PostHog
20
20
  def report(error, handled:, severity:, context:, source: nil)
21
21
  return unless PostHog::Rails.config&.auto_capture_exceptions
22
22
  return unless PostHog::Rails.config&.should_capture_exception?(error)
23
- # Skip if in a web request - CaptureExceptions middleware will handle it
24
- # with richer context (URL, params, controller, etc.)
25
- return if PostHog::Rails.in_web_request?
26
- # ActionDispatch::Executor reports the exception once the response has
27
- # unwound past our middleware, so the flag above is already cleared.
28
- # CaptureExceptions marks what it captured to catch that late report.
29
- return if PostHog::Rails.web_exception_captured?(error)
30
- return if PostHog::Rails.config&.auto_instrument_active_job &&
31
- PostHog::Rails.active_job_exception_captured?(error)
32
23
 
33
24
  distinct_id = context[:user_id] || context[:distinct_id]
34
25
 
@@ -20,7 +20,7 @@ module PostHog
20
20
  def call(env)
21
21
  request = build_request(env)
22
22
 
23
- Internal::Context.with_context(context_data(request), fresh: true) do
23
+ Internal::Context.with_context(context_data(request), fresh: true, owner: PostHog.client) do
24
24
  @app.call(env)
25
25
  end
26
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PostHog
4
4
  module Rails
5
- VERSION = '3.19.6'
5
+ VERSION = '3.20.0'
6
6
  end
7
7
  end
data/lib/posthog/rails.rb CHANGED
@@ -18,14 +18,6 @@ require 'posthog/rails/railtie'
18
18
 
19
19
  module PostHog
20
20
  module Rails
21
- # Thread-local key for tracking web request context
22
- IN_WEB_REQUEST_KEY = :posthog_in_web_request
23
- ACTIVE_JOB_CAPTURED_EXCEPTION_IVAR = :@posthog_active_job_exception_captured
24
- WEB_CAPTURED_EXCEPTION_IVAR = :@posthog_web_exception_captured
25
-
26
- private_constant :ACTIVE_JOB_CAPTURED_EXCEPTION_IVAR
27
- private_constant :WEB_CAPTURED_EXCEPTION_IVAR
28
-
29
21
  class << self
30
22
  # @return [PostHog::Rails::Configuration] Rails integration configuration.
31
23
  def config
@@ -42,87 +34,6 @@ module PostHog
42
34
  def configure
43
35
  yield config if block_given?
44
36
  end
45
-
46
- # Mark that we're in a web request context
47
- # CaptureExceptions middleware will handle exception capture
48
- # @api private
49
- # @return [void]
50
- def enter_web_request
51
- Thread.current[IN_WEB_REQUEST_KEY] = true
52
- end
53
-
54
- # Clear web request context (called at end of request)
55
- # @api private
56
- # @return [void]
57
- def exit_web_request
58
- Thread.current[IN_WEB_REQUEST_KEY] = false
59
- end
60
-
61
- # Check if we're currently in a web request context
62
- # Used by ErrorSubscriber to avoid duplicate captures
63
- # @api private
64
- # @return [Boolean]
65
- def in_web_request?
66
- Thread.current[IN_WEB_REQUEST_KEY] == true
67
- end
68
-
69
- # Mark an exception as already captured by the ActiveJob integration.
70
- # Used by ErrorSubscriber to avoid duplicate captures when Rails reports
71
- # the same re-raised job exception via Rails.error.
72
- # @api private
73
- # @param exception [Exception]
74
- # @return [void]
75
- def mark_active_job_exception_captured(exception)
76
- exception.instance_variable_set(ACTIVE_JOB_CAPTURED_EXCEPTION_IVAR, true)
77
- rescue StandardError
78
- nil
79
- end
80
-
81
- # Check whether an exception was already captured by ActiveJobExtensions.
82
- # @api private
83
- # @param exception [Exception]
84
- # @return [Boolean]
85
- def active_job_exception_captured?(exception)
86
- exception.instance_variable_get(ACTIVE_JOB_CAPTURED_EXCEPTION_IVAR) == true
87
- rescue StandardError
88
- false
89
- end
90
-
91
- # Mark an exception as already captured by the CaptureExceptions middleware.
92
- #
93
- # ActionDispatch::Executor sits above our middleware and reports unhandled
94
- # exceptions to Rails.error *after* the response has unwound back through
95
- # CaptureExceptions, so `in_web_request?` is already false by then. The mark
96
- # lets ErrorSubscriber recognize the report as a duplicate.
97
- #
98
- # The whole cause chain is marked because ActionDispatch::ExceptionWrapper
99
- # unwraps ActionView::Template::Error to its cause, meaning Rails can report
100
- # a different object than the one the middleware captured.
101
- # @api private
102
- # @param exception [Exception]
103
- # @return [void]
104
- def mark_web_exception_captured(exception)
105
- current = exception
106
- seen = {}.compare_by_identity
107
-
108
- while current.is_a?(Exception) && !seen[current]
109
- seen[current] = true
110
- current.instance_variable_set(WEB_CAPTURED_EXCEPTION_IVAR, true)
111
- current = current.cause
112
- end
113
- rescue StandardError
114
- nil
115
- end
116
-
117
- # Check whether an exception was already captured by CaptureExceptions.
118
- # @api private
119
- # @param exception [Exception]
120
- # @return [Boolean]
121
- def web_exception_captured?(exception)
122
- exception.instance_variable_get(WEB_CAPTURED_EXCEPTION_IVAR) == true
123
- rescue StandardError
124
- false
125
- end
126
37
  end
127
38
  end
128
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.19.6
4
+ version: 3.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PostHog
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 3.28.4
32
+ version: 3.29.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 3.28.4
39
+ version: 3.29.0
40
40
  description: Automatic exception tracking and instrumentation for Ruby on Rails applications
41
41
  using PostHog
42
42
  email: engineering@posthog.com