def decompose_args(method, url, body, headers, encode_chunked=False): # skip httplib tracing for SDK built-in centralized sampling pollers if (('/GetSamplingRules' in args or '/SamplingTargets' in args) and type(instance).__name__ == 'botocore.awsrequest.AWSHTTPConnection'): return wrapped(*args, **kwargs) # Only injects headers when the subsegment for the outgoing # calls are opened successfully. subsegment = None try: subsegment = xray_recorder.current_subsegment() except SegmentNotFoundException: pass if subsegment: inject_trace_header(headers, subsegment) ssl_cxt = getattr(instance, '_context', None) scheme = 'https' if ssl_cxt and type( ssl_cxt).__name__ == 'SSLContext' else 'http' xray_url = '{}://{}{}'.format(scheme, instance.host, url) xray_data = _XRay_Data(method, instance.host, xray_url) setattr(instance, _XRAY_PROP, xray_data) # we add a segment here in case connect fails return xray_recorder.record_subsegment( wrapped, instance, args, kwargs, name=strip_url(xray_data.url), namespace='remote', meta_processor=http_send_request_processor)
def _inject_header(wrapped, instance, args, kwargs): request = args[0] headers = getattr(request, 'headers', {}) inject_trace_header(headers, xray_recorder.current_subsegment()) setattr(request, 'headers', headers) return wrapped(*args, **kwargs)
def begin_subsegment(request: Request, recorder: AsyncAWSXRayRecorder, name: str = None) -> Optional[Subsegment]: """ Begins a subsegment before sending an interservice request. :param request: The httpx request object for interservice communications. :param recorder: The AWS X-Ray recorder for this application. :return: The started subsegment. """ name = name or strip_url(str(request.url)) try: subsegment = recorder.begin_subsegment(name, REMOTE_NAMESPACE) except ( exceptions.SegmentNotFoundException, exceptions.AlreadyEndedException, ): subsegment = None # No-op if subsegment is `None` due to `LOG_ERROR`. if not subsegment: request.give_up = True else: request.give_up = False subsegment.put_http_meta(http.METHOD, request.method) subsegment.put_http_meta(http.URL, str(request.url)) inject_trace_header(request.headers, subsegment) return subsegment
async def begin_subsegment(session, trace_config_ctx, params): name = trace_config_ctx.name if trace_config_ctx.name else strip_url( str(params.url)) subsegment = xray_recorder.begin_subsegment(name, REMOTE_NAMESPACE) subsegment.put_http_meta(http.METHOD, params.method) subsegment.put_http_meta(http.URL, params.url.human_repr()) inject_trace_header(params.headers, subsegment)
async def begin_subsegment(session, trace_config_ctx, params): name = trace_config_ctx.name if trace_config_ctx.name else strip_url(str(params.url)) subsegment = xray_recorder.begin_subsegment(name, REMOTE_NAMESPACE) # No-op if subsegment is `None` due to `LOG_ERROR`. if not subsegment: trace_config_ctx.give_up = True else: trace_config_ctx.give_up = False subsegment.put_http_meta(http.METHOD, params.method) subsegment.put_http_meta(http.URL, params.url.human_repr()) inject_trace_header(params.headers, subsegment)
def inject_header(wrapped, instance, args, kwargs): # skip tracing for SDK built-in centralized sampling pollers url = args[0].url if 'GetCentralizedSamplingRules' in url or 'SamplingTargets' in url: return wrapped(*args, **kwargs) headers = args[0].headers # skip if the recorder is unable to open the subsegment # for the outgoing request subsegment = None try: subsegment = xray_recorder.current_subsegment() except SegmentNotFoundException: pass if subsegment: inject_trace_header(headers, subsegment) return wrapped(*args, **kwargs)
def decompose_args(method, url, body, headers, encode_chunked=False): inject_trace_header(headers, xray_recorder.current_subsegment()) # we have to check against sock because urllib3's HTTPSConnection inherit's from http.client.HTTPConnection scheme = 'https' if isinstance(instance.sock, ssl.SSLSocket) else 'http' xray_url = '{}://{}{}'.format(scheme, instance.host, url) xray_data = _XRay_Data(method, instance.host, xray_url) setattr(instance, _XRAY_PROP, xray_data) # we add a segment here in case connect fails return xray_recorder.record_subsegment( wrapped, instance, args, kwargs, name=strip_url(xray_data.url), namespace='remote', meta_processor=http_send_request_processor)
def decompose_args(method, url, body, headers, encode_chunked=False): # skip httplib tracing for SDK built-in centralized sampling pollers if (('/GetSamplingRules' in args or '/SamplingTargets' in args) and type(instance).__name__ == 'botocore.awsrequest.AWSHTTPConnection'): return wrapped(*args, **kwargs) # Only injects headers when the subsegment for the outgoing # calls are opened successfully. subsegment = None try: subsegment = xray_recorder.current_subsegment() except SegmentNotFoundException: pass if subsegment: inject_trace_header(headers, subsegment) if issubclass(instance.__class__, urllib3.connection.HTTPSConnection): ssl_cxt = getattr(instance, 'ssl_context', None) elif issubclass(instance.__class__, httplib.HTTPSConnection): ssl_cxt = getattr(instance, '_context', None) else: # In this case, the patcher can't determine which module the connection instance is from. # We default to it to check ssl_context but may be None so that the default scheme would be # (and may falsely be) http. ssl_cxt = getattr(instance, 'ssl_context', None) scheme = 'https' if ssl_cxt and type( ssl_cxt).__name__ == 'SSLContext' else 'http' xray_url = '{}://{}{}'.format(scheme, instance.host, url) xray_data = _XRay_Data(method, instance.host, xray_url) setattr(instance, _XRAY_PROP, xray_data) # we add a segment here in case connect fails return xray_recorder.record_subsegment( wrapped, instance, args, kwargs, name=get_hostname(xray_data.url), namespace='remote', meta_processor=http_send_request_processor)
def inject_header(wrapped, instance, args, kwargs): headers = args[0] inject_trace_header(headers, xray_recorder.current_subsegment()) return wrapped(*args, **kwargs)
def _inject_header(wrapped, instance, args, kwargs): headers = kwargs.get('headers', {}) inject_trace_header(headers, xray_recorder.current_subsegment()) return wrapped(*args, **kwargs)