Class: PostHog::Rails::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/posthog/rails/railtie.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.broadcast_rails_logger(appender) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Attach the appender to Rails.logger, supporting both the Rails 7.1+ BroadcastLogger and the older ActiveSupport::Logger.broadcast mechanism.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/posthog/rails/railtie.rb', line 201

def self.broadcast_rails_logger(appender)
  logger = ::Rails.logger
  return unless logger

  if logger.respond_to?(:broadcast_to)
    logger.broadcast_to(appender)
  elsif defined?(ActiveSupport::Logger) && ActiveSupport::Logger.respond_to?(:broadcast)
    logger.extend(ActiveSupport::Logger.broadcast(appender))
  else
    PostHog::Logging.logger.warn(
      'PostHog Logs could not broadcast Rails.logger; no compatible broadcast mechanism found.'
    )
  end
end

.install_posthog_logsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Build the PostHog Logs pipeline and broadcast Rails.logger into it.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/posthog/rails/railtie.rb', line 172

def self.install_posthog_logs
  unless PostHog.initialized?
    # logs_enabled is an explicit opt-in, so leave a breadcrumb instead
    # of silently skipping when PostHog.init never ran.
    PostHog::Logging.logger.warn(
      'PostHog Logs is enabled but PostHog.init has not been called; ' \
      'skipping log forwarding. Call PostHog.init in your initializer.'
    )
    return
  end

  # Mirror the core client: when it is disabled (missing/blank api_key)
  # every capture no-ops, so log forwarding should stay off too. The
  # client already logs its own missing-api_key error, so skip quietly.
  return unless PostHog.client.enabled?

  appender = PostHog::Rails::Logs::Setup.install
  return if appender.nil?

  broadcast_rails_logger(appender) if PostHog::Rails.config&.logs_forward_rails_logger
rescue StandardError => e
  PostHog::Logging.logger.warn("Failed to set up PostHog Logs: #{e.message}")
end

.rails_version_above_7?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


230
231
232
# File 'lib/posthog/rails/railtie.rb', line 230

def self.rails_version_above_7?
  ::Rails.version.to_f >= 7.0
end

.register_error_subscribervoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



218
219
220
221
222
223
224
225
226
# File 'lib/posthog/rails/railtie.rb', line 218

def self.register_error_subscriber
  return unless PostHog::Rails.config&.auto_capture_exceptions

  subscriber = PostHog::Rails::ErrorSubscriber.new
  ::Rails.error.subscribe(subscriber)
rescue StandardError => e
  PostHog::Logging.logger.warn("Failed to register error subscriber: #{e.message}")
  PostHog::Logging.logger.warn("Backtrace: #{e.backtrace&.first(5)&.join("\n")}")
end

Instance Method Details

#insert_middleware_after(app, target, middleware) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



152
153
154
155
156
157
# File 'lib/posthog/rails/railtie.rb', line 152

def insert_middleware_after(app, target, middleware)
  # During initialization, app.config.middleware is a MiddlewareStackProxy
  # which only supports recording operations (insert_after, use, etc.)
  # and does NOT support query methods like include?.
  app.config.middleware.insert_after(target, middleware)
end

#insert_middleware_before(app, target, middleware) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



161
162
163
164
165
166
# File 'lib/posthog/rails/railtie.rb', line 161

def insert_middleware_before(app, target, middleware)
  # During initialization, app.config.middleware is a MiddlewareStackProxy
  # which only supports recording operations (insert_before, use, etc.)
  # and does NOT support query methods like include?.
  app.config.middleware.insert_before(target, middleware)
end