-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow skipping methods with decorators (#139)
* 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
1 parent
16d6926
commit fbebdbd
Showing
22 changed files
with
676 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.