Sha256: 6062b85e50e4653310e9418782f8d549d2fa6c50cdbe780c1c8ba132bc795032

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

# Re-open sinatra middleware to customize middleware to call our Hypertrace specific capture
class OpenTelemetry::Instrumentation::Sinatra::Middlewares::TracerMiddleware
  def call env
    extracted_context = OpenTelemetry.propagation.extract(
      env,
      getter: OpenTelemetry::Common::Propagation.rack_env_getter
    )
    OpenTelemetry::Context.with_current(extracted_context) do
      attrs = Hypertrace::Instrumentation::RackCompatible.extract_req_headers_as_attributes(env, 'http.request.header')
      if Hypertrace::Instrumentation::RackCompatible.should_record_env?(env)
        body_cap = Hypertrace::Instrumentation::DataCapture.capturable_body(env['rack.input'])
        attrs['http.request.body'] = body_cap if body_cap
      end

      tracer.in_span(
        env['PATH_INFO'],
        attributes: request_span_attributes(env: env).merge!(attrs),
        kind: :server
      ) do |span|
        @app.call(env).tap do |resp|
          response_attributes = Hypertrace::Instrumentation::RackCompatible.extract_response_headers_as_attributes(resp, 'http.response.header')
          if Hypertrace::Instrumentation::RackCompatible.should_record_rack_array?(resp)
            cap_body = Hypertrace::Instrumentation::RackCompatible.extract_response_body(resp)
            response_attributes['http.response.body'] = cap_body if cap_body
          end
          span.add_attributes(response_attributes)
          trace_response(span, env, resp)
        end
      end
    end
  end

  def request_span_attributes(env:)
    attributes = {
      'http.method' => env['REQUEST_METHOD'],
      'http.host' => env['HTTP_HOST'] || 'unknown',
      'http.scheme' => env['rack.url_scheme'],
      'http.target' => env['QUERY_STRING'].empty? ? env['PATH_INFO'] : "#{env['PATH_INFO']}?#{env['QUERY_STRING']}"
    }

    attributes['http.user_agent'] = env['HTTP_USER_AGENT'] if env['HTTP_USER_AGENT']
    attributes
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hypertrace-agent-0.3.0 lib/hypertrace/instrumentation/sinatra.rb
hypertrace-agent-0.2.2 lib/hypertrace/instrumentation/sinatra.rb
hypertrace-agent-0.2.1 lib/hypertrace/instrumentation/sinatra.rb
hypertrace-agent-0.1.2 lib/hypertrace/instrumentation/sinatra.rb
hypertrace-agent-0.1.1 lib/hypertrace/instrumentation/sinatra.rb
hypertrace-agent-0.1.0 lib/hypertrace/instrumentation/sinatra.rb