def _sw_send_message(this: Cursor, operation): address = this.collection.database.client.address peer = "%s:%s" % address context = get_context() carrier = Carrier() op = "FindOperation" with context.new_exit_span(op="MongoDB/" + op, peer=peer, carrier=carrier) as span: span.layer = Layer.Database span.component = Component.MongoDB try: # __send_message return nothing __send_message(this, operation) span.tag(Tag(key=tags.DbType, val="MongoDB")) span.tag( Tag(key=tags.DbInstance, val=this.collection.database.name)) if config.pymongo_trace_parameters: filters = "find " + str(operation.spec) max_len = config.pymongo_parameters_max_length filters = filters[0:max_len] + "..." if len( filters) > max_len else filters span.tag(Tag(key=tags.DbStatement, val=filters)) except BaseException as e: span.raised() raise e return
async def _sw_handle_request(self, request, write_callback, stream_callback): req = request context = get_context() carrier = Carrier() for item in carrier: if item.key.capitalize() in req.headers: item.val = req.headers[item.key.capitalize()] with context.new_entry_span(op=req.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Sanic span.peer = '%s:%s' % (req.remote_addr or req.ip, req.port) span.tag(Tag(key=tags.HttpMethod, val=req.method)) span.tag(Tag(key=tags.HttpUrl, val=req.url.split("?")[0])) if config.sanic_collect_http_params and req.args: span.tag( Tag(key=tags.HttpParams, val=params_tostring( req.args)[0:config.http_params_length_threshold])) resp = _handle_request(self, request, write_callback, stream_callback) if isawaitable(resp): result = await resp return result
def _sw__on_deliver(this, method_frame, header_frame, body): peer = '%s:%s' % (this.connection.params.host, this.connection.params.port) context = get_context() exchange = method_frame.method.exchange routing_key = method_frame.method.routing_key carrier = Carrier() for item in carrier: if item.key in header_frame.properties.headers: item.val = header_frame.properties.headers[item.key] with context.new_entry_span(op="RabbitMQ/Topic/" + exchange + "/Queue/" + routing_key + "/Consumer" or "", carrier=carrier) as span: span.layer = Layer.MQ span.component = Component.RabbitmqConsumer try: __on_deliver(this, method_frame, header_frame, body) span.tag(Tag(key=tags.MqBroker, val=peer)) span.tag(Tag(key=tags.MqTopic, val=exchange)) span.tag(Tag(key=tags.MqQueue, val=routing_key)) except BaseException as e: span.raised() raise e
def _sw_get_response(self, *args, **kwargs): request = self.request context = get_context() carrier = Carrier() for item in carrier: if item.key.capitalize() in request.headers: item.val = request.headers[item.key.capitalize()] with context.new_entry_span(op=request.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Tornado peer = request.connection.stream.socket.getpeername() span.peer = '{0}:{1}'.format(*peer) span.tag(Tag(key=tags.HttpMethod, val=request.method)) span.tag( Tag(key=tags.HttpUrl, val='{}://{}{}'.format(request.protocol, request.host, request.path))) result = yield from old_execute(self, *args, **kwargs) span.tag( Tag(key=tags.HttpStatus, val=self._status_code, overridable=True)) if self._status_code >= 400: span.error_occurred = True return result
def _sw_invoke_request(self, request, *args, **kwargs): context = get_context() carrier = Carrier() for item in carrier: val = request.headers.get(item.key) if val is not None: item.val = val with context.new_entry_span(op=request.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Pyramid span.peer = request.remote_host or request.remote_addr span.tag(Tag(key=tags.HttpMethod, val=request.method)) span.tag(Tag(key=tags.HttpUrl, val=str(request.url))) resp = _invoke_request(self, request, *args, **kwargs) span.tag(Tag(key=tags.HttpStatus, val=resp.status_code, overridable=True)) if resp.status_code >= 400: span.error_occurred = True return resp
def _sw_execute(this: _Bulk, *args, **kwargs): address = this.collection.database.client.address peer = "%s:%s" % address context = get_context() carrier = Carrier() sw_op = "MixedBulkWriteOperation" with context.new_exit_span(op="MongoDB/" + sw_op, peer=peer, carrier=carrier) as span: span.layer = Layer.Database span.component = Component.MongoDB bulk_result = _execute(this, *args, **kwargs) span.tag(Tag(key=tags.DbType, val="MongoDB")) span.tag( Tag(key=tags.DbInstance, val=this.collection.database.name)) if config.pymongo_trace_parameters: filters = "" bulk_ops = this.ops for bulk_op in bulk_ops: opname = bulk_op_map.get(bulk_op[0]) _filter = opname + " " + str(bulk_op[1]) filters = filters + _filter + " " max_len = config.pymongo_parameters_max_length filters = filters[0:max_len] + "..." if len( filters) > max_len else filters span.tag(Tag(key=tags.DbStatement, val=filters)) return bulk_result
def _sw_open(this: OpenerDirector, fullurl, data, timeout): if isinstance(fullurl, str): fullurl = Request(fullurl, data) context = get_context() carrier = Carrier() with context.new_exit_span(op=fullurl.selector or '/', peer=fullurl.host, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.General [fullurl.add_header(item.key, item.val) for item in carrier] try: res = _open(this, fullurl, data, timeout) span.tag(Tag(key=tags.HttpMethod, val=fullurl.get_method())) span.tag(Tag(key=tags.HttpUrl, val=fullurl.full_url)) span.tag(Tag(key=tags.HttpStatus, val=res.code)) if res.code >= 400: span.error_occurred = True except HTTPError as e: span.raised() raise e return res
def _sw__poll_once(this, timeout_ms, max_records, update_offsets=True): res = __poll_once(this, timeout_ms, max_records, update_offsets=update_offsets) if res: brokers = ";".join(this.config["bootstrap_servers"]) context = get_context() topics = ";".join( this._subscription.subscription or [t.topic for t in this._subscription._user_assignment]) with context.new_entry_span( op="Kafka/" + topics + "/Consumer/" + (this.config["group_id"] or "")) as span: for consumerRecords in res.values(): for record in consumerRecords: carrier = Carrier() for item in carrier: for header in record.headers: if item.key == header[0]: item.val = str(header[1]) span.extract(carrier) span.tag(Tag(key=tags.MqBroker, val=brokers)) span.tag(Tag(key=tags.MqTopic, val=topics)) span.layer = Layer.MQ span.component = Component.KafkaConsumer return res
def _sw_execute(this: Cursor, query, args=None): peer = "%s:%s" % (this.connection.host, this.connection.port) context = get_context() carrier = Carrier() with context.new_exit_span(op="Mysql/PyMsql/execute", peer=peer, carrier=carrier) as span: span.layer = Layer.Database span.component = Component.PyMysql res = _execute(this, query, args) span.tag(Tag(key=tags.DbType, val="mysql")) span.tag( Tag(key=tags.DbInstance, val=this.connection.db.decode("utf-8"))) span.tag(Tag(key=tags.DbStatement, val=query)) if config.mysql_trace_sql_parameters and args: parameter = ",".join([str(arg) for arg in args]) max_len = config.mysql_sql_parameters_max_length parameter = parameter[0:max_len] + "..." if len( parameter) > max_len else parameter span.tag( Tag(key=tags.DbSqlParameters, val='[' + parameter + ']')) return res
def _sw_get_response(this: BaseHandler, request): if request is None: resp = _get_response(this, request) return resp context = get_context() carrier = Carrier() for item in carrier: if item.key.capitalize() in request.headers: item.val = request.headers[item.key.capitalize()] with context.new_entry_span(op=request.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Django span.peer = '%s:%s' % (request.META.get('REMOTE_ADDR'), request.META.get('REMOTE_PORT') or "80") span.tag(Tag(key=tags.HttpMethod, val=request.method)) span.tag( Tag(key=tags.HttpUrl, val=request.build_absolute_uri())) resp = _get_response(this, request) span.tag(Tag(key=tags.HttpStatus, val=resp.status_code)) if resp.status_code >= 400: span.error_occurred = True return resp
def _sw_get_response(this, request): if request is None: resp = _get_response(this, request) return resp context = get_context() carrier = Carrier() for item in carrier: # Any HTTP headers in the request are converted to META keys by converting all characters to uppercase, # replacing any hyphens with underscores and adding an HTTP_ prefix to the name. # https://docs.djangoproject.com/en/3.0/ref/request-response/#django.http.HttpRequest.META sw_http_header_key = 'HTTP_%s' % item.key.upper().replace('-', '_') if sw_http_header_key in request.META: item.val = request.META[sw_http_header_key] with context.new_entry_span(op=request.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Django span.peer = '%s:%s' % (request.META.get('REMOTE_ADDR'), request.META.get('REMOTE_PORT') or "80") span.tag(Tag(key=tags.HttpMethod, val=request.method)) span.tag(Tag(key=tags.HttpUrl, val=request.build_absolute_uri().split("?")[0])) # you can get request parameters by `request.GET` even though client are using POST or other methods if config.django_collect_http_params and request.GET: span.tag(Tag(key=tags.HttpParams, val=params_tostring(request.GET)[0:config.http_params_length_threshold])) resp = _get_response(this, request) span.tag(Tag(key=tags.HttpStatus, val=resp.status_code, overridable=True)) if resp.status_code >= 400: span.error_occurred = True return resp
def _sw_full_dispatch_request(this: Flask): import flask req = flask.request context = get_context() carrier = Carrier() for item in carrier: if item.key.capitalize() in req.headers: item.val = req.headers[item.key.capitalize()] with context.new_entry_span(op=req.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Flask span.peer = '%s:%s' % (req.environ["REMOTE_ADDR"], req.environ["REMOTE_PORT"]) span.tag(Tag(key=tags.HttpMethod, val=req.method)) span.tag(Tag(key=tags.HttpUrl, val=req.url.split("?")[0])) if config.flask_collect_http_params and req.values: span.tag( Tag(key=tags.HttpParams, val=params_tostring(req.values) [0:config.http_params_length_threshold])) resp = _full_dispatch_request(this) if resp.status_code >= 400: span.error_occurred = True span.tag( Tag(key=tags.HttpStatus, val=resp.status_code, overridable=True)) return resp
def _sw_command(this: SocketInfo, dbname, spec, *args, **kwargs): # pymongo sends `ismaster` command continuously. ignore it. if spec.get("ismaster") is None: address = this.sock.getpeername() peer = "%s:%s" % address context = get_context() carrier = Carrier() operation = list(spec.keys())[0] sw_op = operation.capitalize() + "Operation" with context.new_exit_span(op="MongoDB/" + sw_op, peer=peer, carrier=carrier) as span: result = _command(this, dbname, spec, *args, **kwargs) span.layer = Layer.Database span.component = Component.MongoDB span.tag(Tag(key=tags.DbType, val="MongoDB")) span.tag(Tag(key=tags.DbInstance, val=dbname)) if config.pymongo_trace_parameters: # get filters filters = _get_filter(operation, spec) max_len = config.pymongo_parameters_max_length filters = filters[0:max_len] + "..." if len( filters) > max_len else filters span.tag(Tag(key=tags.DbStatement, val=filters)) else: result = _command(this, dbname, spec, *args, **kwargs) return result
async def _sw_handle_request(self, request, start_time: float): context = get_context() carrier = Carrier() for item in carrier: val = request.headers.get(item.key) if val is not None: item.val = val with context.new_entry_span(op=request.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.AioHttp span.peer = '%s:%d' % request._transport_peername if isinstance(request._transport_peername, (list, tuple))\ else request._transport_peername span.tag(Tag(key=tags.HttpMethod, val=request.method)) # pyre-ignore span.tag(Tag(key=tags.HttpUrl, val=str(request.url))) # pyre-ignore resp, reset = await _handle_request(self, request, start_time) span.tag(Tag(key=tags.HttpStatus, val=resp.status, overridable=True)) if resp.status >= 400: span.error_occurred = True return resp, reset
def _wrap_run_wsgi(): context = get_context() carrier = Carrier() for item in carrier: item.val = handler.headers[item.key.capitalize()] path = handler.path or '/' with context.new_entry_span(op=path.split("?")[0], carrier=carrier) as span: url = 'http://' + handler.headers[ "Host"] + path if 'Host' in handler.headers else path span.layer = Layer.Http span.component = Component.General span.peer = '%s:%s' % handler.client_address span.tag(Tag(key=tags.HttpMethod, val=handler.command)) span.tag(Tag(key=tags.HttpUrl, val=url)) try: return _run_wsgi() finally: status_code = int(getattr(handler, '_status_code', -1)) if status_code > -1: span.tag( Tag(key=tags.HttpStatus, val=status_code, overridable=True)) if status_code >= 400: span.error_occurred = True
def _sw_open(this: OpenerDirector, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): if isinstance(fullurl, str): fullurl = Request(fullurl, data) context = get_context() carrier = Carrier() url = fullurl.selector.split("?")[0] if fullurl.selector else '/' with context.new_exit_span(op=url, peer=fullurl.host, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.General code = None [fullurl.add_header(item.key, item.val) for item in carrier] try: res = _open(this, fullurl, data, timeout) code = res.code except HTTPError as e: code = e.code raise finally: # we do this here because it may change in _open() span.tag(Tag(key=tags.HttpMethod, val=fullurl.get_method())) span.tag(Tag(key=tags.HttpUrl, val=fullurl.full_url)) if code is not None: span.tag(Tag(key=tags.HttpStatus, val=code, overridable=True)) if code >= 400: span.error_occurred = True return res
def _sw_request(this: Session, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None): from urllib.parse import urlparse url_param = urlparse(url) # ignore trace skywalking self request if config.protocol == 'http' and config.collector_address.rstrip( '/').endswith(url_param.netloc): return _request(this, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) context = get_context() carrier = Carrier() with context.new_exit_span(op=url_param.path or "/", peer=url_param.netloc, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.General if headers is None: headers = {} for item in carrier: headers[item.key] = item.val else: for item in carrier: headers[item.key] = item.val try: res = _request(this, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) span.tag(Tag(key=tags.HttpMethod, val=method.upper())) span.tag(Tag(key=tags.HttpUrl, val=url)) span.tag(Tag(key=tags.HttpStatus, val=res.status_code)) if res.status_code >= 400: span.error_occurred = True except BaseException as e: span.raised() raise e return res
async def _create_span(request: Request) -> Span: context = get_context() span = context.new_entry_span(op=request.url.path, carrier=Carrier()) span.start() span.layer = Layer.Http span.component = Component.Requests span.peer = f"{request.client.host}:{request.client.port}" span.tag(tag=Tag(key=tags.HttpMethod, val=request.method, overridable=False)) return span
def build_ref(cls, snapshot: 'Snapshot'): from skywalking.trace.carrier import Carrier carrier = Carrier() carrier.trace_id = str(snapshot.trace_id) carrier.segment_id = str(snapshot.segment_id) carrier.endpoint = snapshot.endpoint carrier.span_id = snapshot.span_id carrier.service = config.service_name carrier.service_instance = config.service_instance return SegmentRef(carrier, ref_type="CrossThread")
def inject(self) -> 'Carrier': return Carrier( trace_id=str(self.context.segment.related_traces[0]), segment_id=str(self.context.segment.segment_id), span_id=str(self.sid), service=config.service_name, service_instance=config.service_instance, endpoint=self.op, client_address=self.peer, correlation=self.context._correlation, )
def _wrap_run_wsgi(): context = get_context() carrier = Carrier() for item in carrier: item.val = handler.headers[item.key.capitalize()] with context.new_entry_span(op=handler.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.General span.peer = '%s:%s' % handler.client_address span.tag(Tag(key=tags.HttpMethod, val=handler.command)) return _run_wsgi()
def before_request_tracing(request: Request): carrier = Carrier() for item in carrier: item.val = request.headers.get(item.key.capitalize(), None) context = get_context() span = context.new_entry_span(op=request.path, carrier=carrier) span.start() span.layer = Layer.Http span.component = Component.General span.peer = '%s:%s' % request.socket span.tag(Tag(key=tags.HttpMethod, val=request.method)) request.ctx.sw_span = span
def _sw_do_method(): context = get_context() carrier = Carrier() for item in carrier: item.val = this.headers[item.key.capitalize()] with context.new_entry_span(op=this.path, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.General span.peer = '%s:%s' % this.client_address span.tag(Tag(key=tags.HttpMethod, val=method)) _do_method()
def _sw_send(this, topic, value=None, key=None, headers=None, partition=None, timestamp_ms=None): # ignore trace skywalking self request if config.protocol == 'kafka' and config.kafka_topic_segment == topic or config.kafka_topic_management == topic: return _send(this, topic, value=value, key=key, headers=headers, partition=partition, timestamp_ms=timestamp_ms) peer = ";".join(this.config["bootstrap_servers"]) context = get_context() carrier = Carrier() with context.new_exit_span(op="Kafka/" + topic + "/Producer" or "/", peer=peer, carrier=carrier) as span: span.layer = Layer.MQ span.component = Component.KafkaProducer if headers is None: headers = [] for item in carrier: headers.append((item.key, item.val.encode("utf-8"))) else: for item in carrier: headers.append((item.key, item.val.encode("utf-8"))) try: res = _send(this, topic, value=value, key=key, headers=headers, partition=partition, timestamp_ms=timestamp_ms) span.tag(Tag(key=tags.MqBroker, val=peer)) span.tag(Tag(key=tags.MqTopic, val=topic)) except BaseException as e: span.raised() raise e return res
def _sw_basic_publish(this, exchange, routing_key, body, properties=None, mandatory=False): peer = '%s:%s' % (this.connection.params.host, this.connection.params.port) context = get_context() carrier = Carrier() import pika with context.new_exit_span(op="RabbitMQ/Topic/" + exchange + "/Queue/" + routing_key + "/Producer" or "/", peer=peer, carrier=carrier) as span: span.layer = Layer.MQ span.component = Component.RabbitmqProducer properties = pika.BasicProperties( ) if properties is None else properties if properties.headers is None: headers = {} for item in carrier: headers[item.key] = item.val properties.headers = headers else: for item in carrier: properties.headers[item.key] = item.val try: res = _basic_publish(this, exchange, routing_key, body, properties=properties, mandatory=mandatory) span.tag(Tag(key=tags.MqBroker, val=peer)) span.tag(Tag(key=tags.MqTopic, val=exchange)) span.tag(Tag(key=tags.MqQueue, val=routing_key)) except BaseException as e: span.raised() raise e return res
def _sw_request(this: RequestMethods, method, url, fields=None, headers=None, **urlopen_kw): from urllib.parse import urlparse url_param = urlparse(url) carrier = Carrier() context = get_context() with context.new_exit_span(op=url_param.path or "/", peer=url_param.netloc, carrier=carrier) as span: span.layer = Layer.Http span.component = Component.Urllib3 if headers is None: headers = {} for item in carrier: headers[item.key] = item.val else: for item in carrier: headers[item.key] = item.val try: res = _request(this, method, url, fields=fields, headers=headers, **urlopen_kw) span.tag(Tag(key=tags.HttpMethod, val=method.upper())) span.tag(Tag(key=tags.HttpUrl, val=url)) span.tag(Tag(key=tags.HttpStatus, val=res.status)) if res.status >= 400: span.error_occurred = True except BaseException as e: span.raised() raise e return res
def fun(*args, **kwargs): req = task.request_stack.top # di = req.get('delivery_info') # exchange = di and di.get('exchange') # queue = di and di.get('routing_key') # op = 'celery/{}/{}/{}'.format(exchange or '', queue or '', name) op = 'celery/' + name carrier = Carrier() for item in carrier: val = req.get(item.key) if val: item.val = val context = get_context() origin = req.get('origin') if origin: span = context.new_entry_span(op=op, carrier=carrier) span.peer = origin.split('@', 1)[-1] else: span = context.new_local_span(op=op) with span: span.layer = Layer.MQ span.component = Component.Celery span.tag( Tag(key=tags.MqBroker, val=task.app.conf['broker_url'])) # span.tag(Tag(key=tags.MqTopic, val=exchange)) # span.tag(Tag(key=tags.MqQueue, val=queue)) if config.celery_parameters_length: params = '*{}, **{}'.format( args, kwargs)[:config.celery_parameters_length] span.tag(Tag(key=tags.CeleryParameters, val=params)) return _fun(*args, **kwargs)
def finish(self): """Called when all response data has been written to this Request. Overrides twisted.web.server.Request.finish to record the finish time and do logging. """ from skywalking.trace.carrier import Carrier from skywalking.trace.context import get_context from skywalking.trace import tags from skywalking.trace.tags import Tag from skywalking import Layer, Component context = get_context() carrier = Carrier() for item in carrier: val = self.getAllHeaders().get(item.key.encode()) if val is not None: item.val = val.decode() with context.new_entry_span(op=self.path.decode(), carrier=carrier) as span: span.layer = Layer.Http span.component = Component.General span.tag(Tag(key=tags.HttpMethod, val=self.method.decode())) span.tag(Tag(key=tags.HttpUrl, val=self.uri.decode())) span.tag(Tag(key=tags.HttpStatus, val=self.code, overridable=True)) if self.code >= 400: span.error_occurred = True logger.info(self.code) logger.info(self.getClientAddress()) self.finish_time = time.time() Request.finish(self) if not self._is_processing: assert self.logcontext is not None with PreserveLoggingContext(self.logcontext): self._finished_processing()
def inject(self) -> 'Carrier': return Carrier(correlation=self.context._correlation)