diff --git a/biome.json b/biome.json index 541b0be..beb3365 100644 --- a/biome.json +++ b/biome.json @@ -5,7 +5,7 @@ "formatter": { "enabled": true, "indentStyle": "space", - "indentSize": 2 + "indentWidth": 2 }, "organizeImports": { "enabled": true diff --git a/examples/express/tests/callerInfo.test.ts b/examples/express/tests/callerInfo.test.ts index b52b803..c703d73 100644 --- a/examples/express/tests/callerInfo.test.ts +++ b/examples/express/tests/callerInfo.test.ts @@ -22,11 +22,11 @@ test("Caller info test", async (t) => { assert.match( serialized, - /function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm, + /function_calls_total\{\S*function="bar"\S*caller_function=""\S*\} 1/gm, ); assert.match( serialized, - /function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm, + /function_calls_total\{\S*function="foo"\S*caller_function="bar"\S*caller_module="\/dist\/tests\/callerInfo.test.js"\S*\} 1/gm, ); }, ); @@ -49,11 +49,11 @@ test("Caller info test", async (t) => { assert.match( serialized, - /function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm, + /function_calls_total\{\S*function="bar"\S*caller_function=""\S*\} 1/gm, ); assert.match( serialized, - /function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm, + /function_calls_total\{\S*function="foo"\S*caller_function="bar"\S*caller_module="\/dist\/tests\/callerInfo.test.js"\S*\} 1/gm, ); }, ); diff --git a/examples/hono-bun/tests/callerInfo.test.ts b/examples/hono-bun/tests/callerInfo.test.ts index 9611cf1..71d78e7 100644 --- a/examples/hono-bun/tests/callerInfo.test.ts +++ b/examples/hono-bun/tests/callerInfo.test.ts @@ -17,10 +17,10 @@ test("Caller info test -- synchronous call", async () => { const serialized = await collectAndSerialize(metricReader); expect(serialized).toMatch( - /function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm, + /function_calls_total\{\S*function="bar"\S*caller_function=""\S*\} 1/gm, ); expect(serialized).toMatch( - /function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm, + /function_calls_total\{\S*function="foo"\S*caller_function="bar"\S*caller_module="\/tests\/callerInfo.test.ts"\S*\} 1/gm, ); }); }); @@ -40,10 +40,10 @@ test("Caller info test -- asynchronous call", async () => { const serialized = await collectAndSerialize(metricReader); expect(serialized).toMatch( - /function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm, + /function_calls_total\{\S*function="bar"\S*caller_function=""\S*\} 1/gm, ); expect(serialized).toMatch( - /function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm, + /function_calls_total\{\S*function="foo"\S*caller_function="bar"\S*caller_module="\/tests\/callerInfo.test.ts"\S*\} 1/gm, ); }); }); diff --git a/packages/autometrics/src/platform.deno.ts b/packages/autometrics/src/platform.deno.ts index e5f1a79..ea22530 100644 --- a/packages/autometrics/src/platform.deno.ts +++ b/packages/autometrics/src/platform.deno.ts @@ -43,7 +43,7 @@ export function getCwd(): string { * * @internal */ -export type AsyncContext = { caller?: string }; +export type AsyncContext = { callerFunction?: string; callerModule?: string }; /** * Returns a new `AsyncLocalStorage` instance for storing caller information. diff --git a/packages/autometrics/src/platform.node.ts b/packages/autometrics/src/platform.node.ts index 171b3be..5b026bb 100644 --- a/packages/autometrics/src/platform.node.ts +++ b/packages/autometrics/src/platform.node.ts @@ -58,5 +58,8 @@ export function getCwd(): string { * @internal */ export function getALSInstance() { - return new AsyncLocalStorage<{ caller?: string }>(); + return new AsyncLocalStorage<{ + callerFunction?: string; + callerModule?: string; + }>(); } diff --git a/packages/autometrics/src/wrappers.ts b/packages/autometrics/src/wrappers.ts index 99ca79e..7498dd6 100644 --- a/packages/autometrics/src/wrappers.ts +++ b/packages/autometrics/src/wrappers.ts @@ -9,11 +9,11 @@ import { HISTOGRAM_DESCRIPTION, HISTOGRAM_NAME, } from "./constants.ts"; -import { getALSInstance } from "./platform.deno.ts"; import { getMeter, metricsRecorded } from "./instrumentation.ts"; -import { getModulePath, isFunction, isObject, isPromise } from "./utils.ts"; import { trace, warn } from "./logger.ts"; import type { Objective } from "./objectives.ts"; +import { getALSInstance } from "./platform.deno.ts"; +import { getModulePath, isFunction, isObject, isPromise } from "./utils.ts"; const asyncLocalStorage = getALSInstance(); @@ -284,7 +284,8 @@ export function autometrics( function: functionName, module: moduleName, result: "ok", - caller: "", + caller_function: "", + caller_module: "", ...counterObjectiveAttributes, }); @@ -295,7 +296,9 @@ export function autometrics( module: moduleName, }); - const caller = asyncLocalStorage?.getStore()?.caller ?? ""; + const callerData = asyncLocalStorage?.getStore(); + const callerFunction = callerData?.callerFunction ?? ""; + const callerModule = callerData?.callerModule ?? ""; const onSuccess = () => { const autometricsDuration = (performance.now() - autometricsStart) / 1000; @@ -304,14 +307,16 @@ export function autometrics( function: functionName, module: moduleName, result: "ok", - caller, + caller_function: callerFunction, + caller_module: callerModule, ...counterObjectiveAttributes, }); histogram.record(autometricsDuration, { function: functionName, module: moduleName, - caller, + caller_function: callerFunction, + caller_module: callerModule, ...histogramObjectiveAttributes, }); @@ -330,21 +335,24 @@ export function autometrics( function: functionName, module: moduleName, result: "error", - caller, + caller_function: callerFunction, + caller_module: callerModule, ...counterObjectiveAttributes, }); histogram.record(autometricsDuration, { function: functionName, module: moduleName, - caller, + caller_function: callerFunction, + caller_module: callerModule, ...histogramObjectiveAttributes, }); concurrencyGauge?.add(-1, { function: functionName, module: moduleName, - caller, + caller_function: callerFunction, + caller_module: callerModule, }); metricsRecorded(); @@ -402,7 +410,10 @@ export function autometrics( } return asyncLocalStorage - ? asyncLocalStorage.run({ caller: functionName }, instrumentedFn) + ? asyncLocalStorage.run( + { callerFunction: functionName, callerModule: moduleName }, + instrumentedFn, + ) : instrumentedFn(); }; } diff --git a/packages/autometrics/tests/__snapshots__/otlpHttpExporter.test.ts.snap b/packages/autometrics/tests/__snapshots__/otlpHttpExporter.test.ts.snap index f96b000..1169299 100644 --- a/packages/autometrics/tests/__snapshots__/otlpHttpExporter.test.ts.snap +++ b/packages/autometrics/tests/__snapshots__/otlpHttpExporter.test.ts.snap @@ -2,7 +2,8 @@ export const snapshot = {}; snapshot[`OTLP/HTTP exporter 1`] = ` { - caller: "", + caller_function: "", + caller_module: "", function: "foo", module: "/packages/autometrics/tests/otlpHttpExporter.test.ts", objective_name: "", diff --git a/packages/autometrics/tests/callerInfo.test.ts b/packages/autometrics/tests/callerInfo.test.ts index 7c5a49a..67a4b98 100644 --- a/packages/autometrics/tests/callerInfo.test.ts +++ b/packages/autometrics/tests/callerInfo.test.ts @@ -21,11 +21,11 @@ Deno.test("Caller info test", async (t) => { assertMatch( serialized, - /function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm, + /function_calls_total\{\S*function="bar"\S*caller_function=""\S*\} 1/gm, ); assertMatch( serialized, - /function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm, + /function_calls_total\{\S*function="foo"\S*caller_function="bar"\S*caller_module="\/packages\/autometrics\/tests\/callerInfo.test.ts"\S*\} 1/gm, ); }, ); @@ -48,11 +48,11 @@ Deno.test("Caller info test", async (t) => { assertMatch( serialized, - /function_calls_total\{\S*function="bar"\S*caller=""\S*\} 1/gm, + /function_calls_total\{\S*function="bar"\S*caller_function=""\S*\} 1/gm, ); assertMatch( serialized, - /function_calls_total\{\S*function="foo"\S*caller="bar"\S*\} 1/gm, + /function_calls_total\{\S*function="foo"\S*caller_function="bar"\S*caller_module="\/packages\/autometrics\/tests\/callerInfo.test.ts"\S*\} 1/gm, ); }, );