Skip to content

Commit

Permalink
Fix tracking caller
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Oct 24, 2023
1 parent 0af6a6d commit 13f067d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/express/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function stepWithMetricReader(
registerExporter({ metricReader });

try {
t.test(stepName, () => testFn(metricReader));
await t.test(stepName, () => testFn(metricReader));
} finally {
await metricReader.forceFlush();
}
Expand Down
25 changes: 24 additions & 1 deletion examples/hono-bun/tests/callerInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { autometrics } from "@autometrics/autometrics";

import { collectAndSerialize, testWithMetricReader } from "./test-utils.js";

test("Caller info test", async () => {
test("Caller info test -- synchronous call", async () => {
await testWithMetricReader(async (metricReader) => {
const foo = autometrics(function foo() {});

Expand All @@ -24,3 +24,26 @@ test("Caller info test", async () => {
);
});
});

test("Caller info test -- asynchronous call", async () => {
await testWithMetricReader(async (metricReader) => {
const foo = autometrics(function foo() {
return Promise.resolve();
});

const bar = autometrics(async function bar() {
await foo();
});

await bar();

const serialized = await collectAndSerialize(metricReader);

expect(serialized).toMatch(
/function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm,
);
expect(serialized).toMatch(
/function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm,
);
});
});
5 changes: 3 additions & 2 deletions packages/autometrics/src/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,12 @@ export function autometrics<F extends FunctionSig>(
valueType: ValueType.INT,
})
: null;
const caller = asyncLocalStorage?.getStore()?.caller;

counter.add(0, {
function: functionName,
module: moduleName,
result: "ok",
caller,
caller: "",
...counterObjectiveAttributes,
});

Expand All @@ -296,6 +295,8 @@ export function autometrics<F extends FunctionSig>(
module: moduleName,
});

const caller = asyncLocalStorage?.getStore()?.caller ?? "";

const onSuccess = () => {
const autometricsDuration = (performance.now() - autometricsStart) / 1000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const snapshot = {};

snapshot[`OTLP/HTTP exporter 1`] = `
{
caller: undefined,
caller: "",
function: "foo",
module: "/packages/autometrics/tests/otlpHttpExporter.test.ts",
objective_name: "",
Expand Down

0 comments on commit 13f067d

Please sign in to comment.