Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Add Cloud Profiler to Forseti server #3113

Merged
merged 6 commits into from
Aug 21, 2019
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
2 changes: 1 addition & 1 deletion google/cloud/forseti/common/gcp_api/_base_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

import google_auth_httplib2
import googleapiclient
import uritemplate
from googleapiclient import discovery
from ratelimiter import RateLimiter
from retrying import retry
import uritemplate

import google.auth
from google.auth.credentials import with_scopes_if_required
Expand Down
30 changes: 27 additions & 3 deletions google/cloud/forseti/services/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@

LOGGER = logger.get_logger(__name__)

try:
import googlecloudprofiler
LOGGER.info('Cloud Profiler library successfully imported.')
CLOUD_PROFILER_IMPORTED = True
except ImportError:
LOGGER.warning('Cannot enable Cloud Profiler because the '
'`googlecloudprofiler` library was not found. Run '
'`sudo pip3 install .[profiler]` to install '
'Cloud Profiler.')
CLOUD_PROFILER_IMPORTED = False

SERVICE_MAP = {
'explain': GrpcExplainerFactory,
'inventory': GrpcInventoryFactory,
Expand Down Expand Up @@ -141,9 +152,6 @@ def check_args(args):
return (0, 'All good!')


# pylint: enable=too-many-locals


def main():
"""Run."""
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -176,6 +184,10 @@ def main():
'--enable_console_log',
action='store_true',
help='Print log to console.')
parser.add_argument(
'--enable_profiler',
action='store_true',
help='Enable Cloud Profiler.')

args = vars(parser.parse_args())

Expand All @@ -186,6 +198,18 @@ def main():
parser.print_usage()
sys.exit(exit_code)

if args['enable_profiler'] and CLOUD_PROFILER_IMPORTED:
try:
googlecloudprofiler.start(
service='forseti-server',
verbose=2,
# project_id must be set if not running on GCP, such as in your
# local dev environment.
# project_id='my_project_id',
)
except (ValueError, NotImplementedError) as exc:
LOGGER.warning('Unable to enable Cloud Profiler: %s', exc)

serve(args['endpoint'],
args['services'],
args['forseti_db'],
Expand Down
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ enable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
disable=no-name-in-module,relative-import,suppressed-message,locally-disabled,locally-enabled,too-few-public-methods
disable=no-name-in-module,relative-import,suppressed-message,locally-disabled,locally-enabled,too-few-public-methods,ungrouped-imports

[REPORTS]

Expand Down
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
'sqlalchemy-migrate==0.11.0'
]

OPTIONAL_PACKAGES = {
'profiler': [
'google-cloud-profiler==1.0.8'
]
}

if sys.version_info.major < 3:
sys.exit('Sorry, Python 2 is not supported.')

Expand Down Expand Up @@ -122,6 +128,7 @@ def run(self):
install_requires=REQUIRED_PACKAGES,
setup_requires=REQUIRED_PACKAGES,
tests_require=REQUIRED_PACKAGES,
extras_require=OPTIONAL_PACKAGES,
packages=find_packages(exclude=[
'*.tests', '*.tests.*', 'tests.*', 'tests']),
include_package_data=True,
Expand Down