diff --git a/examples/bun/bun.lockb b/examples/bun/bun.lockb new file mode 100755 index 0000000..2826ab4 Binary files /dev/null and b/examples/bun/bun.lockb differ diff --git a/examples/bun/package.json b/examples/bun/package.json new file mode 100644 index 0000000..2eb8f00 --- /dev/null +++ b/examples/bun/package.json @@ -0,0 +1,8 @@ +{ + "devDependencies": { + "bun-types": "1.0.14" + }, + "dependencies": { + "rian": "^0.3.7" + } +} diff --git a/examples/bun/server.ts b/examples/bun/server.ts new file mode 100644 index 0000000..406ab11 --- /dev/null +++ b/examples/bun/server.ts @@ -0,0 +1,69 @@ +import * as Rian from 'rian/async'; +import { exporter } from 'rian/exporter.otel.http'; + +Rian.configure('bun-api', { + 'bun.version': Bun.version, +}); + +async function get_data() { + return Rian.span('get_data')(async () => { + const users = await Rian.span('SELECT * FROM users')(async (s) => { + s.set_context({ + 'db.system': 'mysql', + }); + + await new Promise((resolve) => setTimeout(resolve, 100)); + + return [{ user: 'test' }]; + }); + + Rian.currentSpan().set_context({ + 'users.count': users.length, + }); + + return users; + }); +} + +const tracer = Rian.tracer('bun-api'); + +setInterval(() => { + Rian.report( + exporter((p) => + fetch('http://127.0.0.1:3000', { + method: 'POST', + body: JSON.stringify(p), + }), + ), + ); +}, 1e3); + +Bun.serve({ + port: 8080, + async fetch(req: Request) { + const u = new URL(req.url); + + return tracer(async () => + Rian.span(`${req.method} ${u.pathname}`)(async (s) => { + s.set_context({ + 'http.method': req.method, + 'http.pathname': u.pathname, + 'http.scheme': u.protocol.replace(':', ''), + 'http.host': u.host, + 'http.port': u.port, + 'bun.development': this.development, + }); + + // Routing + // ----------------------------------------- + + if (u.pathname == '/users') { + const users = await get_data(); + return new Response(JSON.stringify(users)); + } + + return new Response('', { status: 404 }); + }), + ); + }, +}); diff --git a/examples/bun/tsconfig.json b/examples/bun/tsconfig.json new file mode 100644 index 0000000..b4ecf7e --- /dev/null +++ b/examples/bun/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "types": ["bun-types"] + } +}