Skip to content

Commit

Permalink
feat: more robust winston support (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
harelmo-lumigo authored Dec 4, 2024
1 parent 758cef0 commit a4017d3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 49 deletions.
107 changes: 92 additions & 15 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@opentelemetry/sdk-trace-base": "1.9.1",
"@opentelemetry/sdk-trace-node": "1.9.1",
"@opentelemetry/semantic-conventions": "1.17.1",
"@opentelemetry/winston-transport": "^0.8.0",
"@prisma/instrumentation": "^4.14.0",
"deasync": "^0.1.30",
"opentelemetry-instrumentation-express": "0.39.1",
Expand Down
1 change: 0 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as https from 'https';

import { logger } from './logging';
import { sortify } from './tools/jsonSortify';
import path from 'path';

export const DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT = 2048;
export const DEFAULT_CONNECTION_TIMEOUT = 5000;
Expand Down
9 changes: 2 additions & 7 deletions test/instrumentations/winston/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
"version": "1.0.0",
"description": "",
"scripts": {
"start": "node -r @lumigo/opentelemetry winston_app.js"
"start": "node -r @lumigo/opentelemetry/sync winston_app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@lumigo/opentelemetry": "file:../../../../distro.tgz",
"@opentelemetry/winston-transport": "^0.2.0",
"winston": "^3.14.0"
}
"license": "ISC"
}
16 changes: 5 additions & 11 deletions test/instrumentations/winston/app/winston_app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const http = require('http');
const url = require('url');
const { init } = require("@lumigo/opentelemetry")
const winston = require('winston');

require('log-timestamp');
const winstonLogger = winston.createLogger({
transports: [new winston.transports.Console()],
})

const host = 'localhost';
let httpServer;
Expand All @@ -16,15 +18,7 @@ function respond(res, status, body) {
}

const requestListener = async function (req, res) {
await init;

const winston = require('winston');
const winstonLogger = winston.createLogger({
transports: [new winston.transports.Console()],
})

console.error(`Received request: ${req.method} ${req.url}`);

console.log(`Received request: ${req.method} ${req.url}`);
const requestUrl = url.parse(req.url, true);

switch (requestUrl.pathname) {
Expand Down
5 changes: 5 additions & 0 deletions test/instrumentations/winston/deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@lumigo/opentelemetry": "file:./distro.tgz"
}
}
35 changes: 20 additions & 15 deletions test/instrumentations/winston/winston.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import fs from 'fs';
import { join } from 'path';
import path from 'path';
import { itTest } from '../../integration/setup';
import { TestApp } from '../../utils/test-apps';
import { installPackage, reinstallPackages, uninstallPackage } from '../../utils/test-setup';
import { installPackage, reinstallPackages } from '../../utils/test-setup';
import { versionsToTest } from '../../utils/versions';
import { FakeEdge } from '../../utils/fake-edge';
import tmp from 'tmp';
import fs from 'fs-extra';

const INSTRUMENTATION_NAME = 'winston';
const LOGS_DIR = join(__dirname, 'logs');
const TEST_APP_DIR = join(__dirname, 'app');
const LOGS_DIR = path.join(__dirname, 'logs');

describe.each(versionsToTest(INSTRUMENTATION_NAME, INSTRUMENTATION_NAME))(
`Instrumentation tests for the ${INSTRUMENTATION_NAME} package`,
function (versionToTest) {
let testApp: TestApp;
const fakeEdge = new FakeEdge();
let targetAppDir: string;

beforeAll(async () => {
await fakeEdge.start();
await fs.mkdir(LOGS_DIR, { recursive: true });
});

reinstallPackages({ appDir: TEST_APP_DIR });
fs.mkdirSync(LOGS_DIR, { recursive: true });
beforeEach(async () => {
// copy the entire test project-root to a temp folder, to isolate dependencies
const sourceFolder = path.join(__dirname);
const targetTestProjectDir = tmp.dirSync({ keep: process.env.KEEP_TEMP_TEST_FOLDERS == "true" }).name;
targetAppDir = path.join(targetTestProjectDir, "app");
await fs.copy(sourceFolder, targetTestProjectDir),
await fs.copy("distro.tgz", path.join(targetTestProjectDir, "deps", "distro.tgz"));
reinstallPackages({ appDir: path.join(targetTestProjectDir, "deps") });
installPackage({
appDir: TEST_APP_DIR,
appDir: targetAppDir,
packageName: INSTRUMENTATION_NAME,
packageVersion: versionToTest,
});
Expand All @@ -38,11 +47,6 @@ describe.each(versionsToTest(INSTRUMENTATION_NAME, INSTRUMENTATION_NAME))(

afterAll(async () => {
await fakeEdge.stop();
uninstallPackage({
appDir: TEST_APP_DIR,
packageName: INSTRUMENTATION_NAME,
packageVersion: versionToTest,
});
});

itTest(
Expand All @@ -53,21 +57,22 @@ describe.each(versionsToTest(INSTRUMENTATION_NAME, INSTRUMENTATION_NAME))(
timeout: 30_000,
},
async function () {
testApp = new TestApp(TEST_APP_DIR, INSTRUMENTATION_NAME, {
testApp = new TestApp(targetAppDir, INSTRUMENTATION_NAME, {
logDumpPath: `${LOGS_DIR}/${INSTRUMENTATION_NAME}.${INSTRUMENTATION_NAME}-logs@${versionToTest}.json`,
env: {
LUMIGO_ENABLE_LOGS: 'true',
LUMIGO_SECRET_MASKING_REGEX: '[".*sekret.*"]',
LUMIGO_LOGS_ENDPOINT: fakeEdge.logsUrl,
LUMIGO_ENDPOINT: fakeEdge.tracesUrl,
LUMIGO_TRACER_TOKEN: 't_123456789',
NODE_PATH: path.join(targetAppDir, '..', "deps", "node_modules"),
},
});

await writeLogLine('Hello Winston!');
await writeLogLine({ a: 1, sekret: 'this is secret!' });

await expect(fakeEdge.waitFor(({ logs }) => logs.length === 2, 'waiting for logs to be processed')).toBeTruthy();
await expect(fakeEdge.waitFor(({ logs }) => logs.length === 2, 'waiting for logs to be processed')).resolves.toBeTruthy();
await expect(fakeEdge.waitFor(({ resources }) => resources.length >= 1, 'waiting for resources to be processed')).resolves.toBeTruthy();

expect(fakeEdge.resources[0].attributes).toIncludeAllMembers([
Expand Down

0 comments on commit a4017d3

Please sign in to comment.