Moved Profiler init to a function

This commit is contained in:
Kalyana Chadalavada 2019-03-06 20:19:03 -08:00 committed by Kalyana Chadalavada
parent 0e827b380b
commit e865a5aa53
3 changed files with 38 additions and 13 deletions

View file

@ -1,19 +1,23 @@
FROM python:3-slim as base FROM python:3-slim as base
FROM base as builder FROM base as builder
FROM base as final
RUN apt-get -qq update \ RUN apt-get -qq update \
&& apt-get install -y --no-install-recommends \ && apt-get install -y --no-install-recommends \
wget g++ g++
# get packages # get packages
COPY requirements.txt . COPY requirements.txt .
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
FROM base as final
# Enable unbuffered logging # Enable unbuffered logging
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
RUN apt-get -qq update \
&& apt-get install -y --no-install-recommends \
wget
# Download the grpc health probe # Download the grpc health probe
RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \ wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \

View file

@ -144,13 +144,24 @@ def start(dummy_mode):
except KeyboardInterrupt: except KeyboardInterrupt:
server.stop(0) server.stop(0)
def InitProfiler():
for retry in range(3):
try:
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 in email_server.py.\n" + str(exc))
if (retry < 3):
logger.info("Sleeping %d to retry initializing Stackdriver Profiler"%(retry*10))
time.sleep (retry *10)
else:
logger.warning("could not initialize stackdriver profiler after retrying, giving up")
return
if __name__ == '__main__': if __name__ == '__main__':
logger.info('starting the email service in dummy mode.') logger.info('starting the email service in dummy mode.')
# Start the Stackdriver Profiler Python agent # Start the Stackdriver Profiler Python agent
try: InitProfiler()
googlecloudprofiler.start(service='email_server', service_version='1.0.1', verbose=0)
except (ValueError, NotImplementedError) as exc:
logger.info("Unable to start Stackdriver Profiler Python agent in email_server.py.\n" + str(exc))
start(dummy_mode = True) start(dummy_mode = True)

View file

@ -37,6 +37,21 @@ from logger import getJSONLogger
logger = getJSONLogger('recommendationservice-server') logger = getJSONLogger('recommendationservice-server')
def InitProfiler():
for retry in xrange(3):
try:
googlecloudprofiler.start(service='recommendation_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 in recommendation_server.py.\n" + str(exc))
if (retry < 3):
logger.info("Sleeping %d to retry initializing Stackdriver Profiler"%(retry*10))
time.sleep (retry*10)
else:
logger.warning("Could not initialize Stackdriver Profiler after retrying, giving up")
return
class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer): class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
def ListRecommendations(self, request, context): def ListRecommendations(self, request, context):
max_responses = 5 max_responses = 5
@ -65,12 +80,7 @@ if __name__ == "__main__":
logger.info("initializing recommendationservice") logger.info("initializing recommendationservice")
# Start the Stackdriver Profiler Python agent # Start the Stackdriver Profiler Python agent
try: InitProfiler()
googlecloudprofiler.start(service='recommendation_server', service_version='1.0.0', verbose=0)
except (ValueError, NotImplementedError) as exc:
logger.info("Unable to start Stackdriver Profiler Python agent in recommendation_server.py.\n" +
str(exc))
try: try:
sampler = always_on.AlwaysOnSampler() sampler = always_on.AlwaysOnSampler()