Skip to content

Commit

Permalink
Enhance the output with request headers and env variable (hostname)
Browse files Browse the repository at this point in the history
  • Loading branch information
haf-tech committed Aug 22, 2020
1 parent 53b343c commit 375696a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
24 changes: 21 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const promClient = require('prom-client');
const promBundle = require("express-prom-bundle");
const app = require('express')()
const axios = require('axios');
const util = require('util');

// ############# Prometheus
// include HTTP method and URL path into the labels
Expand Down Expand Up @@ -104,13 +105,28 @@ if(app.get('envTracingEnabled')) {
// ############# Entry points
app.get('/', (req, res) => {

var ret = "[" + app.get('envTektonName') + "]: Hello from NodeJS Playground! TEKTON_101_ENV_EXAMPLE=" + app.get('envTektonExample');
//var ret = "[" + app.get('envTektonName') + "]: Hello from NodeJS Playground! TEKTON_101_ENV_EXAMPLE=" + app.get('envTektonExample');
var ret = util.format("[%s]: Hello from NodeJS Playground! TEKTON_101_ENV_EXAMPLE=%s\n\n", app.get('envTektonName'), app.get('envTektonExample'));
var userAgent = req.get('User-Agent');
console.log('user-agent: ' + userAgent);

// Prometheus Metric: inc and set the user agent
counterUserAgent.labels(userAgent).inc();

// append some env vars
ret += "Environment variables:\n";
ret += util.format("%s=%s\n", "HOSTNAME", (process.env.HOSTNAME || 'localhost'));

// append all headers
ret += "\n\nRequest Headers:\n";
for (const headerName in req.headers) {

const headerValue = req.headers[headerName];
ret += util.format("%s=%s\n", headerName.toUpperCase(), headerValue);

}


// simulated processing
var processDelay = app.get('envDelay');
sleep(processDelay).then(() => {
Expand Down Expand Up @@ -234,14 +250,16 @@ function callBackendService(ret, req, res) {

app.listen(app.get('port'), app.get('ip'), function() {
console.log("App.Version: " + process.env.npm_package_version)
console.log("--------------------------------------------------------------------")
console.log("ENV.TEKTON_101: " + app.get('envTektonName'))
console.log("ENV.TEKTON_101_ENV_EXAMPLE: " + app.get('envTektonExample'))
console.log("ENV.TEKTON_101_ENV_DELAY: " + app.get('envDelay'))
console.log("ENV.TEKTON_101_ENV_BACKEND_SERVICE: " + app.get('envBackendService'))
console.log("ENV.TEKTON_101_ENV_BACKEND_SERVICE_DELAY: " + app.get('envBackendServiceDelay'))
console.log("ENV.TEKTON_101_ENV_TRACING_ENABLED: " + app.get('envTracingEnabled'))

console.log("Node app is running at localhost:" + app.get('port'))
console.log("--------------------------------------------------------------------")
console.log("Node app is running at http://localhost:" + app.get('port'))
console.log("--------------------------------------------------------------------")
})

module.exports.app = app;
7 changes: 4 additions & 3 deletions devfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ components:
- id: che-incubator/typescript/latest
memoryLimit: 256Mi
type: chePlugin
- id: redhat/vscode-openshift-connector/latest
type: chePlugin
# plugin has high memory utilization
# - id: redhat/vscode-openshift-connector/latest
# type: chePlugin
- mountSources: true
endpoints:
- name: nodejs
port: 3000
memoryLimit: 256Mi
memoryLimit: 512Mi
type: dockerimage
alias: nodejs
image: >-
Expand Down

0 comments on commit 375696a

Please sign in to comment.