15.0.1 • Published 7 months ago

gcf-utils v15.0.1

Weekly downloads
535
License
Apache-2.0
Repository
github
Last release
7 months ago

gcf-utils

Helper module to make Probot applications easier and more secure to run in Google Cloud Functions

Usage

GCFBootstrapper

In your_bot/src/app.ts:

import {GCFBootstrapper} from 'gcf-utils';
import {handler} from './your-bot';        // the handler that will be called for probot events, of type ApplicationFunction

const bootstrap = new GCFBootstrapper();

const wrapOptions =  {
  background: true,     // enables the use of Cloud Tasks to execute in the background
  logging: true,        // enables automatic logging of metrics
}

module.exports['your_bot'] = bootstrap.gcf(handler, wrapOptions);

GCFLogger

GCFLogger is a standardized logger for Google Cloud Functions. It has in-built support for:

gcf-util exports a configured instance of GCFLogger for bots to use. This instance will be synchronously flushed after the Bot's app function has completed, so you do not need to flush this in your Bot's code.

Import this in your bot

import {logger} from 'gcf-utils';

Log a string statement

logger.info('An info message');  // will show up as INFO log in Cloud Logging
logger.debug('A debug message');  // will show up as DEBUG log in Cloud Logging
logger.error('An error message');  // will show up as ERROR log in Cloud Logging

Log a structured JSON

logger.debug({ 'debug-property': 'value' });

This means you don't have to stringify errors anymore:

try {
    methodThatThrows()
} catch (errorThrown) {      // errorThrown = { name: 'HTTP Error', code: 404 }
    logger.error({
        message: `methodThatThrows threw an error`
        error: errorThrown
    })
}

Cloud Logging output for the lines above:

{
    ... other log properties
    jsonPayload: {
        message: `methodThatThrows threw an error`
        error: {
            name: 'HTTP Error',
            code: 404
        }
    }
    ... other log properties
}

Synchronously Flush Logs

:warning: If you are importing logger from gcf-utils as shown above, you do not need to call the synchronous flush method. gcf-utils automatically handles this once the Bot's application function completes

:warning: GCFLogger.flush() only works for SonicBoom destinations, which is the default destination for this logger

To synchronously flush logs:

import {logger} from 'gcf-utils';

logger.flush()

Automatically Logged Metrics

gcf-utils logs some metrics automatically with the help of GCFLogger. This is completely transparent to the Bot and requires no action from the Bot developer.

:warning: If your Bot is not automatically logging the information above, please ensure that you are using gcf-utils 5.5.1 or above and you have logging: true in the WrapperOptions passed to GCFBootstrapper.gcf()

Execution Trigger Information

gcf-utils automatically logs information related to the trigger that initiated the Bot's execution. The following properties are logged:

PropertyValue TypeValueNotes
messagestringA descriptive message about the trigger
triggerobjectObject containing trigger information
____trigger_typestringTrigger source (eg. Github Webhook, Cloud Scheduler)
____trigger_senderstringGitHub username of user that triggered the webhookOnly for GitHub WebHook triggers
____github_delivery_guidstringGUID of the GitHub WebHookOnly for GitHub WebHook triggers and subsequent Cloud Tasks
____payload_hashstringAn MD5 hash of the GitHub Event PayloadOnly for GitHub WebHook triggers
____trigger_source_repoobjectObject containing GitHub source repository informationOnly for GitHub WebHook triggers
____ownerstringUsername of the repository ownerOnly for GitHub WebHook triggers
____owner_typestringThe type of owner (eg. 'User' or 'Organization')Only for GitHub WebHook triggers
____repo_namestringThe name of the GitHub repositoryOnly for GitHub WebHook triggers
____urlstringURL to the GitHub repository (may be private)Only for GitHub WebHook triggers
GitHub Action Information

gcf-utils automatically logs information related to any actions taken by a Bot on GitHub.

Note: only calls to GitHub that make a change to any GitHub objects are logged. Calls to GitHub that simply retrieve information are not logged. For example, if a Bot retrieves a list of issues from a repository and then adds a label to some issues, only the 'add label' actions will be logged, not the retrieving of the issues.

The following properties are logged:

PropertyValue TypeValueNotes
actionobjectObject containing action information
____typestringThe type of action (eg. ISSUE_ADD_LABEL)
____valuestringThe value associated with the action. (eg. name of label)If action has no associated value, then value is 'NONE'
____destination_objectobjectThe GitHub object that received the actionOnly for actions that have an associated object
____object_typestringThe type of object (eg. PULL_REQUEST)Only for actions that have an associated object
____object_idnumberThe GitHub ID corresponding to the objectOnly for actions that have an associated object
____destination_repoobjectThe GitHub repository that received the action
____repo_namestringThe name of the GitHub repository
____ownerstringThe username of the GitHub repository owner

Manually Log Metrics

GCFLogger also allows for custom logs-based metrics in addition to the metrics logged above.

To log a metric for the event my_bot.event with additional structured information url:

import {logger} from 'gcf-utils'

logger.metric('my_bot.event', {url: 'http://www.example.com'})

Where url could be any additional structured information you wish to associate with the metric.

Development Setup

# Install dependencies
npm install

Testing

Run Unit Tests

Run npm run test from the root directory to run the unit tests

Run Integration/System Tests

Additional setup is required to run the test/integration/gcf-bootstrapper-integration tests. If you don't wish to run these tests, you can skip this setup. Else, follow these steps:

  1. Create a GitHub personal access token
  2. Create a test GitHub App and give it the necessary permissions
  3. Navigate to https://github.com/settings/apps/{your-app} to find the necessary information for step 4
  4. Enable secret manager on your GCP project and create a new secret with the following values:
{
    "id": <your GitHub app id>,
    "cert": <your GitHub app private key at the bottom of the page>,
    "secret": <your GitHub app's webhook secret (not client secret)>,
    "githubToken": <your personal access token from step 1>
}
  1. Create a file in gcf-utils root directory called ".env" with the following:
PROJECT_ID=<your GCP project id>
GCF_SHORT_FUNCTION_NAME=<the name of your secret>

Run npm run system-test from the root directory to run the system tests

Contributing

If you have suggestions for how gcf-utils could be improved, or want to report a bug, open an issue! We'd love all and any contributions.

For more, check out the Contributing Guide.

License

Apache 2.0 © 2019 Google Inc.

15.0.0

7 months ago

15.0.1

7 months ago

14.4.5

1 year ago

14.4.6

1 year ago

14.3.1

1 year ago

14.3.2

1 year ago

14.3.3

1 year ago

14.4.0

1 year ago

14.4.1

1 year ago

14.4.2

1 year ago

14.4.3

1 year ago

14.4.4

1 year ago

14.1.0

2 years ago

14.1.1

2 years ago

14.2.0

2 years ago

14.2.1

2 years ago

14.3.0

2 years ago

14.0.2

2 years ago

13.7.0

2 years ago

13.8.4

2 years ago

13.8.2

2 years ago

13.8.3

2 years ago

13.8.0

2 years ago

13.8.1

2 years ago

13.5.3

2 years ago

13.5.4

2 years ago

13.6.0

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

13.3.1

2 years ago

13.4.0

2 years ago

13.5.1

2 years ago

13.5.2

2 years ago

13.5.0

2 years ago

13.3.0

2 years ago

13.3.2-beta.2

2 years ago

13.3.2-beta.0

2 years ago

13.3.2-beta.1

2 years ago

13.2.2

2 years ago

13.2.0

2 years ago

13.2.1

2 years ago

13.1.0

2 years ago

13.0.2

3 years ago

13.0.1

3 years ago

13.0.0

3 years ago

12.0.0

3 years ago

11.4.3

3 years ago

11.4.2

3 years ago

11.4.1

3 years ago

11.4.0

3 years ago

11.3.1

3 years ago

11.3.0-debug.1

3 years ago

11.3.0-debug.2

3 years ago

11.3.0-debug.3

3 years ago

11.3.0-debug.4

3 years ago

11.3.0-debug.0

3 years ago

11.2.0

3 years ago

11.3.0

3 years ago

11.1.0

3 years ago

11.0.2

3 years ago

11.0.0

3 years ago

11.0.1

3 years ago

10.1.0

3 years ago

10.0.2

3 years ago

10.0.1

3 years ago

10.0.0

3 years ago

8.0.2

3 years ago

7.2.2

3 years ago

7.2.1

3 years ago

7.2.0

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

7.1.4

3 years ago

7.1.3

3 years ago

7.1.2-debug

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.0.28-debug

3 years ago

7.0.27-debug

3 years ago

7.0.26-debug

3 years ago

7.0.24-debug

3 years ago

7.0.25-debug

3 years ago

7.0.20-debug

3 years ago

7.0.15-debug

3 years ago

7.0.16-debug

3 years ago

7.0.19-debug

3 years ago

7.0.14-debug

3 years ago

7.0.21-debug

3 years ago

7.0.23-debug

3 years ago

7.0.12-debug

3 years ago

7.1.0

3 years ago

7.0.17-debug

3 years ago

7.0.22-debug

3 years ago

7.0.13-debug

3 years ago

7.0.18-debug

3 years ago

7.0.10-debug

3 years ago

7.0.11-debug

3 years ago

7.0.9-debug

3 years ago

7.0.8-debug

3 years ago

7.0.7-debug

3 years ago

7.0.6-debug

3 years ago

7.0.5-debug

3 years ago

7.0.4-debug

3 years ago

7.0.3-debug

3 years ago

7.0.3

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.3.1

3 years ago

6.3.0

3 years ago

6.2.0

3 years ago

6.1.3

3 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.0

4 years ago

6.0.0-beta.7

4 years ago

6.0.0-beta.6

4 years ago

6.0.0-beta.5

4 years ago

6.0.0-beta.4

4 years ago

6.0.0-beta.3

4 years ago

6.0.0-beta.2

4 years ago

6.0.0-beta.1

4 years ago

6.0.0-beta.0

4 years ago

5.5.1

4 years ago

5.5.0

4 years ago

5.4.0

4 years ago

5.3.2-logging-2

4 years ago

5.3.2-logging-1

4 years ago

5.3.2-logging-0

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.6

4 years ago

5.2.5

4 years ago

5.2.4

4 years ago

5.2.4-beta.1

4 years ago

5.2.4-beta.0

4 years ago

5.2.3

4 years ago

5.2.2

4 years ago

5.2.1

4 years ago

5.2.0

4 years ago

5.1.0

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

3.0.4

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.3

4 years ago

3.0.2-debug.8

4 years ago

3.0.2-debug.7

4 years ago

3.0.2-debug.6

4 years ago

3.0.2-debug.5

4 years ago

3.0.2-debug.4

4 years ago

3.0.2-debug.3

4 years ago

3.0.2-debug.2

4 years ago

3.0.2-debug.1

4 years ago

3.0.2-debug.0

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.3.0

4 years ago

1.3.0-beta.4

4 years ago

1.3.0-beta.3

4 years ago

1.3.0-beta.2

4 years ago

1.3.0-beta.1

4 years ago

1.3.0-beta.0

4 years ago

1.2.0

4 years ago

1.2.0-beta.3

4 years ago

1.2.0-beta.2

4 years ago

1.2.0-beta.1

4 years ago

1.2.0-beta.0

4 years ago

1.1.0

4 years ago

1.0.0

5 years ago

0.6.0

5 years ago

0.4.0

5 years ago