Skip to content

feat: add @opentelemetry/instrumentation-console package for capturing console calls as OpenTelemetry logs#98

Merged
david-luna merged 23 commits intoopen-telemetry:mainfrom
drdreo:ot-console-instrumentation
Apr 27, 2026
Merged

feat: add @opentelemetry/instrumentation-console package for capturing console calls as OpenTelemetry logs#98
david-luna merged 23 commits intoopen-telemetry:mainfrom
drdreo:ot-console-instrumentation

Conversation

@drdreo
Copy link
Copy Markdown
Contributor

@drdreo drdreo commented Dec 17, 2025

Which problem is this PR solving?

Users should be able to capture console logs and export them to OpenTelemetry. This PR adds a browser console method instrumentation which sends them as opentelemetry logs

Fixes #15

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Unit tests for console logs being sent to the in memory processor as well as the config options were verified in unit tests with edge case testing for the custom serializer failing.

  • Unit tests

Checklist:

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

OPEN QUESTIONS

what semantic attributes?

  • browser.console for event and browser.console.method for type

severity numberfor log vs info?

  • INFO = 9?

is the traceparent of the current active span enough?

  • from my testing that can get disconnected when the frontend was served by the backend and we could use the traceparent from meta tags

@drdreo drdreo requested a review from a team as a code owner December 17, 2025 21:41
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Dec 17, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Comment thread packages/instrumentation-console/package.json Outdated
drdreo and others added 2 commits December 18, 2025 12:59
Co-authored-by: Jared Freeze <overbalance@users.noreply.github.com>
@drdreo
Copy link
Copy Markdown
Contributor Author

drdreo commented Jan 2, 2026

@overbalance happy new year. I got back at this PR and fixed the linting error. May you reapprove the workflow please?

@overbalance
Copy link
Copy Markdown
Contributor

overbalance commented Jan 5, 2026

@drdreo happy new year! the license header rules changed on main. to repair please update from main and run npm run lint to add them back automatically. I'll run the instrumentation and continue my review.

@overbalance
Copy link
Copy Markdown
Contributor

overbalance commented Jan 5, 2026

and npm i to update the lockfile @drdreo

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.

Code looks good. Let me know if you have concerns about my suggestion.

One issue I ran into was an infinite loop if I use the recommended OTel example https://opentelemetry.io/docs/languages/js/getting-started/browser/#creating-an-exporter

Would you mind adding a section to the docs stating the conflict if you also use ConsoleSpanExporter during development?

Comment thread packages/instrumentation-console/src/utils.ts Outdated
Comment thread packages/instrumentation-console/src/instrumentation.ts Outdated
@drdreo
Copy link
Copy Markdown
Contributor Author

drdreo commented Jan 7, 2026

Hej, i've addressed your feedback. I added a simple vite test app at instrumentation-console/examples to verify the instrumentation - let me know if you like to have some example app or if i should remove it again.

Comment thread packages/instrumentation-console/examples/package.json Outdated
Co-authored-by: Jared Freeze <overbalance@users.noreply.github.com>
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.

Re-request me for review once the conversation with Martin resolves.

Comment thread packages/instrumentation-console/package.json Outdated
Comment thread packages/instrumentation-console/src/utils.ts Outdated
Comment thread packages/instrumentation/src/console/instrumentation.test.ts
Comment thread packages/instrumentation/src/console/instrumentation.test.ts
Comment thread packages/instrumentation-console/src/instrumentation.test.ts Outdated
Comment thread packages/instrumentation-console/src/instrumentation.ts Outdated
@drdreo
Copy link
Copy Markdown
Contributor Author

drdreo commented Jan 15, 2026

@martinkuba adressed your feedback. Thanks for the input.

@martinkuba
Copy link
Copy Markdown
Contributor

@drdreo We discussed the meta tag use case for passing trace context from the backend in last week's SIG meeting, and we decided to not include it for now. I have created this this issue #129 to discuss further. Can you please remove that part for now? That way we can merge this PR, and if we decide we want to include it later, we can add separately.

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.

@drdreo There was an issue in another instrumentation recently where repeatedly calling enable() would add listeners over and over. I think this code may be subject to that as well. Does this also need a guard?

@drdreo
Copy link
Copy Markdown
Contributor Author

drdreo commented Jan 28, 2026

@overbalance good point. I can add something like this, since the InstrumentationBase should have that isEnabled function but it doesnt exist when i tested it. You know why? Getting TypeError: this.isEnabled is not a function

 override enable(): void {
    if (this.isEnabled()) {
      return;
    }

Comment thread packages/instrumentation-console/README.md Outdated
Copy link
Copy Markdown
Contributor

@david-luna david-luna left a comment

Choose a reason for hiding this comment

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

Overall looks good. #175 moved all instrumentations in a single package I think this one should live there too.

Checklist

  • move the code into /packages/instrumentation/src/console
  • set the scope name to @opentelemetrry/browser-instrumentation/console

const methods = this._getLogMethods();
for (const method of methods) {
if (typeof console[method] === 'function') {
this._wrap(console, method, this._patchConsoleMethod(method));
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.

nit: Although it's unlikely that another instrumentation patches console methods there was a discussion recently since there is a potential incompatibility between instrumentations when they unpatch APIs.
The conclusion was that instrumentations should patch the API once and the patched functions should check for the enabled state of the instrumentation. ref: #204 (comment)

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.

I have moved the console instrumentation to the new folder structure and read through the unpatching discussion. So no unwrap on disable anymore, just is active false

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.

@drdreo thanks for doing it :)

I'd leverage this conversation to point to another topic/question which is if the configuration should be static or could change in time. In this instrumentation I see the console methods option is final and if the user excluded a method there is no way to change that unless the config is changed and the application reloaded.

IMHO if a instrumentation can observe changes in configuration and behave accordingly the user has more control on the telemetry generated and even can implement mechanisms to tune/config instrumentations without the need of a reload or re-deploy. A scenario that comes to mind is an app containing a bug which spits a ton of errors in the console (like in an endless loop). If the app is accessed my many users this error rate (logs sent to OTLP endpoint) could be multiplied by the # of users. In that case I wish I had a way to tell the instrumented apps to stop sending logs, at least of error severity.

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.

Worth mention with "dynamic config" we should have only one instance of the instrumentation for all the test and just adjust the configuration for the feature we want to test. I think having duplicated instances of the same instrumentation in the same web page is not desired.

Copy link
Copy Markdown
Contributor Author

@drdreo drdreo Apr 14, 2026

Choose a reason for hiding this comment

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

Hmm, i think we could easily add that to dynamically check the configured methods on call and just patch all from the start.
Want me to add it here or create a new feature PR?

Copy link
Copy Markdown
Contributor

@david-luna david-luna Apr 20, 2026

Choose a reason for hiding this comment

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

@drdreo

would you consider doing it for this PR? I'm okay if we do it in a follow up PR

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.

@david-luna i have added dynamic config for log methods in this PR :)

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.

Oops. I missed what folder this was in.

Comment thread package-lock.json
@david-luna david-luna merged commit aa4e658 into open-telemetry:main Apr 27, 2026
5 checks passed
@david-luna
Copy link
Copy Markdown
Contributor

@drdreo thank you for your contribution :)

@drdreo
Copy link
Copy Markdown
Contributor Author

drdreo commented Apr 27, 2026

@drdreo thank you for your contribution :)

@david-luna Thanks for having me :) glad to contribute

@overbalance
Copy link
Copy Markdown
Contributor

@drdreo Thanks again, this has been released. It would be great to add an entry to the readme inside the instrumentation folder.

@martinkuba
Copy link
Copy Markdown
Contributor

martinkuba commented Apr 30, 2026

It would be great to add an entry to the readme inside the instrumentation folder.

I have created #260, which is related

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.

Instrumentation to capture console logs as OTel logs

5 participants