Module: Webmachine::Trace::FSM

Included in:
Decision::FSM
Defined in:
lib/webmachine/trace/fsm.rb

Overview

This module is injected into Decision::FSM when tracing is enabled for a resource, enabling the capturing of traces.

Instance Method Summary collapse

Instance Method Details

#initialize(_resource, _request, _response) ⇒ Object

Overrides the default resource accessor so that incoming callbacks are traced.



9
10
11
12
13
14
15
16
17
# File 'lib/webmachine/trace/fsm.rb', line 9

def initialize(_resource, _request, _response)
  if trace?
    class << self
      def resource
        @resource_proxy ||= ResourceProxy.new(@resource)
      end
    end
  end
end

#trace?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/webmachine/trace/fsm.rb', line 19

def trace?
  Trace.trace?(@resource)
end

#trace_decision(decision) ⇒ Object

Adds a decision to the trace.

Parameters:

  • decision (Symbol)

    the decision being processed



60
61
62
# File 'lib/webmachine/trace/fsm.rb', line 60

def trace_decision(decision)
  response.trace << {type: :decision, decision: decision} if trace?
end

#trace_request(request) ⇒ Object

Adds the request to the trace.

Parameters:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webmachine/trace/fsm.rb', line 25

def trace_request(request)
  if trace?
    response.trace << {
      type: :request,
      method: request.method,
      path: request.uri.request_uri.to_s,
      headers: request.headers,
      body: request.body.to_s
    }
  end
end

#trace_response(response) ⇒ Object

Adds the response to the trace and then commits the trace to separate storage which can be discovered by the debugger.

Parameters:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webmachine/trace/fsm.rb', line 40

def trace_response(response)
  if trace?
    response.trace << {
      type: :response,
      code: response.code.to_s,
      headers: response.headers,
      body: trace_response_body(response.body)
    }
  end
ensure
  if trace?
    Webmachine::Events.publish('wm.trace.record', {
      trace_id: resource.object_id.to_s,
      trace: response.trace
    })
  end
end