Module: LogStruct::Integrations::GoodJob

Extended by:
IntegrationInterface, T::Sig
Defined in:
lib/log_struct/integrations/good_job.rb,
lib/log_struct/integrations/good_job/logger.rb

Overview

GoodJob integration for structured logging

GoodJob is a PostgreSQL-based ActiveJob backend. Its jobs are logged by the ActiveJob integration (enqueued, started, finished, failed), so this integration covers GoodJob itself:

  • Replaces GoodJob.logger with a LogStruct logger, so GoodJob's own messages are JSON lines
  • Logs each thread error as a structured error line, then calls the app's on_thread_error
  • Stops GoodJob's own log subscriber logging those errors again with the whole backtrace

Configuration:

The integration is automatically enabled when GoodJob is detected and LogStruct configuration allows it. It can be disabled by setting:

config.integrations.enable_goodjob = false

Defined Under Namespace

Modules: CompactErrorLines, LogThreadErrors Classes: Logger

Class Method Summary collapse

Methods included from IntegrationInterface

setup

Class Method Details

.log_thread_error(exception) ⇒ void

This method returns an undefined value.

A failed job's exception was already logged by the ActiveJob integration, with the job's ID and class, so it isn't logged again here

Parameters:

  • exception (Exception)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/log_struct/integrations/good_job.rb', line 139

def self.log_thread_error(exception)
  return if LogStruct::LoggedErrors.logged?(exception)

  ::GoodJob.logger.error(
    LogStruct::Log::GoodJob::Error.new(
      error_class: exception.class.name || exception.class.to_s,
      error_message: exception.message,
      backtrace: exception.backtrace,
      level: LogStruct::Level::Error,
      process_id: ::Process.pid,
      thread_id: Thread.current.object_id.to_s(36)
    )
  )
end

.notifier_connection_error?(exception) ⇒ Boolean

The errors GoodJob's notifier holds back until they repeat (GoodJob::Notifier::CONNECTION_ERRORS)

Parameters:

  • exception (Exception)

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
# File 'lib/log_struct/integrations/good_job.rb', line 156

def self.notifier_connection_error?(exception)
  return true unless defined?(::GoodJob::Notifier::CONNECTION_ERRORS)

  ::GoodJob::Notifier::CONNECTION_ERRORS.any? do |error_name|
    error_class = error_name.safe_constantize
    error_class.is_a?(Class) && exception.is_a?(error_class)
  end
end

.setup(config) ⇒ Boolean?

Set up GoodJob structured logging

This method replaces GoodJob's logger and logs its thread errors as structured lines.

Parameters:

Returns:

  • (Boolean, nil) —

    Returns true if setup was successful, nil if skipped



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/log_struct/integrations/good_job.rb', line 41

def self.setup(config)
  return nil unless defined?(::GoodJob)
  return nil unless config.enabled
  return nil unless config.integrations.enable_goodjob

  # Replace GoodJob's logger with our structured logger
  configure_logger

  compact_error_lines

  true
end