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 2 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
48 changes: 48 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import debug, { Debugger } from 'debug';
import async_hooks from 'async_hooks';

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

// any child async calls/promises will inherit the asyncContex from their parent (triggerAsyncId)
async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (asyncContext.has(triggerAsyncId)) {
asyncContext.set(asyncId, asyncContext.get(triggerAsyncId));

Check warning on line 11 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L11

Added line #L11 was not covered by tests
}
},
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>>> => {

Check warning on line 20 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L20

Added line #L20 was not covered by tests
// associates the promise created by this function with the postfix to be added to the debug namespace
const asyncId = async_hooks.executionAsyncId();
asyncContext.set(asyncId, postfix);

Check warning on line 23 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L22-L23

Added lines #L22 - L23 were not covered by tests

try {
return await fn(...args);

Check warning on line 26 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L25-L26

Added lines #L25 - L26 were not covered by tests
} finally {
asyncContext.delete(asyncId);

Check warning on line 28 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L28

Added line #L28 was not covered by tests
}
};
}

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

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 44 in src/debug.ts

View check run for this annotation

Codecov / codecov/patch

src/debug.ts#L44

Added line #L44 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