33const http = require ( 'http' ) ;
44const express = require ( 'express' ) ;
55const mysql = require ( 'mysql2' ) ;
6+ const bunyan = require ( 'bunyan' ) ;
67const { S3Client, GetBucketLocationCommand } = require ( '@aws-sdk/client-s3' ) ;
78
89const PORT = parseInt ( process . env . SAMPLE_APP_PORT || '8000' , 10 ) ;
910
1011const app = express ( ) ;
1112
13+ // Create bunyan logger
14+ const logger = bunyan . createLogger ( { name : 'express-app' , level : 'info' } ) ;
15+
1216app . get ( '/healthcheck' , ( req , res ) => {
13- console . log ( ` /healthcheck called successfully` )
17+ logger . info ( ' /healthcheck called successfully' ) ;
1418 res . send ( 'healthcheck' ) ;
1519} ) ;
1620
@@ -23,12 +27,14 @@ app.get('/outgoing-http-call', (req, res) => {
2327 const httpRequest = http . request ( options , ( rs ) => {
2428 rs . setEncoding ( 'utf8' ) ;
2529 rs . on ( 'data' , ( result ) => {
26- console . log ( `/outgoing-http-call called successfully` )
27- res . send ( `/outgoing-http-call called successfully` ) ;
30+ const msg = '/outgoing-http-call called successfully' ;
31+ logger . info ( msg ) ;
32+ res . send ( msg ) ;
2833 } ) ;
2934 rs . on ( 'error' , ( err ) => {
30- console . log ( `/outgoing-http-call called with error: ${ err } ` )
31- res . send ( `/outgoing-http-call called with error: ${ err } ` ) ;
35+ const msg = `/outgoing-http-call called with error: ${ err } ` ;
36+ logger . error ( msg ) ;
37+ res . send ( msg ) ;
3238 } ) ;
3339 } ) ;
3440 httpRequest . end ( ) ;
@@ -43,19 +49,21 @@ app.get('/aws-sdk-call', async (req, res) => {
4349 Bucket : bucketName ,
4450 } ) ,
4551 ) . then ( ( data ) => {
46- console . log ( '/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data ) ;
47- res . send ( '/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data ) ;
52+ const msg = '/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data ;
53+ logger . info ( msg ) ;
54+ res . send ( msg ) ;
4855 } ) ;
4956 } catch ( e ) {
5057 if ( e instanceof Error ) {
51- console . log ( '/aws-sdk-call called successfully' )
52- res . send ( '/aws-sdk-call called successfully' ) ;
58+ const msg = '/aws-sdk-call called successfully' ;
59+ logger . info ( msg ) ;
60+ res . send ( msg ) ;
5361 }
5462 }
5563} ) ;
5664
5765app . get ( '/remote-service' , ( req , res ) => {
58- const endpoint = req . query . ip || 'localhost ' ;
66+ const endpoint = req . query . ip || '********* ' ;
5967 const options = {
6068 hostname : endpoint ,
6169 port : 8001 ,
@@ -66,74 +74,76 @@ app.get('/remote-service', (req, res) => {
6674 const request = http . request ( options , ( rs ) => {
6775 rs . setEncoding ( 'utf8' ) ;
6876 rs . on ( 'data' , ( result ) => {
69- console . log ( `/remote-service called successfully: ${ result } ` ) ;
70- res . send ( `/remote-service called successfully: ${ result } ` ) ;
77+ const msg = `/remote-service called successfully: ${ result } ` ;
78+ logger . info ( msg ) ;
79+ res . send ( msg ) ;
7180 } ) ;
7281 } ) ;
7382 request . on ( 'error' , ( err ) => {
74- console . log ( '/remote-service called with errors: ' + err . errors ) ;
75- res . send ( '/remote-service called with errors: ' + err . errors ) ;
76- } )
83+ const msg = '/remote-service called with errors: ' + err . errors ;
84+ logger . error ( msg ) ;
85+ res . send ( msg ) ;
86+ } ) ;
7787 request . end ( ) ;
7888} ) ;
7989
80- // The following logic serves as the async call made by the /client-call API
8190let makeAsyncCall = false ;
8291setInterval ( ( ) => {
8392 if ( makeAsyncCall ) {
8493 makeAsyncCall = false ;
85- console . log ( 'Async call triggered by /client-call API' ) ;
94+ logger . info ( 'Async call triggered by /client-call API' ) ;
8695
8796 const request = http . get ( 'http://local-root-client-call' , ( rs ) => {
8897 rs . setEncoding ( 'utf8' ) ;
8998 rs . on ( 'data' , ( result ) => {
90- res . send ( `GET local-root-client-call response: ${ result } ` ) ;
99+ const msg = `GET local-root-client-call response: ${ result } ` ;
100+ logger . info ( msg ) ;
101+ res . send ( msg ) ;
91102 } ) ;
92103 } ) ;
93104 request . on ( 'error' , ( err ) => { } ) ; // Expected
94105 request . end ( ) ;
95106 }
96- } , 5000 ) ; // Check every 5 seconds
107+ } , 5000 ) ;
97108
98109app . get ( '/client-call' , ( req , res ) => {
99- res . send ( '/client-call called successfully' ) ;
100- console . log ( '/client-call called successfully' ) ;
101-
102- // Trigger async call to generate telemetry for InternalOperation use case
110+ const msg = '/client-call called successfully' ;
111+ logger . info ( msg ) ;
112+ res . send ( msg ) ;
103113 makeAsyncCall = true ;
104114} ) ;
105115
106116app . get ( '/mysql' , ( req , res ) => {
107- // Create a connection to the MySQL database
108117 const connection = mysql . createConnection ( {
109118 host : process . env . RDS_MYSQL_CLUSTER_ENDPOINT ,
110119 user : process . env . RDS_MYSQL_CLUSTER_USERNAME ,
111120 password : process . env . RDS_MYSQL_CLUSTER_PASSWORD ,
112121 database : process . env . RDS_MYSQL_CLUSTER_DATABASE ,
113122 } ) ;
114123
115- // Connect to the database
116124 connection . connect ( ( err ) => {
117125 if ( err ) {
118- console . log ( '/mysql called with an error: ' , err . errors ) ;
119- return res . status ( 500 ) . send ( '/mysql called with an error: ' + err . errors ) ;
126+ const msg = '/mysql called with an error: ' + err . errors ;
127+ logger . error ( msg ) ;
128+ return res . status ( 500 ) . send ( msg ) ;
120129 }
121130
122- // Perform a simple query
123131 connection . query ( 'SELECT * FROM tables LIMIT 1;' , ( queryErr , results ) => {
124- // Close the connection
125132 connection . end ( ) ;
126133
127134 if ( queryErr ) {
128- return res . status ( 500 ) . send ( 'Could not complete http request to RDS database:' + queryErr . message ) ;
135+ const msg = 'Could not complete http request to RDS database:' + queryErr . message ;
136+ logger . error ( msg ) ;
137+ return res . status ( 500 ) . send ( msg ) ;
129138 }
130139
131- // Send the query results as the response
132- res . send ( `/outgoing-http-call response: ${ results } ` ) ;
140+ const msg = `/outgoing-http-call response: ${ results } ` ;
141+ logger . info ( msg ) ;
142+ res . send ( msg ) ;
133143 } ) ;
134144 } ) ;
135145} ) ;
136146
137147app . listen ( PORT , ( ) => {
138- console . log ( `Listening for requests on http://localhost:${ PORT } ` ) ;
139- } ) ;
148+ logger . info ( `Listening for requests on http://localhost:${ PORT } ` ) ;
149+ } ) ;
0 commit comments