Skip to content

Commit

Permalink
feat: expose more logger config options (#29)
Browse files Browse the repository at this point in the history
* feat: add 'bufferSize' + 'sendIntervalMs' + 'timeout' options

* feat: pass more logger options (otel pkg)

* docs: add changeset
  • Loading branch information
wrn14897 authored Jul 29, 2023
1 parent e7663d9 commit 34d8d5e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/violet-rice-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/node-opentelemetry': patch
'@hyperdx/node-logger': patch
---

feat: expose 'bufferSize', 'sendIntervalMs' and 'timeout' logger options
16 changes: 16 additions & 0 deletions packages/node-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ const logger = winston.createLogger({
export default logger;
```

#### Options

- **apiKey** - Required. Your HyperDX ingestion API key.
- **service** - The name of the service.
- **sendIntervalMs** - Time in milliseconds to wait between retry attempts. Default: `2000` (2 sec)
- **bufferSize** - The maximum number of messages the logger will accumulate before sending them all as a bulk. Default: `100`.
- **timeout** - The read/write/connection timeout in milliseconds. Default: `30000`.

### Pino Transport

Create a new HyperDX Pino Transport and append it to your list of transports. Example:
Expand All @@ -77,6 +85,14 @@ const logger = pino(
export default logger;
```

#### Options

- **apiKey** - Required. Your HyperDX ingestion API key.
- **service** - The name of the service.
- **sendIntervalMs** - Time in milliseconds to wait between retry attempts. Default: `2000` (2 sec)
- **bufferSize** - The maximum number of messages the logger will accumulate before sending them all as a bulk. Default: `100`.
- **timeout** - The read/write/connection timeout in milliseconds. Default: `30000`.

### NestJS Custom Logger

(powered by [nest-winston](https://www.npmjs.com/package/nest-winston?activeTab=readme))
Expand Down
3 changes: 2 additions & 1 deletion packages/node-logger/src/_logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HyperdxLogger {
token,
host = 'in.hyperdx.io',
type = 'nodejs',
sendIntervalMs = 10 * 1000,
sendIntervalMs = 2 * 1000,
bufferSize = 100,
numberOfRetries = 3,
supressErrors = false,
Expand Down Expand Up @@ -150,6 +150,7 @@ class HyperdxLogger {
}

_timerSend() {
this._debug('Timer fired. Trying to wake up and send messages...');
if (this.messages.length > 0) {
this._debug(
`Woke up and saw ${this.messages.length} messages to send. Sending now...`,
Expand Down
19 changes: 18 additions & 1 deletion packages/node-logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ export const parseWinstonLog = (log: {

const DEFAULT_TIMEOUT = 30000;

export type LoggerOptions = {
apiKey: string;
baseUrl?: string;
bufferSize?: number;
sendIntervalMs?: number;
service?: string;
timeout?: number; // The read/write/connection timeout in milliseconds
};

export class Logger {
private readonly service: string;

Expand All @@ -68,11 +77,17 @@ export class Logger {
constructor({
apiKey,
baseUrl,
bufferSize,
sendIntervalMs,
service,
timeout,
}: {
apiKey: string;
baseUrl?: string;
bufferSize?: number;
sendIntervalMs?: number;
service?: string;
timeout?: number;
}) {
if (!apiKey) {
console.error(`${LOG_PREFIX} API key not found`);
Expand All @@ -96,10 +111,12 @@ export class Logger {

this.client = apiKey
? createLogger({
bufferSize,
host,
port,
protocol,
timeout: DEFAULT_TIMEOUT,
sendIntervalMs,
timeout: timeout ?? DEFAULT_TIMEOUT,
token: apiKey,
})
: null;
Expand Down
10 changes: 5 additions & 5 deletions packages/node-logger/src/pino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import build from 'pino-abstract-transport';
import hdx from './debug';
import { Logger, parsePinoLog } from './logger';

import type { LoggerOptions } from './logger';

// map pino level to text
const PINO_LEVELS = {
10: 'trace',
Expand All @@ -13,11 +15,9 @@ const PINO_LEVELS = {
60: 'fatal',
};

export default (opts: {
apiKey: string;
baseUrl?: string;
service?: string;
}) => {
export type HyperDXPinoOptions = LoggerOptions;

export default (opts: HyperDXPinoOptions) => {
try {
hdx('Initializing HyperDX pino transport...');
const logger = new Logger(opts);
Expand Down
25 changes: 18 additions & 7 deletions packages/node-logger/src/winston.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@ import Transport from 'winston-transport';
import hdx from './debug';
import { Logger, parseWinstonLog } from './logger';

import type { LoggerOptions } from './logger';

export type HyperDXWinstonOptions = LoggerOptions & {
maxLevel?: string;
};

export default class HyperDXWinston extends Transport {
private readonly logger: Logger;

constructor({
apiKey,
baseUrl,
bufferSize,
maxLevel,
sendIntervalMs,
service,
}: {
apiKey: string;
baseUrl?: string;
maxLevel?: string;
service?: string;
}) {
timeout,
}: HyperDXWinstonOptions) {
hdx('Initializing HyperDX winston transport...');
super({ level: maxLevel ?? 'info' });
this.logger = new Logger({ apiKey, baseUrl, service });
this.logger = new Logger({
apiKey,
baseUrl,
bufferSize,
sendIntervalMs,
service,
timeout,
});
hdx(`HyperDX winston transport initialized!`);
}

Expand Down
21 changes: 19 additions & 2 deletions packages/node-opentelemetry/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@ const HYPERDX_API_KEY = (env.HYPERDX_API_KEY ??

const SERVICE_NAME = env.OTEL_SERVICE_NAME as string;

export const getWinsonTransport = (maxLevel = 'info') => {
type WinstonTransportOptions = {
baseUrl?: string;
bufferSize?: number;
sendIntervalMs?: number;
timeout?: number; // The read/write/connection timeout in milliseconds
};

type PinotTransportOptions = WinstonTransportOptions;

export const getWinsonTransport = (
maxLevel = 'info',
options: WinstonTransportOptions = {},
) => {
hdx('Initializing winston transport');
return new HyperDXWinston({
apiKey: HYPERDX_API_KEY,
maxLevel,
service: SERVICE_NAME,
...options,
});
};

export const getPinoTransport = (maxLevel = 'info') => ({
export const getPinoTransport = (
maxLevel = 'info',
options: PinotTransportOptions = {},
) => ({
target: '@hyperdx/node-logger/build/src/pino',
options: {
apiKey: HYPERDX_API_KEY,
service: SERVICE_NAME,
...options,
},
level: maxLevel,
});

0 comments on commit 34d8d5e

Please sign in to comment.