Moved Profiler init to a function

This commit is contained in:
Kalyana Chadalavada 2019-03-06 20:19:03 -08:00 committed by kalyana
parent 0e827b380b
commit 2c1a6f618e
8 changed files with 97 additions and 18 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 && \
@ -24,6 +28,19 @@ WORKDIR /email_server
# Grab packages from builder # Grab packages from builder
COPY --from=builder /usr/local/lib/python3.7/ /usr/local/lib/python3.7/ COPY --from=builder /usr/local/lib/python3.7/ /usr/local/lib/python3.7/
# Enable/disable Stackdriver Profiler agent
ENV ENABLE_PROFILER 1
# If code is not running in GCP, follow the documentation at
# https://cloud.google.com/profiler/docs/profiling-external
# and set the following environment variables. Credential key of the service
# account should have the following roles:
# Stackdriver Profiler Agent
# Cloud Trace Agent
#ENV GOOGLE_APPLICATION_CREDENTIALS "key.json"
#ENV GCP_PROJECT_ID "Your GCP project ID"
# Add the application # Add the application
COPY . . COPY . .

View file

@ -144,13 +144,38 @@ def start(dummy_mode):
except KeyboardInterrupt: except KeyboardInterrupt:
server.stop(0) server.stop(0)
def initStackdriverProfiling():
enable_profiler = None
project_id = None
try:
enable_profiler = os.environ["ENABLE_PROFILER"]
project_id = os.environ["GCP_PROJECT_ID"]
except KeyError:
# Environment variable not set
pass
if enable_profiler != "1":
logger.info("Skipping Stackdriver Profiler Python agent initialization. Set environment variable ENABLE_PROFILER=1 to enable.")
return
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 (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 initStackdriverProfiling()
try:
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

@ -4,4 +4,4 @@ grpcio==1.16.1
jinja2==2.10 jinja2==2.10
opencensus[stackdriver]==0.1.10 opencensus[stackdriver]==0.1.10
python-json-logger==0.1.9 python-json-logger==0.1.9
google-cloud-profiler google-cloud-profiler==1.0.8

View file

@ -12,7 +12,7 @@ google-api-python-client==1.7.8 # via google-cloud-profiler
google-auth-httplib2==0.0.3 # via google-api-python-client, google-cloud-profiler google-auth-httplib2==0.0.3 # via google-api-python-client, google-cloud-profiler
google-auth==1.6.2 # via google-api-core, google-api-python-client, google-auth-httplib2, google-cloud-profiler google-auth==1.6.2 # via google-api-core, google-api-python-client, google-auth-httplib2, google-cloud-profiler
google-cloud-core==0.29.1 # via google-cloud-trace google-cloud-core==0.29.1 # via google-cloud-trace
google-cloud-profiler==1.0.3 google-cloud-profiler==1.0.8
google-cloud-trace==0.20.2 # via opencensus google-cloud-trace==0.20.2 # via opencensus
googleapis-common-protos==1.5.5 # via google-api-core googleapis-common-protos==1.5.5 # via google-api-core
grpcio-health-checking==1.12.1 grpcio-health-checking==1.12.1

View file

@ -15,6 +15,19 @@ WORKDIR /recommendationservice
COPY requirements.txt requirements.txt COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
# Enable/disable Stackdriver Profiler agent
ENV ENABLE_PROFILER 0
# If code is not running in GCP, follow the documentation at
# https://cloud.google.com/profiler/docs/profiling-external
# and set the following environment variables. Credential key of the service
# account should have the following roles:
# Stackdriver Profiler Agent
# Cloud Trace Agent
#ENV GOOGLE_APPLICATION_CREDENTIALS "key.json"
#ENV GCP_PROJECT_ID "Your GCP project ID"
# add files into working directory # add files into working directory
COPY . . COPY . .

View file

@ -37,6 +37,36 @@ from logger import getJSONLogger
logger = getJSONLogger('recommendationservice-server') logger = getJSONLogger('recommendationservice-server')
def initStackdriverProfiling():
enable_profiler = None
project_id = None
try:
enable_profiler = os.environ["ENABLE_PROFILER"]
project_id = os.environ["GCP_PROJECT_ID"]
except KeyError:
# Environment variable not set
pass
if enable_profiler != "1":
logger.info("Skipping Stackdriver Profiler Python agent initialization. Set environment variable ENABLE_PROFILER=1 to enable.")
return
for retry in xrange(1,4):
try:
if project_id:
googlecloudprofiler.start(service='recommendation_server-out', service_version='1.0.0', verbose=0, project_id=project_id)
else:
googlecloudprofiler.start(service='recommendation_server-out', 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 seconds to retry Stackdriver Profiler agent initialization"%(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
@ -64,13 +94,7 @@ class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
if __name__ == "__main__": if __name__ == "__main__":
logger.info("initializing recommendationservice") logger.info("initializing recommendationservice")
# Start the Stackdriver Profiler Python agent initStackdriverProfiling()
try:
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()

View file

@ -4,4 +4,4 @@ grpcio-health-checking==1.13.0
grpcio==1.16.1 grpcio==1.16.1
opencensus[stackdriver]==0.1.10 opencensus[stackdriver]==0.1.10
python-json-logger==0.1.9 python-json-logger==0.1.9
google-cloud-profiler google-cloud-profiler==1.0.8

View file

@ -12,7 +12,7 @@ google-api-python-client==1.7.8 # via google-cloud-profiler, google-python-clou
google-auth-httplib2==0.0.3 # via google-api-python-client, google-cloud-profiler, google-python-cloud-debugger google-auth-httplib2==0.0.3 # via google-api-python-client, google-cloud-profiler, google-python-cloud-debugger
google-auth==1.6.2 # via google-api-core, google-api-python-client, google-auth-httplib2, google-cloud-profiler, google-python-cloud-debugger google-auth==1.6.2 # via google-api-core, google-api-python-client, google-auth-httplib2, google-cloud-profiler, google-python-cloud-debugger
google-cloud-core==0.29.1 # via google-cloud-trace google-cloud-core==0.29.1 # via google-cloud-trace
google-cloud-profiler==1.0.3 google-cloud-profiler==1.0.8
google-cloud-trace==0.20.2 # via opencensus google-cloud-trace==0.20.2 # via opencensus
google-python-cloud-debugger==2.9 google-python-cloud-debugger==2.9
googleapis-common-protos==1.5.6 # via google-api-core googleapis-common-protos==1.5.6 # via google-api-core