Skip to content
Merged
Show file tree
Hide file tree
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 Dec 17, 2025
e92f1f3
chore: format
drdreo Dec 17, 2025
eab5e16
chore: remove open questions
drdreo Dec 17, 2025
5ce4f9a
chore: trigger CLA verification
drdreo Dec 17, 2025
43402e8
chore: trigger CLA verification
drdreo Dec 17, 2025
1b992d6
Update packages/instrumentation-console/package.json
drdreo Dec 18, 2025
cf2687d
chore: clean up imports and fix linting
drdreo Jan 2, 2026
5e9f1f2
Merge branch 'open-telemetry:main' into ot-console-instrumentation
drdreo Jan 6, 2026
e0c7941
chore: update linting from main
drdreo Jan 6, 2026
3dede67
refactor: PR feedback
drdreo Jan 7, 2026
9bbc0e7
chore: add instrumentation test app
drdreo Jan 7, 2026
f8a6bd7
Update packages/instrumentation-console/examples/package.json
drdreo Jan 9, 2026
04b8c00
Merge branch 'open-telemetry:main' into ot-console-instrumentation
drdreo Jan 15, 2026
caf96de
refactor: log context improvements
drdreo Jan 15, 2026
0c7b2e5
test: remove log spam in tests
drdreo Jan 15, 2026
d455a1d
refactor: remove trace parent context
drdreo Jan 22, 2026
5e9cb98
chore: update readme
drdreo Jan 25, 2026
20c0861
Merge branch 'main' into ot-console-instrumentation
martinkuba Jan 28, 2026
636d32a
Merge remote-tracking branch 'upstream/main' into ot-console-instrume…
drdreo Apr 13, 2026
331e74b
chore: move console instrumentation to new folder structure
drdreo Apr 13, 2026
4f81b2a
Merge remote-tracking branch 'upstream/main' into ot-console-instrume…
drdreo Apr 25, 2026
d261cf0
feat: make log methods dynamically configurable
drdreo Apr 25, 2026
35d05c7
Merge branch 'main' into ot-console-instrumentation
david-luna Apr 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions packages/instrumentation-console/README.md
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
47 changes: 47 additions & 0 deletions packages/instrumentation-console/package.json
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": "*"
Comment thread
drdreo marked this conversation as resolved.
Outdated
},
"publishConfig": {
"access": "public"
}
}
21 changes: 21 additions & 0 deletions packages/instrumentation-console/src/index.ts
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';
Loading