Skip to content

Commit

Permalink
feat: debug mode (otel pkg) (#28)
Browse files Browse the repository at this point in the history
* feat: use debug

* style: reuse log prefix

* docs: add node-logger changeset

* feat: set OTEL_LOG_LEVEL if hdx debug is enabled

* style: cleanup

* docs: add changeset
  • Loading branch information
wrn14897 authored Jul 25, 2023
1 parent 90da807 commit 8863dab
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-chicken-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/node-logger': patch
---

style: reuse LOG_PREFIX
5 changes: 5 additions & 0 deletions .changeset/sixty-kiwis-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/node-opentelemetry': patch
---

feat: debug mode
2 changes: 1 addition & 1 deletion packages/node-logger/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
4 changes: 2 additions & 2 deletions packages/node-logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions packages/node-opentelemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
14 changes: 14 additions & 0 deletions packages/node-opentelemetry/src/debug.ts
Original file line number Diff line number Diff line change
@@ -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}`);
8 changes: 6 additions & 2 deletions packages/node-opentelemetry/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// @ts-ignore
import { HyperDXWinston } from '@hyperdx/node-logger';

import hdx from './debug';

const env = process.env;

const HYPERDX_API_KEY = (env.HYPERDX_API_KEY ??
env.OTEL_EXPORTER_OTLP_HEADERS?.split('=')[1]) as string;

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',
Expand Down
6 changes: 2 additions & 4 deletions packages/node-opentelemetry/src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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({
Expand Down
20 changes: 16 additions & 4 deletions packages/node-opentelemetry/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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`,
);
}

Expand All @@ -68,6 +79,7 @@ patchConsoleLog();

// Graceful shutdown
process.on('SIGTERM', () => {
hdx('SIGTERM received, shutting down');
sdk
.shutdown()
.then(
Expand Down

0 comments on commit 8863dab

Please sign in to comment.