diff --git a/src/emailservice/Dockerfile b/src/emailservice/Dockerfile index c7169d5..65a58a3 100644 --- a/src/emailservice/Dockerfile +++ b/src/emailservice/Dockerfile @@ -1,19 +1,23 @@ FROM python:3-slim as base FROM base as builder -FROM base as final RUN apt-get -qq update \ && apt-get install -y --no-install-recommends \ - wget g++ + g++ # get packages COPY requirements.txt . RUN pip install -r requirements.txt +FROM base as final # Enable unbuffered logging ENV PYTHONUNBUFFERED=1 +RUN apt-get -qq update \ + && apt-get install -y --no-install-recommends \ + wget + # Download the grpc health probe 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 && \ @@ -24,6 +28,19 @@ WORKDIR /email_server # Grab packages from builder 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 COPY . . diff --git a/src/emailservice/email_server.py b/src/emailservice/email_server.py index 536a162..dc341e8 100644 --- a/src/emailservice/email_server.py +++ b/src/emailservice/email_server.py @@ -144,13 +144,38 @@ def start(dummy_mode): except KeyboardInterrupt: 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__': logger.info('starting the email service in dummy mode.') - # Start the Stackdriver Profiler Python agent - 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)) + initStackdriverProfiling() start(dummy_mode = True) diff --git a/src/emailservice/requirements.in b/src/emailservice/requirements.in index f87a3d3..bb60d5a 100644 --- a/src/emailservice/requirements.in +++ b/src/emailservice/requirements.in @@ -4,4 +4,4 @@ grpcio==1.16.1 jinja2==2.10 opencensus[stackdriver]==0.1.10 python-json-logger==0.1.9 -google-cloud-profiler +google-cloud-profiler==1.0.8 diff --git a/src/emailservice/requirements.txt b/src/emailservice/requirements.txt index 9faca19..c0d5346 100644 --- a/src/emailservice/requirements.txt +++ b/src/emailservice/requirements.txt @@ -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==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-profiler==1.0.3 +google-cloud-profiler==1.0.8 google-cloud-trace==0.20.2 # via opencensus googleapis-common-protos==1.5.5 # via google-api-core grpcio-health-checking==1.12.1 diff --git a/src/recommendationservice/Dockerfile b/src/recommendationservice/Dockerfile index fc70bec..7f6f6ce 100644 --- a/src/recommendationservice/Dockerfile +++ b/src/recommendationservice/Dockerfile @@ -15,6 +15,19 @@ WORKDIR /recommendationservice COPY requirements.txt 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 COPY . . diff --git a/src/recommendationservice/recommendation_server.py b/src/recommendationservice/recommendation_server.py index 66e161a..9c81045 100644 --- a/src/recommendationservice/recommendation_server.py +++ b/src/recommendationservice/recommendation_server.py @@ -37,6 +37,36 @@ from logger import getJSONLogger 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): def ListRecommendations(self, request, context): max_responses = 5 @@ -64,13 +94,7 @@ class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer): if __name__ == "__main__": logger.info("initializing recommendationservice") - # Start the Stackdriver Profiler Python agent - 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)) - + initStackdriverProfiling() try: sampler = always_on.AlwaysOnSampler() diff --git a/src/recommendationservice/requirements.in b/src/recommendationservice/requirements.in index f7b87c7..bfc28a5 100644 --- a/src/recommendationservice/requirements.in +++ b/src/recommendationservice/requirements.in @@ -4,4 +4,4 @@ grpcio-health-checking==1.13.0 grpcio==1.16.1 opencensus[stackdriver]==0.1.10 python-json-logger==0.1.9 -google-cloud-profiler +google-cloud-profiler==1.0.8 diff --git a/src/recommendationservice/requirements.txt b/src/recommendationservice/requirements.txt index 75f3725..b504fff 100644 --- a/src/recommendationservice/requirements.txt +++ b/src/recommendationservice/requirements.txt @@ -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==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-profiler==1.0.3 +google-cloud-profiler==1.0.8 google-cloud-trace==0.20.2 # via opencensus google-python-cloud-debugger==2.9 googleapis-common-protos==1.5.6 # via google-api-core