Skip to content

Commit

Permalink
feat: parse log severity number/array of object attributes + setup li…
Browse files Browse the repository at this point in the history
…nting
  • Loading branch information
wrn14897 committed Jul 20, 2024
1 parent 6472326 commit 479ee8f
Show file tree
Hide file tree
Showing 26 changed files with 326 additions and 107 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-humans-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/node-opentelemetry': minor
---

feat: use getSeverityNumber and parseLogAttributes for logger
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
"prepare": "husky install"
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
"**/*.{ts,tsx}": [
"prettier --write --ignore-unknown",
"eslint --fix"
],
"**/*.{json,yml}": [
"prettier --write --ignore-unknown"
]
},
"devDependencies": {
"@changesets/cli": "^2.26.1",
Expand All @@ -29,13 +35,15 @@
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-simple-import-sort": "^12.1.1",
"husky": "^8.0.3",
"jest": "^29.5.0",
"lint-staged": "^13.2.2",
"nx": "^15.9.2",
"prettier": "^2.8.7",
"prettier": "^3.3.3",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
Expand Down
15 changes: 7 additions & 8 deletions packages/node-opentelemetry/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
"sourceType": "module",
"ecmaVersion": 2020
},
"plugins": ["@typescript-eslint", "jest"],
"plugins": ["@typescript-eslint", "jest", "prettier", "simple-import-sort"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended",
"prettier"
"plugin:prettier/recommended"
],
"rules": {
// The following rule is enabled only to supplement the inline suppression
// examples, and because it is not a recommended rule, you should either
// disable it, or understand what it enforces.
// https://typescript-eslint.io/rules/explicit-function-return-type/
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/ban-ts-comment": "warn",
"@typescript-eslint/no-this-alias": "warn"
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/no-this-alias": "warn",
"prettier/prettier": "error",
"simple-import-sort/exports": "error",
"simple-import-sort/imports": "error"
}
}
15 changes: 0 additions & 15 deletions packages/node-opentelemetry/__tests__/otel.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import process from 'process';
import fs from 'fs';
import process from 'process';
import { promisify } from 'util';

const realpath = promisify(fs.realpath);
Expand Down
30 changes: 19 additions & 11 deletions packages/node-opentelemetry/examples/dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,30 @@ app.get('/instruments', async (req, res) => {
});

app.get('/logs', async (req, res) => {
console.debug({
const nestedObj = {
headers: req.headers,
method: req.method,
url: req.url,
query: req.query,
nested: [
{
foo: 'bar',
},
],
nested2: {
nested3: {
foo: 'bar',
},
},
};
console.error({
message: 'Console 🍕',
...nestedObj,
});
console.error('BANG !!!');
console.log('Console 🍕');
logger.info({
message: 'Winston 🍕',
headers: req.headers,
method: req.method,
url: req.url,
logger.info('Winston 🍕', nestedObj);
pinoLogger.info({
message: 'Pino 🍕',
...nestedObj,
});
pinoLogger.info('Pino 🍕');

bunyanLogger.info('Bunyan 🍕');

console.log(await sendGetRequest());
Expand Down
1 change: 1 addition & 0 deletions packages/node-opentelemetry/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config: Config = {
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.(m)?js$': '$1',
},
modulePathIgnorePatterns: ['<rootDir>/build/'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(m)?ts$',
coverageDirectory: 'coverage',
collectCoverageFrom: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { Context, ROOT_CONTEXT } from '@opentelemetry/api';
import { AsyncLocalStorage } from 'async_hooks';

import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager';

type MutableContextStore = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import winston from 'winston';

import { getWinstonTransport } from '../src/logger';
import { getWinstonTransport } from '../logger';

const MAX_LEVEL = 'info';

Expand Down
30 changes: 30 additions & 0 deletions packages/node-opentelemetry/src/__tests__/otel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { init, initSDK, shutdown } from '../otel';

describe('otel', () => {
it('can sucessively shutdown without initialization', () => {
shutdown();
shutdown();
});

it('should be able to initialize the SDK with initSDK', async () => {
initSDK({
apiKey: 'blabla',
advancedNetworkCapture: true,
consoleCapture: true,
});

await new Promise((resolve) => setTimeout(resolve, 1000));

shutdown();
});

it('should be able to initialize the SDK with init', async () => {
init({
apiKey: 'blabla',
});

await new Promise((resolve) => setTimeout(resolve, 1000));

shutdown();
});
});
2 changes: 1 addition & 1 deletion packages/node-opentelemetry/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core';
import { defaultServiceName } from '@opentelemetry/resources';
import { getEnvWithoutDefaults, getEnv } from '@opentelemetry/core';

import { stringToBoolean } from './utils';

Expand Down
4 changes: 2 additions & 2 deletions packages/node-opentelemetry/src/gcp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { context, SpanKind, trace } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SpanKind, context, trace } from '@opentelemetry/api';

import { SDKConfig, initSDK } from './otel';
import { name as PKG_NAME, version as PKG_VERSION } from '../package.json';
import { initSDK, SDKConfig } from './otel';

export const registerGCPCloudFunctionEventHandler = (
handler: (event: any) => Promise<void>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PassThrough, Readable } from 'stream';

import { _parseConsoleArgs } from '../src/instrumentations/console';
import { _parseConsoleArgs } from '../console';
import {
getShouldRecordBody,
interceptReadableStream,
splitCommaSeparatedStrings,
} from '../src/instrumentations/http';
} from '../http';

describe('instrumentations', () => {
describe('console', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/node-opentelemetry/src/instrumentations/console.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import isObject from 'lodash.isobject';
import isPlainObject from 'lodash.isplainobject';
import opentelemetry, { Attributes } from '@opentelemetry/api';
import {
InstrumentationBase,
InstrumentationConfig,
InstrumentationNodeModuleDefinition,
} from '@opentelemetry/instrumentation';
import isObject from 'lodash.isobject';
import isPlainObject from 'lodash.isplainobject';

import { MutableAsyncLocalStorageContextManager } from '../MutableAsyncLocalStorageContextManager';
import { Logger, LoggerOptions } from '../otel-logger';
import { parseWinstonLog } from '../otel-logger/winston';
import { MutableAsyncLocalStorageContextManager } from '../MutableAsyncLocalStorageContextManager';

const PACKAGE_NAME = '@hyperdx/instrumentation-console';
const PACKAGE_VERSION = '0.1.0';
Expand Down
7 changes: 3 additions & 4 deletions packages/node-opentelemetry/src/instrumentations/http.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { diag, Span } from '@opentelemetry/api';
import { headerCapture } from '@opentelemetry/instrumentation-http';
import * as http from 'http';
import zlib from 'zlib';
import { PassThrough, Readable } from 'stream';

import { Span, diag } from '@opentelemetry/api';
import { headerCapture } from '@opentelemetry/instrumentation-http';
import zlib from 'zlib';

const SENSITIVE_DATA_SUBSTITUTE = '[Filtered]';
// https://github.com/getsentry/sentry-python/blob/1.18.0/sentry_sdk/scrubber.py#L17
Expand Down
5 changes: 2 additions & 3 deletions packages/node-opentelemetry/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
DEFAULT_HDX_NODE_BETA_MODE,
DEFAULT_SERVICE_NAME,
} from './constants';
import * as HyperDXPino from './otel-logger/pino';
import HyperDXWinston from './otel-logger/winston';

import type { HyperDXPinoOptions } from './otel-logger/pino';
import * as HyperDXPino from './otel-logger/pino';
import type { HyperDXWinstonOptions } from './otel-logger/winston';
import HyperDXWinston from './otel-logger/winston';

type WinstonTransportOptions = Omit<
HyperDXWinstonOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`otel-logger getSeverityNumber 1`] = `
{
"alert": 22,
"debug": 5,
"emerg": 23,
"error": 17,
"info": 9,
"verbose": 6,
"warn": 13,
"warning": 13,
}
`;

exports[`otel-logger parseLogAttributes 1`] = `
{
"a": 1,
"b": "2",
"c": "[{"d":3}]",
"e": [
1,
2,
3,
],
"f": [
"a",
"b",
"c",
],
"g": {
"h": "i",
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getSeverityNumber, parseLogAttributes } from '..';

describe('otel-logger', () => {
it('getSeverityNumber', () => {
expect({
alert: getSeverityNumber('alert'),
debug: getSeverityNumber('debug'),
emerg: getSeverityNumber('emerg'),
error: getSeverityNumber('error'),
info: getSeverityNumber('info'),
verbose: getSeverityNumber('verbose'),
warn: getSeverityNumber('warn'),
warning: getSeverityNumber('warning'),
}).toMatchSnapshot();
});

it('parseLogAttributes', () => {
expect(
parseLogAttributes({
a: 1,
b: '2',
c: [{ d: 3 }],
e: [1, 2, 3],
f: ['a', 'b', 'c'],
g: {
h: 'i',
},
}),
).toMatchSnapshot();
});
});
Loading

0 comments on commit 479ee8f

Please sign in to comment.