U mfmV@sddlZddlZddlZddlmZmZddlmZm Z m Z ddl m Z ddZ edhZd d Zd d d dZddZedZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Z dS)*N)MetricMETRIC_LABEL_NAME_RE)ExemplarSample Timestamp)floatToGoStringccstt|EdHdS)z^Parse Openmetrics text format from a unicode string. See text_fd_to_metric_families. N)text_fd_to_metric_familiesStringIO)textr R/opt/hc_python/lib/python3.8/site-packages/prometheus_client/openmetrics/parser.pytext_string_to_metric_families srinfcCs t|}|tkrdS|t|kS)NF)float_CANONICAL_NUMBERSr)sfr r r _isUncanonicalNumbersr\ ")z\\z\nz\"cCst|dS)Nr)ESCAPE_SEQUENCESgroup)matchr r r _replace_escape_sequence&srz\\[\\n"]cCs tt|SN) ESCAPING_REsubr)rr r r _replace_escaping-srcCsg}d}|D]p}|rd|dkr(|dn6|dkr<|dn"|dkrP|dn|d|d}q |dkrrd}q ||q |r|dd|S)NFrrnrT)appendjoin)r resultslashcharr r r _unescape_help1s$     r'cCsXd|}||ksd|kr,td|z t|WStk rRt|YSXdS)Nr!_zInvalid value: )r#strip ValueErrorintr)valuer r r _parse_valueLs  r-c Csd|}|sdS||ks&d|kr4td|ztt|dWStk rz<|dd}tt|dt|dddddWYStk rt|}t |st |rtd||YYSXYnXdS) Nr!r(zInvalid timestamp: r. 0) r#r)r*rr+splitljustrmathisnanisinf) timestamppartstsr r r _parse_timestampVs   0r:cCs6d}||kr*||d|dkr*|d7}q|ddkS)Nrr/rrr )rZcharposZ num_bslashesr r r _is_character_escapedls  r;cCsd}g}g}i}d}|D]}|dkrF|dkr4d}nd}||n|dkrj|dkr\d}n ||nd|dkr|dkrd }n td |n>|d kr|d krd }nd|dkrd |}t|std|||krtd|d |||<g}g}d}n ||n|dkrF|dkr(d}n|dkr8d}n td |n|d krd }|d krj|d n:|dkr|dn$|dkr|dn|d |n(|dkr|dkrqn td ||d7}q||fS)NZstartoflabelnamer}Z endoflabels labelname=Zlabelvaluequoter labelvalueInvalid line: rZlabelvalueslashr!zInvalid line, bad label name: z$Invalid line, duplicate label name: Zendoflabelvalue,r r r/)r"r*r#rr)r stater=r?labels labels_lenr&lnr r r _parse_labels_with_state_machinetsl                      rGc Csi}|rd|krt|}zR|rh|d}|d|}||dd}|rX|ddkr\t|dd}|rx|ddkr|t|r|ddkrtd}|t|kr|d|}t|d||sq|d7}q|d}|d|}d|krt|}t|std|||krtd ||||<||dd}|drFd} nd} || d}|r| dkrtq|WStk rtd |YnXdS) Nr>r/rrrArzinvalid line, bad label name: z$invalid line, duplicate label name: zInvalid labels: )r*indexlenr;rrr startswith) r rDZ sub_labelsZ value_startZ label_nameZ value_substriZ quote_endZ label_valueZ next_commar r r _parse_labelssR               rMc Csd}|d}|dks&||d|krj|d}|d|}||dd}t|\}}}t|i|||S|d|}||kr|d} ||d| } t| } n$t||dd\} } | t|} || dd}t|\}}}t|| |||S)Nz # {rHrBr/r<r)findrI_parse_remaining_textrrindexrMrGrJ) r separator label_startZname_endnameZremaining_textr,r7exemplar label_endlabelrDrEr r r _parse_samples$       rXcCsr|dd}t|d}t|dkr.|ddfSg}g}g}d}d}|d}t|}|D]j} |dkr| dkrv|svd}n| dkrd}n || qV|dkr| dkrd}n td|qV|dkr| dkrd }n td|qV|d kr*| d kr|d |d } } t|| d| }d }n td|qV|d krD| d krd }qV|d krl| dkr^d}n td|qV|dkr| dkr|std|n| dkrd}n || qV|dkrV|| qV|dkr|std||dkr|std||dkrtd|t |} d} |dk rht dd| D}|dkrTtd|t |t|t |} || | fS)NrBr/rr7# exemplarspace exemplarhashr@exemplarstartoflabelsrNr<exemplarparsedlabelsZexemplarvaluespaceZ exemplarvalueZexemplartimestamp)r[rZr\r]css"|]\}}t|t|VqdSr)rJ).0kvr r r csz(_parse_remaining_text..zExemplar labels are too long: ) r2r-rJiterr"r*rIrQrMr:sumitemsr)r Z split_textvalr7Zexemplar_valueZexemplar_timestampZexemplar_labelsrCitr&rSrVr9rUZexemplar_lengthr r r rPs                        rPcCs~|dkr iS|dkr2|j|kr2|j}|d=|S|dkrN|j}||=|S|dkrx|j|dkrx|j}|d=|S|jS)Ninfosummaryquantilestateset histogramgaugehistogram_bucketle)rTrDcopy)samplerTtypdr r r _group_for_sampleos   ruc sBd}d}fdd}|D]}|jtd}t|d}||ks\|j|kr|dk rj|ddddddd|}|j}|dkrt|jd}|dkrddk r|krtd |jkrtd ||jq&|d kr|jq&|d krdq&|d kr&d|jdkr&dq&|dk r>|dS)Ncstdkrtddk r4kr4tdrLdkrLtdrddkrdtdssdk rtdrrtdsrtddS) Nz+Infz+Inf bucket missing: z!Count does not match +Inf value: z+_count must be present if _sum is present: z-_gcount must be present if _gsum is present: z1_sum/_gsum must be present if _count is present: z(Cannot have _sum with negative buckets: z6Cannot have negative _gsum with non-negative buckets: )rr*r ZbucketcountZhas_gsumZhas_negative_bucketsZhas_negative_gsumZhas_sumrTr,r r do_checkss         z#_check_histogram..do_checksrmFrrorpTzBuckets out of order: zBucket values out of order: )_count_gcount)_sum)_gsum)rTrJrur7rrDr*r,) samplesrTrr7rxrsuffixgbr rvr _check_histogramsL         rc#sdg}d}tddgddddgddddgd d dgd gd fd d}|D]n}|ddkrn|dd}|r~td||std|dkrd}qP|dr|dd}t|dkrtd||dkr|rtd||dkrDdk r|||| |V|dd} d}d}d} t} d} t} g}|dg}|ddkrv|dk rhtd|t|d}n|ddkr|dk rtd ||d}|d!krtd"|fd#d$|dgD}n:|dd%kr| dk rtd&||d} n td|qPt|}|j|krtdk rB|||| |V|jd}d} d'}g}d} d} t} t} |jg}|d(kr|j krtd)|d|jkr|j d*d+d+kst |j d*rtd,|d|jkr t |j t s |j s td-|d|jks,d |jkrRt |j t sR|j sRtd.||d/kr|jkrd0t|j d1dkrdkrnnt |j d1rtd2|ttt||}| dk r|| kr|| krtd3|| dk rR|| krR|jdk| dkkr$td4|| dk rX| |jkrX|d5krXtd6|nt} |jtt|j f}|j| ks|| kr||| ||} |j} | ||d(kr|j d7krtd8||d5kr|j dkrtd9||d/kr |jkr |j d0kr td:||jtdd;krRt|j rRtd<||jtdd=kr|j d0krtd>||jrP|d?kr|jdsP|d@kr|jdsPtdA|qPdk r|||| |V|stdBdS)CzParse 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. NF_totalZ_createdr!ryr{rorzr|_info)counterrirmrnrhcs|dkr d}t|gdgD].}||krBtd||||q"|dkr^d}|dkrjd}|r|d|std||r|dkrtd||dkrt||t||||}||_|S) Nunknownr!zClashing name: r(z!Unit does not match metric name: )rhrkz(Units not allowed for this metric type: rl)setgetr*addendswithrrr})rT documentationrsunitr}r~Zmetric) seen_names type_suffixesr r build_metrics&     z0text_fd_to_metric_families..build_metricrHrzReceived line after # EOF: zReceived blank linez# EOFTrYrBr@rz!Received metadata after samples: r/ZHELPzMore than one HELP for metric: ZTYPEzMore than one TYPE for metric: ZuntypedzInvalid TYPE for metric: csg|] }|qSr r )r^r )rTr r sz.text_fd_to_metric_families..ZUNITzMore than one UNIT for metric: rrkzStateset missing label: rpNaNzInvalid le label: z!Bucket value must be an integer: z Count value must be an integer: rirrjzInvalid quantile label: zInvalid metric grouping: z*Mix of timestamp presence within a group: rhz*Timestamps went backwards within a group: )rr/z4Stateset samples can only have values zero and one: z&Info samples can only have value one: z$Quantile values cannot be negative: )rr{ryrorzr|z$Counter-like samples cannot be NaN: )rr{ryrorzz)Counter-like samples cannot be negative: rl)rzTInvalid line only histogram/gaugehistogram buckets and counters can have exemplars: zMissing # EOF at end)rr*rKr2rJr'rrXrTrDr isinstancer,r+ is_integerrtuplesortedrurer7r"rr4r5rUr)fdZ allowed_nameseofrliner8r}rrsrrZ seen_groupsZgroup_timestampZgroup_timestamp_samplesrrrZ series_idr )rTrrr r s                                             "  $    r )!ior r4reZ metrics_corerrr}rrrutilsrrrrrrrcompilerrr'r-r:r;rGrMrXrPrurr r r r r s4    BDV;