Skip to content

Instantly share code, notes, and snippets.

@sumjha
Created April 30, 2019 06:16
Show Gist options
  • Save sumjha/ba3c82796e86bb6e11aad8a3ef5f699f to your computer and use it in GitHub Desktop.
Save sumjha/ba3c82796e86bb6e11aad8a3ef5f699f to your computer and use it in GitHub Desktop.
Sample node.js script to post data to custom metric api endpoint of SAP Cloud Platform's Auto Scaler Service
var request = require("request");
var cfenv = require("cfenv")
var appEnv = cfenv.getAppEnv()
var as_cred = appEnv.getServiceCreds('as');
var custom_metric_url = as_cred.custom_metrics.url+'/v1/apps/'+process.env.CF_INSTANCE_GUID+'/metrics',
auth = "Basic " + Buffer.from(as_cred.custom_metrics.username+ ":" +as_cred.custom_metrics.password).toString("base64");
console.log('user name ='+as_cred.custom_metrics.username)
console.log('password ='+ as_cred.custom_metrics.password)
console.log('url='+as_cred.custom_metrics.url)
console.log('Instance Index='+process.env.CF_INSTANCE_INDEX)
console.log('GUID ='+process.env.CF_INSTANCE_GUID)
console.log('basic auth ='+auth)
var options = { method: 'POST',
url:custom_metric_url,
headers:
{
Authorization:auth,
},
body:
{ instance_index: process.env.CF_INSTANCE_INDEX,
metrics: [ { name: 'testmetric', value: 30, unit: 'unit' } ] },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment