Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCF: Add Stackdriver trigger sample #740

Merged
merged 2 commits into from
Sep 25, 2018
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: 'me@example.com'
}
}
});

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: me@example.com`));
t.is(callback.callCount, 1);
});