Skip to content

Instantly share code, notes, and snippets.

@josephlewis42
Created May 4, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josephlewis42/0aa5368ecc1fe9d177afbdf2ab892cbf to your computer and use it in GitHub Desktop.
Save josephlewis42/0aa5368ecc1fe9d177afbdf2ab892cbf to your computer and use it in GitHub Desktop.
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