Skip to content

Commit

Permalink
add template to creative debug
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Nov 1, 2024
1 parent 0e5358b commit c108c48
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/biolink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BioLink } from 'biolink-model';
import Debug from 'debug';
import { Debug } from "./debug";
const debug = Debug('bte:biothings-explorer-trapi:EdgeReverse');

class BioLinkModel {
Expand Down
43 changes: 43 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import debug, { Debugger } from 'debug';
import async_hooks from 'async_hooks';

const debugInstances: {[namespace: string]: {[postfix: string]: Debugger}} = {};
const asyncContext = new Map();

async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (asyncContext.has(triggerAsyncId)) {
asyncContext.set(asyncId, asyncContext.get(triggerAsyncId));
}
},
destroy(asyncId) {
asyncContext.delete(asyncId);
}
}).enable();

export function withDebugContext<F extends (...args: any[]) => any>(postfix: string, fn: F) {
return async (...args: Parameters<F>): Promise<Awaited<ReturnType<F>>> => {
const asyncId = async_hooks.executionAsyncId();
asyncContext.set(asyncId, postfix);
try {
return await fn(...args);
} finally {
asyncContext.delete(asyncId);
}
};
}

function getContext() {
const asyncId = async_hooks.executionAsyncId();
return asyncContext.get(asyncId);
}

export function Debug(namespace: string) {
debugInstances[namespace] = {'': debug(namespace)};
return function(...args: Parameters<Debugger>) {
if (!debugInstances[namespace][getContext() ?? '']) {
debugInstances[namespace][getContext()] = debug(namespace + getContext());
}
debugInstances[namespace][getContext() ?? ''](...args);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./telemetry";
export * from "./misc";
export * from "./redis-client";
export * from "./biolink";
export * from "./debug";
2 changes: 1 addition & 1 deletion src/redis-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Redis, { Cluster, RedisKey } from "ioredis";
import Debug from "debug";
import { Debug } from "./debug";
const debug = Debug("bte:biothings-explorer-trapi:redis-client");
import Redlock, { RedlockAbortSignal } from "redlock";

Expand Down
2 changes: 1 addition & 1 deletion src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Sentry from "@sentry/node";
import Debug from "debug";
import { Debug } from "./debug";
import opentelemetry, {
Span as OtelSpan,
SpanStatusCode,
Expand Down

0 comments on commit c108c48

Please sign in to comment.