Module: Webmachine::Trace

Defined in:
lib/webmachine/trace.rb,
lib/webmachine/trace/fsm.rb,
lib/webmachine/trace/listener.rb,
lib/webmachine/trace/resource_proxy.rb,
lib/webmachine/trace/trace_resource.rb,
lib/webmachine/trace/pstore_trace_store.rb

Overview

Contains means to enable the Webmachine visual debugger.

Defined Under Namespace

Modules: FSM Classes: Listener, PStoreTraceStore, ResourceProxy, TraceResource

Constant Summary collapse

TRACE_STORES =

Classes that implement storage for visual debugger traces.

{
  :memory => Hash,
  :pstore => PStoreTraceStore
}
DEFAULT_TRACE_SUBSCRIBER =
Webmachine::Events.subscribe(
  /wm\.trace\..+/,
  Webmachine::Trace::Listener.new
)

Class Method Summary collapse

Class Method Details

.fetch(key) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetches a given trace from the trace store. This is used to send specific trace information to the visual debugger.

Parameters:

  • key (String)

    the trace’s key

Returns:

  • (Array)

    the raw trace



56
57
58
# File 'lib/webmachine/trace.rb', line 56

def fetch(key)
  trace_store.fetch(key)
end

.record(key, trace) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Records a trace from a processed request. This is automatically called by ResourceProxy when finishing the request.

Parameters:

  • key (String)

    a unique identifier for the request

  • trace (Array)

    the raw trace generated by processing the request



39
40
41
# File 'lib/webmachine/trace.rb', line 39

def record(key, trace)
  trace_store[key] = trace
end

.trace?(resource) ⇒ true, false

Determines whether this resource instance should be traced.

Parameters:

Returns:

  • (true, false)

    whether to trace the resource



25
26
27
28
29
30
# File 'lib/webmachine/trace.rb', line 25

def trace?(resource)
  # For now this defers to the resource to enable tracing in the
  # initialize method. At a later time, we can add tracing at the
  # Application level, perhaps.
  resource.trace?
end

.trace_listener=(listeners) ⇒ Array<Object>

Sets the trace listener objects. Defaults to Webmachine::Trace::Listener.new.

Parameters:

  • listeners (Array<Object>)

    a list of event listeners

Returns:

  • (Array<Object>)

    a list of event subscribers



84
85
86
87
88
89
90
# File 'lib/webmachine/trace.rb', line 84

def trace_listener=(listeners)
  Webmachine::Events.unsubscribe(DEFAULT_TRACE_SUBSCRIBER)

  Array(listeners).map do |listener|
    Webmachine::Events.subscribe(/wm\.trace\..+/, listener)
  end
end

.trace_storeObject



71
72
73
74
75
76
77
# File 'lib/webmachine/trace.rb', line 71

def trace_store
  @trace_store ||= begin
                     opts = Array(@trace_store_opts).dup
                     type = opts.shift
                     TRACE_STORES[type].new(*opts)
                   end
end

.trace_store=(*args) ⇒ Object

Sets the trace storage method. The first parameter should be a Symbol, followed by any additional options for the store. Defaults to :memory, which is an in-memory Hash.

Examples:

Webmachine::Trace.trace_store = :pstore, "/tmp/webmachine.trace"


65
66
67
68
# File 'lib/webmachine/trace.rb', line 65

def trace_store=(*args)
  @trace_store = nil
  @trace_store_opts = args
end

.tracesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves keys of traces that have been recorded. This is used to present a list of available traces in the visual debugger.

Returns:

  • (Array<String>)

    a list of recorded traces



47
48
49
# File 'lib/webmachine/trace.rb', line 47

def traces
  trace_store.keys
end