Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 Sep 22, 2023
7d8a21f
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm Oct 3, 2023
03c1c16
markdown lint fixes
trentm Oct 3, 2023
3fbfc0c
markdown lint fixes
trentm Oct 3, 2023
57b6628
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm Oct 15, 2023
d5a92d1
catch up with recent core-deps update
trentm Oct 15, 2023
5b9aecb
some type tweaks suggested by David
trentm Oct 15, 2023
e3ed22c
more specific type
trentm Nov 8, 2023
bd507e2
use more self-explanatory code for mapping Bunyan level to OTel sever…
trentm Nov 8, 2023
45f2b1d
export OpenTelemetryBunyanStream for direct usage in Bunyan loggers w…
trentm Nov 8, 2023
7d1532f
.apply over .call suggestion
trentm Nov 8, 2023
ed9ddaf
consistency suggestion
trentm Nov 8, 2023
0f77516
suggestion to use the longer (perhaps clearer) logger var name
trentm Nov 8, 2023
aa81256
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm Nov 8, 2023
4c94dba
switch to false-by-default config vars to avoid surprises with undefi…
trentm Nov 8, 2023
21e3a55
document using OpenTelemetryBunyanStream without the instrumentation
trentm Nov 8, 2023
44d20bd
fix https://eslint.org/docs/latest/rules/prefer-spread lint error
trentm Nov 9, 2023
751f95a
drop options to OpenTelemetryBunyanStream constructor because YAGNI
trentm Nov 9, 2023
43ab672
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm Nov 9, 2023
2f0a89a
temporarily drop CI caching to test theory on unit-test (18) CI failure
trentm Nov 9, 2023
c283178
more CI debugging: restore cache, add some 'npm ls -a' to look into N…
trentm Nov 9, 2023
3638f72
elide Bunyan 'pid' and 'hostname' fields in OTel log record attributes
trentm Nov 9, 2023
303b6ab
update test for having elided 'pid' and 'hostname' fields
trentm Nov 9, 2023
018118e
CI debugging: ignore the 'npm ls -a' exit status, they shouldn't brea…
trentm Nov 9, 2023
284e4d6
fix lint and compile errors
trentm Nov 9, 2023
7fc0842
CI debugging: turn on diag DEBUG to test a theory
trentm Nov 10, 2023
54d2477
turn off diag in this example
trentm Nov 11, 2023
6de5de1
undo CI debugging changes
trentm Nov 11, 2023
bb9b2e4
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm Nov 11, 2023
af2a216
Merge branch 'main' into trentm/logs-bridge-bunyan
hectorhdzg Nov 20, 2023
f3c4d03
update deps to current releases and sync package-lock.json
trentm Nov 21, 2023
c740b76
disableInjection -> disableLogCorrelation
trentm Nov 22, 2023
f850a63
disableLogsBridge -> disableLogSending
trentm Nov 23, 2023
45cbb8b
Merge branch 'main' into trentm/logs-bridge-bunyan
trentm Nov 23, 2023
b597a92
Merge branch 'main' into trentm/logs-bridge-bunyan
hectorhdzg Nov 27, 2023
39b8507
correct the default instrumentation scope name (as discussed earlier)
trentm Nov 27, 2023
a149912
tests: fix test for intrumentationScope.name change in previous commit
trentm Nov 27, 2023
ca18034
fix lint
trentm Nov 27, 2023
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
Prev Previous commit
Next Next commit
disableInjection -> disableLogCorrelation
  • Loading branch information
trentm committed Nov 22, 2023
commit c740b767e2bb3ef3950d1e97b39f2034294dff05
26 changes: 15 additions & 11 deletions plugins/node/opentelemetry-instrumentation-bunyan/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 of the [`bunyan`](https://www.npmjs.com/package/bunyan) module to inject trace-context into Bunyan log records and to bridge Bunyan logging to the OpenTelemetry Logging SDK. 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.
This module provides automatic instrumentation of the [`bunyan`](https://www.npmjs.com/package/bunyan) module to inject trace-context into Bunyan log records (Log correlation) and to bridge Bunyan logging to the OpenTelemetry Logging SDK. 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 Down Expand Up @@ -44,8 +44,9 @@ logger.info('hi');
const tracer = api.trace.getTracer('example');
tracer.startActiveSpan('manual-span', span => {
logger.info('in a span');
// 2. Fields identifying the current span will be injected into log records:
// 2. Fields identifying the current span will be added to log records:
// {"name":"example",...,"msg":"in a span","trace_id":"d61b4e4af1032e0aae279d12f3ab0159","span_id":"d140da862204f2a2","trace_flags":"01"}
// This is called "log correlation".
})
```

Expand All @@ -57,10 +58,13 @@ If the OpenTelemetry SDK is not configured with a Logger provider, then this add

The logs bridge can be disabled with the `disableLogsBridge: true` option.

### Log injection
### Log correlation

Bunyan logger calls in the context of a tracing span will have fields indentifying
the span ([spec](https://opentelemetry.io/docs/specs/otel/compatibility/logging_trace_context/)):
Bunyan 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`
Expand All @@ -75,15 +79,15 @@ After adding these fields, the optional `logHook` is called to allow injecting a
```

When no span context is active or the span context is invalid, injection is skipped.
Log injection can be disabled with the `disableInjection: true` option.
Log injection can be disabled with the `disableLogCorrelation: true` option.

### Bunyan instrumentation options

| Option | Type | Description |
| ------------------- | ----------------- | ----------- |
| `disableLogsBridge` | `boolean` | Whether to disable [logs bridging](#logs-bridge). Default `false`. |
| `disableInjection` | `boolean` | Whether to disable [log injection](#log-injection). Default `false`. |
| `logHook` | `LogHookFunction` | An option hook to inject additional context to a log record after span context has been added. This requires `disableInjection` to be false. |
| Option | Type | Description |
| ----------------------- | ----------------- | ----------- |
| `disableLogsBridge` | `boolean` | Whether to disable [logs bridging](#logs-bridge). 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. |

### Using OpenTelemetryBunyanStream without instrumentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ $ node -r ./telemetry.js app.js
}
```

There are two separate Bunyan instrumentation functionalities. The first, called
"log injection", is that Bunyan log records emitted in the context of a tracing
There are two separate Bunyan instrumentation features. The first, called
"log correlation", 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 collected tracing data.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type * as BunyanLogger from 'bunyan';

const DEFAULT_CONFIG: BunyanInstrumentationConfig = {
disableLogsBridge: false,
disableInjection: false,
disableLogCorrelation: false,
};

export class BunyanInstrumentation extends InstrumentationBase<
Expand Down Expand Up @@ -114,7 +114,7 @@ export class BunyanInstrumentation extends InstrumentationBase<
const instrumentation = this;
return function patchedEmit(this: BunyanLogger, ...args: unknown[]) {
const config = instrumentation.getConfig();
if (!instrumentation.isEnabled() || config.disableInjection) {
if (!instrumentation.isEnabled() || config.disableLogCorrelation) {
return original.apply(this, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ export interface BunyanInstrumentationConfig extends InstrumentationConfig {
disableLogsBridge?: boolean;

/**
* Whether to disable the injection of data such as trace-context fields into
* log output.
* Whether to disable the injection trace-context fields, and possibly other
* fields from `logHook()`, into log records for log correlation.
* @default false
*/
disableInjection?: boolean;
disableLogCorrelation?: boolean;

/**
* A function that allows injecting additional fields in log records. It is
* called, as `logHook(span, record)`, for each log record emitted in a valid
* span context. It requires `disableInjection` to be false.
* span context. It requires `disableLogCorrelation` to be false.
*/
logHook?: LogHookFunction;
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ describe('BunyanInstrumentation', () => {
});
});

it('does not inject or call logHook if disableInjection=true', () => {
it('does not inject or call logHook if disableLogCorrelation=true', () => {
instrumentation.setConfig({
disableInjection: true,
disableLogCorrelation: true,
logHook: (_span, record) => {
record['resource.service.name'] = 'test-service';
},
Expand Down Expand Up @@ -278,7 +278,7 @@ describe('BunyanInstrumentation', () => {
log.info('foo');
assert.strictEqual(memExporter.getFinishedLogRecords().length, 0);

// Test log injection still works.
// Test log correlation still works.
sinon.assert.calledOnce(writeSpy);
const record = JSON.parse(writeSpy.firstCall.args[0].toString());
assert.strictEqual('foo', record['msg']);
Expand Down