11require ( 'dotenv-extended' ) . load ( ) ;
22const config = require ( './config' ) ;
3- var appInsights = require ( "applicationinsights" ) ;
43
5- if ( config . instrumentationKey ) {
6- appInsights . setup ( config . instrumentationKey )
4+ const appInsights = require ( "applicationinsights" ) ;
5+
6+ if ( config . aicstring ) {
7+ appInsights . setup ( config . aicstring )
78 . setAutoDependencyCorrelation ( true )
9+ . setAutoCollectRequests ( true )
10+ . setAutoCollectPerformance ( true , true )
11+ . setAutoCollectExceptions ( true )
812 . setAutoCollectDependencies ( true )
9- . setAutoCollectPerformance ( true )
13+ . setAutoCollectConsole ( true )
14+ . setUseDiskRetryCaching ( true )
1015 . setSendLiveMetrics ( true )
1116 . setDistributedTracingMode ( appInsights . DistributedTracingModes . AI_AND_W3C ) ;
12- appInsights . defaultClient . context . tags [ appInsights . defaultClient . context . keys . cloudRole ] = "http-calcback " ;
17+ appInsights . defaultClient . context . tags [ appInsights . defaultClient . context . keys . cloudRole ] = "http-backend " ;
1318 appInsights . start ( ) ;
14- var client = appInsights . defaultClient ;
15- client . commonProperties = {
19+ appInsights . defaultClient . commonProperties = {
1620 slot : config . version
1721 } ;
1822}
19-
20- var client = appInsights . defaultClient ;
23+ const swaggerUi = require ( 'swagger-ui-express' ) , swaggerDocument = require ( './swagger.json' ) ;
24+ const OS = require ( 'os' ) ;
2125
2226const express = require ( 'express' ) ;
2327const app = express ( ) ;
28+ app . use ( express . json ( ) )
2429const morgan = require ( 'morgan' ) ;
25-
26- var swaggerUi = require ( 'swagger-ui-express' ) ,
27- swaggerDocument = require ( './swagger.json' ) ;
28-
29- const OS = require ( 'os' ) ;
30-
31- // add logging middleware
3230app . use ( morgan ( 'dev' ) ) ;
3331
3432// Routes
@@ -40,12 +38,19 @@ app.get('/', function(req, res) {
4038} ) ;
4139app . get ( '/ping' , function ( req , res ) {
4240 console . log ( 'received ping' ) ;
43- var pong = { response : "pong!" , host : OS . hostname ( ) , version : config . version } ;
41+ const sourceIp = req . connection . remoteAddress ;
42+ const forwardedFrom = ( req . headers [ 'x-forwarded-for' ] || '' ) . split ( ',' ) . pop ( ) ;
43+ const pong = { response : "pong!" , correlation : "" , timestamp : new Date ( ) , host : OS . hostname ( ) , source : sourceIp , forwarded : forwardedFrom , version : config . version } ;
4444 console . log ( pong ) ;
45- res . send ( pong ) ;
45+ res . status ( 200 ) . send ( pong ) ;
4646} ) ;
4747app . get ( '/healthz' , function ( req , res ) {
48- res . send ( 'OK' ) ;
48+ const data = {
49+ uptime : process . uptime ( ) ,
50+ message : 'Ok' ,
51+ date : new Date ( )
52+ }
53+ res . status ( 200 ) . send ( data ) ;
4954} ) ;
5055
5156var primeFactors = function getAllFactorsFor ( remainder ) {
@@ -70,64 +75,50 @@ var primeFactors = function getAllFactorsFor(remainder) {
7075 return factors ;
7176}
7277
73- // curl -X POST --header "number: 3" --header "randomvictim: true" http://localhost:3002/api/calculation
74- app . post ( '/api/calculation' , function ( req , res ) {
78+ // curl -X POST --header "number: 3" --header "randomvictim: true" http://localhost:8082/api/calculate
79+ // curl -X POST --url http://localhost:8082/api/calculate --header 'content-type: application/json' --data '{"number": "42", "randomvictim": "true", "laggy": "true"}'
80+ app . post ( '/api/calculate' , function ( req , res ) {
7581 console . log ( "received client request:" ) ;
7682 console . log ( req . headers ) ;
77- if ( config . instrumentationKey ) {
78- var startDate = new Date ( ) ;
79- client . trackEvent ( { name : "calculation-js-backend-call" } ) ;
80- }
83+ console . log ( req . body ) ;
84+ console . log ( req . body . number ) ;
85+ const requestId = req . headers [ 'traceparent' ] || '' ;
8186 var resultValue = [ 0 ] ;
8287 try {
83- resultValue = primeFactors ( req . headers . number ) ;
88+ resultValue = primeFactors ( req . body . number ) ;
8489 console . log ( "calculated:" ) ;
8590 console . log ( resultValue ) ;
8691 } catch ( e ) {
92+ console . log ( "correlation: " + requestId ) ;
8793 console . log ( e ) ;
88- if ( config . instrumentationKey ) {
89- client . trackException ( e ) ;
90- }
9194 resultValue = [ 0 ] ;
9295 }
93- var endDate = new Date ( ) ;
94- if ( config . instrumentationKey ) {
95- var duration = endDate - startDate ;
96- client . trackEvent ( { name : "calculation-js-backend-result" } ) ;
97- client . trackMetric ( { name :"calculation-js-backend-duration" , value : duration } ) ;
98- }
99- if ( req . headers . joker ) {
100- resultValue = "42" ;
101- }
96+ const endDate = new Date ( ) ;
10297
103- var randomNumber = Math . floor ( ( Math . random ( ) * 20 ) + 1 ) ;
98+ const randomNumber = Math . floor ( ( Math . random ( ) * 20 ) + 1 ) ;
99+ const remoteAddress = req . connection . remoteAddress ;
100+ const forwardedFrom = ( req . headers [ 'x-forwarded-for' ] || '' ) . split ( ',' ) . pop ( ) ;
104101
105- if ( ( req . headers . randomvictim && req . headers . randomvictim === true ) || ( config . buggy && randomNumber > 19 ) ) {
102+ if ( ( req . body . randomvictim && req . body . randomvictim === true ) || ( config . buggy && randomNumber > 19 ) ) {
106103 console . log ( "looks like a 19 bug" ) ;
107- res . status ( 500 ) . send ( { value : "[ b, u, g]" , error : "looks like a 19 bug" , host : OS . hostname ( ) , remote : remoteAddress , version : config . version } ) ;
104+ res . status ( 500 ) . send ( { timestamp : endDate , correlation : requestId , values : [ 'b' , 'u' , 'g' ] , host : OS . hostname ( ) , remote : remoteAddress , forwarded : forwardedFrom , version : config . version } ) ;
108105 }
109106 else {
110- var remoteAddress = req . connection . remoteAddress ;
111- var serverResult = JSON . stringify ( { timestamp : endDate , value : resultValue , host : OS . hostname ( ) , remote : remoteAddress , version : config . version } ) ;
107+ const serverResult = { timestamp : endDate , correlation : requestId , values : resultValue , host : OS . hostname ( ) , remote : remoteAddress , forwarded : forwardedFrom , version : config . version } ;
112108 console . log ( serverResult ) ;
113- res . send ( serverResult . toString ( ) ) ;
109+ res . status ( 200 ) . send ( serverResult ) ;
114110 }
115111} ) ;
116112
117113app . post ( '/api/dummy' , function ( req , res ) {
118114 console . log ( "received dummy request:" ) ;
119- console . log ( req . headers )
120- if ( config . instrumentationKey ) {
121- client . trackEvent ( { name : "dummy-js-backend-call" } ) ;
122- }
115+ console . log ( req . headers ) ;
116+ console . log ( req . body ) ;
123117 res . send ( '42' ) ;
124118} ) ;
125119
126120console . log ( config ) ;
127121console . log ( OS . hostname ( ) ) ;
128- // Listen
129- if ( config . instrumentationKey ) {
130- client . trackEvent ( { name : "js-backend-initializing" } ) ;
131- }
122+
132123app . listen ( config . port ) ;
133124console . log ( 'Listening on localhost:' + config . port ) ;
0 commit comments