U mf @s"ddlZddlZddlmZmZmZmZmZm Z m Z ddl m Z ddl mZeee dddZd d d d Zeeed ddZedZedZeedddZeedddZeeedddZeeeefdddZee eeefdddZeeddd Ze ee d!d"d#ZdS)$N)DictIterableListMatchOptionalTextIOTuple)Metric)Sample)textreturnccstt|EdHdS)z]Parse Prometheus text format from a unicode string. See text_fd_to_metric_families. N)text_fd_to_metric_familiesStringIO)r rF/opt/hc_python/lib/python3.8/site-packages/prometheus_client/parser.pytext_string_to_metric_families sr\ ")z\\z\nz\")matchr cCst|dS)Nr)ESCAPE_SEQUENCESgroup)rrrrreplace_escape_sequencesrz\\[\\n]z\\[\\n"])sr cCs tt|SN)HELP_ESCAPING_REsubrrrrr_replace_help_escaping srcCs tt|Sr) ESCAPING_RErrrrrr_replace_escaping$sr!)rcharposr cCs6d}||kr*||d|dkr*|d7}q|ddkS)Nrr rr)rr"Z num_bslashesrrr_is_character_escaped(s  r$) labels_stringr c Cs(i}d|kr|Sd}d|kr d}|}z|r|d}|d|}||dd}|dd}||d}d}|t|kr|d|}t||sq|d7}qr|d} ||| } |rt| } | ||<|| dd}|dd} || d}q&|WStk r"td |YnXdS) N=FrTr rr,zInvalid labels: %s)indexlstriplenr$r!stripfind ValueError) r%labelsZescapingZ sub_labelsZ value_startZ label_nameZ quote_startZ value_substriZ quote_endZ label_valueZ next_commarrr _parse_labels0s>         r0cCsp|}d}||krd}dd||D}|shsz._parse_value_and_timestamp..rr i)r)splitfloatr*)r separatorvaluesr4 timestamprrr_parse_value_and_timestampcs   r<c Cszb|d|d}}|d|}||d|}t||dd\}}t|t|||WStk rd}||krd}||}|d|}t||d\}}t|i||YSXdS)N{}r r1r2)r(rindexr+r<r r0r-) r Z label_startZ label_endnamelabelr4r;r9Zname_endrrr _parse_sampleps  rB)fdr c #sdd}d}g}g}ttttttddd}|D]}|}|dr\|dd}t|d krfq2|d d kr|d krdkr||||V|d d}g}|d g}t|d krt|d}nd}n|d d kr|d krdkr||||V|d d}g}|d}dgdgdddgdddgd |dg}fdd|D}nq2|dkrhq2t |}|j |krˆdkr||||Vdd}d}g}g}||d|||gVq2| |q2dkr||||VdS)zParse Prometheus text format from a file descriptor. This is a laxer parser than the main Go parser, so successful parsing does not imply that the parsed text meets the specification. Yields Metric's. Zuntyped)r@ documentationtypsamplesr cSsl|dkrV|dr |dd}n6g}|D],}|t|ddf|dd|}q(t|||}||_|S)NcounterZ_totalirr )endswithappendr r rG)r@rErFrGZ new_samplesrZmetricrrr build_metrics $ z0text_fd_to_metric_families..build_metric#Nr#r ZHELPZTYPE_countZ_sumZ_bucket)rHZgaugesummaryZ histogramcsg|] }|qSrr)r3nr@rrr5sz.text_fd_to_metric_families..r) strrr r r+ startswithr7r*rgetrBr@rJ) rCrErFrGZ allowed_namesrKlinepartssamplerrRrrsp               r) iorretypingrrrrrrrZ metrics_corer rGr rSrrrcompilerr rr!intboolr$r0r8r<rBrrrrrs&$     3