Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions functions/log/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
12 changes: 12 additions & 0 deletions functions/log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,17 @@ function getMetrics (callback) {
// [END functions_log_get_metrics]
}

// [START functions_log_stackdriver]
exports.processLogEntry = (data, callback) => {
const dataBuffer = Buffer.from(data.data.data, 'base64');
const logEntry = JSON.parse(dataBuffer.toString('ascii')).protoPayload;

console.log(`Method: ${logEntry.methodName}`);
console.log(`Resource: ${logEntry.resourceName}`);
console.log(`Initiator: ${logEntry.authenticationInfo.principalEmail}`);
callback();
};
// [END functions_log_stackdriver]

exports.getLogEntries = getLogEntries;
exports.getMetrics = getMetrics;
28 changes: 28 additions & 0 deletions functions/log/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,31 @@ test.serial(`getMetrics: should retrieve metrics`, (t) => {

t.is(callback.callCount, 1);
});

test(`processLogEntry: should process log entry`, (t) => {
const sample = getSample();
const callback = sinon.stub();

const json = JSON.stringify({
protoPayload: {
methodName: 'method',
resourceName: 'resource',
authenticationInfo: {
principalEmail: '[email protected]'
}
}
});

const data = {
data: {
data: Buffer.from(json, 'ascii')
}
};

sample.program.processLogEntry(data, callback);

t.true(console.log.calledWith(`Method: method`));
t.true(console.log.calledWith(`Resource: resource`));
t.true(console.log.calledWith(`Initiator: [email protected]`));
t.is(callback.callCount, 1);
});