Class: Instana::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/instana/instrumentation/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



9
10
11
# File 'lib/instana/instrumentation/rack.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/instana/instrumentation/rack.rb', line 13

def call(env)
  req = InstrumentedRequest.new(env)
  kvs = {
    http: req.request_tags
  }.reject { |_, v| v.nil? }

  parent_context = extract_trace_context(req.incoming_context)

  span = OpenTelemetry::Trace.non_recording_span(parent_context) if parent_context
  parent_context = Trace.context_with_span(span) if parent_context

  current_span = ::Instana.tracer.start_span(:rack, attributes: {}, with_parent: parent_context)
  trace_ctx = OpenTelemetry::Trace.context_with_span(current_span)
  @trace_token = OpenTelemetry::Context.attach(trace_ctx)
  status, headers, response = @app.call(env)

  trace_context = process_span_tags(req, current_span, kvs, status, env) if ::Instana.tracer.tracing?
  merge_response_headers(kvs, headers)
  [status, headers, response]
rescue Exception => e
  current_span.record_exception(e) if ::Instana.tracer.tracing?
  raise
ensure
  finalize_trace(current_span, kvs, headers, trace_context) if ::Instana.tracer.tracing?
end