Skip to content

⚗️ Add instrumentConstructor utility#4714

Open
bdibon wants to merge 20 commits into
mainfrom
boris.dibon/ws-1-instrument-method-constructor
Open

⚗️ Add instrumentConstructor utility#4714
bdibon wants to merge 20 commits into
mainfrom
boris.dibon/ws-1-instrument-method-constructor

Conversation

@bdibon

@bdibon bdibon commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Motivation

The WebSocket instrumentation added higher in this stack needs to wrap the WebSocket constructor, but instrumentMethod only supported instrumenting regular methods.

Changes

  • Add support for instrumenting constructors to instrumentMethod: handle new.target, call the original via Reflect.construct, and preserve the original prototype so instanceof checks against the instrumented global keep working.
  • Introduce MethodParameters / MethodReturnType type helpers that resolve the parameters/return type from either a call or a construct signature.

Test instructions

yarn test:unit --spec packages/core/src/tools/instrumentMethod.spec.ts

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

bdibon commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

@bdibon bdibon marked this pull request as ready for review June 3, 2026 11:36
@bdibon bdibon requested a review from a team as a code owner June 3, 2026 11:36
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jun 3, 2026

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 171.88 KiB 171.98 KiB +101 B +0.06%
Rum Profiler 7.88 KiB 7.88 KiB 0 B 0.00%
Rum Recorder 21.21 KiB 21.22 KiB +9 B +0.04%
Logs 54.34 KiB 54.43 KiB +89 B +0.16%
Rum Slim 129.72 KiB 129.80 KiB +89 B +0.07%
Worker 22.96 KiB 22.96 KiB 0 B 0.00%

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c445e3c783

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
@datadog-official

datadog-official Bot commented Jun 3, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 83.72%
Overall Coverage: 76.83% (+0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 8d89c48 | Docs | Datadog PR Page | Give us feedback!

@bdibon bdibon force-pushed the boris.dibon/ws-1-instrument-method-constructor branch from c445e3c to e2bdea2 Compare June 4, 2026 09:16

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2bdea2f62

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b52da5151

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f0b7a0fda

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
Comment thread packages/browser-core/src/tools/instrumentMethod.spec.ts
}

return replaceWithInstrumentation(target, name, original, (isStopped) => {
const instrumentation = function (this: TARGET): ConstructorInstanceOf<TARGET[NAME]> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

💬 suggestion:this is actually not TARGET here but since we don't use it, we could just remove it

Suggested change
const instrumentation = function (this: TARGET): ConstructorInstanceOf<TARGET[NAME]> {
const instrumentation = function (): ConstructorInstanceOf<TARGET[NAME]> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For the types, I've decided to rename TARGET to CONTAINER which is more accurate in our context (instrumenting global constructors).

I am using this when I replicate the native behavior for constructor called without new (i.e runtime throws an exception). I probably could have used undefined instead, but I preferred to be defensive and not to temper with what the caller of the instrumentation was doing. In this context, this is unknown.

Comment thread packages/browser-core/src/tools/instrumentMethod.ts Outdated
@bdibon bdibon force-pushed the boris.dibon/ws-1-instrument-method-constructor branch from 0f0b7a0 to 751858d Compare June 8, 2026 11:17

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 751858d195

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-core/src/tools/instrumentMethod.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 201cc19f39

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-core/src/tools/instrumentMethod.ts
@bdibon bdibon force-pushed the boris.dibon/ws-1-instrument-method-constructor branch from 201cc19 to 8d89c48 Compare June 8, 2026 15:12
}

interface InstrumentationCallbacks<RESULT> {
onPostCall: (callback: (result: RESULT) => void) => void

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.

After instrumentation, instance.constructor === window.WebSocket returns false because prototype.constructor still points to the original. I can't think of real-world code that checks this, so it's probably fine, but wanted to confirm it's a known acceptable limitation. What do you think?

@bdibon bdibon Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right for instances that have been constructed before instrumentation, I have documented it here.

@bdibon bdibon changed the title ✨ Add constructor support to instrumentMethod ✨ Add instrumentConstructor utility Jun 8, 2026
@bdibon bdibon changed the title ✨ Add instrumentConstructor utility ⚗️ Add instrumentConstructor utility Jun 8, 2026
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.

3 participants