Instrumented everything but Python
This commit is contained in:
parent
11e68b3733
commit
99ce1f84c2
19 changed files with 952 additions and 24 deletions
|
@ -7,6 +7,11 @@ ENV PYTHONUNBUFFERED=0
|
|||
COPY . /home/
|
||||
WORKDIR /home
|
||||
|
||||
# get packages
|
||||
RUN apt-get update && apt-get install python3-pip -y
|
||||
RUN pip install opencensus
|
||||
RUN pip install google-python-cloud-debugger
|
||||
|
||||
# set listen port
|
||||
ENV PORT "8080"
|
||||
EXPOSE 8080
|
||||
|
|
|
@ -3,6 +3,10 @@ import demo_pb2
|
|||
import demo_pb2_grpc
|
||||
import sys
|
||||
|
||||
from opencensus.trace.tracer import Tracer
|
||||
from opencensus.trace.exporters import stackdriver_exporter
|
||||
from opencensus.trace.ext.grpc import client_interceptor
|
||||
|
||||
if __name__ == "__main__":
|
||||
# get port
|
||||
if len(sys.argv) > 1:
|
||||
|
@ -10,13 +14,16 @@ if __name__ == "__main__":
|
|||
else:
|
||||
port = "8080"
|
||||
|
||||
exporter = stackdriver_exporter.StackdriverExporter()
|
||||
tracer = Tracer(exporter=exporter)
|
||||
tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(tracer, host_port='localhost:'+port)
|
||||
|
||||
# set up server stub
|
||||
channel = grpc.insecure_channel('localhost:'+port)
|
||||
channel = grpc.intercept_channel(channel, tracer_interceptor)
|
||||
stub = demo_pb2_grpc.RecommendationServiceStub(channel)
|
||||
|
||||
# form request
|
||||
request = demo_pb2.ListRecommendationsRequest(user_id="test", product_ids=["test"])
|
||||
|
||||
# make call to server
|
||||
response = stub.ListRecommendations(request)
|
||||
print(response)
|
||||
print(response)
|
|
@ -6,58 +6,63 @@ import time
|
|||
import random
|
||||
import os
|
||||
|
||||
# from opencensus.trace.ext.grpc import server_interceptor
|
||||
# from opencensus.trace.samplers import always_on
|
||||
# from opencensus.trace.exporters import stackdriver_exporter
|
||||
|
||||
class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
|
||||
def ListRecommendations(self, request, context):
|
||||
max_responses = 5
|
||||
|
||||
# fetch list of products from product catalog stub
|
||||
cat_response = stub.ListProducts(demo_pb2.Empty())
|
||||
|
||||
product_ids = [x.id for x in cat_response.products]
|
||||
filtered_products = list(set(product_ids)-set(request.product_ids))
|
||||
|
||||
num_products = len(filtered_products)
|
||||
num_return = min(max_responses, num_products)
|
||||
|
||||
# sample list of indicies to return
|
||||
indices = random.sample(range(num_products), num_return)
|
||||
# fetch product ids from indices
|
||||
prod_list = [filtered_products[i] for i in indices]
|
||||
print("handling request: {}".format(prod_list))
|
||||
|
||||
# build and return response
|
||||
response = demo_pb2.ListRecommendationsResponse()
|
||||
response.product_ids.extend(prod_list)
|
||||
return response
|
||||
|
||||
if __name__ == "__main__":
|
||||
# sampler = always_on.AlwaysOnSampler()
|
||||
# exporter = stackdriver_exporter.StackdriverExporter()
|
||||
# tracer_interceptor = server_interceptor.OpenCensusServerInterceptor(sampler, exporter)
|
||||
|
||||
# try:
|
||||
# import googleclouddebugger
|
||||
# googleclouddebugger.enable(
|
||||
# module='recommendationserver',
|
||||
# version='[VERSION]'
|
||||
# )
|
||||
# except ImportError:
|
||||
# pass
|
||||
|
||||
# get port from $PORT envar
|
||||
port = os.environ.get('PORT', "8080")
|
||||
# get product catalog service address from $PRODUCT_CATALOG_SERVICE_ADDR envar
|
||||
catalog_addr = os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', '')
|
||||
if catalog_addr == "":
|
||||
raise Exception('PRODUCT_CATALOG_SERVICE_ADDR environment not set')
|
||||
|
||||
print("product catalog address: " + catalog_addr)
|
||||
print("listening on port: " + port)
|
||||
|
||||
# stub for product catalog service
|
||||
channel = grpc.insecure_channel(catalog_addr)
|
||||
stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)
|
||||
|
||||
# create gRPC server
|
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
||||
|
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))#,interceptors=(tracer_interceptor))
|
||||
# add class to gRPC server
|
||||
demo_pb2_grpc.add_RecommendationServiceServicer_to_server(RecommendationService(), server)
|
||||
|
||||
# start server
|
||||
server.add_insecure_port('[::]:'+port)
|
||||
server.start()
|
||||
|
||||
# keep alive
|
||||
try:
|
||||
while True:
|
||||
time.sleep(10000)
|
||||
except KeyboardInterrupt:
|
||||
server.stop(0)
|
||||
server.stop(0)
|
|
@ -1,2 +1,4 @@
|
|||
grpcio-tools
|
||||
googleapis-common-protos
|
||||
opencensus
|
||||
google-cloud-trace
|
Loading…
Add table
Add a link
Reference in a new issue