Skip to content

Commit

Permalink
[recommendation] - add manual tracing instrumentation (open-telemetry…
Browse files Browse the repository at this point in the history
…#163)

* add manual spans and attributes

* fix Check function args

* Update src/recommendationservice/recommendation_server.py

Co-authored-by: Michael Maxwell <mike.ian.maxwell@gmail.com>

Co-authored-by: Michael Maxwell <mike.ian.maxwell@gmail.com>
  • Loading branch information
puckpuck and mic-max authored Jul 2, 2022
1 parent 8012eac commit 9bef358
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/recommendationservice/recommendation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,14 @@
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
tracer = trace.get_tracer("recommendationservice")


class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
def ListRecommendations(self, request, context):
max_responses = 5
# fetch list of products from product catalog stub
cat_response = product_catalog_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]
prod_list = get_product_list(request.product_ids)
span = trace.get_current_span()
span.set_attribute("app.products_recommended.count", len(prod_list))
logger.info("[Recv ListRecommendations] product_ids={}".format(prod_list))
# build and return response
response = demo_pb2.ListRecommendationsResponse()
Expand All @@ -66,12 +59,34 @@ def Watch(self, request, context):
return health_pb2.HealthCheckResponse(
status=health_pb2.HealthCheckResponse.UNIMPLEMENTED)


def get_product_list(request_product_ids):
with tracer.start_as_current_span("get_product_list") as span:
max_responses = 5
# fetch list of products from product catalog stub
cat_response = product_catalog_stub.ListProducts(demo_pb2.Empty())
product_ids = [x.id for x in cat_response.products]
span.set_attribute("app.products.count", len(product_ids))

filtered_products = list(set(product_ids) - set(request_product_ids))
num_products = len(filtered_products)
span.set_attribute("app.filtered_products.count", num_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]
return prod_list


def must_map_env(key: str):
value = os.environ.get(key)
if value is None:
raise Exception(f'{key} environment variable must be set')
return value


if __name__ == "__main__":
logger.info("initializing recommendationservice")

Expand Down

0 comments on commit 9bef358

Please sign in to comment.