Skip to content
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

feat(instrumentation-winston): add log sending to Logs API #1837

Merged
merged 62 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
638c36d
Add recordLog emit
hectorhdzg Nov 30, 2023
d461556
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Nov 30, 2023
4ec2196
Readme lint
hectorhdzg Nov 30, 2023
20f58ab
Merge branch 'hectorhdzg/winstonlogs' of https://github.com/hectorhdz…
hectorhdzg Nov 30, 2023
01286b5
Update
hectorhdzg Dec 2, 2023
adc8f7a
Lint
hectorhdzg Dec 2, 2023
7d24291
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Dec 2, 2023
fe2bdbb
Update
hectorhdzg Dec 8, 2023
1d4b91a
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Dec 8, 2023
e76321b
Update
hectorhdzg Dec 8, 2023
d80c76a
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Dec 8, 2023
683c578
Lint
hectorhdzg Dec 8, 2023
ffa9f9b
Tests
hectorhdzg Dec 8, 2023
441f72a
Added transports
hectorhdzg Dec 29, 2023
6731e97
Add log appenders(Winston transports)
hectorhdzg Jan 3, 2024
1c7ac82
Merge remote-tracking branch 'upstream/main' into hectorhdzg/winstonlogs
hectorhdzg Jan 3, 2024
c662cd0
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Jan 3, 2024
47db601
npm i
hectorhdzg Jan 3, 2024
9d92764
Lint
hectorhdzg Jan 3, 2024
6595727
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Jan 3, 2024
4c6e813
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Jan 12, 2024
4dc6dca
Updating dependencies and readme
hectorhdzg Jan 23, 2024
ff81990
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Jan 23, 2024
3800b1c
Update
hectorhdzg Jan 31, 2024
54b7156
Merge remote-tracking branch 'upstream/main' into hectorhdzg/winstonlogs
hectorhdzg Jan 31, 2024
8c730a5
npm i
hectorhdzg Jan 31, 2024
13fce4d
Update dependencies
hectorhdzg Jan 31, 2024
175a020
Update
hectorhdzg Feb 1, 2024
ca07f95
Update readme
hectorhdzg Feb 1, 2024
d4a84c4
Readme lint
hectorhdzg Feb 1, 2024
7399017
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Feb 6, 2024
03b8ce7
Update
hectorhdzg Feb 6, 2024
2d7704c
lint
hectorhdzg Feb 6, 2024
091bddf
Update release manifest
hectorhdzg Feb 6, 2024
c447a6e
Addressing comments
hectorhdzg Feb 8, 2024
b57803c
npm i
hectorhdzg Feb 8, 2024
574ac19
remove inspect break
hectorhdzg Feb 8, 2024
e74650c
Addressing comments
hectorhdzg Feb 9, 2024
6247337
Update
hectorhdzg Feb 9, 2024
3fa81c0
Update packages/winston-transport/README.md
hectorhdzg Feb 10, 2024
3b43b31
Update plugins/node/opentelemetry-instrumentation-winston/README.md
hectorhdzg Feb 10, 2024
5089079
Update packages/winston-transport/test/openTelemetryTransport.test.ts
hectorhdzg Feb 10, 2024
69ae0ea
Update packages/winston-transport/README.md
hectorhdzg Feb 10, 2024
a2bb13e
Addressing comments
hectorhdzg Feb 10, 2024
fc39c78
Update component owners
hectorhdzg Feb 10, 2024
51564b8
Update tav
hectorhdzg Feb 10, 2024
2405492
Addressing comments
hectorhdzg Feb 12, 2024
c2dc4eb
Merge remote-tracking branch 'upstream/main' into hectorhdzg/winstonlogs
hectorhdzg Feb 26, 2024
95d8a6a
Update logger name
hectorhdzg Feb 26, 2024
fb41f93
Merge remote-tracking branch 'upstream/main' into hectorhdzg/winstonlogs
hectorhdzg Feb 28, 2024
ec9da89
Merge branch 'hectorhdzg/winstonlogs' of https://github.com/hectorhdz…
hectorhdzg Feb 28, 2024
1fe760d
Update
hectorhdzg Feb 28, 2024
615c702
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Feb 28, 2024
8cf9b57
Merge remote-tracking branch 'upstream/main' into hectorhdzg/winstonlogs
hectorhdzg Mar 7, 2024
064cba7
Update logs api dependency
hectorhdzg Mar 7, 2024
9e9e7d1
Merge remote-tracking branch 'upstream/main' into hectorhdzg/winstonlogs
hectorhdzg Mar 11, 2024
fae82c9
Update
hectorhdzg Mar 11, 2024
1750b3e
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Mar 11, 2024
a36d4e8
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Mar 13, 2024
a5e1ad6
Addressing comments
hectorhdzg Mar 14, 2024
32fc1a2
Merge branch 'main' into hectorhdzg/winstonlogs
hectorhdzg Mar 14, 2024
5e0291e
Lint
hectorhdzg Mar 14, 2024
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
5 changes: 4 additions & 1 deletion package-lock.json

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

52 changes: 37 additions & 15 deletions plugins/node/opentelemetry-instrumentation-winston/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![NPM Published Version][npm-img]][npm-url]
[![Apache License][license-image]][license-image]

This module provides automatic instrumentation for injection of trace context for the [`winston`](https://www.npmjs.com/package/winston) module, which may be loaded using the [`@opentelemetry/sdk-trace-node`](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package and is included in the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle.
This module provides automatic instrumentation of the [`winston`](https://www.npmjs.com/package/winston) module to inject trace-context into Winston log records (log correlation) and to send Winston logging to the OpenTelemetry Logging SDK (log sending). It may be loaded using the [`@opentelemetry/sdk-trace-node`](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package and is included in the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle.

If total installation size is not constrained, it is recommended to use the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle with [@opentelemetry/sdk-node](`https://www.npmjs.com/package/@opentelemetry/sdk-node`) for the most seamless instrumentation experience.

Expand All @@ -18,23 +18,17 @@ npm install --save @opentelemetry/instrumentation-winston
## Usage

```js
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { NodeSDK, tracing, logs, api } = require('@opentelemetry/sdk-node');
const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
const sdk = new NodeSDK({
spanProcessor: new tracing.SimpleSpanProcessor(new tracing.ConsoleSpanExporter()),
logRecordProcessor: new logs.SimpleLogRecordProcessor(new logs.ConsoleLogRecordExporter()),
instrumentations: [
new WinstonInstrumentation({
// Optional hook to insert additional context to log metadata.
// Called after trace context is injected to metadata.
logHook: (span, record) => {
record['resource.service.name'] = provider.resource.attributes['service.name'];
},
// See below for Winston instrumentation options.
}),
// other instrumentations
],
});

hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -46,15 +40,43 @@ logger.info('foobar');
// {"message":"foobar","trace_id":"e21c7a95fff34e04f77c7bd518779621","span_id":"b7589a981fde09f4","trace_flags":"01", ...}
```

### Fields added to Winston metadata
### Log sending

Winston Logger will automatically send log records to the OpenTelemetry Logs SDK. The OpenTelemetry SDK can be configured to handle those records -- for example, sending them on to an OpenTelemetry collector for log archiving and processing. The example above shows a minimal configuration that emits OpenTelemetry log records to the console for debugging.
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

If the OpenTelemetry SDK is not configured with a Logger provider, then this will be a no-op.

For the current active span, the following fields are injected:
Log sending can be disabled with the `disableLogSending: true` option.

### Log correlation

Winston logger calls in the context of a tracing span will have fields
indentifying the span added to the log record. This allows
[correlating](https://opentelemetry.io/docs/specs/otel/logs/#log-correlation)
log records with tracing data. The added fields are
([spec](https://opentelemetry.io/docs/specs/otel/compatibility/logging_trace_context/)):

* `trace_id`
* `span_id`
* `trace_flags`

When no span context is active or the span context is invalid, injection is skipped.
After adding these fields, the optional `logHook` is called to allow injecting additional fields. For example:

```js
logHook: (span, record) => {
record['resource.service.name'] = provider.resource.attributes['service.name'];
}
```

Log injection can be disabled with the `disableLogCorrelation: true` option.

### Winston instrumentation options

| Option | Type | Description |
| ----------------------- | ----------------- | ----------- |
| `disableLogSending` | `boolean` | Whether to disable [log sending](#log-sending). Default `false`. |
| `disableLogCorrelation` | `boolean` | Whether to disable [log correlation](#log-correlation). Default `false`. |
| `logHook` | `LogHookFunction` | An option hook to inject additional context to a log record after trace-context has been added. This requires `disableLogCorrelation` to be false. |
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

### Supported versions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"access": "public"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0"
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/api-logs": "^0.45.1"
Copy link
Member

Choose a reason for hiding this comment

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

Since it is also installed as a regular dependency, I think we should not list it as peer dependency.
This is also what bunyan is currently doing.

Now that I think about it again, perhaps they should both use peer dependency to be consistent with "@opentelemetry/api" usage?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well I believe we should update Bunyan as well, I can create a different PR so people are aware specific changes are happening for that instrumentation, some issues in the readme that were brough up by @mhennoch should apply to bunyan as well

},
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
Expand All @@ -63,6 +64,7 @@
"winston2": "npm:[email protected]"
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@opentelemetry/api-logs": "^0.45.1",
"@opentelemetry/instrumentation": "^0.45.1"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-winston#readme"
Expand Down
Loading
Loading