Class: PostHog::Rails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/posthog/rails/configuration.rb

Constant Summary collapse

DEFAULT_LOGS_MAX_RECORDS_PER_MINUTE =

Default cap on log records forwarded to PostHog Logs per minute.

6_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePostHog::Rails::Configuration



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/posthog/rails/configuration.rb', line 63

def initialize
  @auto_capture_exceptions = false
  @report_rescued_exceptions = false
  @auto_instrument_active_job = false
  @excluded_exceptions = []
  @use_tracing_headers = true
  @capture_user_context = true
  @current_user_method = :current_user
  @user_id_method = nil
  @logs_enabled = false
  @logs_forward_rails_logger = true
  @logs_level = nil
  @logs_max_records_per_minute = DEFAULT_LOGS_MAX_RECORDS_PER_MINUTE
  @logs_before_send = nil
end

Instance Attribute Details

#auto_capture_exceptionsBoolean

Returns Whether to automatically capture exceptions from Rails. Defaults to false.

Returns:

  • (Boolean)

    Whether to automatically capture exceptions from Rails. Defaults to false.



14
15
16
# File 'lib/posthog/rails/configuration.rb', line 14

def auto_capture_exceptions
  @auto_capture_exceptions
end

#auto_instrument_active_jobBoolean

Returns Whether to automatically instrument ActiveJob. Defaults to false.

Returns:

  • (Boolean)

    Whether to automatically instrument ActiveJob. Defaults to false.



20
21
22
# File 'lib/posthog/rails/configuration.rb', line 20

def auto_instrument_active_job
  @auto_instrument_active_job
end

#capture_user_contextBoolean

Returns Whether to capture the current user context in exceptions. Defaults to true.

Returns:

  • (Boolean)

    Whether to capture the current user context in exceptions. Defaults to true.



30
31
32
# File 'lib/posthog/rails/configuration.rb', line 30

def capture_user_context
  @capture_user_context
end

#current_user_methodSymbol

Returns Method name to call on controller to get the current user. Defaults to :current_user.

Returns:

  • (Symbol)

    Method name to call on controller to get the current user. Defaults to :current_user.



33
34
35
# File 'lib/posthog/rails/configuration.rb', line 33

def current_user_method
  @current_user_method
end

#excluded_exceptionsArray<String>

Returns Exception class names to ignore in addition to the defaults.

Returns:

  • (Array<String>)

    Exception class names to ignore in addition to the defaults.



23
24
25
# File 'lib/posthog/rails/configuration.rb', line 23

def excluded_exceptions
  @excluded_exceptions
end

#logs_before_sendProc?

Callback invoked with each log record hash (:timestamp, :severity, :body, :attributes — where :severity is a symbol such as :warn) before it is sent to PostHog Logs. Return a (possibly modified) hash to send, or nil to drop the record — useful for scrubbing PII. If the callback raises, the record is dropped. Defaults to nil.

Returns:

  • (Proc, nil)

    Callback invoked with each log record hash (:timestamp, :severity, :body, :attributes — where :severity is a symbol such as :warn) before it is sent to PostHog Logs. Return a (possibly modified) hash to send, or nil to drop the record — useful for scrubbing PII. If the callback raises, the record is dropped. Defaults to nil.



60
61
62
# File 'lib/posthog/rails/configuration.rb', line 60

def logs_before_send
  @logs_before_send
end

#logs_enabledBoolean

Returns Master switch for forwarding logs to PostHog Logs over OTLP. Defaults to false.

Returns:

  • (Boolean)

    Master switch for forwarding logs to PostHog Logs over OTLP. Defaults to false.



40
41
42
# File 'lib/posthog/rails/configuration.rb', line 40

def logs_enabled
  @logs_enabled
end

#logs_forward_rails_loggerBoolean

Returns Whether to broadcast Rails.logger output into the PostHog Logs sink. Defaults to true (only takes effect when #logs_enabled is true).

Returns:

  • (Boolean)

    Whether to broadcast Rails.logger output into the PostHog Logs sink. Defaults to true (only takes effect when #logs_enabled is true).



44
45
46
# File 'lib/posthog/rails/configuration.rb', line 44

def logs_forward_rails_logger
  @logs_forward_rails_logger
end

#logs_levelInteger, ...

Returns Minimum severity to forward to PostHog Logs. When nil, inherits the current Rails.logger level. Accepts a Logger severity constant (e.g. Logger::INFO) or symbol (:info).

Returns:

  • (Integer, Symbol, nil)

    Minimum severity to forward to PostHog Logs. When nil, inherits the current Rails.logger level. Accepts a Logger severity constant (e.g. Logger::INFO) or symbol (:info).



48
49
50
# File 'lib/posthog/rails/configuration.rb', line 48

def logs_level
  @logs_level
end

#logs_max_records_per_minuteInteger, ...

Returns Maximum log records forwarded to PostHog Logs per minute, protecting the ingestion quota from runaway log volume. Defaults to 6000. Numeric strings (e.g. from ENV) are coerced. Set to nil, 0, or a negative value to disable the cap; an unparseable value falls back to the default with a warning.

Returns:

  • (Integer, String, nil)

    Maximum log records forwarded to PostHog Logs per minute, protecting the ingestion quota from runaway log volume. Defaults to 6000. Numeric strings (e.g. from ENV) are coerced. Set to nil, 0, or a negative value to disable the cap; an unparseable value falls back to the default with a warning.



54
55
56
# File 'lib/posthog/rails/configuration.rb', line 54

def logs_max_records_per_minute
  @logs_max_records_per_minute
end

#report_rescued_exceptionsBoolean

Returns Whether to capture exceptions that Rails rescues (e.g., with rescue_from). Defaults to false.

Returns:

  • (Boolean)

    Whether to capture exceptions that Rails rescues (e.g., with rescue_from). Defaults to false.



17
18
19
# File 'lib/posthog/rails/configuration.rb', line 17

def report_rescued_exceptions
  @report_rescued_exceptions
end

#use_tracing_headersBoolean

Returns Whether to use PostHog tracing headers for request-scoped identity/session context. Defaults to true.

Returns:

  • (Boolean)

    Whether to use PostHog tracing headers for request-scoped identity/session context. Defaults to true.



27
28
29
# File 'lib/posthog/rails/configuration.rb', line 27

def use_tracing_headers
  @use_tracing_headers
end

#user_id_methodSymbol?

Returns Method name to call on the user object to get distinct_id. When nil, tries: posthog_distinct_id, distinct_id, id, pk, uuid in order.

Returns:

  • (Symbol, nil)

    Method name to call on the user object to get distinct_id. When nil, tries: posthog_distinct_id, distinct_id, id, pk, uuid in order.



37
38
39
# File 'lib/posthog/rails/configuration.rb', line 37

def user_id_method
  @user_id_method
end

Instance Method Details

#default_excluded_exceptionsArray<String>

Default exceptions that Rails apps typically don’t want to track.

Returns:

  • (Array<String>)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/posthog/rails/configuration.rb', line 82

def default_excluded_exceptions
  [
    'AbstractController::ActionNotFound',
    'ActionController::BadRequest',
    'ActionController::InvalidAuthenticityToken',
    'ActionController::InvalidCrossOriginRequest',
    'ActionController::MethodNotAllowed',
    'ActionController::NotImplemented',
    'ActionController::ParameterMissing',
    'ActionController::RoutingError',
    'ActionController::UnknownFormat',
    'ActionController::UnknownHttpMethod',
    'ActionDispatch::Http::MimeNegotiation::InvalidType',
    'ActionDispatch::Http::Parameters::ParseError',
    'ActiveRecord::RecordNotFound',
    'ActiveRecord::RecordNotUnique'
  ]
end

#should_capture_exception?(exception) ⇒ Boolean

Returns Whether the exception should be captured.

Parameters:

  • exception (Exception)

    The exception to check.

Returns:

  • (Boolean)

    Whether the exception should be captured.



103
104
105
106
# File 'lib/posthog/rails/configuration.rb', line 103

def should_capture_exception?(exception)
  exception_name = exception.class.name
  !all_excluded_exceptions.include?(exception_name)
end