Skip to content

Commit

Permalink
feat(api): set up google cloud profiler (#2936)
Browse files Browse the repository at this point in the history
#2793

This PR attempts to set up Google Cloud Profiler following this
[guide](https://cloud.google.com/profiler/docs/profiling-python).
  • Loading branch information
cuixq authored Nov 29, 2024
1 parent 8a8febc commit 05fa231
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 13 deletions.
101 changes: 88 additions & 13 deletions gcp/api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gcp/api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ requests = "==2.32.3"
grpcio = "==1.64.1"
grpcio-reflection = "==1.62.2"
grpcio-health-checking = "==1.62.2"
google-cloud-profiler = "==4.1.0"

osv = { path = "../../", develop = true }

Expand Down
9 changes: 9 additions & 0 deletions gcp/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

from gcp.api.cursor import QueryCursor

import googlecloudprofiler

_SHUTDOWN_GRACE_DURATION = 5

_MAX_SINGLE_QUERY_TIME = timedelta(seconds=20)
Expand Down Expand Up @@ -1395,6 +1397,13 @@ def main():
setup_gcp_logging('api-backend')
logging.getLogger().addFilter(trace_filter)

# Profiler initialization. It starts a daemon thread which continuously
# collects and uploads profiles. Best done as early as possible.
try:
googlecloudprofiler.start(service="osv-api-profiler")
except (ValueError, NotImplementedError) as e:
logging.error(e)

logging.getLogger().setLevel(logging.INFO)

parser = argparse.ArgumentParser(
Expand Down

0 comments on commit 05fa231

Please sign in to comment.