-
Notifications
You must be signed in to change notification settings - Fork 24
Add output plugins (incl. OpenTelemetry support) #316
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e31923e
Add OpenTelemetry output plugin
christiand93 4979977
Allow explicit context for OpenTelemetry logs
florian-rhinow c656ea0
Add docs for output plugins (incl. OTel)
christiand93 a9f26d3
Update dependencies
christiand93 3c51262
Fix test setup
christiand93 4cebdca
Add code docs
christiand93 8c1b20a
Fix level-to-severity mapping
christiand93 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
There are no files selected for viewing
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| build/ | ||
| node_modules/ | ||
| coverage/ | ||
| build/ | ||
| .vscode/ | ||
| .nyc_output/ |
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,97 @@ | ||
| --- | ||
| layout: default | ||
| title: Output Plugins | ||
| parent: Advanced Usage | ||
| nav_order: 7 | ||
| permalink: /advanced-usage/output-plugins | ||
| --- | ||
|
|
||
| # Output Plugins | ||
|
|
||
| Output plugins define where and how log records are emitted. The library supports multiple active plugins simultaneously and ships with two built-in implementations. | ||
|
|
||
| ## Plugin Management | ||
|
|
||
| Output plugins can be managed at runtime using the following methods: | ||
|
|
||
| | Method | Description | | ||
| |---|---| | ||
| | `log.addOutputPlugin(plugin)` | Adds a plugin alongside any existing ones. | | ||
| | `log.setOutputPlugins(...plugins)` | Replaces all existing plugins with the given one(s). | | ||
| | `log.getOutputPlugins()` | Returns the list of currently registered plugins. | | ||
|
|
||
| ## StdoutOutputPlugin | ||
|
|
||
| The default plugin, registered automatically on startup. | ||
| It writes log records to stdout or a configured [sink function](/cf-nodejs-logging-support/advanced-usage/custom-sink-function). | ||
| It remains active unless explicitly replaced via `setOutputPlugins()`. | ||
|
|
||
| ## OpenTelemetryLogsOutputPlugin <span class="label label-yellow">Experimental</span> | ||
|
|
||
| > **Note:** This plugin relies on [`@opentelemetry/api-logs`](https://www.npmjs.com/package/@opentelemetry/api-logs), which is marked as experimental by the OpenTelemetry project. Therefore consider this plugin experimental as well and be prepared for potential breaking changes in future releases. | ||
|
|
||
| Available since version 8.1.0. Emits log records via the [OpenTelemetry Logs API](https://opentelemetry.io/docs/specs/otel/logs/). | ||
| Only message logs are forwarded, whereas request logs are not emitted. | ||
| It requires a configured OTel SDK with a `LoggerProvider` and appropriate exporters, either via the global OTel SDK (e.g. `@opentelemetry/sdk-node`) or passed explicitly to the constructor. | ||
| The plugin itself does not initialize any OTel SDK components. | ||
|
|
||
| ### Registering the Plugin | ||
|
|
||
| ```js | ||
| import log, { OpenTelemetryLogsOutputPlugin } from 'cf-nodejs-logging-support'; | ||
|
|
||
| // Keep stdout output and also emit via OTel: | ||
| log.addOutputPlugin(new OpenTelemetryLogsOutputPlugin()); | ||
|
|
||
| // Emit via OTel only: | ||
| log.setOutputPlugins(new OpenTelemetryLogsOutputPlugin()); | ||
| ``` | ||
|
|
||
| The constructor accepts the following optional parameters: | ||
|
|
||
| | Parameter | Type | Description | | ||
| |---|---|---| | ||
| | `loggerProvider` | `LoggerProvider` | OTel `LoggerProvider` to use. Defaults to the global provider. | | ||
| | `context` | `Context` or `(record) => Context | undefined` | OTel context to attach to emitted log records. Can be a static `Context` or a resolver function that receives the log record and returns a context. | | ||
|
|
||
| ### Including Fields as Attributes | ||
|
|
||
| By default, only custom fields are forwarded as OTel log attributes. This can be changed with `setIncludeFieldsAsAttributes()`: | ||
|
|
||
| ```js | ||
| import log, { OpenTelemetryLogsOutputPlugin, FieldInclusionMode } from 'cf-nodejs-logging-support'; | ||
|
|
||
| const plugin = new OpenTelemetryLogsOutputPlugin(); | ||
| plugin.setIncludeFieldsAsAttributes(FieldInclusionMode.AllFields); | ||
|
|
||
| log.addOutputPlugin(plugin); | ||
| ``` | ||
|
|
||
| | Mode | Description | | ||
| |---|---| | ||
| | `FieldInclusionMode.CustomFieldsOnly` | Only custom fields are added as attributes (default) | | ||
| | `FieldInclusionMode.AllFields` | All log record fields are added as attributes | | ||
| | `FieldInclusionMode.None` | No fields are added as attributes | | ||
|
|
||
| ### Exception Attributes | ||
|
|
||
| When logging an error, the plugin automatically maps error information to the standard OTel exception attributes: | ||
|
|
||
| | OTel Attribute | Source | | ||
| |---|---| | ||
| | `exception.type` | Error name | | ||
| | `exception.message` | Error message | | ||
| | `exception.stacktrace` | Stack trace | | ||
|
|
||
| ### Logging Level Mapping | ||
|
|
||
| The plugin maps the library's log levels to OTel severity levels as follows: | ||
|
|
||
| | Logging Level | OTel Severity Text | OTel Severity Number | | ||
| |---|---|---| | ||
| | `error` | `ERROR` | 17 | | ||
| | `warn` | `WARN` | 13 | | ||
| | `info` | `INFO` | 9 | | ||
| | `verbose` | `DEBUG2` | 6 | | ||
| | `debug` | `DEBUG` | 5 | | ||
| | `silly` | `TRACE` | 1 | | ||
|
christiand93 marked this conversation as resolved.
|
||
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
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
Oops, something went wrong.
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.