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 caller.function and caller.module labels #136

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentSize": 2
"indentWidth": 2
},
"organizeImports": {
"enabled": true
Expand Down
8 changes: 4 additions & 4 deletions examples/express/tests/callerInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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="\/examples\/express\/tests\/callerInfo.test.ts"\S*\} 1/gm,
);
},
);
Expand All @@ -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="\/examples\/express\/tests\/callerInfo.test.ts"\S*\} 1/gm,
);
},
);
Expand Down
8 changes: 4 additions & 4 deletions examples/hono-bun/tests/callerInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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="\/examples\/hono-bun\/tests\/callerInfo.test.ts"\S*\} 1/gm,
);
});
});
Expand All @@ -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="\/examples\/hono-bun\/tests\/callerInfo.test.ts"\S*\} 1/gm,
);
});
});
2 changes: 1 addition & 1 deletion packages/autometrics/src/platform.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion packages/autometrics/src/platform.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ export function getCwd(): string {
* @internal
*/
export function getALSInstance() {
return new AsyncLocalStorage<{ caller?: string }>();
return new AsyncLocalStorage<{
callerFunction?: string;
callerModule?: string;
}>();
}
31 changes: 21 additions & 10 deletions packages/autometrics/src/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -284,7 +284,8 @@ export function autometrics<F extends FunctionSig>(
function: functionName,
module: moduleName,
result: "ok",
caller: "",
caller_function: "",
caller_module: "",
...counterObjectiveAttributes,
});

Expand All @@ -295,7 +296,9 @@ export function autometrics<F extends FunctionSig>(
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;
Expand All @@ -304,14 +307,16 @@ export function autometrics<F extends FunctionSig>(
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,
});

Expand All @@ -330,21 +335,24 @@ export function autometrics<F extends FunctionSig>(
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();
Expand Down Expand Up @@ -402,7 +410,10 @@ export function autometrics<F extends FunctionSig>(
}

return asyncLocalStorage
? asyncLocalStorage.run({ caller: functionName }, instrumentedFn)
? asyncLocalStorage.run(
{ callerFunction: functionName, callerModule: moduleName },
instrumentedFn,
)
: instrumentedFn();
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down
8 changes: 4 additions & 4 deletions packages/autometrics/tests/callerInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
},
);
Expand All @@ -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,
);
},
);
Expand Down
Loading