forked from open-telemetry/opentelemetry-js-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumentation-bunyan): add log sending to Logs Bridge API
This extends the Bunyan instrumentation to automatically add a Bunyan stream to created loggers that will send log records to the Logs Bridge API: https://opentelemetry.io/docs/specs/otel/logs/bridge-api/ Now that the instrumentation supports separate "injection" of fields and "bridging" of log records functionality, this also adds two boolean options to disable those independently: `enableInjection` and `enableLogsBridge`. This also updates the instrumentation to work with ES module usage. Closes: open-telemetry#1559
- Loading branch information
Showing
11 changed files
with
747 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
80 changes: 80 additions & 0 deletions
80
plugins/node/opentelemetry-instrumentation-bunyan/examples/README.md
This file contains 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,80 @@ | ||
This is a small example app, "app.js", that shows using the | ||
[Bunyan](https://github.com/trentm/node-bunyan) logger with OpenTelemetry. See | ||
[the OpenTelemetry Bunyan instrumentation README](../) for full details. | ||
|
||
# Usage | ||
|
||
``` | ||
npm install | ||
node -r ./telemetry.js app.js | ||
``` | ||
|
||
# Overview | ||
|
||
"telemetry.js" sets up the OpenTelemetry SDK to write OTel tracing spans and | ||
log records to the *console* for simplicity. In a real setup you would | ||
configure exporters to send to remote observability apps for viewing and | ||
analysis. | ||
|
||
An example run looks like this: | ||
|
||
``` | ||
$ node -r ./telemetry.js app.js | ||
{"name":"myapp","hostname":"amachine.local","pid":93017,"level":20,"msg":"hi","time":"2023-09-27T23:24:06.074Z","v":0} | ||
{ | ||
timestamp: 1695857046074000, | ||
traceId: undefined, | ||
spanId: undefined, | ||
traceFlags: undefined, | ||
severityText: 'debug', | ||
severityNumber: 5, | ||
body: 'hi', | ||
attributes: { name: 'myapp', hostname: 'amachine.local', pid: 93017 } | ||
} | ||
{"name":"myapp","hostname":"amachine.local","pid":93017,"level":30,"msg":"this record will have trace_id et al fields for the current span","time":"2023-09-27T23:24:06.079Z","v":0,"trace_id":"af5ce23816c4feabb713ee1dc84ef4d3","span_id":"5f50e181ec7bc621","trace_flags":"01"} | ||
{ | ||
timestamp: 1695857046079000, | ||
traceId: 'af5ce23816c4feabb713ee1dc84ef4d3', | ||
spanId: '5f50e181ec7bc621', | ||
traceFlags: 1, | ||
severityText: 'info', | ||
severityNumber: 9, | ||
body: 'this record will have trace_id et al fields for the current span', | ||
attributes: { | ||
name: 'myapp', | ||
hostname: 'amachine.local', | ||
pid: 93017, | ||
trace_id: 'af5ce23816c4feabb713ee1dc84ef4d3', | ||
span_id: '5f50e181ec7bc621', | ||
trace_flags: '01' | ||
} | ||
} | ||
{ | ||
traceId: 'af5ce23816c4feabb713ee1dc84ef4d3', | ||
parentId: undefined, | ||
traceState: undefined, | ||
name: 'manual-span', | ||
id: '5f50e181ec7bc621', | ||
kind: 0, | ||
timestamp: 1695857046079000, | ||
duration: 1407.196, | ||
attributes: {}, | ||
status: { code: 0 }, | ||
events: [], | ||
links: [] | ||
} | ||
``` | ||
|
||
There are two separate Bunyan instrumentation functionalities. The first is | ||
that Bunyan log records emitted in the context of a tracing span will include | ||
`trace_id` and `span_id` fields that can be used for correlating with collect | ||
tracing data. Line 3 shows an example of this. | ||
|
||
The second is that a [Bunyan | ||
stream](https://github.com/trentm/node-bunyan#streams) is automatically added | ||
to created Loggers that will send every log record to the OpenTelemetry Logs | ||
Bridge API. This means that if the OpenTelemetry SDK has been configured with | ||
a Logger Provider, it will receive them. (If the OpenTelemetry SDK is not | ||
configured for this, then the added Bunyan stream will be a no-op.) Lines 2 | ||
and 4 show a dumped OpenTelemetry Log Record. | ||
|
32 changes: 32 additions & 0 deletions
32
plugins/node/opentelemetry-instrumentation-bunyan/examples/app.js
This file contains 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,32 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// A small example that shows using OpenTelemetry's instrumentation of | ||
// Bunyan loggers. Usage: | ||
// node --require ./telemetry.js app.js | ||
|
||
const otel = require('@opentelemetry/api'); | ||
const bunyan = require('bunyan'); | ||
|
||
const log = bunyan.createLogger({name: 'myapp', level: 'debug'}); | ||
|
||
log.debug('hi'); | ||
|
||
const tracer = otel.trace.getTracer('example'); | ||
tracer.startActiveSpan('manual-span', span => { | ||
log.info('this record will have trace_id et al fields for the current span'); | ||
span.end(); | ||
}); |
33 changes: 33 additions & 0 deletions
33
plugins/node/opentelemetry-instrumentation-bunyan/examples/app.mjs
This file contains 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,33 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// The equivalent of "app.js", but showing usage with ESM code. | ||
// Usage: | ||
// node --require ./telemetry.js --experimental-loader ../node_modules/@opentelemetry/instrumentation/hook.mjs app.js | ||
|
||
import { trace } from '@opentelemetry/api'; | ||
import bunyan from 'bunyan'; | ||
|
||
const log = bunyan.createLogger({name: 'myapp', level: 'debug'}); | ||
|
||
log.debug('hi'); | ||
|
||
const tracer = trace.getTracer('example'); | ||
tracer.startActiveSpan('manual-span', span => { | ||
log.info('this record will have trace_id et al fields for the current span'); | ||
span.end(); | ||
}); | ||
|
25 changes: 25 additions & 0 deletions
25
plugins/node/opentelemetry-instrumentation-bunyan/examples/package.json
This file contains 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,25 @@ | ||
{ | ||
"name": "bunyan-example", | ||
"private": true, | ||
"version": "0.43.0", | ||
"description": "Example of Bunyan integration with OpenTelemetry", | ||
"scripts": { | ||
"start": "node -r ./telemetry.js app.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"author": "OpenTelemetry Authors", | ||
"license": "Apache-2.0", | ||
"// @opentelemetry/instrumentation-bunyan": "TODO: change to a ver when there is a next release", | ||
"dependencies": { | ||
"@opentelemetry/api": "^1.6.0", | ||
"@opentelemetry/instrumentation-bunyan": "../", | ||
"@opentelemetry/sdk-node": "^0.43.0", | ||
"bunyan": "^1.8.15" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
plugins/node/opentelemetry-instrumentation-bunyan/examples/telemetry.js
This file contains 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,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// Setup telemetry for tracing and Bunyan logging. | ||
// | ||
// This writes OTel spans and log records to the console for simplicity. In a | ||
// real setup you would configure exporters to send to remote observability apps | ||
// for viewing and analysis. | ||
|
||
const { NodeSDK, tracing, logs, api } = require('@opentelemetry/sdk-node'); | ||
// api.diag.setLogger(new api.DiagConsoleLogger(), api.DiagLogLevel.DEBUG); | ||
|
||
const { BunyanInstrumentation } = require('@opentelemetry/instrumentation-bunyan'); | ||
|
||
const sdk = new NodeSDK({ | ||
serviceName: 'bunyan-example', | ||
spanProcessor: new tracing.SimpleSpanProcessor(new tracing.ConsoleSpanExporter()), | ||
logRecordProcessor: new logs.SimpleLogRecordProcessor(new logs.ConsoleLogRecordExporter()), | ||
instrumentations: [ | ||
new BunyanInstrumentation(), | ||
] | ||
}) | ||
process.on("SIGTERM", () => { | ||
sdk | ||
.shutdown() | ||
.then( | ||
() => {}, | ||
(err) => console.log("warning: error shutting down OTel SDK", err) | ||
) | ||
.finally(() => process.exit(0)); | ||
}); | ||
sdk.start(); |
This file contains 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.