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

fix: Ensure opts.headers is defined in request instrumenting #1926

Merged
merged 4 commits into from Dec 21, 2023

Conversation

OuranosSkia
Copy link
Contributor

Description

This fixes a bug where if opts.headers is null, it is not handled by further down functions and causes a crash.

How to Test

Unfortunately, the situations in which this is reproduced are very specific to a flow that I have difficulty artificially reproducing. I am applying this fix as a technical code fix purely based on the way that it is crashing.

Related Issues

Fixes #1925

This fixes a bug where if `opts.headers` is `null`, which is not caught by further down functions and causes a crash.
@CLAassistant
Copy link

CLAassistant commented Dec 21, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@bizob2828 bizob2828 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, sorry for the troubles here.

@@ -138,6 +138,8 @@ function instrumentRequest(agent, opts, makeRequest, hostname, segment) {
const transaction = segment.transaction
const outboundHeaders = Object.create(null)

opts.headers = opts.headers || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please write a unit test for this?

Here's one that exhibits the issue.

  t.test('should not crash when req.headers is null', (t) => {
    const req = new events.EventEmitter()
    helper.runInTransaction(agent, function (transaction) {
      const path = '/asdf'
      const name = NAMES.EXTERNAL.PREFIX + HOSTNAME + ':' + PORT + path

      instrumentOutbound(agent, { headers: null, host: HOSTNAME, port: PORT }, makeFakeRequest)
      t.equal(transaction.trace.root.children[0].name, name)

      function makeFakeRequest() {
        req.path = '/asdf?a=b&another=yourself&thing&grownup=true'
        return req
      }
    })
    t.end()
  })

You should put it in /test/unit/instrumentation/http/outbound.test.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I had actually looked for a test to update, but was looking in /test/unit/instrumentation/core and didn't see one. I added the test and will @ you once I see things have passed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tweaked it slightly to be more specific. I didn't mean for you to copy/paste but use for inspiration. Anyways, we appreciate the fix 🙏🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry! Haha. Thanks for the tweak. I should have paid more attention to what you were proposing.

Copy link

codecov bot commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bd0a5dc) 96.98% compared to head (0857ad1) 96.98%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1926   +/-   ##
=======================================
  Coverage   96.98%   96.98%           
=======================================
  Files         212      212           
  Lines       40109    40111    +2     
=======================================
+ Hits        38899    38901    +2     
  Misses       1210     1210           
Flag Coverage Δ
integration-tests-16.x 78.74% <100.00%> (+<0.01%) ⬆️
integration-tests-18.x 79.01% <100.00%> (-0.01%) ⬇️
integration-tests-20.x 79.01% <100.00%> (-0.01%) ⬇️
unit-tests-16.x 91.03% <100.00%> (+<0.01%) ⬆️
unit-tests-18.x 91.02% <100.00%> (+<0.01%) ⬆️
unit-tests-20.x 91.02% <100.00%> (+<0.01%) ⬆️
versioned-tests-16.x 73.81% <100.00%> (-0.03%) ⬇️
versioned-tests-18.x 73.83% <100.00%> (-0.03%) ⬇️
versioned-tests-20.x 73.84% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@OuranosSkia OuranosSkia changed the title Ensure opts.headers is defined in request instrumenting fix: Ensure opts.headers is defined in request instrumenting Dec 21, 2023
@bizob2828 bizob2828 merged commit 7ea31a3 into newrelic:main Dec 21, 2023
22 checks passed
Node.js Engineering Board automation moved this from Needs PR Review to Done: Issues recently completed Dec 21, 2023
@OuranosSkia OuranosSkia deleted the patch-1 branch December 21, 2023 20:02
@github-actions github-actions bot mentioned this pull request Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Node.js Engineering Board
  
Done: Issues recently completed
Development

Successfully merging this pull request may close these issues.

Crash when instrumenting request with null headers
3 participants