|
| 1 | +# OpenTelemetry Web Exception Instrumentation |
| 2 | + |
| 3 | +[![NPM Published Version][npm-img]][npm-url] |
| 4 | +[![Apache License][license-image]][license-image-url] |
| 5 | + |
| 6 | +This module provides automatic instrumentation for capturing unhandled exceptions and promise rejections in web applications. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +npm install --save @opentelemetry/instrumentation-web-exception |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```typescript |
| 17 | +import { LoggerProvider } from '@opentelemetry/sdk-logs'; |
| 18 | +import { EventLoggerProvider, events } from '@opentelemetry/api-events'; |
| 19 | +import { WebExceptionInstrumentation } from '@opentelemetry/instrumentation-web-exception'; |
| 20 | +import { registerInstrumentations } from '@opentelemetry/instrumentation'; |
| 21 | + |
| 22 | +// Set up the logger provider and event logger |
| 23 | +const loggerProvider = new LoggerProvider(); |
| 24 | +const eventLoggerProvider = new EventLoggerProvider(loggerProvider); |
| 25 | +events.setGlobalEventLoggerProvider(eventLoggerProvider); |
| 26 | + |
| 27 | +// Register the instrumentation |
| 28 | +registerInstrumentations({ |
| 29 | + instrumentations: [ |
| 30 | + new WebExceptionInstrumentation({ |
| 31 | + // Optional: customize attributes added to error events |
| 32 | + applyCustomAttributes: (error) => ({ |
| 33 | + 'app.error.severity': error.name === 'ValidationError' ? 'warning' : 'error', |
| 34 | + 'custom.correlation.id': window.correlationId, |
| 35 | + }), |
| 36 | + }), |
| 37 | + ], |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## Configuration |
| 42 | + |
| 43 | +The instrumentation can be configured with the following options: |
| 44 | + |
| 45 | +| Option | Type | Description | |
| 46 | +| ------- | ---- | ----------- | |
| 47 | +| `enabled` | `boolean` | Whether to enable the instrumentation. Default: `true` | |
| 48 | +| `applyCustomAttributes` | `(error: Error) => Attributes` | Optional callback to add custom attributes to error events | |
| 49 | + |
| 50 | +## Features |
| 51 | + |
| 52 | +- Automatically captures unhandled exceptions |
| 53 | +- Captures unhandled promise rejections |
| 54 | +- Records error name, message, and stack trace using OpenTelemetry semantic conventions |
| 55 | +- Supports custom attributes through configuration |
| 56 | +- Integrates with OpenTelemetry Events API |
| 57 | + |
| 58 | +## Semantic Attributes |
| 59 | + |
| 60 | +The following semantic attributes are added to each error event: |
| 61 | + |
| 62 | +| Attribute | Type | Description | |
| 63 | +| --------- | ---- | ----------- | |
| 64 | +| `exception.type` | string | The error name or type | |
| 65 | +| `exception.message` | string | The error message | |
| 66 | +| `exception.stacktrace` | string | The error stack trace | |
| 67 | + |
| 68 | +## Example |
| 69 | + |
| 70 | +```typescript |
| 71 | +// Initialize the instrumentation |
| 72 | +const exceptionInstrumentation = new WebExceptionInstrumentation({ |
| 73 | + applyCustomAttributes: (error) => ({ |
| 74 | + 'error.category': error instanceof TypeError ? 'type_error' : 'runtime_error', |
| 75 | + 'app.version': '1.0.0', |
| 76 | + }), |
| 77 | +}); |
| 78 | + |
| 79 | +// The instrumentation will automatically capture unhandled errors |
| 80 | +throw new Error('Unhandled error'); |
| 81 | + |
| 82 | +// And unhandled promise rejections |
| 83 | +Promise.reject(new Error('Unhandled rejection')); |
| 84 | +``` |
| 85 | + |
| 86 | +## Contributing |
| 87 | + |
| 88 | +This instrumentation is maintained by the OpenTelemetry authors. We welcome contributions! |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +Apache 2.0 - See [LICENSE][license-url] for more information. |
| 93 | + |
| 94 | +[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-web-exception |
| 95 | +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-web-exception.svg |
| 96 | +[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE |
| 97 | +[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg |
| 98 | +[license-image-url]: https://img.shields.io/badge/license-Apache_2.0-green.svg |
0 commit comments