Skip to content

Conversation

@BenoitZugmeyer
Copy link

@BenoitZugmeyer BenoitZugmeyer commented Dec 19, 2025

Which problem is this PR solving?

The README.md advises to use the instrumentation like this:

registerInstrumentations({
  instrumentations: [
    new ExceptionInstrumentation({ ... }),
  ],
});

But this does not work because the function passed to addEventListener('error', ...) is the unbounded method, so accesses to this.getConfig() fails.

I know this sounds surprising, because the onError method was properly reaffected with the bounded method in the constructor. But actually, enable() is called by the base class, during super(), which is called before the onError is bound!

export class ExceptionInstrumentation extends InstrumentationBase<GlobalErrorsInstrumentationConfig> {
  constructor(config: GlobalErrorsInstrumentationConfig = {}) {
    super(PACKAGE_NAME, PACKAGE_VERSION, config); // calls this.enable()
    this.onError = this.onError.bind(this); // this is done too late!
  }

This wasn't caught in tests because in test, enable() is called two times, once when instantiating the instrumentation, and once explicitely when calling instr.enable(). This second call do register the correctly bounded method.

Short description of the changes

  • To work around this, this PR make sure the onError method is proprely bound in enable().
  • Adjust the tests to be closer to the documented setup instructions.

The README.md advises to use the instrumentation like this:

```
registerInstrumentations({
  instrumentations: [
    new ExceptionInstrumentation({ ... }),
  ],
});
```

But this does not work because the function passed to
`addEventListener('error', ...)` is the unbounded method, so accesses to
`this.getConfig()` failed.

I know this sounds surprising, because the `onError` method was properly
reaffected with the bounded method in the constructor. But actually,
`enable()` is called by the base class, during `super()`, which is
called *before the `onError` is bound*!

This wasn't caught in tests because in test, `enable()` is called two
times, once when instantiating the instrumentation, and once explicitely
when calling `instr.enable()`. This second call *do* register the
correctly bounded method.

To work around this, this PR make sure the `onError` method is proprely
bound in `enable()`.
Copy link
Contributor

@wolfgangcodes wolfgangcodes left a comment

Choose a reason for hiding this comment

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants