templates/app/lib/plug_app/middleware/tracing_attributes.rb in hephaestus-0.6.2 vs templates/app/lib/plug_app/middleware/tracing_attributes.rb in hephaestus-0.6.3

- old
+ new

@@ -12,36 +12,35 @@ sig { returns(T.untyped) } attr_reader :app HTTP_REQUEST_BODY = "http.request.body" PLUG_APP_PATH_PREFIX = "/app/" + RACK_REQUEST_BODY = "rack.input" sig { params(app: T.untyped).void } def initialize(app) @app = T.let(app, T.untyped) @filterer = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) end sig { params(env: T.untyped).returns(T.untyped) } def call(env) - request = ActionDispatch::Request.new(env.dup) - OpenTelemetry::Trace.current_span.add_attributes({ OpenTelemetry::VERSION => PlugApp::Application::GIT_SHA, OpenTelemetry::SemanticConventions::Trace::HTTP_REQUEST_CONTENT_LENGTH => env["CONTENT_LENGTH"].to_i, - HTTP_REQUEST_BODY => filtered_params(request), + HTTP_REQUEST_BODY => filtered_params(env), }) app.call(env) end - def filtered_params(request) - params = request.params - if request.path.starts_with?(PLUG_APP_PATH_PREFIX) - {} - else - @filterer.filter(params) - end.to_json + def filtered_params(env) + body = env[RACK_REQUEST_BODY]&.read + return "{}" if body.blank? || body == "{}" + + @filterer.filter(JSON.parse(body)).to_json + ensure + env[RACK_REQUEST_BODY]&.try(:rewind) end end end end