Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create fake HTTP style logs to test Stackdriver using Google Cloud Functions
/**
Copyright 2018 Google LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
// package.json
// {
// "name": "sample-http",
// "version": "0.0.1",
// "dependencies": {
// "faker": "4.1.0"
// }
// }
var faker = require('faker')
/**
* Generates fake HTTP logs to test stackdriver.
* Provide a "count" in your invocation to produce multiple logs. e.g. {"count":200}
*/
exports.helloWorld = (req, res) => {
var count = req.body.count || 1;
for(var i = 0; i < count; i++) {
console.log(generateFakeLog());
}
res.status(200).send('Sent: ' + count + ' logs like ' + generateFakeLog())
};
function generateFakeLog() {
var file = '/' + faker.system.fileName() + faker.system.fileExt()
return faker.internet.ip() + ' - - [' + faker.date.recent() + '] "GET ' + file + ' HTTP/1.1" ' + faker.internet.userAgent()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment