-
Notifications
You must be signed in to change notification settings - Fork 40
/
handler.ts
64 lines (57 loc) · 1.94 KB
/
handler.ts
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
63
64
import { trace } from '@opentelemetry/api'
export interface Env {
OTEL_TEST: KVNamespace
Test_Otel_DO: DurableObjectNamespace
'otel.exporter.headers.x-honeycomb-team': string
}
const handleDO = async (request: Request, env: Env): Promise<Response> => {
const ns = env.Test_Otel_DO
const id = ns.idFromName('testing')
const stub = ns.get(id)
trace.getActiveSpan()?.setAttribute('http.route', '/do')
return await stub.fetch('https://does-not-exist.com/blah')
}
const handleRest = async (request: Request, env: Env, ctx: ExecutionContext): Promise<Response> => {
trace.getActiveSpan()?.setAttribute('http.route', '/*')
await fetch('https://cloudflare.com')
const cache = await caches.open('stuff')
const promises = [env.OTEL_TEST.get('non-existant'), cache.match(new Request('https://no-exist.com'))]
await Promise.all(promises)
const greeting = "G'day World"
trace.getActiveSpan()?.setAttribute('greeting', greeting)
ctx.waitUntil(fetch('https://workers.dev'))
return new Response(`${greeting}!`)
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const pathname = new URL(request.url).pathname
if (pathname === '/do') {
return handleDO(request, env)
} else if (pathname === '/error') {
throw new Error('You asked for it!')
} else {
return handleRest(request, env, ctx)
}
},
}
export class OtelDO implements DurableObject {
constructor(
protected state: DurableObjectState,
protected env: Env,
) {
state.blockConcurrencyWhile(async () => {
await this.state.storage.getAlarm()
})
}
async fetch(request: Request): Promise<Response> {
await fetch('https://cloudflare.com')
await this.state.storage.put('something', 'else')
await this.state.storage.setAlarm(Date.now() + 1000)
return new Response('Hello World!')
}
async alarm(): Promise<void> {
console.log('ding ding!')
await this.state.storage.get('something')
await this.state.storage.get('something_else')
}
}