Module: RackPatch::HeliosRackMiddleware

Defined in:
lib/helios/opentelemetry/sdk/patches/rack_patch.rb

Instance Method Summary collapse

Instance Method Details

#extract_request_attributes(env, res) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/helios/opentelemetry/sdk/patches/rack_patch.rb', line 18

def extract_request_attributes(env, res)
  # Request headers are attributes in the env hash that start with HTTP_
  request_headers = env.select { |k, _v| k.start_with? 'HTTP_' }.transform_keys { |k| k.sub(/^HTTP_/, '').downcase }
  semantic_attributes = Helios::OpenTelemetry::SemanticAttributes
  res[semantic_attributes::HTTP_REQUEST_HEADERS] = request_headers.to_json
  request_body = env['rack.input']&.read
  env['rack.input']&.rewind
  res[semantic_attributes::HTTP_REQUEST_BODY] = request_body if request_body&.length&.positive?
rescue StandardError => e
  ::OpenTelemetry.logger.debug("Error extracting rack request attributes: #{e}")
end

#extract_response_attributes(span, headers, response) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/helios/opentelemetry/sdk/patches/rack_patch.rb', line 30

def extract_response_attributes(span, headers, response)
  semantic_attributes = Helios::OpenTelemetry::SemanticAttributes
  span.set_attribute(semantic_attributes::HTTP_RESPONSE_HEADERS, headers.to_json)
  if response.respond_to?(:first) && response.first&.length&.positive?
    span.set_attribute(semantic_attributes::HTTP_RESPONSE_BODY, response.first)
  end
rescue StandardError => e
  ::OpenTelemetry.logger.debug("Error extracting rack response attributes: #{e}")
end

#request_span_attributes(env:) ⇒ Object



7
8
9
10
11
# File 'lib/helios/opentelemetry/sdk/patches/rack_patch.rb', line 7

def request_span_attributes(env:)
  res = super(env: env)
  extract_request_attributes(env, res)
  res
end

#set_attributes_after_request(span, status, headers, response) ⇒ Object



13
14
15
16
# File 'lib/helios/opentelemetry/sdk/patches/rack_patch.rb', line 13

def set_attributes_after_request(span, status, headers, response)
  extract_response_attributes(span, headers, response)
  super(span, status, headers, response)
end