Skip to content

Commit

Permalink
Merge d9ff29a into bdfbcd4
Browse files Browse the repository at this point in the history
  • Loading branch information
rauno56 authored Nov 30, 2021
2 parents bdfbcd4 + d9ff29a commit cddaf86
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
5 changes: 5 additions & 0 deletions plugins/node/opentelemetry-instrumentation-winston/.tav.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
winston:
# a sample from supported versions
- versions: "3.3.3 || 3.2.1 || 2.4.5 || 2.4.4 || 2.1.1"
commands: npm run test

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"repository": "open-telemetry/opentelemetry-js-contrib",
"scripts": {
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test-all-versions": "tav",
"tdd": "npm run test -- --watch-extensions ts --watch",
"clean": "rimraf build/*",
"lint": "eslint . --ext .ts",
Expand Down Expand Up @@ -60,6 +61,7 @@
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "11.1.2",
"test-all-versions": "^5.0.1",
"ts-mocha": "8.0.0",
"typescript": "4.3.5",
"winston": "3.3.3",
Expand All @@ -68,4 +70,4 @@
"dependencies": {
"@opentelemetry/instrumentation": "^0.26.0"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export type { Winston2LogMethod };
export type Winston2LoggerModule = {
Logger: Winston2Logger & { prototype: { log: Winston2LogMethod } };
};
export type { Winston2Logger };
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { Writable } from 'stream';
import type * as Winston from 'winston';
import type { Winston3Logger } from '../src/types';
import type { Winston2Logger, Winston3Logger } from '../src/types';
import { WinstonInstrumentation } from '../src';

const memoryExporter = new InMemorySpanExporter();
Expand All @@ -38,12 +37,37 @@ context.setGlobalContextManager(new AsyncHooksContextManager());
const kMessage = 'log-message';

describe('WinstonInstrumentation', () => {
let logger: Winston3Logger;
let stream;
let logger: Winston3Logger | Winston2Logger;
let writeSpy: sinon.SinonSpy;
let winston: typeof Winston;
let instrumentation: WinstonInstrumentation;

function initLogger() {
const winston = require('winston');
const stream = new Writable();
stream._write = () => {};
writeSpy = sinon.spy(stream, 'write');

if (winston['createLogger']) {
// winston 3.x
logger = winston.createLogger({
transports: [
new winston.transports.Stream({
stream,
}),
],
});
} else if (winston['Logger']) {
// winston 2.x
logger = new winston.Logger({
transports: [
new winston.transports.File({
stream,
}),
],
});
}
}

function testInjection(span: Span) {
logger.info(kMessage);
sinon.assert.calledOnce(writeSpy);
Expand All @@ -70,22 +94,20 @@ describe('WinstonInstrumentation', () => {
before(() => {
instrumentation = new WinstonInstrumentation();
instrumentation.enable();
winston = require('winston');
assert.ok(isWrapped(winston.createLogger()['write']));
});

describe('enabled instrumentation', () => {
beforeEach(() => {
stream = new Writable();
stream._write = () => {};
writeSpy = sinon.spy(stream, 'write');
logger = winston.createLogger({
transports: [
new winston.transports.Stream({
stream,
}),
],
});
beforeEach(initLogger);

it('wraps write', () => {
if ('write' in logger) {
// winston 3.x
assert.ok(isWrapped(logger['write']));
} else {
// winston 2.x
// winston 3.x also has "log", so the order for the checks has to be this
assert.ok(isWrapped(logger['log']));
}
});

it('injects span context to records', () => {
Expand Down Expand Up @@ -144,18 +166,7 @@ describe('WinstonInstrumentation', () => {
instrumentation.enable();
});

beforeEach(() => {
stream = new Writable();
stream._write = () => {};
writeSpy = sinon.spy(stream, 'write');
logger = winston.createLogger({
transports: [
new winston.transports.Stream({
stream,
}),
],
});
});
beforeEach(initLogger);

it('does not inject span context', () => {
const span = tracer.startSpan('abc');
Expand Down

0 comments on commit cddaf86

Please sign in to comment.