Module: PostHog::Rails
- Defined in:
- lib/posthog/rails.rb,
lib/posthog/rails/facade.rb,
lib/posthog/rails/railtie.rb,
lib/posthog/rails/active_job.rb,
lib/posthog/rails/logs/setup.rb,
lib/posthog/rails/configuration.rb,
lib/posthog/rails/logs/appender.rb,
lib/posthog/rails/logs/severity.rb,
lib/posthog/rails/request_context.rb,
lib/posthog/rails/tracing_headers.rb,
lib/posthog/rails/error_subscriber.rb,
lib/posthog/rails/parameter_filter.rb,
lib/posthog/rails/request_metadata.rb,
lib/posthog/rails/logs/rate_limiter.rb,
lib/posthog/rails/capture_exceptions.rb,
lib/posthog/rails/rescued_exception_interceptor.rb
Defined Under Namespace
Modules: ActiveJobExtensions, Logs, ParameterFilter Classes: CaptureExceptions, Configuration, ErrorSubscriber, InitConfig, Railtie, RequestContext, RescuedExceptionInterceptor
Constant Summary collapse
- VERSION =
PostHog::VERSION
- IN_WEB_REQUEST_KEY =
Thread-local key for tracking web request context
:posthog_in_web_request
Class Attribute Summary collapse
-
.config ⇒ PostHog::Rails::Configuration
Rails integration configuration.
Class Method Summary collapse
-
.configure {|config| ... } ⇒ void
Configure Rails integration options.
-
.enter_web_request ⇒ void
private
Mark that we’re in a web request context CaptureExceptions middleware will handle exception capture.
-
.exit_web_request ⇒ void
private
Clear web request context (called at end of request).
-
.in_web_request? ⇒ Boolean
private
Check if we’re currently in a web request context Used by ErrorSubscriber to avoid duplicate captures.
-
.install_posthog_facade! ⇒ void
private
Install the Rails singleton-style PostHog facade at load time so Rails app initializers can call PostHog.init before Railtie initializers run.
Class Attribute Details
.config ⇒ PostHog::Rails::Configuration
Returns Rails integration configuration.
27 28 29 |
# File 'lib/posthog/rails.rb', line 27 def config @config ||= Configuration.new end |
Class Method Details
.configure {|config| ... } ⇒ void
This method returns an undefined value.
Configure Rails integration options.
38 39 40 |
# File 'lib/posthog/rails.rb', line 38 def configure yield config if block_given? end |
.enter_web_request ⇒ 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.
Mark that we’re in a web request context CaptureExceptions middleware will handle exception capture
46 47 48 |
# File 'lib/posthog/rails.rb', line 46 def enter_web_request Thread.current[IN_WEB_REQUEST_KEY] = true end |
.exit_web_request ⇒ 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.
Clear web request context (called at end of request)
53 54 55 |
# File 'lib/posthog/rails.rb', line 53 def exit_web_request Thread.current[IN_WEB_REQUEST_KEY] = false end |
.in_web_request? ⇒ 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.
Check if we’re currently in a web request context Used by ErrorSubscriber to avoid duplicate captures
61 62 63 |
# File 'lib/posthog/rails.rb', line 61 def in_web_request? Thread.current[IN_WEB_REQUEST_KEY] == true end |
.install_posthog_facade! ⇒ 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.
Install the Rails singleton-style PostHog facade at load time so Rails app initializers can call PostHog.init before Railtie initializers run.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/posthog/rails/facade.rb', line 10 def self.install_posthog_facade! return if @posthog_facade_installed PostHog.class_eval do class << self attr_accessor :client # Initialize the singleton PostHog client used by Rails delegators. # # @param options [Hash] Core {PostHog::Client} options. # @yieldparam config [PostHog::Rails::InitConfig] Block-based core SDK configuration. # @return [PostHog::Client] def init( = {}) # If block given, yield to configuration if block_given? config = PostHog::Rails::InitConfig.new() yield config = config. end # Let the PostHog Logs pipeline reuse the same api_key/host without # the core client exposing public readers. PostHog::Rails::Logs::Setup.() if defined?(PostHog::Rails::Logs::Setup) # Create the PostHog client. If a client already exists, shut it down # after replacement so repeated init calls do not leave background # resources from the previous instance running. previous_client = @client @client = PostHog::Client.new() begin previous_client&.shutdown rescue StandardError => e PostHog::Logging.logger.warn("Failed to shut down previous PostHog client: #{e.}") end @client end # @return [Boolean] Whether {PostHog.init} has created a client. def initialized? !@client.nil? end # Fallback for any client methods not explicitly defined. # # @api private def method_missing(method_name, ...) ensure_initialized! if client.respond_to?(method_name) client.public_send(method_name, ...) else super end end # @api private def respond_to_missing?(method_name, include_private = false) ensure_initialized! client.respond_to?(method_name, include_private) || super end private def ensure_initialized! return if initialized? @client = PostHog::Client.new(api_key: nil, silence_disabled_client_error: true) end end end %i[ capture capture_exception identify alias group_identify is_feature_enabled get_feature_flag get_all_flags ].each do |method_name| PostHog.define_singleton_method(method_name) do |*args, **kwargs, &block| ensure_initialized! client.public_send(method_name, *args, **kwargs, &block) end end @posthog_facade_installed = true end |