U mfs@sddlmZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZzeWnek reZYnXGd d d Zd d ejDZdd dZdS)) defaultdictN)Gauge)Metric) MmapedDict)Sample)floatToGoStringc@sHeZdZdZdddZedddZedd Zed d Zd d Z dS)MultiProcessCollectorz+Collector for files for multi-process mode.NcCst|dkrDdtjkr8dtjkr8tjdtjd<tdttjd}|rTtj|s\td||_ |rp| |dS)Nprometheus_multiproc_dirPROMETHEUS_MULTIPROC_DIRzpprometheus_multiproc_dir variable has been deprecated in favor of the upper case naming PROMETHEUS_MULTIPROC_DIRz:env PROMETHEUS_MULTIPROC_DIR is not set or not a directory) osenvironwarningswarnDeprecationWarninggetpathisdir ValueError_pathregister)selfregistryrrL/opt/hc_python/lib/python3.8/site-packages/prometheus_client/multiprocess.py__init__s  zMultiProcessCollector.__init__TcCst|}t||S)zMerge metrics from given mmap files. By default, histograms are accumulated, as per prometheus wire format. But if writing the merged data back to mmap files, use accumulate=False to avoid compound accumulation. )r _read_metrics_accumulate_metrics)files accumulatemetricsrrrmerge#s zMultiProcessCollector.mergec si}ifdd}|D]}tj|d}|d}zt|}Wn0tk rt|dkrn|ddrnYqYnX|D]\}}} } ||\} } } }}|| }|dkrt | ||}||| <|dkr|ddd }|d|_ | | |d |ff|| qz| | ||qzq|S) NcsJ|}|sFt|\}}}}tt|}|||||f}|<|SN)rjsonloadstuplesorteditems)keyval metric_namenamelabels help_text labels_keyZ key_cacherr _parse_key3s  z7MultiProcessCollector._read_metrics.._parse_key_rgaugerlivepid) r rbasenamesplitrZread_all_values_from_fileFileNotFoundError startswithrr_multiprocess_modeZ add_sample)rr r0fpartstypZ file_valuesr(value timestampr1r*r+r,r.r-metricr6rr/rr.s0    z#MultiProcessCollector._read_metricscCsV|D]B}tt}tt}tdd}|j}|jD]n}|\}} } } } |jdkr|tdd| Df} |jdkr|| | }| |kr| || <n|jdkr|| | }| |kr| || <nd|jdkr|| | 7<nH|jd kr|| }t| pd } || kr| || <| || <n | ||| f<q6|jd kr| D]L}|d d kr.t|d }tdd| D}|||| 7<qq.||| f| 7<q6||| f| 7<q6|jd kr8|D]z\} }d}t |D]F\}} |j d| d t |fff}|r|| 7}|||<n| ||<q|r|||j d| f<qdd|D|_q|S)NcSsttSr")rfloatrrrr]z;MultiProcessCollector._accumulate_metrics..r2css|]}|ddkr|VqdS)rr6Nr.0lrrr bs z.)minZlivemin)maxZlivemax)sumZlivesum)Z mostrecentZlivemostrecentrZ histogramlercss|]}|ddkr|VqdS)rrLNrrErrrrH}s gZ_bucket_countcSs$g|]\\}}}t|t||qSr)rdict)rFZname_r,r?rrr s z=MultiProcessCollector._accumulate_metrics..) valuesrrB setdefaultsamplestyper%r;r'r&r+r)r rrArRZsample_timestampsZbucketsZsamples_setdefaultsr+r,r?r@ZexemplarZwithout_pid_keycurrentZcurrent_timestamprGZ bucket_valueZ without_lerPaccZbucketZ sample_keyrrrrXsd                    z)MultiProcessCollector._accumulate_metricscCs$ttj|jd}|j|ddS)Nz*.dbT)r)globr rjoinrr!)rrrrrcollectszMultiProcessCollector.collect)N)T) __name__ __module__ __qualname____doc__r staticmethodr!rrrYrrrrr s   ) Cr cCsh|]}|dr|qS)r3)r:)rFmrrr s r`c Cs^|dkrtjdtjd}tD]6}ttj|d|d|dD]}t|qHq"dS)zBDo bookkeeping for when one process dies in a multi-process setup.Nr r Zgauge_r1z.db)r r r_LIVE_GAUGE_MULTIPROCESS_MODESrWrrXremove)r6rmoder<rrrmark_process_deads &rd)N) collectionsrrWr#r rr rZ metrics_corerZ mmap_dictrrRrutilsrr9 NameErrorIOErrorr Z_MULTIPROC_MODESrardrrrrs"