-
Notifications
You must be signed in to change notification settings - Fork 616
feat(instrumentation-bunyan): add log sending to Logs Bridge API #1713
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
pichlermarc
merged 38 commits into
open-telemetry:main
from
trentm:trentm/logs-bridge-bunyan
Nov 28, 2023
Merged
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
59f11f3
feat(instrumentation-bunyan): add log sending to Logs Bridge API
trentm 7d8a21f
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm 03c1c16
markdown lint fixes
trentm 3fbfc0c
markdown lint fixes
trentm 57b6628
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm d5a92d1
catch up with recent core-deps update
trentm 5b9aecb
some type tweaks suggested by David
trentm e3ed22c
more specific type
trentm bd507e2
use more self-explanatory code for mapping Bunyan level to OTel sever…
trentm 45f2b1d
export OpenTelemetryBunyanStream for direct usage in Bunyan loggers w…
trentm 7d1532f
.apply over .call suggestion
trentm ed9ddaf
consistency suggestion
trentm 0f77516
suggestion to use the longer (perhaps clearer) logger var name
trentm aa81256
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm 4c94dba
switch to false-by-default config vars to avoid surprises with undefi…
trentm 21e3a55
document using OpenTelemetryBunyanStream without the instrumentation
trentm 44d20bd
fix https://eslint.org/docs/latest/rules/prefer-spread lint error
trentm 751f95a
drop options to OpenTelemetryBunyanStream constructor because YAGNI
trentm 43ab672
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm 2f0a89a
temporarily drop CI caching to test theory on unit-test (18) CI failure
trentm c283178
more CI debugging: restore cache, add some 'npm ls -a' to look into N…
trentm 3638f72
elide Bunyan 'pid' and 'hostname' fields in OTel log record attributes
trentm 303b6ab
update test for having elided 'pid' and 'hostname' fields
trentm 018118e
CI debugging: ignore the 'npm ls -a' exit status, they shouldn't brea…
trentm 284e4d6
fix lint and compile errors
trentm 7fc0842
CI debugging: turn on diag DEBUG to test a theory
trentm 54d2477
turn off diag in this example
trentm 6de5de1
undo CI debugging changes
trentm bb9b2e4
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm af2a216
Merge branch 'main' into trentm/logs-bridge-bunyan
hectorhdzg f3c4d03
update deps to current releases and sync package-lock.json
trentm c740b76
disableInjection -> disableLogCorrelation
trentm f850a63
disableLogsBridge -> disableLogSending
trentm 45cbb8b
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm b597a92
Merge branch 'main' into trentm/logs-bridge-bunyan
hectorhdzg 39b8507
correct the default instrumentation scope name (as discussed earlier)
trentm a149912
tests: fix test for intrumentationScope.name change in previous commit
trentm ca18034
fix lint
trentm 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
79 changes: 79 additions & 0 deletions
79
plugins/node/opentelemetry-instrumentation-bunyan/examples/README.md
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,79 @@ | ||
| 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 | ||
|
|
||
| ```bash | ||
| 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: | ||
|
|
||
| ```bash | ||
| $ 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 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,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 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,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 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,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://[email protected]/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", | ||
pichlermarc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "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 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,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 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.
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.