Skip to content

refactor(instrumentation-fetch): replace instanceof Request checks#6250

Closed
js62789 wants to merge 6 commits intoopen-telemetry:mainfrom
js62789:main
Closed

refactor(instrumentation-fetch): replace instanceof Request checks#6250
js62789 wants to merge 6 commits intoopen-telemetry:mainfrom
js62789:main

Conversation

@js62789
Copy link
Copy Markdown

@js62789 js62789 commented Dec 23, 2025

Which problem is this PR solving?

Our team is using DataDome for bot detection. They have set us up with a custom Captcha script which overrides the global Request constructor. We're going to see if they can eliminate this code smell, but in the case where they can't, I believe it would be valuable to improve the Request detection with this duck-typing helper instead. This will prevent requests to incorrect URLs when a patched request instance is provided to fetch().

Short description of the changes

  • Use structural Request type guard instead of relying on global Request identity
  • Add test coverage for overwritten global Request constructor

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added a test with global Request overwritten
  • All tests passed with rpm run test

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added

@js62789 js62789 requested a review from a team as a code owner December 23, 2025 16:56
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Dec 23, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

namespace: '@opentelemetry/opentelemetry-instrumentation-fetch/utils',
});

export function isRequest(value: unknown): value is Request {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We may want to document that we duck-type for Request in this instrumentation as a result of this PR just to make it clear what the risk (even if minimal) of false positives.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've added some inline documentation. Do you think this change warrants an addition to the README as well?

Copy link
Copy Markdown
Contributor

@overbalance overbalance Mar 19, 2026

Choose a reason for hiding this comment

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

What do you think about an early exit for the common case?

if (value instanceOf Request) return true;

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions Bot added the stale label Feb 23, 2026
@pichlermarc
Copy link
Copy Markdown
Member

@js62789 are you still planning to work on this? Please keep in mind that the CLA has to be signed before we can accept any contributions.

@js62789
Copy link
Copy Markdown
Author

js62789 commented Mar 18, 2026

Yes, sorry about that. I forgot that this was open. Let me get this up to date.

@js62789 js62789 requested a review from a team as a code owner March 18, 2026 17:47
js62789 added 2 commits March 18, 2026 13:54
Resolved conflicts in instrumentation-fetch by keeping isRequest() helper
while incorporating upstream changes to header injection logic.
@overbalance
Copy link
Copy Markdown
Contributor

@js62789 After you resolve conflicts, request me again and I'll take another look.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.38%. Comparing base (36ce569) to head (b1e3eeb).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6250      +/-   ##
==========================================
- Coverage   95.75%   95.38%   -0.38%     
==========================================
  Files         375      337      -38     
  Lines       12725    11214    -1511     
  Branches     3013     2647     -366     
==========================================
- Hits        12185    10696    -1489     
+ Misses        540      518      -22     

see 44 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot removed the stale label Apr 13, 2026
# Conflicts:
#	experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts
@js62789
Copy link
Copy Markdown
Author

js62789 commented Apr 13, 2026

I've resolved the merge conflicts @overbalance

Copy link
Copy Markdown
Contributor

@overbalance overbalance left a comment

Choose a reason for hiding this comment

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

I left a comment about naming but it looks good. Thanks! Please let us know if it doesn't resolve your issue

FetchCustomAttributeFunction,
FetchInstrumentationConfig,
} from '../src';
import { isRequest as isRequestLike } from '../src/utils';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would stick with one name throughout. (isRequestLike would be my choice)

@overbalance
Copy link
Copy Markdown
Contributor

Make sure to run lint:fix and check out the failing test suites.

@js62789
Copy link
Copy Markdown
Author

js62789 commented Apr 14, 2026

@overbalance Our team was able to upgrade our implementation of DataDome's scripts so that we didn't need to include the one monkey-patching the global Request constructor. For that reason, this is no longer an issue for us. I'm happy to get this ready to merge if you think it's something that will be beneficial for others. What do you think?

@overbalance
Copy link
Copy Markdown
Contributor

@js62789 If that's the case, I recommend closing this without merging. If someone needs this in the future, we can revisit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants