def create_http_event(self, request): # Code to be executed for each request before # the view (and later middleware) are called. trace_id, parent_id, parent_context = _get_trace_context(request) request_context = self.get_context_from_request(request) trace = beeline.start_trace(context=request_context, trace_id=trace_id, parent_span_id=parent_id) if isinstance(parent_context, dict): for k, v in parent_context.items(): beeline.add_trace_field(k, v) response = self.get_response(request) # Code to be executed for each request/response after # the view is called. response_context = self.get_context_from_response(request, response) beeline.add_context(response_context) beeline.finish_trace(trace) return response
def lambda_handler(event, context): trace_context = None input = None parent_trace = None output = {} logging.debug(f"event: {json.dumps(event)}") init_beeline() # Attempt to get trace_context(s) from the input input = event.get("Input", None) if input: trace_context = input.get("trace_context", None) # Start trace if it isn't already, otherwise resume if trace_context: trace_id, parent_id, context = beeline.trace.unmarshal_trace_context( trace_context) logging.info(f"Resuming trace: {trace_id}") trace = beeline.start_trace(trace_id=trace_id, parent_span_id=parent_id, context=context) # add a field to test context propogation beeline.add_trace_field( event.get("Path", "UnknownPath").lower(), uuid.uuid4()) beeline.add_context( {"name": event.get("Path", "Missing Path Information")}) beeline.add_context( {"function_name": event.get("Path", "Missing Path Information")}) random_sleep() beeline.finish_span(trace) else: trace = start_trace() beeline.add_trace_field("c3po", "r2d2") logging.info(f"Starting Trace") with beeline.tracer( name=event.get("Path", "Missing Path Information")): random_sleep() trace_context = beeline.get_beeline( ).tracer_impl.marshal_trace_context() # If final step close the parent trace if event.get("Path") == "Step4": # 2019-03-26T20:14:13.192Z parent_trace_id, parent_parent_id, parent_context_data = beeline.trace.unmarshal_trace_context( trace_context) start_time = datetime.strptime(event.get("start_time"), "%Y-%m-%dT%H:%M:%S.%fZ") close_final_trace(parent_trace_id, parent_parent_id, parent_context_data, start_time) # Close only (send pending) beeline.close() # Return the trace_context to the SFN output["trace_context"] = trace_context return output
def create_http_event(self, request): # Code to be executed for each request before # the view (and later middleware) are called. trace_id, parent_id, context = _get_trace_context(request) trace_name = "django_http_%s" % request.method.lower() trace = beeline.start_trace(context={ "name": trace_name, "type": "http_server", "request.host": request.get_host(), "request.method": request.method, "request.path": request.path, "request.remote_addr": request.META.get('REMOTE_ADDR'), "request.content_length": request.META.get('CONTENT_LENGTH', 0), "request.user_agent": request.META.get('HTTP_USER_AGENT'), "request.scheme": request.scheme, "request.secure": request.is_secure(), "request.query": request.GET.dict(), "request.xhr": request.is_ajax(), "request.post": request.POST.dict() }, trace_id=trace_id, parent_span_id=parent_id) if isinstance(context, dict): for k, v in context.items(): beeline.add_trace_field(k, v) response = self.get_response(request) # Code to be executed for each request/response after # the view is called. beeline.add_context_field("response.status_code", response.status_code) beeline.finish_trace(trace) return response
def __call__(self, environ, start_response): request_method = environ.get('REQUEST_METHOD') if request_method: trace_name = "flask_http_%s" % request_method.lower() else: trace_name = "flask_http" trace_id, parent_id, context = _get_trace_context(environ) root_span = beeline.start_trace(context={ "type": "http_server", "name": trace_name, "request.host": environ.get('HTTP_HOST'), "request.method": request_method, "request.path": environ.get('PATH_INFO'), "request.remote_addr": environ.get('REMOTE_ADDR'), "request.content_length": environ.get('CONTENT_LENGTH', 0), "request.user_agent": environ.get('HTTP_USER_AGENT'), "request.scheme": environ.get('wsgi.url_scheme'), "request.query": environ.get('QUERY_STRING') }, trace_id=trace_id, parent_span_id=parent_id) # populate any propagated custom context if isinstance(context, dict): for k, v in context.items(): beeline.add_trace_field(k, v) def _start_response(status, headers, *args): status_code = int(status[0:4]) beeline.add_context_field("response.status_code", status_code) if status_code != 500: beeline.finish_trace(root_span) elif status_code == 500 and not signals.signals_available: beeline.finish_trace(root_span) return start_response(status, headers, *args) return self.app(environ, _start_response)
def _beeline_wrapper(event, context): global COLD_START # don't blow up the world if the beeline has not been initialized if not beeline.get_beeline(): return handler(event, context) try: # assume we're going to get bad values sometimes in our headers trace_id, parent_id, trace_context = None, None, None try: trace_id, parent_id, trace_context = _get_trace_data(event) except Exception as e: beeline.internal.log( 'error attempting to extract trace context: %s', beeline.internal.stringify_exception(e)) pass with beeline.tracer(name=handler.__name__, trace_id=trace_id, parent_id=parent_id): beeline.add_context({ "app.function_name": getattr(context, 'function_name', ""), "app.function_version": getattr(context, 'function_version', ""), "app.request_id": getattr(context, 'aws_request_id', ""), "app.event": event, "meta.cold_start": COLD_START, }) # if there is custom context attached from upstream, add that now if isinstance(trace_context, dict): for k, v in trace_context.items(): beeline.add_trace_field(k, v) resp = handler(event, context) if resp is not None: beeline.add_context_field('app.response', resp) return resp finally: # This remains false for the lifetime of the module COLD_START = False # we have to flush events before the lambda returns beeline.get_beeline().client.flush()
def __call__(self, environ, start_response): trace_id, parent_id, context = _get_trace_context(environ) root_span = beeline.start_trace( context=self.get_context_from_environ(environ), trace_id=trace_id, parent_span_id=parent_id) # populate any propagated custom context if isinstance(context, dict): for k, v in context.items(): beeline.add_trace_field(k, v) def _start_response(status, headers, *args): status_code = int(status[0:4]) beeline.add_context_field("response.status_code", status_code) if status_code != 500: beeline.finish_trace(root_span) elif status_code == 500 and not signals.signals_available: beeline.finish_trace(root_span) return start_response(status, headers, *args) return self.app(environ, _start_response)
def hello_world(): span = beeline.start_span(context={"name": "Preparing to greet the world"}) message = "Hello World" beeline.add_trace_field('message', message) beeline.finish_span(span) return message