Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to order the Rack middleware so that Rack::Deflater occurs afterwards for capture_response_content_length #2329

Closed
mbarany opened this issue Nov 21, 2023 · 3 comments
Labels
community To tag external issues and PRs submitted by the community

Comments

@mbarany
Copy link

mbarany commented Nov 21, 2023

We're using the Rack::Deflater rack middleware and we can't figure out how to order it to occur after the newrelic rack middleware. Rack::Deflater deletes the Content-Length header so that its no longer available in capture_response_content_length.

We are currently on newrelic_rpm version 9.1.0

@workato-integration
Copy link

@github-actions github-actions bot added the community To tag external issues and PRs submitted by the community label Nov 21, 2023
@hannahramadan
Copy link
Contributor

Hi @mbarany! Unfortunately, what you're looking to do isn't possible with the agent's default behavior. When the New Relic ruby agent instruments middleware, it doesn't insert its own middleware into the Rack middleware stack, but instead wraps the request as a whole. Since this is the agent's intended behavior, I'm going to remove the bug label.

That said, we've come up with two potential options that could work for you.

  1. Manually setting the content length yourself. If you have the content length available (perhaps in your own custom middleware), you can set it manually on the transaction using:
NewRelic::Agent::Tracer.current_transaction.response_content_length = length
  1. As mentioned, the default behavior of the agent is to wrap requests. But if disable_middleware_instrumentation is set to true, the agent will instead insert its own middleware. However, the agent middleware will always be inserted at the end of the Rack middleware stack. This is because the intent of this middleware is to capture the final state of the request. Due to the way the agent works, agent code may have to be altered to reorder the middlewares. If you're interested, we can try to help create a workaround that may give you what you're looking for. Disabling middleware instrumentation can limit the functionality of the agent, and a workaround we considered is outside the scope of our public API (and therefore potentially unstable if changes are made to this part of the agent in the future). So this might not be an ideal choice.

@hannahramadan hannahramadan removed the bug label Nov 22, 2023
@tannalynn
Copy link
Contributor

I'm going to close this issue, please let us know if you have any other questions!

@github-project-automation github-project-automation bot moved this from Triage to Code Complete/Done in Ruby Engineering Board Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community To tag external issues and PRs submitted by the community
Projects
Archived in project
Development

No branches or pull requests

3 participants