示例#1
0
    def ListRecommendations(self, request, context):
        tag = ''
        for key, value in context.invocation_metadata():
            if key == "x-dynatrace":
                tag = value
        incall = oneagent.get_sdk().trace_incoming_remote_call(
            'ListRecommendations',
            'RecommendationService',
            'grpc://hipstershop.RecommendationService',
            protocol_name='gRPC',
            str_tag=tag)

        with incall:
            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]
            logger.info(
                "[Recv ListRecommendations] product_ids={}".format(prod_list))
            # build and return response
            response = demo_pb2.ListRecommendationsResponse()
            response.product_ids.extend(prod_list)
        return response
示例#2
0
 def ListRecommendations(self, request, context):
     wasm_header = None
     for key, value in context.invocation_metadata():
         if key == "x-wasm-path":
             logger.warn("x-wasm-path type: %s, value: %s", type(value),
                         repr(value))
             wasm_header = value
             break
     metadata = (("x-wasm-path",
                  wasm_header), ) if wasm_header is not None else None
     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]
     logger.info(
         "[Recv ListRecommendations] product_ids={}".format(prod_list))
     # build and return response
     response = demo_pb2.ListRecommendationsResponse()
     response.product_ids.extend(prod_list)
     return response
    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]
        logger.info("[Recv ListRecommendations] product_ids={}".format(prod_list))
        # build and return response
        response = demo_pb2.ListRecommendationsResponse()
        response.product_ids.extend(prod_list)

        a = 0
        b = 1

        for i in range(100000):
          
          res = a+b
          a = b
          b = res  

        return response
示例#4
0
    def ListRecommendations(self, request, context):
        # manually populate service map
        # this can be removed once 7.8 is out and the python agent adds this by itself
        product_catalog_destination_info = {
            "address": os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', ''),
            "port": int(os.environ.get('PORT', 8080)),
            "service": {
                "name": "grpc",
                "resource": os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', ''),
                "type": "external"
            },
        }

        trace_parent = self.extract_trace_parent(context)
        transaction = client.begin_transaction('request',
                                               trace_parent=trace_parent)
        request_dict = MessageToDict(request)
        elasticapm.label(**{'request': request_dict})
        max_responses = 5
        # fetch list of products from product catalog stub
        list_product_req = demo_pb2.Empty()
        with elasticapm.capture_span(
                '/hipstershop.ProductCatalogService/ListProducts',
                labels=MessageToDict(list_product_req),
                extra={"destination":
                       product_catalog_destination_info}) as span:
            trace_parent = transaction.trace_parent.copy_from(
                span_id=span.id, trace_options=TracingOptions(recorded=True))
            cat_response, call = product_catalog_stub.ListProducts.with_call(
                list_product_req,
                metadata=[(constants.TRACEPARENT_HEADER_NAME,
                           trace_parent.to_string())])
        with elasticapm.capture_span('CalculateRecommendations',
                                     span_subtype='grpc',
                                     span_action='calculate') as span:
            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]
            logger.info(
                '[Recv ListRecommendations] product_ids={}'.format(prod_list),
                extra=get_extra_logging_payload())
            # build and return response
        response = demo_pb2.ListRecommendationsResponse()
        response.product_ids.extend(prod_list)
        elasticapm.label(**{'response': MessageToDict(response)})
        elasticapm.set_custom_context({
            'request': request_dict,
            'response': MessageToDict(response)
        })
        client.end_transaction(
            '/hipstershop.RecommendationService/ListRecommendations',
            'success')
        return response
示例#5
0
 def ListRecommendations(self, request, context):
     newrelic.agent.set_transaction_name(
         'hipstershop.RecommendationService/ListRecommendations')
     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]
     logger.info(
         "[Recv ListRecommendations] product_ids={}".format(prod_list))
     # build and return response
     response = demo_pb2.ListRecommendationsResponse()
     response.product_ids.extend(prod_list)
     return response
示例#6
0
    def ListRecommendations(self, request, context):
        max_responses = 5

        # fetch list of products from product catalog stub
        # otel context propagation in grpc instrumentation is broken so we manually
        # inject the context
        metadata = {}
        propagate.inject(metadata)
        cat_response = product_catalog_stub.ListProducts(demo_pb2.Empty(),
                                                         metadata=metadata)
        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]
        logger.info(
            "[Recv ListRecommendations] product_ids={}".format(prod_list))
        # build and return response
        response = demo_pb2.ListRecommendationsResponse()
        response.product_ids.extend(prod_list)
        return response
示例#7
0
 def ListRecommendations(self, request, context):
     response = demo_pb2.ListRecommendationsResponse()
     response.product_ids[:] = recommend.findMatchingProducts(
         request.product_category)
     #logging.info('Returning recommendations...')
     return response