Skip to content

Commit

Permalink
GCF: Add Stackdriver trigger sample (#740)
Browse files Browse the repository at this point in the history
* Add stackdriver BigQuery trigger sample

* Make sample product-agnostic
  • Loading branch information
Ace Nassri authored Sep 25, 2018
1 parent 6189438 commit 292308b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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);
});

0 comments on commit 292308b

Please sign in to comment.