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

Merged
merged 10 commits into from
Apr 19, 2024
2 changes: 2 additions & 0 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ logger.info('foobar');
| 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`, it will use Winston Logger's current level when unspecified. |
| `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 @@ -64,6 +64,7 @@
"winston2": "npm:[email protected]"
},
"dependencies": {
"@opentelemetry/api-logs": "^0.49.1",
"@opentelemetry/instrumentation": "^0.49.1"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-winston#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { context, trace, isSpanContextValid, Span } from '@opentelemetry/api';
import { SeverityNumber } from '@opentelemetry/api-logs';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
Expand Down Expand Up @@ -206,7 +207,16 @@
let newTransports = Array.isArray(originalTransports)
? originalTransports
: [];
const openTelemetryTransport = new OpenTelemetryTransportV3();
let transportOptions = {};
if (config.logSeverity) {
const winstonLevel = winstonLevelFromSeverity(
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
config.logSeverity
);
transportOptions = { level: winstonLevel };
}
const openTelemetryTransport = new OpenTelemetryTransportV3(
transportOptions
);
if (originalTransports && !Array.isArray(originalTransports)) {
newTransports = [originalTransports];
}
Expand Down Expand Up @@ -245,3 +255,20 @@
return record;
}
}

function winstonLevelFromSeverity(
severity: SeverityNumber
): string | undefined {
if (severity >= SeverityNumber.ERROR) {
return 'error';
} 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 'silly';
}
return;

Check warning on line 273 in plugins/node/opentelemetry-instrumentation-winston/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-winston/src/instrumentation.ts#L273

Added line #L273 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 WinstonInstrumentationConfig 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 @@ -383,4 +383,86 @@ describe('WinstonInstrumentation', () => {
}
});
});
describe('logSeverity config', () => {
beforeEach(() => {
instrumentation.setConfig({
disableLogSending: false,
});
memoryLogExporter.getFinishedLogRecords().length = 0; // clear
});

it('error severity', () => {
if (!isWinston2) {
instrumentation.setConfig({
disableLogSending: false,
logSeverity: SeverityNumber.ERROR,
});
initLogger();
logger.warn('warn');
logger.error('error');
const logRecords = memoryLogExporter.getFinishedLogRecords();
assert.strictEqual(logRecords.length, 1);
assert.strictEqual(logRecords[0].body, 'error');
}
});

it('warn severity', () => {
if (!isWinston2) {
instrumentation.setConfig({
disableLogSending: false,
logSeverity: SeverityNumber.WARN,
});
initLogger();
logger.info('info');
logger.warn('warn');
const logRecords = memoryLogExporter.getFinishedLogRecords();
assert.strictEqual(logRecords.length, 1);
assert.strictEqual(logRecords[0].body, 'warn');
}
});

it('info severity', () => {
if (!isWinston2) {
instrumentation.setConfig({
disableLogSending: false,
logSeverity: SeverityNumber.INFO,
});
initLogger();
logger.debug('debug');
logger.info('info');
const logRecords = memoryLogExporter.getFinishedLogRecords();
assert.strictEqual(logRecords.length, 1);
assert.strictEqual(logRecords[0].body, 'info');
}
});

it('debug severity', () => {
if (!isWinston2) {
instrumentation.setConfig({
disableLogSending: false,
logSeverity: SeverityNumber.DEBUG,
});
initLogger();
logger.silly('silly');
logger.debug('debug');
const logRecords = memoryLogExporter.getFinishedLogRecords();
assert.strictEqual(logRecords.length, 1);
assert.strictEqual(logRecords[0].body, 'debug');
}
});

it('trace severity', () => {
if (!isWinston2) {
instrumentation.setConfig({
disableLogSending: false,
logSeverity: SeverityNumber.TRACE,
});
initLogger();
logger.silly('silly');
const logRecords = memoryLogExporter.getFinishedLogRecords();
assert.strictEqual(logRecords.length, 1);
assert.strictEqual(logRecords[0].body, 'silly');
}
});
});
});
Loading