I created a grpc server written in python. When ther is an error, I need to raise an exception in the server, this function in grpc/_servery.py fails with the raised exception. This is causing the program to print the traces which just uses print function. Is there a way to make this funciton use jsonlogger which is used for all other logging?
Because of the multiline traces, I am getting multiple lines of log for same error in datadog. How do I fix the multi line traces.
Sample code of the server
class UnaryService(pb2_grpc.UnaryServicer):
def __init__(self, *args, **kwargs):
pass
def GetServerResponse(self, request, context):
message = request.message
raise Exception("error raised")
I have tried follwoing approaches:
-
Returning None: I tried setting status code, error details in the gRPC context and return None when when there is an exception instead of raising. This worked in the sense, it is now not logging the traces but I got an issue with datadog tracing. The request was considered as succeeded even when it fails. I also tried adding tags to the current span but it still was not marked as failed.
-
Patching print_exc(): Another solution I approaced was to patch the traceback.print_exc() function. This also works but I do not like this solution as if something changes in traceback or grpc library, it could be impacted.
og_print_exc = traceback.print_exc()
def patched_print_exc(*args, **kwargs):
tb = traceback.format_exc()
logger.error("error traceback: ", tb)
traceback.print_exc = patched_print_exc
Are there better ways to handle this scenario?
Thank You