Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 44 additions & 34 deletions sample-apps/node/frontend-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
const http = require('http');
const express = require('express');
const mysql = require('mysql2');
const bunyan = require('bunyan');
const { S3Client, GetBucketLocationCommand } = require('@aws-sdk/client-s3');

const PORT = parseInt(process.env.SAMPLE_APP_PORT || '8000', 10);

const app = express();

// Create bunyan logger
const logger = bunyan.createLogger({name: 'express-app', level: 'info'});

app.get('/healthcheck', (req, res) => {
console.log(`/healthcheck called successfully`)
logger.info('/healthcheck called successfully');
res.send('healthcheck');
});

Expand All @@ -23,12 +27,14 @@ app.get('/outgoing-http-call', (req, res) => {
const httpRequest = http.request(options, (rs) => {
rs.setEncoding('utf8');
rs.on('data', (result) => {
console.log(`/outgoing-http-call called successfully`)
res.send(`/outgoing-http-call called successfully`);
const msg = '/outgoing-http-call called successfully';
logger.info(msg);
res.send(msg);
});
rs.on('error', (err) => {
console.log(`/outgoing-http-call called with error: ${err}`)
res.send(`/outgoing-http-call called with error: ${err}`);
const msg = `/outgoing-http-call called with error: ${err}`;
logger.error(msg);
res.send(msg);
});
});
httpRequest.end();
Expand All @@ -43,19 +49,21 @@ app.get('/aws-sdk-call', async (req, res) => {
Bucket: bucketName,
}),
).then((data) => {
console.log('/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data);
res.send('/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data);
const msg = '/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data;
logger.info(msg);
res.send(msg);
});
} catch (e) {
if (e instanceof Error) {
console.log('/aws-sdk-call called successfully')
res.send('/aws-sdk-call called successfully');
const msg = '/aws-sdk-call called successfully';
logger.info(msg);
res.send(msg);
}
}
});

app.get('/remote-service', (req, res) => {
const endpoint = req.query.ip || 'localhost';
const endpoint = req.query.ip || '*********';
const options = {
hostname: endpoint,
port: 8001,
Expand All @@ -66,74 +74,76 @@ app.get('/remote-service', (req, res) => {
const request = http.request(options, (rs) => {
rs.setEncoding('utf8');
rs.on('data', (result) => {
console.log(`/remote-service called successfully: ${result}`);
res.send(`/remote-service called successfully: ${result}`);
const msg = `/remote-service called successfully: ${result}`;
logger.info(msg);
res.send(msg);
});
});
request.on('error', (err) => {
console.log('/remote-service called with errors: ' + err.errors);
res.send('/remote-service called with errors: ' + err.errors);
})
const msg = '/remote-service called with errors: ' + err.errors;
logger.error(msg);
res.send(msg);
});
request.end();
});

// The following logic serves as the async call made by the /client-call API
let makeAsyncCall = false;
setInterval(() => {
if (makeAsyncCall) {
makeAsyncCall = false;
console.log('Async call triggered by /client-call API');
logger.info('Async call triggered by /client-call API');

const request = http.get('http://local-root-client-call', (rs) => {
rs.setEncoding('utf8');
rs.on('data', (result) => {
res.send(`GET local-root-client-call response: ${result}`);
const msg = `GET local-root-client-call response: ${result}`;
logger.info(msg);
res.send(msg);
});
});
request.on('error', (err) => {}); // Expected
request.end();
}
}, 5000); // Check every 5 seconds
}, 5000);

app.get('/client-call', (req, res) => {
res.send('/client-call called successfully');
console.log('/client-call called successfully');

// Trigger async call to generate telemetry for InternalOperation use case
const msg = '/client-call called successfully';
logger.info(msg);
res.send(msg);
makeAsyncCall = true;
});

app.get('/mysql', (req, res) => {
// Create a connection to the MySQL database
const connection = mysql.createConnection({
host: process.env.RDS_MYSQL_CLUSTER_ENDPOINT,
user: process.env.RDS_MYSQL_CLUSTER_USERNAME,
password: process.env.RDS_MYSQL_CLUSTER_PASSWORD,
database: process.env.RDS_MYSQL_CLUSTER_DATABASE,
});

// Connect to the database
connection.connect((err) => {
if (err) {
console.log('/mysql called with an error: ', err.errors);
return res.status(500).send('/mysql called with an error: ' + err.errors);
const msg = '/mysql called with an error: ' + err.errors;
logger.error(msg);
return res.status(500).send(msg);
}

// Perform a simple query
connection.query('SELECT * FROM tables LIMIT 1;', (queryErr, results) => {
// Close the connection
connection.end();

if (queryErr) {
return res.status(500).send('Could not complete http request to RDS database:' + queryErr.message);
const msg = 'Could not complete http request to RDS database:' + queryErr.message;
logger.error(msg);
return res.status(500).send(msg);
}

// Send the query results as the response
res.send(`/outgoing-http-call response: ${results}`);
const msg = `/outgoing-http-call response: ${results}`;
logger.info(msg);
res.send(msg);
});
});
});

app.listen(PORT, () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
});
logger.info(`Listening for requests on http://localhost:${PORT}`);
});
3 changes: 2 additions & 1 deletion sample-apps/node/frontend-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/express": "^4.17.21",
"@types/node": "^20.14.6",
"express": "^4.21.2",
"mysql2": "^3.11.0"
"mysql2": "^3.11.0",
"bunyan": "^1.8.15"
}
}
13 changes: 9 additions & 4 deletions sample-apps/node/remote-service/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
'use strict';

const express = require('express');
const bunyan = require('bunyan');

const PORT = parseInt(process.env.SAMPLE_APP_PORT || '8001', 10);

const app = express();

// Create bunyan logger
const logger = bunyan.createLogger({name: 'remote-service', level: 'info'});

app.get('/healthcheck', (req, res) => {
console.log(`/healthcheck (remote-service) called successfully`);
res.send('/healthcheck (remote-service) called successfully');
const msg = '/healthcheck (remote-service) called successfully';
logger.info(msg);
res.send(msg);
});

app.listen(PORT, () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
});
logger.info(`Listening for requests on http://localhost:${PORT}`);
});
3 changes: 2 additions & 1 deletion sample-apps/node/remote-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@types/node": "^20.14.6",
"express": "^4.21.2",
"mysql2": "^3.11.0",
"nodemon": "^3.1.4"
"nodemon": "^3.1.4",
"bunyan": "^1.8.15"
}
}
Loading