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
import {
init,
traced,
traced_handler,
} from "https://esm.town/v/saolsen/tracing?v=126";
import { trace } from "npm:@opentelemetry/api";
init("traced_http_val");
const tracer = trace.getTracer("traced_http_val");
const sleep = (delay: number) =>
new Promise((resolve) => setTimeout(resolve, delay));
function _foo(x: number) {
console.log(x);
}
const foo = traced("foo", _foo);
function _throw() {
throw new Error("errr");
}
const thro = traced("throw", _throw);
const doSomething = traced("do-something", async () => {
await sleep(100);
});
async function afn(x: number): Promise<number> {
return x;
}
const tafn = traced("afn", afn);
async function handler(request: Request): Promise<Response> {
await tracer.startActiveSpan("sub-span", async (span) => {
await doSomething();
await foo();
try {
await thro();
} catch (e) {
console.log("nah");
}
await sleep(100);
span.addEvent("sub event ok");
await tracer.startActiveSpan("sub-span-2", async (span2) => {
await sleep(100);
span2.end();
});
await tracer.startActiveSpan("sub-span-3", async (span3) => {
await sleep(100);
span3.end();
});
span.end();
});
return Response.json({ hello: "world" });
}
export default traced_handler(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.