1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Hono } from "npm:hono@4.2.2";
import {
attribute,
event,
init,
sqlite,
traceAsync,
traced,
tracedHandler,
} from "https://esm.town/v/saolsen/telemetry";
// Set up tracing by passing in `import.meta.url`.
// be sure to await it!!!
await init(import.meta.url);
function sleep(milliseconds = 3000) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
const app = new Hono();
const someTracedFunction: () => Promise<string> = traced(
"someTracedFunction",
async () => {
attribute("note", "this function is traced via the 'traced' helper");
await sleep(100);
event("something has happened", { foo: "bar" });
await sleep(100);
return "widgets";
},
);
app.get("/", async (c) => {
// Pretend to do some stuff.
await sleep(200);
const _ = await someTracedFunction();
// can also do manual tracing.
let what;
await traceAsync("traced block", async () => {
attribute("note", "this block is traced manually");
await sleep(300);
// If you use the built in sqlite helper, it will automatically trace
// all queries.
console.log(await sqlite.execute("SELECT 'foo' AS widget"));
await sleep(300);
what = "widgets";
});
return c.json("widgets");
});
// Wrapping your handler with `tracedHandler` will automatically trace
// all requests to your val.
async function handler(req: Request): Promise<Response> {
return await app.fetch(req);
}
export default tracedHandler(handler);
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.