Fixed recommendations instrumentation

This commit is contained in:
morganmclean 2018-07-16 14:00:42 -07:00
parent 6e1bbe46e2
commit 9486bc24fd
3 changed files with 24 additions and 16 deletions

View file

@ -10,8 +10,8 @@ WORKDIR /home
# get packages # get packages
RUN apt-get update && apt-get install python3-pip -y RUN apt-get update && apt-get install python3-pip -y
RUN pip install opencensus RUN pip install opencensus
RUN pip install google-python-cloud-debugger
RUN pip install google-cloud-trace RUN pip install google-cloud-trace
RUN pip install google-python-cloud-debugger
# set listen port # set listen port
ENV PORT "8080" ENV PORT "8080"

View file

@ -14,9 +14,12 @@ if __name__ == "__main__":
else: else:
port = "8080" port = "8080"
try:
exporter = stackdriver_exporter.StackdriverExporter() exporter = stackdriver_exporter.StackdriverExporter()
tracer = Tracer(exporter=exporter) tracer = Tracer(exporter=exporter)
tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(tracer, host_port='localhost:'+port) tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(tracer, host_port='localhost:'+port)
except:
tracer_interceptor = client_interceptor.OpenCensusClientInterceptor()
# set up server stub # set up server stub
channel = grpc.insecure_channel('localhost:'+port) channel = grpc.insecure_channel('localhost:'+port)

View file

@ -9,6 +9,9 @@ import os
from opencensus.trace.ext.grpc import server_interceptor from opencensus.trace.ext.grpc import server_interceptor
from opencensus.trace.samplers import always_on from opencensus.trace.samplers import always_on
from opencensus.trace.exporters import stackdriver_exporter from opencensus.trace.exporters import stackdriver_exporter
from opencensus.trace.exporters import print_exporter
import googleclouddebugger
class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer): class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
def ListRecommendations(self, request, context): def ListRecommendations(self, request, context):
@ -29,18 +32,20 @@ class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
response.product_ids.extend(prod_list) response.product_ids.extend(prod_list)
return response return response
if __name__ == "__main__": if __name__ == "__main__":
# sampler = always_on.AlwaysOnSampler() try:
# exporter = stackdriver_exporter.StackdriverExporter() sampler = always_on.AlwaysOnSampler()
# tracer_interceptor = server_interceptor.OpenCensusServerInterceptor(sampler, exporter) exporter = stackdriver_exporter.StackdriverExporter()
tracer_interceptor = server_interceptor.OpenCensusServerInterceptor(sampler, exporter)
except:
tracer_interceptor = server_interceptor.OpenCensusServerInterceptor()
# try: try:
# import googleclouddebugger googleclouddebugger.enable(
# googleclouddebugger.enable( module='recommendationserver',
# module='recommendationserver', version='[VERSION]'
# version='[VERSION]' )
# ) except:
# except ImportError: pass
# pass
# get port from $PORT envar # get port from $PORT envar
port = os.environ.get('PORT', "8080") port = os.environ.get('PORT', "8080")
@ -54,7 +59,7 @@ if __name__ == "__main__":
channel = grpc.insecure_channel(catalog_addr) channel = grpc.insecure_channel(catalog_addr)
stub = demo_pb2_grpc.ProductCatalogServiceStub(channel) stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)
# create gRPC server # create gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))#,interceptors=(tracer_interceptor)) server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),interceptors=(tracer_interceptor,))
# add class to gRPC server # add class to gRPC server
demo_pb2_grpc.add_RecommendationServiceServicer_to_server(RecommendationService(), server) demo_pb2_grpc.add_RecommendationServiceServicer_to_server(RecommendationService(), server)
# start server # start server