Method: TraceView::API::Tracing#start_trace

Defined in:
lib/traceview/api/tracing.rb

#start_trace(layer, xtrace = nil, opts = {}) ⇒ Object

Public: Trace a given block of code which can start a trace depending on configuration and probability. Detect any exceptions thrown by the block and report errors.

When start_trace returns control to the calling context, the oboe context will be cleared.

layer - The layer the block of code belongs to. opts - A hash containing key/value pairs that will be reported along with the first event of this layer (optional).

Example

def handle_request(request, response)
  # ... code that modifies request and response ...
end

def handle_request_with_oboe(request, response)
  result, xtrace = start_trace('rails', request['X-Trace']) do
    handle_request(request, response)
  end
  result
rescue Exception => e
  xtrace = e.xtrace
ensure
  response['X-trace'] = xtrace
end

Returns a list of length two, the first element of which is the result of the block, and the second element of which is the oboe context that was set when the block completed execution.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/traceview/api/tracing.rb', line 77

def start_trace(layer, xtrace = nil, opts = {})
  log_start(layer, xtrace, opts)
  begin
    result = yield
  rescue Exception => e
    log_exception(layer, e)
    e.instance_variable_set(:@xtrace, log_end(layer))
    raise
  end
  xtrace = log_end(layer)

  [result, xtrace]
end