Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add template to creative debug #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 29 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import debug, { Debugger } from 'debug';
import { AsyncLocalStorage } from 'node:async_hooks';

const debugInstances: {[namespace: string]: {[postfix: string]: Debugger}} = {};
const asyncLocalStorage = new AsyncLocalStorage<{postfix: string}>();

export function withDebugContext<F extends (...args: any[]) => any>(postfix: string, fn: F) {
return async (...args: Parameters<F>): Promise<Awaited<ReturnType<F>>> => {
return await asyncLocalStorage.run({ postfix }, () => {
return fn(...args);

Check warning on line 10 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L8-L10

Added lines #L8 - L10 were not covered by tests
})
};
}

function getContext() {
return asyncLocalStorage.getStore()?.postfix;
}

export function Debug(namespace: string) {
// debug instances are organized by postfix to the namespace (default is no postfix)
debugInstances[namespace] = {'': debug(namespace)};
return function(...args: Parameters<Debugger>) {
// getContext gives the postfix to the namespace (create new debug instance if we don't have one yet)
if (!debugInstances[namespace][getContext() ?? '']) {
debugInstances[namespace][getContext()] = debug(namespace + getContext());

Check warning on line 25 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L25

Added line #L25 was not covered by tests
}
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
Loading