Skip to content

Commit

Permalink
docs: adds bun example
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Nov 30, 2023
1 parent 4b21c15 commit c131cf2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Binary file added examples/bun/bun.lockb
Binary file not shown.
8 changes: 8 additions & 0 deletions examples/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"devDependencies": {
"bun-types": "1.0.14"
},
"dependencies": {
"rian": "^0.3.7"
}
}
69 changes: 69 additions & 0 deletions examples/bun/server.ts
Original file line number Diff line number Diff line change
@@ -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 });
}),
);
},
});
5 changes: 5 additions & 0 deletions examples/bun/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"types": ["bun-types"]
}
}

0 comments on commit c131cf2

Please sign in to comment.