Class: Multiplayer::ResponseMiddleware

Inherits:
Middleware
  • Object
show all
Defined in:
lib/multiplayer/middleware/response_middleware.rb

Instance Attribute Summary

Attributes inherited from Middleware

#headers_to_mask, #mask_deb_span_payload, #max_payload_size, #schemify_doc_span_payload

Instance Method Summary collapse

Methods inherited from Middleware

#initialize

Constructor Details

This class inherits a constructor from Multiplayer::Middleware

Instance Method Details

#call(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/multiplayer/middleware/response_middleware.rb', line 3

def call(env)
  status, headers, response = @app.call(env)

  begin
    current_span = OpenTelemetry::Trace.current_span
    trace_id = current_span.context.trace_id.unpack1("H*")

    headers["x-trace-id"] = trace_id

    masked_headers = mask_headers(headers, @headers_to_mask)
    response_body = (extract_response_body(response)).dup

    if trace_id.start_with?(MULTIPLAYER_TRACE_DEBUG_PREFIX) && @mask_deb_span_payload
      response_body = mask_body(response_body)
    elsif trace_id.start_with?(MULTIPLAYER_TRACE_DOC_PREFIX) || @schemify_doc_span_payload
      response_body = schemify(response_body)
    elsif !response_body.is_a?(String)
      response_body = JSON.generate(response_body)
    end
    response_body = truncate_if_needed(response_body)

    current_span.set_attribute(ATTR_MULTIPLAYER_HTTP_RESPONSE_HEADERS, masked_headers.to_json)
    current_span.set_attribute(ATTR_MULTIPLAYER_HTTP_RESPONSE_BODY, response_body.is_a?(String) ? response_body : response_body.to_json)
    [ status, headers, response ]
  rescue
    [ status, headers, response ]
  end
end