-
Notifications
You must be signed in to change notification settings - Fork 31
feat: add @opentelemetry/instrumentation-console package for capturing console calls as OpenTelemetry logs
#98
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
Merged
david-luna
merged 23 commits into
open-telemetry:main
from
drdreo:ot-console-instrumentation
Apr 27, 2026
Merged
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3002ca9
feat: add `@opentelemetry/instrumentation-console` package for captur…
drdreo e92f1f3
chore: format
drdreo eab5e16
chore: remove open questions
drdreo 5ce4f9a
chore: trigger CLA verification
drdreo 43402e8
chore: trigger CLA verification
drdreo 1b992d6
Update packages/instrumentation-console/package.json
drdreo cf2687d
chore: clean up imports and fix linting
drdreo 5e9f1f2
Merge branch 'open-telemetry:main' into ot-console-instrumentation
drdreo e0c7941
chore: update linting from main
drdreo 3dede67
refactor: PR feedback
drdreo 9bbc0e7
chore: add instrumentation test app
drdreo f8a6bd7
Update packages/instrumentation-console/examples/package.json
drdreo 04b8c00
Merge branch 'open-telemetry:main' into ot-console-instrumentation
drdreo caf96de
refactor: log context improvements
drdreo 0c7b2e5
test: remove log spam in tests
drdreo d455a1d
refactor: remove trace parent context
drdreo 5e9cb98
chore: update readme
drdreo 20c0861
Merge branch 'main' into ot-console-instrumentation
martinkuba 636d32a
Merge remote-tracking branch 'upstream/main' into ot-console-instrume…
drdreo 331e74b
chore: move console instrumentation to new folder structure
drdreo 4f81b2a
Merge remote-tracking branch 'upstream/main' into ot-console-instrume…
drdreo d261cf0
feat: make log methods dynamically configurable
drdreo 35d05c7
Merge branch 'main' into ot-console-instrumentation
david-luna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # OpenTelemetry Console Instrumentation for Web | ||
|
|
||
| [![NPM Published Version][npm-img]][npm-url] | ||
| [![Apache License][license-image]][license-image] | ||
|
|
||
| This module provides automatic instrumentation for *console* methods (log, warn, error, info, debug) for Web applications, emitting them as OpenTelemetry logs which may be collected using the [`@opentelemetry/sdk-logs`](https://www.npmjs.com/package/@opentelemetry/sdk-logs) package. | ||
|
|
||
| Compatible with OpenTelemetry JS API and SDK `1.0+`. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @opentelemetry/instrumentation-console | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Initialize | ||
|
|
||
| ```typescript | ||
| import { logs } from '@opentelemetry/api-logs'; | ||
| import { | ||
| ConsoleLogRecordExporter, | ||
| LoggerProvider, | ||
| SimpleLogRecordProcessor, | ||
| } from '@opentelemetry/sdk-logs'; | ||
| import { ConsoleInstrumentation } from '@opentelemetry/instrumentation-console'; | ||
| import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
|
|
||
| const logProvider = new LoggerProvider({ | ||
| processors: [ | ||
| new SimpleLogRecordProcessor(new ConsoleLogRecordExporter()), | ||
| ], | ||
| }); | ||
| logs.setGlobalLoggerProvider(logProvider); | ||
|
|
||
| registerInstrumentations({ | ||
| instrumentations: [ | ||
| new ConsoleInstrumentation(), | ||
| ], | ||
| }); | ||
|
|
||
| // Now all console calls will be captured as OpenTelemetry logs | ||
| console.log('Hello, World!'); | ||
| console.error('Something went wrong!'); | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Option | Type | Default | Description | | ||
| |--------|------|---------|-------------| | ||
| | `logMethods` | `ConsoleMethod[]` | `['log', 'warn', 'error', 'info', 'debug']` | Console methods to instrument | | ||
| | `messageSerializer` | `(args: unknown[]) => string` | See below | Custom serializer for console arguments | | ||
|
|
||
| ### logMethods | ||
|
|
||
| Configure which console methods to instrument: | ||
|
|
||
| ```typescript | ||
| new ConsoleInstrumentation({ | ||
| logMethods: ['error', 'warn'], // Only capture errors and warnings | ||
| }); | ||
| ``` | ||
|
|
||
| ### messageSerializer | ||
|
|
||
| Provide a custom serializer for console arguments: | ||
|
|
||
| ```typescript | ||
| new ConsoleInstrumentation({ | ||
| messageSerializer: (args) => args.map(arg => String(arg)).join(' | '), | ||
| }); | ||
| ``` | ||
|
|
||
| The default serializer joins arguments as strings, with objects serialized via `JSON.stringify`. Circular references are handled gracefully by falling back to `String(arg)`. | ||
|
|
||
| ## Severity Mapping | ||
|
|
||
| Console methods are mapped to OpenTelemetry severity levels: | ||
|
|
||
| | Console Method | SeverityNumber | SeverityText | | ||
| |----------------|----------------|--------------| | ||
| | `debug` | DEBUG (5) | 'debug' | | ||
| | `log` | INFO (9) | 'log' | | ||
| | `info` | INFO (9) | 'info' | | ||
| | `warn` | WARN (13) | 'warn' | | ||
| | `error` | ERROR (17) | 'error' | | ||
|
|
||
| ## Semantic Conventions | ||
|
|
||
| This package emits logs with the following attributes: | ||
|
|
||
| | Attribute | Description | | ||
| |-----------|-------------| | ||
| | `browser.console.method` | The console method that was called (e.g., 'log', 'error') | | ||
|
|
||
| Event name: `browser.console` | ||
|
|
||
| ## Useful links | ||
|
|
||
| - For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
| - For more about OpenTelemetry Browser: <https://github.com/open-telemetry/opentelemetry-browser> | ||
| - For help or feedback on this project, join us in [GitHub Discussions][discussions-url] | ||
|
|
||
| ## License | ||
|
|
||
| Apache 2.0 - See [LICENSE][license-url] for more information. | ||
|
|
||
| [discussions-url]: https://github.com/open-telemetry/opentelemetry-browser/discussions/landing | ||
| [license-url]: https://github.com/open-telemetry/opentelemetry-browser/blob/main/LICENSE | ||
| [license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat | ||
| [npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-console | ||
| [npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-console.svg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "name": "@opentelemetry/instrumentation-console", | ||
| "version": "0.1.0", | ||
| "description": "OpenTelemetry instrumentation that captures console log/warn/error/info/debug calls and emits them as OpenTelemetry logs.", | ||
| "keywords": [ | ||
| "opentelemetry", | ||
| "browser", | ||
| "web", | ||
| "instrumentation", | ||
| "console" | ||
| ], | ||
| "homepage": "https://github.com/open-telemetry/opentelemetry-browser", | ||
| "bugs": "https://github.com/open-telemetry/opentelemetry-browser/issues", | ||
| "license": "Apache-2.0", | ||
| "author": "OpenTelemetry Authors", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/open-telemetry/opentelemetry-browser.git", | ||
| "directory": "packages/instrumentation-console" | ||
| }, | ||
| "type": "module", | ||
| "types": "./dist/index.d.ts", | ||
| "exports": { | ||
| ".": "./dist/index.js" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsdown", | ||
| "check-types": "tsc", | ||
| "test": "vitest run", | ||
| "test:watch": "vitest", | ||
| "test:coverage": "vitest --coverage" | ||
| }, | ||
| "peerDependencies": { | ||
| "@opentelemetry/api": "^1.0.0" | ||
| }, | ||
| "dependencies": { | ||
| "@opentelemetry/api-logs": "*", | ||
| "@opentelemetry/core": "*", | ||
| "@opentelemetry/instrumentation": "*" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export { ConsoleInstrumentation } from './instrumentation.ts'; | ||
| export type { | ||
| ConsoleInstrumentationConfig, | ||
| ConsoleMethod, | ||
| } from './types.ts'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.