diff --git a/packages/lib/src/buildInfo.ts b/packages/lib/src/buildInfo.ts index 91904df1..f93af6fe 100644 --- a/packages/lib/src/buildInfo.ts +++ b/packages/lib/src/buildInfo.ts @@ -1,6 +1,7 @@ import { getMeter } from "./instrumentation"; import { getRuntime, Runtime } from "./utils"; import { UpDownCounter } from "@opentelemetry/api"; +import { BUILD_INFO_DESCRIPTION, BUILD_INFO_NAME } from "./constants"; /** * BuildInfo is used to create the `build_info` metric that @@ -18,7 +19,9 @@ let buildInfoGauge: UpDownCounter; export function recordBuildInfo(buildInfo: BuildInfo) { if (!buildInfoGauge) { - buildInfoGauge = getMeter().createUpDownCounter("build_info"); + buildInfoGauge = getMeter().createUpDownCounter(BUILD_INFO_NAME, { + description: BUILD_INFO_DESCRIPTION, + }); } buildInfoGauge.add(1, buildInfo); diff --git a/packages/lib/src/constants.ts b/packages/lib/src/constants.ts new file mode 100644 index 00000000..ff7493b0 --- /dev/null +++ b/packages/lib/src/constants.ts @@ -0,0 +1,18 @@ +// Metrics +export const COUNTER_NAME = "function.calls" as const; +export const HISTOGRAM_NAME = "function.calls.duration" as const; +export const GAUGE_NAME = "function.calls.concurrent" as const; +export const BUILD_INFO_NAME = "build_info" as const; + +// Descriptions +export const COUNTER_DESCRIPTION = + "Autometrics counter for tracking function calls" as const; +export const HISTOGRAM_DESCRIPTION = + "Autometrics histogram for tracking function call duration" as const; +export const GAUGE_DESCRIPTION = + "Autometrics gauge for tracking concurrent function calls" as const; +export const BUILD_INFO_DESCRIPTION = + "Autometrics info metric for tracking software version and build details" as const; + +// Units +export const HISTOGRAM_UNIT = "seconds" as const; diff --git a/packages/lib/src/wrappers.ts b/packages/lib/src/wrappers.ts index 51cfb527..ebede1d8 100644 --- a/packages/lib/src/wrappers.ts +++ b/packages/lib/src/wrappers.ts @@ -1,6 +1,15 @@ import { Attributes } from "@opentelemetry/api"; -import type { Objective } from "./objectives"; +import { setBuildInfo } from "./buildInfo"; +import { + COUNTER_DESCRIPTION, + COUNTER_NAME, + GAUGE_DESCRIPTION, + GAUGE_NAME, + HISTOGRAM_DESCRIPTION, + HISTOGRAM_NAME, +} from "./constants"; import { getMeter } from "./instrumentation"; +import type { Objective } from "./objectives"; import { ALSInstance, getALSCaller, @@ -10,7 +19,6 @@ import { isObject, isPromise, } from "./utils"; -import { setBuildInfo } from "./buildInfo"; let asyncLocalStorage: ALSInstance | undefined; if (typeof window === "undefined") { @@ -224,9 +232,15 @@ export function autometrics( const meter = getMeter(); setBuildInfo(); - const counter = meter.createCounter("function.calls.count"); - const histogram = meter.createHistogram("function.calls.duration"); - const gauge = meter.createUpDownCounter("function.calls.concurrent"); + const counter = meter.createCounter(COUNTER_NAME, { + description: COUNTER_DESCRIPTION, + }); + const histogram = meter.createHistogram(HISTOGRAM_NAME, { + description: HISTOGRAM_DESCRIPTION, + }); + const gauge = meter.createUpDownCounter(GAUGE_NAME, { + description: GAUGE_DESCRIPTION, + }); const caller = getALSCaller(asyncLocalStorage); counter.add(0, { diff --git a/packages/lib/tests/integration.test.ts b/packages/lib/tests/integration.test.ts index c30c8029..6a8fb678 100644 --- a/packages/lib/tests/integration.test.ts +++ b/packages/lib/tests/integration.test.ts @@ -28,7 +28,7 @@ describe("Autometrics integration test", () => { test("single function", async () => { const callCountMetric = - /function_calls_count_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm; + /function_calls_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm; const durationMetric = /function_calls_duration_bucket\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\}/gm; @@ -45,7 +45,7 @@ describe("Autometrics integration test", () => { test("single function with throw", async () => { const errorCountMetric = - /function_calls_count_total\{\S*function="error"\S*result="error"\S*\} 1/gm; + /function_calls_total\{\S*function="error"\S*result="error"\S*\} 1/gm; const errorFn = autometrics(async function error() { return Promise.reject("Oh no"); @@ -60,7 +60,7 @@ describe("Autometrics integration test", () => { test("class method", async () => { const callCountMetric = - /function_calls_count_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm; + /function_calls_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm; const durationMetric = /function_calls_duration_bucket\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\}/gm; diff --git a/packages/lib/tests/objectives.test.ts b/packages/lib/tests/objectives.test.ts index 204d9d14..118deb26 100644 --- a/packages/lib/tests/objectives.test.ts +++ b/packages/lib/tests/objectives.test.ts @@ -45,7 +45,7 @@ describe("Autometrics objectives test", () => { successRateFn(); const callCountMetric = - /function_calls_count_total\{\S*function="successRate"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm; + /function_calls_total\{\S*function="successRate"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm; const serialized = await collectAndSerialize(exporter); @@ -90,7 +90,7 @@ describe("Autometrics objectives test", () => { combinedObjectiveFn(); const callCountMetric = - /function_calls_count_total\{\S*function="combinedObjective"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm; + /function_calls_total\{\S*function="combinedObjective"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm; const durationMetric = /function_calls_duration_bucket\{\S*function="combinedObjective"\S*objective_name="test",objective_latency_threshold="0.1",objective_percentile="99.9"\S*\} 2/gm;