Skip to content

Commit

Permalink
Rename metrics to spec and add descriptions (#90)
Browse files Browse the repository at this point in the history
* add constants file for names and descriptions

* rename metrics and add descriptions

* update tests
  • Loading branch information
keturiosakys authored Jul 14, 2023
1 parent 714888b commit 54e7cc0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
5 changes: 4 additions & 1 deletion packages/lib/src/buildInfo.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions packages/lib/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 19 additions & 5 deletions packages/lib/src/wrappers.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,7 +19,6 @@ import {
isObject,
isPromise,
} from "./utils";
import { setBuildInfo } from "./buildInfo";

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

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, {
Expand Down
6 changes: 3 additions & 3 deletions packages/lib/tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/lib/tests/objectives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 54e7cc0

Please sign in to comment.