github.com/grafana/pyroscope@v1.18.0/examples/language-sdk-instrumentation/nodejs/tinyhttp/index.js (about)

     1  import { init, start } from '@pyroscope/nodejs';
     2  import { App } from '@tinyhttp/app';
     3  import { logger } from '@tinyhttp/logger';
     4  
     5  const port = process.env['PORT'] || 5000;
     6  const region = process.env['REGION'] || 'default';
     7  const appName = process.env['APP_NAME'] || 'tinyhttp';
     8  const pyroscopeUrl = process.env['PYROSCOPE_URL'] || 'http://pyroscope:4040';
     9  
    10  init({
    11    appName: appName,
    12    serverAddress: pyroscopeUrl,
    13    tags: { region },
    14  });
    15  start();
    16  
    17  const app = new App();
    18  app.use(logger());
    19  
    20  app.get('/', (_, res) => {
    21    res.send('Available routes are: /bike, /car, /scooter');
    22  })
    23  
    24  const genericSearchHandler = (p) => (_, res) => {
    25    const time = +new Date() + p * 1000;
    26    let i = 0;
    27    while (+new Date() < time) {
    28      i = i + Math.random();
    29    }
    30    res.send('Vehicle found');
    31  };
    32  
    33  app.get('/bike', (req, res) => {
    34    genericSearchHandler(0.5)(req, res);
    35  });
    36  
    37  app.get('/car', (req, res) => {
    38    genericSearchHandler(1)(req, res);
    39  });
    40  
    41  app.get('/scooter', (req, res) => {
    42    genericSearchHandler(0.25)(req, res);
    43  });
    44  
    45  app.listen(port, () => console.log(`Started on http://localhost:${port}`));