posthog-rails 3.20.7 → 3.21.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: c24f0c6e09810a0c102e291fcfd9978bf665a26a547881518dbe624babd7b93e
4
- data.tar.gz: 4735d74c0bdeb8fca2522d4c2f57003c75141556a1750f4ded54d1638d1d3026
3
+ metadata.gz: 80e2400e119100e9916f8ecc8092d96b0d9552596c8c865895cf36e4389f78cb
4
+ data.tar.gz: 80c09b13dbafaf6f9a5efe3359e9a02dc4d92175a493f214d885c82f6194c9d8
5
5
  SHA512:
6
- metadata.gz: 0f4335531341ae4e473e22ada7ba2ffc37bb6d2ae175f006d7887fae470be06b7743174188c453a26e602a82b1afd8e33ced4966c86ade89a9295beeac765b53
7
- data.tar.gz: ea4436358cfdc575ba84b87e21c3a6452879913d551d6990a6e879f9a2d987aefc3f319b660dbafca76b6e0cf699f25c8b545f1a60a657b919307d9837e74434
6
+ metadata.gz: 89ebcf81d463b9b3990500f95b0a73dc5c7ba6e3b06d7573a96c7c9299a31ef007ec116649733f6629809be4809b8aea5e774d1ff8ed271f486fcb658f42135f
7
+ data.tar.gz: b150e2c4c23ac510bd0b6b50fb64a3c8290bf01503bae5d2795a8c5f214104c7c1ec72a5e47f51442cc35627a71630e4195610cf2532dfb340609cb48df1748b
@@ -33,6 +33,10 @@ module PostHog
33
33
  )
34
34
  end
35
35
 
36
+ initializer 'posthog.streaming_errors' do
37
+ StreamingExceptions.install
38
+ end
39
+
36
40
  # After initialization, set up remaining integrations
37
41
  config.after_initialize do |_app|
38
42
  # Hook into ActiveJob only if enabled
@@ -11,8 +11,9 @@ module PostHog
11
11
  # @api private
12
12
  class RequestContext
13
13
  # @param app [#call] Rack application.
14
- def initialize(app)
14
+ def initialize(app, enrich_current_span: true)
15
15
  @app = app
16
+ @enrich_current_span = enrich_current_span
16
17
  end
17
18
 
18
19
  # @param env [Hash] Rack environment.
@@ -20,7 +21,9 @@ module PostHog
20
21
  def call(env)
21
22
  request = build_request(env)
22
23
 
23
- Internal::Context.with_context(context_data(request), fresh: true, owner: PostHog.client) do
24
+ Internal::Context.with_context(
25
+ context_data(request), fresh: true, owner: PostHog.client, enrich_current_span: @enrich_current_span
26
+ ) do
24
27
  @app.call(env)
25
28
  end
26
29
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PostHog
4
+ module Rails
5
+ # @api private
6
+ class StreamingExceptions < CaptureExceptions
7
+ module LiveReporter
8
+ private
9
+
10
+ def log_error(exception)
11
+ StreamingExceptions.capture(exception, source: 'rails.live', controller: self)
12
+ super
13
+ end
14
+ end
15
+
16
+ module TemplateReporter
17
+ private
18
+
19
+ def log_error(exception)
20
+ StreamingExceptions.capture(exception, source: 'rails.streaming_template')
21
+ super
22
+ end
23
+ end
24
+
25
+ def self.install
26
+ return if @installed
27
+
28
+ ActiveSupport.on_load(:action_controller_live) do
29
+ StreamingExceptions.install_live(self)
30
+ end
31
+ # Rails 8.1.4 added the Live hook to avoid resolving Response too early.
32
+ if Gem::Version.new(::Rails.version) < Gem::Version.new('8.1.4')
33
+ ActiveSupport.on_load(:action_controller) do
34
+ StreamingExceptions.install_live(ActionController::Live)
35
+ end
36
+ end
37
+ ActiveSupport.on_load(:action_view) do
38
+ body = ActionView::StreamingTemplateRenderer::Body
39
+ body.prepend(TemplateReporter) unless body < TemplateReporter
40
+ end
41
+ @installed = true
42
+ end
43
+
44
+ def self.install_live(live)
45
+ return if live < LiveReporter
46
+
47
+ # Older Rails exposes log_error publicly. Keep visibility local to the
48
+ # target instead of changing the shared reporter module.
49
+ reporter = Module.new { include LiveReporter }
50
+ visibility = %i[public protected private].find { |value| live.send("#{value}_method_defined?", :log_error) }
51
+ reporter.send(visibility, :log_error)
52
+ live.prepend(reporter)
53
+ end
54
+
55
+ def self.capture(exception, source:, controller: nil)
56
+ return unless PostHog.initialized? && PostHog.client.enabled?
57
+
58
+ new(nil).report(exception, source: source, controller: controller)
59
+ rescue StandardError
60
+ # Reporting must never replace Rails' logging result or exception.
61
+ nil
62
+ end
63
+
64
+ def report(exception, source:, controller:)
65
+ return unless should_capture?(exception)
66
+
67
+ env = controller&.request&.env
68
+ if env
69
+ # Live may log after middleware has returned or in another scope.
70
+ # Use its own request, never a retained or unrelated ambient context.
71
+ RequestContext.new(lambda do |_env|
72
+ capture_streaming_exception(exception, source, env)
73
+ end, enrich_current_span: false).call(env)
74
+ else
75
+ capture_streaming_exception(exception, source, env)
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def capture_streaming_exception(exception, source, env = nil)
82
+ properties = env ? build_properties(ActionDispatch::Request.new(env), env) : {}
83
+ properties['$exception_source'] = source
84
+ properties['$exception_level'] = 'error'
85
+ PostHog.capture_exception(
86
+ exception,
87
+ env ? extract_distinct_id(env) : nil,
88
+ properties,
89
+ mechanism: { 'type' => 'middleware', 'handled' => false }
90
+ )
91
+ end
92
+ end
93
+
94
+ private_constant :StreamingExceptions
95
+ end
96
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PostHog
4
4
  module Rails
5
- VERSION = '3.20.7'
5
+ VERSION = '3.21.0'
6
6
  end
7
7
  end
data/lib/posthog/rails.rb CHANGED
@@ -6,6 +6,7 @@ require 'posthog/rails/tracing_headers'
6
6
  require 'posthog/rails/request_metadata'
7
7
  require 'posthog/rails/request_context'
8
8
  require 'posthog/rails/capture_exceptions'
9
+ require 'posthog/rails/streaming_exceptions'
9
10
  require 'posthog/rails/rescued_exception_interceptor'
10
11
  require 'posthog/rails/active_job'
11
12
  require 'posthog/rails/error_subscriber'
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.20.7
4
+ version: 3.21.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.32.0
32
+ version: 3.32.1
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.32.0
39
+ version: 3.32.1
40
40
  description: Automatic exception tracking and instrumentation for Ruby on Rails applications
41
41
  using PostHog
42
42
  email: engineering@posthog.com
@@ -62,6 +62,7 @@ files:
62
62
  - lib/posthog/rails/request_context.rb
63
63
  - lib/posthog/rails/request_metadata.rb
64
64
  - lib/posthog/rails/rescued_exception_interceptor.rb
65
+ - lib/posthog/rails/streaming_exceptions.rb
65
66
  - lib/posthog/rails/tracing_headers.rb
66
67
  - lib/posthog/rails/version.rb
67
68
  homepage: https://github.com/PostHog/posthog-ruby