def SendEmail(self, request, context, send_mail=True): trace_parent = None for key, value in context.invocation_metadata(): if key.lower() == TRACEPARENT_HEADER_NAME: trace_parent = TraceParent.from_string(value) tx = client.begin_transaction('request', trace_parent=trace_parent) tx.ensure_parent_id() request_dict = MessageToDict(request) elasticapm.label(**{'request': request_dict}) elasticapm.set_custom_context({ 'request': request_dict, 'response': MessageToDict(demo_pb2.Empty()) }) email = request.email order = request.order parent_id = trace_parent.span_id span = tx._begin_span('template.render', None, parent_span_id=parent_id) try: confirmation = template.render(order=order) span.end() except TemplateError as err: client.capture_exception(context=request_dict, handled=True) context.set_details( 'An error occurred when preparing the confirmation mail.') logger.error(err.message, extra=default_fields) context.set_code(grpc.StatusCode.INTERNAL) span.end() client.end_transaction( '/hipstershop.EmailService/SendOrderConfirmation', 'failure') return demo_pb2.Empty() span = tx._begin_span('send_email', None, parent_span_id=parent_id) try: self.send_email(email, confirmation, send_mail) span.end() except EmailSendException as err: client.capture_exception(context=request_dict, handled=True) context.set_details('An error occurred when sending the email.') logger.error('Failed to send email to %s' % email, extra=get_extra_logging_payload()) logger.error(err.message, extra=get_extra_logging_payload()) context.set_code(grpc.StatusCode.INTERNAL) span.end() client.end_transaction( '/hipstershop.EmailService/SendOrderConfirmation', 'failure') return demo_pb2.Empty() elasticapm.label(**{'response': MessageToDict(demo_pb2.Empty())}) client.end_transaction( '/hipstershop.EmailService/SendOrderConfirmation', 'success') return demo_pb2.Empty()
def SendOrderConfirmation(self, request, context): time.sleep(extraLatency) logger.info( 'A request to send order confirmation email to {} has been received.' .format(request.email)) return demo_pb2.Empty()
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
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): 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
def SendOrderConfirmation(self, request, context): logger.info( "A request to send order confirmation email to {} has been received.".format( request.email ) ) return demo_pb2.Empty()
def SendOrderConfirmation(self, request, context): newrelic.agent.set_transaction_name( 'hipstershop.emailservice/SendOrderConfirmation') logger.info( 'A request to send order confirmation email to {} has been received.' .format(request.email)) return demo_pb2.Empty()
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
def GetAds(self, request, context): request = demo_pb2.Empty() products = self.stub.ListProducts(request).products ads = [ demo_pb2.Ad(redirect_url=random.choice(products).id, text="AdV2 - Items with 25% discount!") ] return demo_pb2.AdResponse(ads=ads)
def SendOrderConfirmation(self, request, context): logger.info('A request to send order confirmation email to {} has been received.'.format(request.email)) if os.getenv('ENCODE_EMAIL').lower() == 'true': try: encoded_email = self.EncodeEmail(request.email) request.email = encoded_email except: logger.error('Err: Could not encode email') context.set_code(grpc.StatusCode.INTERNAL) return demo_pb2.Empty()
def SendOrderConfirmation(self, request, context): email = request.email order = request.order try: confirmation = template.render(order = order) except TemplateError as err: context.set_details("An error occurred when preparing the confirmation mail.") logger.error(err.message) context.set_code(grpc.StatusCode.INTERNAL) return demo_pb2.Empty() try: EmailService.send_email(self.client, email, confirmation) except GoogleAPICallError as err: context.set_details("An error occurred when sending the email.") print(err.message) context.set_code(grpc.StatusCode.INTERNAL) return demo_pb2.Empty() return demo_pb2.Empty()
def SendOrderConfirmation(self, request, context): logger.info( "A request to send order confirmation email to {} has been received." .format(request.email)) if os.getenv("ENCODE_EMAIL", "false").lower() == "true": try: encoded_email = self.EncodeEmail(request.email) request.email = encoded_email except Exception: logger.error("Err: Could not encode email") context.set_code(grpc.StatusCode.INTERNAL) return demo_pb2.Empty()
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
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
def __listen_for_messages(self): for model in self.conn.listenModels(demo_pb2.Empty()): self.model = model print("new model value is %d" % self.model.counter)
def SendOrderConfirmation(self, request, context): print( 'A request to send order confirmation email to {} has been received.' .format(request.email)) return demo_pb2.Empty()
def pushModel(self, model, context): self.model_updates.append(model) return demo_pb2.Empty()