Skip to content

Commit

Permalink
Added basic tracing based on the console logging and printed SQL queries
Browse files Browse the repository at this point in the history
Later on it'll be replaced with real Open Telemetry.
  • Loading branch information
oskardudycz committed Oct 1, 2024
1 parent 0e27f26 commit 5599511
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/packages/dumbo/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './query';
export * from './schema';
export * from './serializer';
export * from './sql';
export * from './tracing';
90 changes: 90 additions & 0 deletions src/packages/dumbo/src/core/tracing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { JSONSerializer } from '../serializer';

export const tracer = () => {};

export type LogLevel = 'DISABLED' | 'INFO' | 'LOG' | 'WARN' | 'ERROR';
export const LogLevel = {
DISABLED: 'DISABLED' as LogLevel,
INFO: 'INFO' as LogLevel,
LOG: 'LOG' as LogLevel,
WARN: 'WARN' as LogLevel,
ERROR: 'ERROR' as LogLevel,
};

const shouldLog = (logLevel: LogLevel): boolean => {
const definedLogLevel = process.env.PONGO_LOG_LEVEL ?? LogLevel.DISABLED;

if (definedLogLevel === LogLevel.ERROR && logLevel === LogLevel.ERROR)
return true;

if (
definedLogLevel === LogLevel.WARN &&
[LogLevel.ERROR, LogLevel.WARN].includes(logLevel)
)
return true;

if (
definedLogLevel === LogLevel.LOG &&
[LogLevel.ERROR, LogLevel.WARN, LogLevel.LOG].includes(logLevel)
)
return true;

if (
definedLogLevel === LogLevel.INFO &&
[LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO].includes(logLevel)
)
return true;

return false;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
tracer.log = (eventName: string, attributes?: Record<string, any>) => {
if (!shouldLog(LogLevel.LOG)) return;

console.log(
JSONSerializer.serialize({
name: eventName,
timestamp: new Date().getTime(),
...attributes,
}),
);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
tracer.warn = (eventName: string, attributes?: Record<string, any>) => {
if (!shouldLog(LogLevel.WARN)) return;

console.warn(
JSONSerializer.serialize({
name: eventName,
timestamp: new Date().getTime(),
...attributes,
}),
);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
tracer.error = (eventName: string, attributes?: Record<string, any>) => {
if (!shouldLog(LogLevel.ERROR)) return;

console.error(
JSONSerializer.serialize({
name: eventName,
timestamp: new Date().getTime(),
...attributes,
}),
);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tracer.info = (eventName: string, attributes?: Record<string, any>) => {
if (!shouldLog(LogLevel.INFO)) return;

console.info(
JSONSerializer.serialize({
name: eventName,
timestamp: new Date().getTime(),
...attributes,
}),
);
};
2 changes: 2 additions & 0 deletions src/packages/dumbo/src/postgres/pg/execute/execute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pg from 'pg';
import {
tracer,
type DbSQLExecutor,
type QueryResult,
type QueryResultRow,
Expand Down Expand Up @@ -88,6 +89,7 @@ async function batch<Result extends QueryResultRow = QueryResultRow>(
//TODO: make it smarter at some point
for (let i = 0; i < sqls.length; i++) {
const result = await client.query<Result>(sqls[i]!);
tracer.info('db:sql:query', { sql: sqls[i]! });
results[i] = { rowCount: result.rowCount, rows: result.rows };
}
return Array.isArray(sqlOrSqls) ? results : results[0]!;
Expand Down

0 comments on commit 5599511

Please sign in to comment.