Skip to content

Commit 54e7cc0

Browse files
authored
Rename metrics to spec and add descriptions (#90)
* add constants file for names and descriptions * rename metrics and add descriptions * update tests
1 parent 714888b commit 54e7cc0

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

packages/lib/src/buildInfo.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getMeter } from "./instrumentation";
22
import { getRuntime, Runtime } from "./utils";
33
import { UpDownCounter } from "@opentelemetry/api";
4+
import { BUILD_INFO_DESCRIPTION, BUILD_INFO_NAME } from "./constants";
45

56
/**
67
* BuildInfo is used to create the `build_info` metric that
@@ -18,7 +19,9 @@ let buildInfoGauge: UpDownCounter;
1819

1920
export function recordBuildInfo(buildInfo: BuildInfo) {
2021
if (!buildInfoGauge) {
21-
buildInfoGauge = getMeter().createUpDownCounter("build_info");
22+
buildInfoGauge = getMeter().createUpDownCounter(BUILD_INFO_NAME, {
23+
description: BUILD_INFO_DESCRIPTION,
24+
});
2225
}
2326

2427
buildInfoGauge.add(1, buildInfo);

packages/lib/src/constants.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Metrics
2+
export const COUNTER_NAME = "function.calls" as const;
3+
export const HISTOGRAM_NAME = "function.calls.duration" as const;
4+
export const GAUGE_NAME = "function.calls.concurrent" as const;
5+
export const BUILD_INFO_NAME = "build_info" as const;
6+
7+
// Descriptions
8+
export const COUNTER_DESCRIPTION =
9+
"Autometrics counter for tracking function calls" as const;
10+
export const HISTOGRAM_DESCRIPTION =
11+
"Autometrics histogram for tracking function call duration" as const;
12+
export const GAUGE_DESCRIPTION =
13+
"Autometrics gauge for tracking concurrent function calls" as const;
14+
export const BUILD_INFO_DESCRIPTION =
15+
"Autometrics info metric for tracking software version and build details" as const;
16+
17+
// Units
18+
export const HISTOGRAM_UNIT = "seconds" as const;

packages/lib/src/wrappers.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { Attributes } from "@opentelemetry/api";
2-
import type { Objective } from "./objectives";
2+
import { setBuildInfo } from "./buildInfo";
3+
import {
4+
COUNTER_DESCRIPTION,
5+
COUNTER_NAME,
6+
GAUGE_DESCRIPTION,
7+
GAUGE_NAME,
8+
HISTOGRAM_DESCRIPTION,
9+
HISTOGRAM_NAME,
10+
} from "./constants";
311
import { getMeter } from "./instrumentation";
12+
import type { Objective } from "./objectives";
413
import {
514
ALSInstance,
615
getALSCaller,
@@ -10,7 +19,6 @@ import {
1019
isObject,
1120
isPromise,
1221
} from "./utils";
13-
import { setBuildInfo } from "./buildInfo";
1422

1523
let asyncLocalStorage: ALSInstance | undefined;
1624
if (typeof window === "undefined") {
@@ -224,9 +232,15 @@ export function autometrics<F extends FunctionSig>(
224232

225233
const meter = getMeter();
226234
setBuildInfo();
227-
const counter = meter.createCounter("function.calls.count");
228-
const histogram = meter.createHistogram("function.calls.duration");
229-
const gauge = meter.createUpDownCounter("function.calls.concurrent");
235+
const counter = meter.createCounter(COUNTER_NAME, {
236+
description: COUNTER_DESCRIPTION,
237+
});
238+
const histogram = meter.createHistogram(HISTOGRAM_NAME, {
239+
description: HISTOGRAM_DESCRIPTION,
240+
});
241+
const gauge = meter.createUpDownCounter(GAUGE_NAME, {
242+
description: GAUGE_DESCRIPTION,
243+
});
230244
const caller = getALSCaller(asyncLocalStorage);
231245

232246
counter.add(0, {

packages/lib/tests/integration.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("Autometrics integration test", () => {
2828

2929
test("single function", async () => {
3030
const callCountMetric =
31-
/function_calls_count_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm;
31+
/function_calls_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm;
3232
const durationMetric =
3333
/function_calls_duration_bucket\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\}/gm;
3434

@@ -45,7 +45,7 @@ describe("Autometrics integration test", () => {
4545

4646
test("single function with throw", async () => {
4747
const errorCountMetric =
48-
/function_calls_count_total\{\S*function="error"\S*result="error"\S*\} 1/gm;
48+
/function_calls_total\{\S*function="error"\S*result="error"\S*\} 1/gm;
4949

5050
const errorFn = autometrics(async function error() {
5151
return Promise.reject("Oh no");
@@ -60,7 +60,7 @@ describe("Autometrics integration test", () => {
6060

6161
test("class method", async () => {
6262
const callCountMetric =
63-
/function_calls_count_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm;
63+
/function_calls_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm;
6464
const durationMetric =
6565
/function_calls_duration_bucket\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\}/gm;
6666

packages/lib/tests/objectives.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("Autometrics objectives test", () => {
4545
successRateFn();
4646

4747
const callCountMetric =
48-
/function_calls_count_total\{\S*function="successRate"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm;
48+
/function_calls_total\{\S*function="successRate"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm;
4949

5050
const serialized = await collectAndSerialize(exporter);
5151

@@ -90,7 +90,7 @@ describe("Autometrics objectives test", () => {
9090
combinedObjectiveFn();
9191

9292
const callCountMetric =
93-
/function_calls_count_total\{\S*function="combinedObjective"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm;
93+
/function_calls_total\{\S*function="combinedObjective"\S*objective_name="test",objective_percentile="99"\S*\} 2/gm;
9494

9595
const durationMetric =
9696
/function_calls_duration_bucket\{\S*function="combinedObjective"\S*objective_name="test",objective_latency_threshold="0.1",objective_percentile="99.9"\S*\} 2/gm;

0 commit comments

Comments
 (0)