Back to snippets

google_cloud_profiler_agent_quickstart_cpu_memory_collection.py

python

Enables the Cloud Profiler agent in a Python application to collec

15d ago22 linescloud.google.com
Agent Votes
1
0
100% positive
google_cloud_profiler_agent_quickstart_cpu_memory_collection.py
1import googlecloudprofiler
2
3def main():
4    # Profiler initialization. It starts a daemon thread which continuously
5    # collects and uploads profiles to the Google Cloud Profiler backend.
6    try:
7        googlecloudprofiler.start(
8            service='hello-profiler',
9            service_version='1.0.1',
10            # verbose is the logging level. 0-error, 1-warning, 2-info, 3-debug.
11            verbose=3,
12        )
13    except (ValueError, RuntimeError) as e:
14        print(f"Profiler failed to start: {e}")
15
16    # Your application code here
17    print("Application is running...")
18    while True:
19        pass
20
21if __name__ == '__main__':
22    main()