Class: Railscope::Subscribers::CommandSubscriber

Inherits:
BaseSubscriber show all
Defined in:
lib/railscope/subscribers/command_subscriber.rb

Defined Under Namespace

Modules: TaskInstrumentation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subscribe ⇒ Object



7
8
9
10
11
# File 'lib/railscope/subscribers/command_subscriber.rb', line 7

def subscribe
  return unless defined?(Rake::Task)

  instrument_rake_tasks
end

Instance Method Details

#record(task, args) ⇒ Object



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
99
# File 'lib/railscope/subscribers/command_subscriber.rb', line 54

def record(task, args)
  return yield unless Railscope.enabled?
  return yield unless Railscope.ready?

  # Setup context for this command (similar to middleware for HTTP requests)
  setup_command_context(task)

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  exit_code = 0
  exception_info = nil
  caught_exception = nil

  begin
    result = yield
    result
  rescue SystemExit => e
    exit_code = e.status
    raise
  rescue Exception => e
    exit_code = 1
    caught_exception = e
    exception_info = {
      class: e.class.name,
      message: e.message,
      backtrace: e.backtrace&.first(20)
    }
    raise
  ensure
    duration = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(2)

    # Create the command entry
    create_entry!(
      entry_type: "command",
      payload: build_payload(task, args, duration, exit_code, exception_info),
      tags: build_tags(task, exit_code),
      family_hash: generate_family_hash("command", task.name),
      should_display_on_index: true
    )

    # Also create a separate exception entry (appears in exceptions list)
    create_exception_entry!(task, caught_exception) if caught_exception

    # Clear context after command completes
    Railscope::Context.clear!
  end
end