lib/instana/instrumentation/rack.rb in instana-1.192.0 vs lib/instana/instrumentation/rack.rb in instana-1.192.1
- old
+ new
@@ -1,66 +1,26 @@
-# Note: We really only need "cgi/util" here but Ruby 2.4.1 has an issue:
-# https://bugs.ruby-lang.org/issues/13539
-require "cgi"
+require 'instana/instrumentation/instrumented_request'
module Instana
class Rack
def initialize(app)
@app = app
end
- def collect_kvs(env)
- kvs = {}
- kvs[:http] = {}
- kvs[:http][:method] = env['REQUEST_METHOD']
- kvs[:http][:url] = ::CGI.unescape(env['PATH_INFO'])
-
- if env.key?('HTTP_HOST')
- kvs[:http][:host] = env['HTTP_HOST']
- elsif env.key?('SERVER_NAME')
- kvs[:http][:host] = env['SERVER_NAME']
- end
-
- if ENV.key?('INSTANA_SERVICE_NAME')
- kvs[:service] = ENV['INSTANA_SERVICE_NAME']
- end
-
- if ::Instana.agent.extra_headers
- ::Instana.agent.extra_headers.each { |custom_header|
- # Headers are available in this format: HTTP_X_CAPTURE_THIS
- rack_header = 'HTTP_' + custom_header.upcase
- rack_header.tr!('-', '_')
-
- if env.key?(rack_header)
- unless kvs[:http].key?(:header)
- kvs[:http][:header] = {}
- end
- kvs[:http][:header][custom_header.to_sym] = env[rack_header]
- end
- }
- end
- return kvs
- end
-
def call(env)
- # Check incoming context
- incoming_context = {}
- if env.key?('HTTP_X_INSTANA_T')
- incoming_context[:trace_id] = ::Instana::Util.header_to_id(env['HTTP_X_INSTANA_T'])
- incoming_context[:span_id] = ::Instana::Util.header_to_id(env['HTTP_X_INSTANA_S']) if env.key?('HTTP_X_INSTANA_S')
- incoming_context[:level] = env['HTTP_X_INSTANA_L'] if env.key?('HTTP_X_INSTANA_L')
+ req = InstrumentedRequest.new(env)
+ return @app.call(env) if req.skip_trace?
+ kvs = {
+ http: req.request_tags,
+ service: ENV['INSTANA_SERVICE_NAME']
+ }.compact
- # Honor X-Instana-L
- if incoming_context[:level] and incoming_context[:level].length > 0
- if incoming_context[:level][0] == "0"
- return @app.call(env)
- end
- end
- end
+ current_span = ::Instana.tracer.log_start_or_continue(:rack, {}, req.incoming_context)
- kvs = collect_kvs(env)
-
- ::Instana.tracer.log_start_or_continue(:rack, {}, incoming_context)
+ unless req.correlation_data.empty?
+ current_span[:crid] = req.correlation_data[:id]
+ current_span[:crtp] = req.correlation_data[:type]
+ end
status, headers, response = @app.call(env)
if ::Instana.tracer.tracing?
# In case some previous middleware returned a string status, make sure that we're dealing with