def log_exception(_log_exception, instance, args, kwargs): try: # expecting signature `log_exception(self, typ, value, tb)`` if len(args) == 3: value = args[2] beeline.send_now({ "request.method": instance.request.method, "request.path": instance.request.uri, "request.remote_addr": instance.request.remote_ip, "request.query": instance.request.query, "request.host": instance.request.get('Host'), "request.error": type(value).__name__, "request.error_detail": beeline.internal.stringify_exception(value), }) except Exception: pass finally: _log_exception(*args, **kwargs)
def log_request(_log_request, instance, args, kwargs): try: # expecting signature `log_request(self, handler)` if len(args) == 1: handler = args[0] beeline.send_now({ "duration_ms": handler.request.request_time() * 1000.0, "request.method": handler.request.method, "request.remote_addr": handler.request.remote_ip, "request.path": handler.request.uri, "request.query": handler.request.query, "request.host": handler.request.headers.get('Host'), "response.status_code": handler.get_status(), }) except Exception: pass finally: _log_request(*args, **kwargs)
def send_to_honeycomb(): """Attempts to log usage data to honeycomb. Honeycomb logging is completely optional. If there are any failures simply continue as normal. This includes if clrenv can not be loaded. These metrics are sent to honeycomb as an event rather than a trace for two reasons: 1) They don't really fit the web requests model. 2) Downstream code might have its own tracing which we don't want to interfere with. """ try: from clrenv import env # clrenv < 0.2.0 has a bug in the `in` operator at the root level. if env.get("honeycomb") is None: return beeline.init( writekey=env.honeycomb.writekey, dataset="clr", service_name="clr", ) # Convert start_time into a duration. honeycomb_data["duration_ms"] = int( 1000 * (time.time() - honeycomb_data["start_time"])) del honeycomb_data["start_time"] honeycomb_data["username"] = getpass.getuser() honeycomb_data["clr_version"] = __version__ honeycomb_data["color_key_mode"] = env.key_mode beeline.send_now(honeycomb_data) beeline.close() except: if DEBUG_MODE: print("Failed to initialize beeline.", file=sys.stderr) print(traceback.format_exc(), file=sys.stderr)