diff --git a/.changeset/few-chicken-prove.md b/.changeset/few-chicken-prove.md new file mode 100644 index 00000000..355ba41f --- /dev/null +++ b/.changeset/few-chicken-prove.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/node-logger': patch +--- + +style: reuse LOG_PREFIX diff --git a/.changeset/sixty-kiwis-ring.md b/.changeset/sixty-kiwis-ring.md new file mode 100644 index 00000000..178ad5e3 --- /dev/null +++ b/.changeset/sixty-kiwis-ring.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/node-opentelemetry': patch +--- + +feat: debug mode diff --git a/packages/node-logger/src/debug.ts b/packages/node-logger/src/debug.ts index d757f3b7..91b4d079 100644 --- a/packages/node-logger/src/debug.ts +++ b/packages/node-logger/src/debug.ts @@ -2,7 +2,7 @@ import debug from 'debug'; import { name as PKG_NAME, version as PKG_VERSION } from '../package.json'; -const LOG_PREFIX = `[${PKG_NAME} v${PKG_VERSION}]`; +export const LOG_PREFIX = `[${PKG_NAME} v${PKG_VERSION}]`; export default (message: string) => debug('hyperdx')(`${LOG_PREFIX} ${message}`); diff --git a/packages/node-logger/src/logger.ts b/packages/node-logger/src/logger.ts index 538a8242..bea605d3 100644 --- a/packages/node-logger/src/logger.ts +++ b/packages/node-logger/src/logger.ts @@ -5,9 +5,9 @@ import stripAnsi from 'strip-ansi'; import { isPlainObject, isString } from 'lodash'; import { ILogger, createLogger, jsonToString } from './_logger'; -import { name as PKG_NAME, version as PKG_VERSION } from '../package.json'; +import { LOG_PREFIX as _LOG_PREFIX } from './debug'; -const LOG_PREFIX = `⚠️ [${PKG_NAME} v${PKG_VERSION}]`; +const LOG_PREFIX = `⚠️ ${_LOG_PREFIX}`; // internal types export type HdxLog = { diff --git a/packages/node-opentelemetry/package.json b/packages/node-opentelemetry/package.json index 4a2d0f68..04cbba29 100644 --- a/packages/node-opentelemetry/package.json +++ b/packages/node-opentelemetry/package.json @@ -40,6 +40,7 @@ "@opentelemetry/sdk-metrics": "^1.15.0", "@opentelemetry/sdk-node": "^0.41.0", "@opentelemetry/semantic-conventions": "^1.15.0", + "debug": "^4.3.4", "lodash": "^4.17.21", "tslib": "^2.5.3" }, diff --git a/packages/node-opentelemetry/src/debug.ts b/packages/node-opentelemetry/src/debug.ts new file mode 100644 index 00000000..299729ff --- /dev/null +++ b/packages/node-opentelemetry/src/debug.ts @@ -0,0 +1,14 @@ +import debug from 'debug'; + +import { name as PKG_NAME, version as PKG_VERSION } from '../package.json'; + +const NAMESPACE = 'hyperdx'; + +const env = process.env; + +export const LOG_PREFIX = `[${PKG_NAME} v${PKG_VERSION}]`; + +export const HDX_DEBUG_MODE_ENABLED = env.DEBUG?.includes(NAMESPACE); + +export default (message: string) => + debug(NAMESPACE)(`${LOG_PREFIX} ${message}`); diff --git a/packages/node-opentelemetry/src/logger.ts b/packages/node-opentelemetry/src/logger.ts index 04590859..fb3de831 100644 --- a/packages/node-opentelemetry/src/logger.ts +++ b/packages/node-opentelemetry/src/logger.ts @@ -1,6 +1,8 @@ // @ts-ignore import { HyperDXWinston } from '@hyperdx/node-logger'; +import hdx from './debug'; + const env = process.env; const HYPERDX_API_KEY = (env.HYPERDX_API_KEY ?? @@ -8,12 +10,14 @@ const HYPERDX_API_KEY = (env.HYPERDX_API_KEY ?? const SERVICE_NAME = env.OTEL_SERVICE_NAME as string; -export const getWinsonTransport = (maxLevel = 'info') => - new HyperDXWinston({ +export const getWinsonTransport = (maxLevel = 'info') => { + hdx('Initializing winston transport'); + return new HyperDXWinston({ apiKey: HYPERDX_API_KEY, maxLevel, service: SERVICE_NAME, }); +}; export const getPinoTransport = (maxLevel = 'info') => ({ target: '@hyperdx/node-logger/build/src/pino', diff --git a/packages/node-opentelemetry/src/patch.ts b/packages/node-opentelemetry/src/patch.ts index 7bbe0764..576296f9 100644 --- a/packages/node-opentelemetry/src/patch.ts +++ b/packages/node-opentelemetry/src/patch.ts @@ -3,9 +3,7 @@ import _ from 'lodash'; // @ts-ignore import { Logger, parseWinstonLog } from '@hyperdx/node-logger/build/src/logger'; -import { name as PKG_NAME, version as PKG_VERSION } from '../package.json'; - -const LOG_PREFIX = `[${PKG_NAME} v${PKG_VERSION}]`; +import hdx from './debug'; const env = process.env; @@ -51,7 +49,7 @@ export const patchConsoleLog = () => { service: SERVICE_NAME, }); - console.warn(`${LOG_PREFIX} Capturing console logs...`); + hdx(`Capturing console logs...`); const _log = (level: string, ...args: any[]) => { const parsedLog = parseWinstonLog({ diff --git a/packages/node-opentelemetry/src/tracing.ts b/packages/node-opentelemetry/src/tracing.ts index 1f0a9fd3..c68a8d03 100644 --- a/packages/node-opentelemetry/src/tracing.ts +++ b/packages/node-opentelemetry/src/tracing.ts @@ -2,13 +2,21 @@ import { NodeSDK } from '@opentelemetry/sdk-node'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; +import hdx, { + HDX_DEBUG_MODE_ENABLED, + LOG_PREFIX as _LOG_PREFIX, +} from './debug'; import { patchConsoleLog } from './patch'; -import { name as PKG_NAME, version as PKG_VERSION } from '../package.json'; -const LOG_PREFIX = `[${PKG_NAME} v${PKG_VERSION}]`; +const LOG_PREFIX = `⚠️ ${_LOG_PREFIX}`; const env = process.env; +// enable otel debug mode if HDX_DEBUG_MODE_ENABLED is set +if (HDX_DEBUG_MODE_ENABLED) { + env.OTEL_LOG_LEVEL = 'debug'; +} + // set default otel env vars env.OTEL_EXPORTER_OTLP_ENDPOINT = env.OTEL_EXPORTER_OTLP_ENDPOINT ?? 'https://in-otel.hyperdx.io'; @@ -21,9 +29,11 @@ env.OTEL_TRACES_SAMPLER_ARG = env.OTEL_TRACES_SAMPLER_ARG ?? '1'; if (env.HYPERDX_API_KEY) { env.OTEL_EXPORTER_OTLP_HEADERS = `${env.OTEL_EXPORTER_OTLP_HEADERS},authorization=${env.HYPERDX_API_KEY}`; } else { - console.warn(`⚠️ ${LOG_PREFIX} HYPERDX_API_KEY is not set`); + console.warn(`${LOG_PREFIX} HYPERDX_API_KEY is not set`); } +hdx('Initializing opentelemetry SDK'); + const sdk = new NodeSDK({ traceExporter: new OTLPTraceExporter({ timeoutMillis: 60000, @@ -56,10 +66,11 @@ if (env.OTEL_EXPORTER_OTLP_ENDPOINT && env.OTEL_EXPORTER_OTLP_HEADERS) { 2, )})...`, ); + hdx('Starting opentelemetry SDK'); sdk.start(); } else { console.warn( - `⚠️ ${LOG_PREFIX} OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_HEADERS are not set, tracing is disabled`, + `${LOG_PREFIX} OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_HEADERS are not set, tracing is disabled`, ); } @@ -68,6 +79,7 @@ patchConsoleLog(); // Graceful shutdown process.on('SIGTERM', () => { + hdx('SIGTERM received, shutting down'); sdk .shutdown() .then(