Method: OneApm::Agent::CrossAppTracing.finish_trace

Defined in:
lib/one_apm/agent/cross_app/cross_app_tracing.rb

.finish_trace(state, t0, segment, request, response) ⇒ Object

Finish tracing the HTTP request that started at t0 with the information in response and the given http connection.

The request must conform to the same interface described in the documentation for start_trace.

The response must respond to the following methods:

  • [](key) - Reads response headers.

  • to_hash - Converts response headers to a Hash



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/one_apm/agent/cross_app/cross_app_tracing.rb', line 95

def finish_trace(state, t0, segment, request, response)
  unless t0
    OneApm::Manager.logger.error("HTTP request trace finished without start time. This is probably an agent bug.")
    return
  end

  t1 = Time.now
  duration = t1.to_f - t0.to_f

  begin
    if request
      # Figure out which metrics we need to report based on the request and response
      # The last (most-specific) one is scoped.
      metrics = metrics_for(request, response)
      scoped_metric = metrics.pop

      stats_engine.record_scoped_and_unscoped_metrics(state, scoped_metric, metrics, duration, nil, &cross_app_run_time_block(response, duration))
     
      # If we don't have segment, something failed during start_trace so
      # the current segment isn't the HTTP call it should have been.
      if segment
        segment.name = scoped_metric
        add_transaction_trace_parameters(request, response)
      end
    end
  ensure
    # If we have a segment, always pop the traced method stack to avoid
    # an inconsistent state, which prevents tracing of whole transaction.
    if segment
      stack = state.traced_method_stack
      stack.pop_frame(state, segment, scoped_metric, t1)
    end
  end
rescue OneApm::Agent::CrossAppTracing::Error => err
  OneApm::Manager.logger.debug "while cross app tracing", err
rescue => err
  OneApm::Manager.logger.error "Uncaught exception while finishing an HTTP request trace", err
end