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-bunyan): Allow log level to be configured for log sending #1919

Merged
merged 9 commits into from
Mar 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Log injection can be disabled with the `disableLogCorrelation: true` option.
| Option | Type | Description |
| ----------------------- | ----------------- | ----------- |
| `disableLogSending` | `boolean` | Whether to disable [log sending](#log-sending). Default `false`. |
| `logSeverity` | `SeverityNumber` | Control severity level for [log sending](#log-sending). Default `SeverityNumber.UNSPECIFIED`. |
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
| `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. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import { VERSION } from './version';
import { OpenTelemetryBunyanStream } from './OpenTelemetryBunyanStream';
import type * as BunyanLogger from 'bunyan';
import { SeverityNumber } from '@opentelemetry/api-logs';

const DEFAULT_CONFIG: BunyanInstrumentationConfig = {
disableLogSending: false,
Expand Down Expand Up @@ -157,10 +158,15 @@
return;
}
this._diag.debug('Adding OpenTelemetryBunyanStream to logger');
let currentLevel = logger.level();
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
if (config.logSeverity) {
const bunyanLevel = bunyanLevelFromSeverity(config.logSeverity);
currentLevel = bunyanLevel || currentLevel;
}
logger.addStream({
type: 'raw',
stream: new OpenTelemetryBunyanStream(),
level: logger.level(),
level: currentLevel,
});
}

Expand All @@ -182,3 +188,20 @@
);
}
}

function bunyanLevelFromSeverity(severity: SeverityNumber): string | undefined {
if (severity >= SeverityNumber.FATAL) {
return 'fatal';

Check warning on line 194 in plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts#L194

Added line #L194 was not covered by tests
} else if (severity >= SeverityNumber.ERROR) {
return 'error';

Check warning on line 196 in plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts#L196

Added line #L196 was not covered by tests
} else if (severity >= SeverityNumber.WARN) {
return 'warn';
} else if (severity >= SeverityNumber.INFO) {
return 'info';
} else if (severity >= SeverityNumber.DEBUG) {
return 'debug';
} else if (severity >= SeverityNumber.TRACE) {
return 'trace';

Check warning on line 204 in plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts#L199-L204

Added lines #L199 - L204 were not covered by tests
}
return;

Check warning on line 206 in plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-bunyan/src/instrumentation.ts#L206

Added line #L206 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { Span } from '@opentelemetry/api';
import { SeverityNumber } from '@opentelemetry/api-logs';
import { InstrumentationConfig } from '@opentelemetry/instrumentation';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -28,6 +29,11 @@ export interface BunyanInstrumentationConfig extends InstrumentationConfig {
*/
disableLogSending?: boolean;

/**
* Control Log sending severity level, logs will be sent for specified severity and higher.
*/
logSeverity?: SeverityNumber;

/**
* Whether to disable the injection trace-context fields, and possibly other
* fields from `logHook()`, into log records for log correlation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ describe('BunyanInstrumentation', () => {
assert.strictEqual(rec.body, 'hi');
assert.strictEqual(rec.attributes.aProperty, 'bar');
});

it('log record level configuration', () => {
instrumentation.setConfig({ logSeverity: SeverityNumber.WARN });

// Changing `disableLogSending` only has an impact on Loggers created
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
// *after* it is set. So we cannot test with the `log` created in
// `beforeEach()` above.
log = Logger.createLogger({ name: 'test-logger-name', stream });

log.info('info log');
log.warn('warn log');
const logRecords = memExporter.getFinishedLogRecords();
// Only one log record match configured severity
assert.strictEqual(logRecords.length, 1);
assert.strictEqual(logRecords[0].body, 'warn log');
});
trentm marked this conversation as resolved.
Show resolved Hide resolved
});

describe('disabled instrumentation', () => {
Expand Down
Loading