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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/posthog/rails/railtie.rb', line 118

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.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/posthog/rails/railtie.rb', line 89

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)


147
148
149
# File 'lib/posthog/rails/railtie.rb', line 147

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.



135
136
137
138
139
140
141
142
143
# File 'lib/posthog/rails/railtie.rb', line 135

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.



69
70
71
72
73
74
# File 'lib/posthog/rails/railtie.rb', line 69

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.



78
79
80
81
82
83
# File 'lib/posthog/rails/railtie.rb', line 78

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