Add Stackdriver Profiler Python agent (#176)
* Add Stackdriver Profiler Python agent to EmailService and RecommendationService * Update recommendation_server.py * Moved Profiler init to a function * Moved Profiler init to a function * Delete key.json * Delete key.json * Delete key.json
This commit is contained in:
parent
f276995585
commit
86fb1662a3
10 changed files with 97 additions and 13 deletions
|
@ -33,6 +33,7 @@ from opencensus.trace.ext.grpc import server_interceptor
|
|||
from opencensus.trace.samplers import always_on
|
||||
|
||||
# import googleclouddebugger
|
||||
import googlecloudprofiler
|
||||
|
||||
try:
|
||||
sampler = always_on.AlwaysOnSampler()
|
||||
|
@ -143,7 +144,41 @@ def start(dummy_mode):
|
|||
except KeyboardInterrupt:
|
||||
server.stop(0)
|
||||
|
||||
def initStackdriverProfiling():
|
||||
project_id = None
|
||||
try:
|
||||
project_id = os.environ["GCP_PROJECT_ID"]
|
||||
except KeyError:
|
||||
# Environment variable not set
|
||||
pass
|
||||
|
||||
for retry in range(1,4):
|
||||
try:
|
||||
if project_id:
|
||||
googlecloudprofiler.start(service='email_server', service_version='1.0.0', verbose=0, project_id=project_id)
|
||||
else:
|
||||
googlecloudprofiler.start(service='email_server', service_version='1.0.0', verbose=0)
|
||||
logger.info("Successfully started Stackdriver Profiler.")
|
||||
return
|
||||
except (BaseException) as exc:
|
||||
logger.info("Unable to start Stackdriver Profiler Python agent. " + str(exc))
|
||||
if (retry < 4):
|
||||
logger.info("Sleeping %d to retry initializing Stackdriver Profiler"%(retry*10))
|
||||
time.sleep (1)
|
||||
else:
|
||||
logger.warning("Could not initialize Stackdriver Profiler after retrying, giving up")
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger.info('starting the email service in dummy mode.')
|
||||
try:
|
||||
enable_profiler = os.environ["ENABLE_PROFILER"]
|
||||
if enable_profiler != "1":
|
||||
raise KeyError()
|
||||
else:
|
||||
initStackdriverProfiling()
|
||||
except KeyError:
|
||||
logger.info("Skipping Stackdriver Profiler Python agent initialization. Set environment variable ENABLE_PROFILER=1 to enable.")
|
||||
|
||||
|
||||
start(dummy_mode = True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue