Module: Trailblazer::Developer::Trace

Defined in:
lib/trailblazer/developer/trace.rb,
lib/trailblazer/developer/trace/present.rb,
lib/trailblazer/developer/trace/focusable.rb,
lib/trailblazer/developer/trace/inspector.rb

Defined Under Namespace

Modules: Focusable, Inspector, Present Classes: Entity, Level, Stack

Constant Summary collapse

Activity =
Trailblazer::Activity

Class Method Summary collapse

Class Method Details

.arguments_for_call(activity, options, original_flow_options, **original_circuit_options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trailblazer/developer/trace.rb', line 20

def arguments_for_call(activity, (options, original_flow_options), **original_circuit_options)
  default_flow_options = {
    stack: Trace::Stack.new,

    input_data_collector: Trace.method(:default_input_data_collector),
    output_data_collector: Trace.method(:default_output_data_collector),
  }

  flow_options = { **default_flow_options, **Hash( original_flow_options ) }

  default_circuit_options = {
    wrap_runtime:  ::Hash.new(Trace.merge_plan), # DISCUSS: this overrides existing {:wrap_runtime}.
  }

  circuit_options = { **original_circuit_options, **default_circuit_options }

  return activity, [ options, flow_options ], circuit_options
end

.call(activity, ctx, flow_options, **circuit_options) ⇒ Object Also known as: invoke

Public entry point to activate tracing when running activity.



10
11
12
13
14
15
16
# File 'lib/trailblazer/developer/trace.rb', line 10

def call(activity, (ctx, flow_options), **circuit_options)
  activity, (ctx, flow_options), circuit_options = Trace.arguments_for_call( activity, [ctx, flow_options], **circuit_options ) # only run once for the entire circuit!

  signal, (ctx, flow_options) = Activity::TaskWrap.invoke(activity, [ctx, flow_options], **circuit_options)

  return flow_options[:stack], signal, [ctx, flow_options]
end

.capture_args(wrap_config, ctx, flow), circuit_options) ⇒ Object

It’s important to understand that :stack is mutated by design. This is needed so in case of exceptions we still have a “global” trace - unfortunately Ruby doesn’t allow us a better way. taskWrap step to capture incoming arguments of a step.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/trailblazer/developer/trace.rb', line 62

def capture_args(wrap_config, ((ctx, flow), circuit_options))
  flow[:stack].indent!

  flow[:stack] << Entity::Input.new(
    wrap_config[:task],
    circuit_options[:activity],
    flow[:input_data_collector].call(wrap_config, [ctx, flow], circuit_options)
  ).freeze

  return wrap_config, [[ctx, flow], circuit_options]
end

.capture_return(wrap_config, ctx, flow), circuit_options) ⇒ Object

taskWrap step to capture outgoing arguments from a step.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/trailblazer/developer/trace.rb', line 75

def capture_return(wrap_config, ((ctx, flow), circuit_options))
  flow[:stack] << Entity::Output.new(
    wrap_config[:task],
    {},
    flow[:output_data_collector].call(wrap_config, [ctx, flow], circuit_options)
  ).freeze

  flow[:stack].unindent!

  return wrap_config, [[ctx, flow], circuit_options]
end

.default_input_data_collector(wrap_config, ctx, _, circuit_options) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/trailblazer/developer/trace.rb', line 87

def default_input_data_collector(wrap_config, (ctx, _), circuit_options)
  graph = Trailblazer::Activity::Introspect::Graph(circuit_options[:activity])
  task  = wrap_config[:task]
  name  = (node = graph.find { |node| node[:task] == task }) ? node[:id] : task

  { ctx: ctx, task_name: name }
end

.default_output_data_collector(wrap_config, ctx, _, _) ⇒ Object



95
96
97
# File 'lib/trailblazer/developer/trace.rb', line 95

def default_output_data_collector(wrap_config, (ctx, _), _)
  { ctx: ctx, signal: wrap_config[:return_signal] }
end

.merge_planObject

Insertions for the trace tasks that capture the arguments just before calling the task, and before the TaskWrap is finished.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trailblazer/developer/trace.rb', line 45

def merge_plan
  Activity::TaskWrap::Extension.new(
    {
      insert: [Activity::Adds::Insert.method(:Prepend), "task_wrap.call_task"],
      row:    Activity::TaskWrap::Pipeline.Row("task_wrap.capture_args", Trace.method(:capture_args))
    },
    {
      insert: [Activity::Adds::Insert.method(:Append)], # append to the very end of tW.
      row:    Activity::TaskWrap::Pipeline.Row("task_wrap.capture_return", Trace.method(:capture_return))
    },
  )
end