a 97ah@sddlZddlmZddlmZddlZddlZejjdkrJddl m Z n ddl m Z e dZ e dZ dZd d Zd d Zd dZddZddZddZddZddZddZdS)N) TraceHeader)http)urlparsez(.)([A-Z][a-z]+)z([a-z0-9])([A-Z])z UNKNOWN HOSTcCsj|sdSt|dr(|jdkr(|j}n|}|r:|jnd}t|j|j|j|d}| }||t j <dS)a Extract trace id, entity id and sampling decision from the input entity and inject these information to headers. :param dict headers: http headers to inject :param Entity entity: trace entity that the trace header value generated from. NtypeZ subsegment)rootparentsampleddata) hasattrrZparent_segmentZget_origin_trace_headerr rtrace_ididr to_header_strr XRAY_HEADER)headersentityheaderr Z to_insertvaluerp/private/var/folders/s6/9n5zrl012gv99k63s4q6ccsd4s6mqz/T/pip-target-f5cq3f2q/lib/python/aws_xray_sdk/ext/util.pyinject_trace_headers  rcCs<|jdur|jdkr|jS|js$dS|j|}|r8|SdS)a Return 1 or the matched rule name if should sample and 0 if should not. The sampling decision coming from ``trace_header`` always has the highest precedence. If the ``trace_header`` doesn't contain sampling decision then it checks if sampling is enabled or not in the recorder. If not enbaled it returns 1. Otherwise it uses user defined sampling rules to decide. N?r)r ZsamplingZsamplerZ should_trace)Z trace_headerrecorderZ sampling_reqZdecisionrrrcalculate_sampling_decision3s  rcCs0|tjp|tj}|r&t|StSdS)z Construct a ``TraceHeader`` object from dictionary headers of the incoming request. This method should always return a ``TraceHeader`` object regardless of tracing header's presence in the incoming request. N)getrrZALT_XRAY_HEADERrZfrom_header_str)rZ header_strrrrconstruct_xray_headerEs rcCs|jr|j|S|jSdS)z Returns the segment name based on recorder configuration and input host name. This is a helper generally used in web framework middleware where a host name is available from incoming request's headers. N)Zdynamic_namingget_nameZservice)Z host_namerrrrcalculate_segment_nameSs rcCs4|r |jdkr t|j|jd}n t|jd}|S)zs Prepare a trace header to be inserted into response based on original header and the request segment. r)rr )r)r rr r)Z origin_headersegmentZ new_headerrrrprepare_response_header_s  r cCstd|}td|S)z9 Convert the input string to snake-cased string. z\1_\2) first_cap_resub all_cap_relower)names1rrr to_snake_casems r'cCs|r|ddS|S)z Will generate a valid url string for use as a segment name :param url: url to strip :return: validated url string rr) partition)urlrrr strip_urlwsr*cCs2|dur tSt|}|j}|dur&tS|r.|S|S)N)UNKNOWN_HOSTNAMErhostname)r)Z url_parser,rrr get_hostnamesr-cCs8t||d}|r4t|tjr4t|dr4t|||jdS)zs Will unwrap a `wrapt` attribute :param obj: base object :param attr: attribute on `obj` to unwrap N __wrapped__)getattr isinstancewraptZ ObjectProxyr setattrr.)objattrfrrrunwraps r6)reZ%aws_xray_sdk.core.models.trace_headerrZaws_xray_sdk.core.modelsrr1sys version_infomajor urllib.parsercompiler!r#r+rrrrr r'r*r-r6rrrrs&