Created
September 15, 2023 16:25
-
-
Save alvarof2/fa9e0221664cb381eba1b57985ffa5cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('@google-cloud/functions-framework'); | |
const Prometheus = require('prom-client'); | |
const http = require('node:http'); | |
function call_sign () { | |
return new Promise(function(resolve, reject){ | |
console.log('Request (promise) to ' + process.env.HOST_REQUEST + ' executing...'); | |
var options = { | |
host: process.env.HOST_REQUEST, | |
port: 80, | |
method: 'GET' | |
}; | |
var req = http.request(options, function(res) { | |
// console.log('STATUS: ' + res.statusCode); | |
// console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
// res.setEncoding('utf8'); | |
// res.on('data', function (chunk) { | |
// console.log('BODY: ' + chunk); | |
// }); | |
success.inc(); | |
resolve(res.statusCode); | |
}); | |
req.on('error', function(e) { | |
fail.inc(); | |
reject(e.message); | |
}); | |
// write data to request body | |
req.end(); | |
total.inc(); | |
}) | |
}; | |
console.log('Creating metric'); | |
const gateway = new Prometheus.Pushgateway('https://pag.integration-tests.celo-networks-dev.org', { | |
timeout: 5000, //Set the request timeout to 5000ms | |
}); | |
const prefix = 'test_http_requests_sent'; | |
const total = new Prometheus.Counter({ | |
name: `${prefix}_total`, | |
help: `Total number of sent requests`, | |
}); | |
const success = new Prometheus.Counter({ | |
name: `${prefix}_success`, | |
help: `Total number of succesful sent requests`, | |
}); | |
const fail = new Prometheus.Counter({ | |
name: `${prefix}_failed`, | |
help: `Total number of failed sent requests`, | |
}); | |
functions.http('helloHttp', (req, res) => { | |
console.log('Sending request (promise) to ' + process.env.HOST_REQUEST + ' ...'); | |
call_sign() | |
.then(resolve => { | |
console.log('Promise resolved!!'); | |
console.log('Request status code: ' + resolve); | |
console.log(''); | |
}) | |
.catch(err => { | |
console.log('Promise rejected!!'); | |
console.log('Error sending request: ' + err); | |
console.log(''); | |
}) | |
.finally(testing => { | |
console.log('Pushing metrics...'); | |
gateway.pushAdd({ jobName: 'test' }) | |
.then(({resp}) => { | |
console.log('Successfully pushed metrics with status code: ' + resp.statusCode); | |
}) | |
.catch(err => { | |
console.log('Error pushing metrics: ' + err); | |
}) | |
.finally(() => { | |
console.log('Resetting metrics'); | |
Prometheus.register.resetMetrics() | |
}); | |
} | |
); | |
console.log('End main code, promise pending...'); | |
console.log(''); | |
res.send('Hello, Cloud Function running'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment