Module: TraceView::Inst::ResqueWorker

Defined in:
lib/traceview/inst/resque.rb

Instance Method Summary collapse

Instance Method Details

#perform_with_traceview(job) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/traceview/inst/resque.rb', line 80

def perform_with_traceview(job)
  report_kvs = {}
  last_arg = nil

  begin
    report_kvs[:Op] = :perform

    # Set these keys for the ability to separate out
    # background tasks into a separate app on the server-side UI
    report_kvs[:Controller] = :Resque
    report_kvs[:Action] = :perform

    report_kvs['HTTP-Host'] = Socket.gethostname
    report_kvs[:URL] = '/resque/' + job.queue
    report_kvs[:Method] = 'NONE'
    report_kvs[:Queue] = job.queue

    report_kvs[:Class] = job.payload['class']

    if TraceView::Config[:resque][:log_args]
      kv_args = job.payload['args'].to_json

      # Limit the argument json string to 1024 bytes
      if kv_args.length > 1024
        report_kvs[:Args] = kv_args[0..1023] + '...[snipped]'
      else
        report_kvs[:Args] = kv_args
      end
    end

    last_arg = job.payload['args'].last
  rescue
  end

  if last_arg.is_a?(Hash) && last_arg.key?('parent_trace_id')
    begin
      # Since the enqueue was traced, we force trace the actual job execution and reference
      # the enqueue trace with ParentTraceID
      report_kvs[:ParentTraceID] = last_arg['parent_trace_id']
      job.payload['args'].pop

    rescue
    end

    # Force this trace regardless of sampling rate so that child trace can be
    # link to parent trace.
    TraceView::API.start_trace('resque-worker', nil, report_kvs.merge('Force' => true)) do
      perform_without_traceview(job)
    end

  else
    TraceView::API.start_trace('resque-worker', nil, report_kvs) do
      perform_without_traceview(job)
    end
  end
end