Module: Trailblazer::Developer::Trace

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

Defined Under Namespace

Modules: Present Classes: Entity, Level, Stack

Constant Summary collapse

Activity =
Trailblazer::Activity

Class Method Summary collapse

Class Method Details

.arguments_for_call(activity, options, flow_options, **circuit_options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trailblazer/developer/trace.rb', line 20

def arguments_for_call(activity, (options, flow_options), **circuit_options)
  tracing_flow_options = {
    stack: Trace::Stack.new,
  }

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

  return activity, [ options, tracing_flow_options.merge(flow_options) ], circuit_options.merge(tracing_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, original_args) ⇒ Object

taskWrap step to capture incoming arguments of a step.



46
47
48
49
50
# File 'lib/trailblazer/developer/trace.rb', line 46

def capture_args(wrap_config, original_args)
  original_args = capture_for(wrap_config[:task], *original_args)

  return wrap_config, original_args
end

.capture_for(task, ctx, flow, activity:, **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.



69
70
71
72
73
74
75
76
77
# File 'lib/trailblazer/developer/trace.rb', line 69

def capture_for(task, (ctx, flow), activity:, **circuit_options)
  flow[:stack].indent!

  flow[:stack] << Entity::Input.new(
    task, activity, [ctx, ctx.inspect]
  ).freeze

  return [ctx, flow], circuit_options.merge(activity: activity)
end

.capture_return(wrap_config, original_args) ⇒ Object

taskWrap step to capture outgoing arguments from a step.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/trailblazer/developer/trace.rb', line 53

def capture_return(wrap_config, original_args)
  (original_options, original_flow_options, _) = original_args[0]

  original_flow_options[:stack] << Entity::Output.new(
    wrap_config[:task], {}, wrap_config[:return_signal]
  ).freeze

  original_flow_options[:stack].unindent!


  return wrap_config, original_args
end

.merge_planObject

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



38
39
40
41
42
43
# File 'lib/trailblazer/developer/trace.rb', line 38

def merge_plan
  Activity::TaskWrap::Pipeline::Merge.new(
    [Activity::TaskWrap::Pipeline.method(:insert_before), "task_wrap.call_task", ["task_wrap.capture_args",   Trace.method(:capture_args)]],
    [Activity::TaskWrap::Pipeline.method(:append),        nil,                   ["task_wrap.capture_return", Trace.method(:capture_return)]],
  )
end