Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
keturiosakys committed Sep 15, 2023
1 parent c73e8c3 commit 62e5388
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 31 deletions.
8 changes: 8 additions & 0 deletions examples/hono-bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```
bun install
bun run dev
```

```
open http://localhost:3000
```
Binary file added examples/hono-bun/bun.lockb
Binary file not shown.
13 changes: 13 additions & 0 deletions examples/hono-bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"dev": "bun run --hot src/index.ts"
},
"dependencies": {
"@autometrics/autometrics": "file:../../packages/autometrics",
"@autometrics/exporter-prometheus": "^0.7.0-beta4",
"hono": "^3.6.0"
},
"devDependencies": {
"bun-types": "^0.6.2"
}
}
15 changes: 15 additions & 0 deletions examples/hono-bun/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Context, Hono } from "hono";
import { logger } from "hono/logger";
import { autometrics } from "@autometrics/autometrics";
import { init } from "@autometrics/exporter-prometheus";

const app = new Hono();

async function rootHandler(ctx: Context) {
return ctx.json({ ok: true, message: "Hello world" });
}

app.use("*", logger());
app.get("/", autometrics(rootHandler));

export default app;
9 changes: 9 additions & 0 deletions examples/hono-bun/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"esModuleInterop": true,
"strict": true,
"jsx": "react-jsx",
"jsxFragmentFactory": "Fragment",
"jsxImportSource": "hono/jsx"
}
}
76 changes: 45 additions & 31 deletions packages/autometrics/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,49 @@ export function getRuntime(): Runtime {
// HACK: this entire function is a hacky way to acquire the module name for a
// given function e.g.: dist/index.js
export function getModulePath(): string | undefined {
const defaultPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const { stack: stackConstructor } = new Error() as Error & {
stack: NodeJS.CallSite[];
};
Error.prepareStackTrace = defaultPrepareStackTrace; // we have to make sure to reset this to normal

const stack = stackConstructor.map((callSite) => ({
name: callSite.getFunctionName(),
file: callSite.getFileName(),
}));
let wrappedFunctionPath: string | undefined;

if ("prepareStackTrace" in Error) {
const defaultPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const { stack: stackConstructor } = new Error() as Error & {
stack: NodeJS.CallSite[];
};
Error.prepareStackTrace = defaultPrepareStackTrace; // we have to make sure to reset this to normal

const stack = stackConstructor.map((callSite) => ({
name: callSite.getFunctionName(),
file: callSite.getFileName(),
}));

if (!stack) {
return;
}

/**
* Finds the original wrapped function, first it checks if it's a decorator,
* and returns that filename or gets the 3th item of the stack trace:
*
* 0: Error
* 1: at getModulePath ...
* 2: at autometrics ...
* 3: at ... -> 4th line is always the original wrapped function
*/
wrappedFunctionPath =
stack.find((call) => {
if (call.name?.includes("__decorate")) return true;
})?.file ?? stack[2]?.file;
} else {
const stack = new Error().stack?.split("\n");
wrappedFunctionPath = stack?.[3]
?.split(" ")
.filter((el) => el.length !== 0)[3];
}

// check if the string is wrapped in parenthesis, if so, remove them
if (wrappedFunctionPath?.startsWith("(")) {
wrappedFunctionPath = wrappedFunctionPath.slice(1, -1);
}

let rootDir: string;
const runtime = getRuntime();
Expand Down Expand Up @@ -59,28 +91,10 @@ export function getModulePath(): string | undefined {
rootDir = "";
}

if (!stack) {
return;
}

/**
* Finds the original wrapped function, first it checks if it's a decorator,
* and returns that filename or gets the 3th item of the stack trace:
*
* 0: Error
* 1: at getModulePath ...
* 2: at autometrics ...
* 3: at ... -> 4th line is always the original wrapped function
*/
const wrappedFunctionPath =
stack.find((call) => {
if (call.name?.includes("__decorate")) return true;
})?.file ?? stack[2]?.file;

const containsFileProtocol = wrappedFunctionPath?.includes("file://");

// We split away everything up to the root directory of the project,
// if the path contains file:// we need to remove it
const containsFileProtocol = wrappedFunctionPath?.includes("file://");

return wrappedFunctionPath?.replace(
containsFileProtocol ? `file://${rootDir}` : rootDir,
"",
Expand Down

0 comments on commit 62e5388

Please sign in to comment.