Skip to content

Commit

Permalink
Allow skipping methods with decorators (#139)
Browse files Browse the repository at this point in the history
* Use the same Node version everywhere

* Add platform.web.ts and put getALSInstance() into platform.*.ts

* Add callerInfo tests

* Fix tracking caller

* Add caller.function and caller.module labels (#136)

* Update caller info to include function and module separately

* Update tests

* Fix import order linter issue

* Update biome.json

* Update packages/autometrics/src/wrappers.ts

Co-authored-by: Arend van Beelen jr. <[email protected]>

* Update otlp exporter snapshot test

* fix example tests

---------

Co-authored-by: Arend van Beelen jr. <[email protected]>

* Make sure platform.web.js gets built

* Add react-app-experimental and let 'just clean' also clean node_modules

* Rewrite NPM packages using Rollup

* Everything builds :party:

* Don't depend on node-fetch-native in web build

* Fix tests

* Add instructions on how to use import maps

* Fix react-app-experimental

* Lint fix

* Submit repository fields with build_info

* Export build_info label constants

* Initialize buildInfo directly instead of waiting for createDefaultBuildInfo()

* Organize imports

* Fix imports

* Resolve warning about node:fs

* Allow skipping methods with decorators

* Rename test file

* Add tests for modern decorators

* Don't use ranges in package files

* Use the same version in the express example

---------

Co-authored-by: Laurynas Keturakis <[email protected]>
  • Loading branch information
arendjr and keturiosakys authored Nov 13, 2023
1 parent 16d6926 commit fbebdbd
Show file tree
Hide file tree
Showing 22 changed files with 676 additions and 322 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"packages/typescript-plugin"
],
"editor.defaultFormatter": "biomejs.biome",
"editor.tabSize": 2,
"files.insertFinalNewline": true,
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
Expand Down
12 changes: 6 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
},
"imports": {
"$std/": "https://deno.land/[email protected]/",
"$otel/api": "npm:@opentelemetry/api@^1.7.0",
"$otel/core": "npm:@opentelemetry/core@^1.18.0",
"$otel/exporter-metrics-otlp-http": "npm:@opentelemetry/exporter-metrics-otlp-http@^0.45.0",
"$otel/exporter-prometheus": "npm:@opentelemetry/exporter-prometheus@^0.45.0",
"$otel/resources": "npm:@opentelemetry/resources@^1.18.0",
"$otel/sdk-metrics": "npm:@opentelemetry/sdk-metrics@^1.18.0",
"$otel/api": "npm:@opentelemetry/[email protected]",
"$otel/core": "npm:@opentelemetry/[email protected]",
"$otel/exporter-metrics-otlp-http": "npm:@opentelemetry/[email protected]",
"$otel/exporter-prometheus": "npm:@opentelemetry/[email protected]",
"$otel/resources": "npm:@opentelemetry/[email protected]",
"$otel/sdk-metrics": "npm:@opentelemetry/[email protected]",
"node-fetch-native": "npm:node-fetch-native@^1.4.1"
},
"lock": false
Expand Down
4 changes: 2 additions & 2 deletions examples/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"license": "ISC",
"devDependencies": {
"@autometrics/typescript-plugin": "^0.5.4",
"@opentelemetry/exporter-prometheus": "^0.45.0",
"@opentelemetry/sdk-metrics": "^1.18.0",
"@opentelemetry/exporter-prometheus": "0.45.0",
"@opentelemetry/sdk-metrics": "1.18.0",
"@types/express": "^4.17.16",
"@types/node": "^18.6.5",
"typescript": "^5.2.2"
Expand Down
41 changes: 0 additions & 41 deletions examples/express/tests/decorator.test.ts

This file was deleted.

120 changes: 120 additions & 0 deletions examples/express/tests/legacyDecorators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import assert from "node:assert";
import test from "node:test";

import { AutometricsLegacy } from "@autometrics/autometrics";

import { collectAndSerialize, stepWithMetricReader } from "./test-utils.js";

test("Legacy decorator tests", async (t) => {
// @Autometrics decorator is likely to be used along-side other decorators
// this tests for any conflicts
function other(
_target: Object,
_propertyKey: string,
descriptor: PropertyDescriptor,
) {
const originalMethod = descriptor.value;
descriptor.value = originalMethod;
}

await stepWithMetricReader(t, "class methods", async (metricReader) => {
@AutometricsLegacy() class Foo {
counter = 0;

@other
increase() {
this.counter += 1;
return this.counter;
}

@AutometricsLegacy({ skip: true })
untrackedDecrease() {
this.counter -= 1;
return this.counter;
}
}

const foo = new Foo();
assert.strictEqual(foo.increase(), 1);
assert.strictEqual(foo.increase(), 2);

assert.strictEqual(foo.untrackedDecrease(), 1);
assert.strictEqual(foo.untrackedDecrease(), 0);

const serialized = await collectAndSerialize(metricReader);

assert.match(
serialized,
/function_calls_total\{\S*function="Foo.prototype.increase"\S*module="\/[^"]*\/tests\/legacyDecorators.test.js"\S*\} 2/gm,
);

assert.doesNotMatch(serialized, /untracked/m);
assert.doesNotMatch(serialized, /function="Foo.prototype."/m);
});

await stepWithMetricReader(t, "individual method", async (metricReader) => {
class Bar {
counter = 0;

// For individually decorated methods, the class name needs to be
// specified explicitly if you want to include it in the metric name.
@AutometricsLegacy({ className: "Bar" })
@other
increase() {
this.counter += 1;
return this.counter;
}

untrackedDecrease() {
this.counter -= 1;
return this.counter;
}
}

const bar = new Bar();
assert.strictEqual(bar.increase(), 1);
assert.strictEqual(bar.increase(), 2);

assert.strictEqual(bar.untrackedDecrease(), 1);
assert.strictEqual(bar.untrackedDecrease(), 0);

const serialized = await collectAndSerialize(metricReader);

assert.match(
serialized,
/function_calls_total\{\S*function="Bar.prototype.increase"\S*module="\/[^"]*\/tests\/legacyDecorators.test.js"\S*\} 2/gm,
);

assert.doesNotMatch(serialized, /untracked/m);
assert.doesNotMatch(serialized, /function="Bar.prototype."/m);
});

await stepWithMetricReader(t, "static methods", async (metricReader) => {
@AutometricsLegacy() class Baz {
static theAnswer() {
return 42;
}

@AutometricsLegacy({ skip: true })
static untrackedStatic() {
return 65;
}
}

assert.strictEqual(Baz.theAnswer(), 42);
assert.strictEqual(Baz.theAnswer(), 42);

assert.strictEqual(Baz.untrackedStatic(), 65);
assert.strictEqual(Baz.untrackedStatic(), 65);

const serialized = await collectAndSerialize(metricReader);

assert.match(
serialized,
/function_calls_total\{\S*function="Baz.theAnswer"\S*module="\/[^"]*\/tests\/legacyDecorators.test.js"\S*\} 2/gm,
);

assert.doesNotMatch(serialized, /untracked/m);
assert.doesNotMatch(serialized, /function="Baz.prototype."/m);
});
});
2 changes: 1 addition & 1 deletion examples/express/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export async function stepWithMetricReader(
try {
await t.test(stepName, () => testFn(metricReader));
} finally {
await metricReader.forceFlush();
await metricReader.shutdown();
}
}
26 changes: 13 additions & 13 deletions examples/fastify/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@autometrics/autometrics@workspace:../../dist/autometrics"
dependencies:
"@opentelemetry/api": ^1.7.0
"@opentelemetry/sdk-metrics": ^1.18.0
"@opentelemetry/api": 1.7.0
"@opentelemetry/sdk-metrics": 1.18.0
"@types/node": ^18.6.5
languageName: unknown
linkType: soft
Expand All @@ -19,8 +19,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@autometrics/autometrics@portal:../../dist/autometrics::locator=fastify-example%40workspace%3A."
dependencies:
"@opentelemetry/api": ^1.7.0
"@opentelemetry/sdk-metrics": ^1.18.0
"@opentelemetry/api": 1.7.0
"@opentelemetry/sdk-metrics": 1.18.0
languageName: node
linkType: soft

Expand All @@ -29,9 +29,9 @@ __metadata:
resolution: "@autometrics/exporter-prometheus@portal:../../dist/exporter-prometheus::locator=fastify-example%40workspace%3A."
dependencies:
"@autometrics/autometrics": 0.8.0-dev
"@opentelemetry/api": ^1.7.0
"@opentelemetry/exporter-prometheus": ^0.45.0
"@opentelemetry/sdk-metrics": ^1.18.0
"@opentelemetry/api": 1.7.0
"@opentelemetry/exporter-prometheus": 0.45.0
"@opentelemetry/sdk-metrics": 1.18.0
languageName: node
linkType: soft

Expand All @@ -40,9 +40,9 @@ __metadata:
resolution: "@autometrics/exporter-prometheus@workspace:../../dist/exporter-prometheus"
dependencies:
"@autometrics/autometrics": 0.8.0-dev
"@opentelemetry/api": ^1.7.0
"@opentelemetry/exporter-prometheus": ^0.45.0
"@opentelemetry/sdk-metrics": ^1.18.0
"@opentelemetry/api": 1.7.0
"@opentelemetry/exporter-prometheus": 0.45.0
"@opentelemetry/sdk-metrics": 1.18.0
"@types/node": ^18.6.5
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -88,7 +88,7 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/api@npm:^1.7.0":
"@opentelemetry/api@npm:1.7.0":
version: 1.7.0
resolution: "@opentelemetry/api@npm:1.7.0"
checksum: 2398cbe65f199c3a7050125b3ad9c835f789bb0a616665e9c7f4475a29ac8334b6a3c15f38db48d345b522180c41c00b04cc174cd0eeffba98eb4874a565fa7e
Expand All @@ -106,7 +106,7 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/exporter-prometheus@npm:^0.45.0":
"@opentelemetry/exporter-prometheus@npm:0.45.0":
version: 0.45.0
resolution: "@opentelemetry/exporter-prometheus@npm:0.45.0"
dependencies:
Expand All @@ -131,7 +131,7 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/sdk-metrics@npm:1.18.0, @opentelemetry/sdk-metrics@npm:^1.18.0":
"@opentelemetry/sdk-metrics@npm:1.18.0":
version: 1.18.0
resolution: "@opentelemetry/sdk-metrics@npm:1.18.0"
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion examples/react-app-experimental/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ start:
yarn preview

test:
true # no tests; no issues
yarn test

type-check:
yarn tsc -p tsconfig.json --noEmit
Expand Down
5 changes: 3 additions & 2 deletions examples/react-app-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
"build": "vite build && tsc -p tsconfig.test.json",
"preview": "vite preview",
"test": "node --test dist/tests/*.test.js"
},
"workspaces": [
"../../dist/autometrics",
Expand Down
Loading

0 comments on commit fbebdbd

Please sign in to comment.