-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserRouter.js
More file actions
40 lines (32 loc) · 1.21 KB
/
userRouter.js
File metadata and controls
40 lines (32 loc) · 1.21 KB
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
// userRouter.js
const express = require('express');
const tracer = require('./tracing'); // Import the tracer from tracing.js
const { trace, context } = require('@opentelemetry/api');
const router = express.Router();
router.get('/profile', (req, res) => {
const span = tracer.startSpan('/getUserProfile');
// Simulate some work
setTimeout(() => {
console.log('Fetching user profile');
span.end(); // End the span when the work is done
res.send('User profile data');
}, 2000);
});
router.get('/settings', async (req, res) => {
const span = tracer.startSpan('/getUserSettings');
await context.with(trace.setSpan(context.active(), span), async () => {
await getUserSettings(); // Some asynchronous operation
span.end(); // End the span when the work is done
res.send('User settings data');
});
});
async function getUserSettings() {
const subSpan = tracer.startSpan('/fetchUserSettings', {
parent: trace.getSpan(context.active()), // Set the parent span
});
setTimeout(() => {
console.log('Fetched user settings');
subSpan.end(); // End the sub-span when the work is done
}, 1000);
}
module.exports = router;