관리-도구
편집 파일: exposition.cpython-38.pyc
U .?�fd � @ sX d dl Z d dlmZ d dlZd dlmZ d dlZd dlZd dlm Z d dl Z d dlZd dlZd dl mZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZ d dlmZmZmZm Z m!Z!m"Z" d d l#m$Z$m%Z%m&Z& d dl'm(Z' d dl)m*Z*m+Z+ d d l,m-Z- dZ.dZ/G dd� de �Z0dd� Z1e+dfe*e2ed�dd�Z3G dd� de%�Z4G dd� de e&�Z5dd� Z6dSe7e7e8ee7 ee7 e2e j9d�dd �Z:d!e+dddde j;dfe8e7e*ee7 ee7 ee7 ee7 e8e2ee&ej<f d"� d#d$�Z=e=Z>e+fe*e?d%�d&d'�Z@e7eee*ge?f e7f d(�d)d*�ZAe7e2d+�d,d-�ZBG d.d/� d/e�ZCe7e*dd0�d1d2�ZDe7e7eeE eee7e7f e?eeeFf eg df d3�d4d5�ZGe7e7eeE eee7e7f e?eg df d6�d7d8�ZHe7e7eeE eee7e7f e?eg df d6�d9d:�ZIdTe7e7eeE eee7e7f e?ee7 ee7 eg df d;�d<d=�ZJde jKdfe7e7eeE eee7e7f e?e7e7ee7 e8e2eg df d>�d?d@�ZLddAeHfe7e7e*eee7ef eeE eddB�dCdD�ZMddAeHfe7e7ee* eee7ef eeE eddB�dEdF�ZNddAeHfe7e7eee7ef eeE eddG�dHdI�ZOe7e7e7ee* eee7ef eeE eddJ�dKdL�ZPdMdN� ZQee7ef dO�dPdQ�ZRd dRlSmTZT dS )U� N)�closing)�BaseHTTPRequestHandler)�ThreadingMixIn)�Any�Callable�Dict�List�Optional�Sequence�Tuple�Union)� HTTPError)�parse_qs� quote_plus�urlparse)�BaseHandler�build_opener�HTTPHandler�HTTPRedirectHandler�HTTPSHandler�Request)�make_server�WSGIRequestHandler� WSGIServer� )� exposition)�CollectorRegistry�REGISTRY)�floatToGoString)�CONTENT_TYPE_LATEST�delete_from_gateway�generate_latest�instance_ip_grouping_key� make_asgi_app� make_wsgi_app�MetricsHandler�push_to_gateway�pushadd_to_gateway�start_http_server�start_wsgi_server�write_to_textfilez(text/plain; version=0.0.4; charset=utf-8c @ s e Zd ZdZdd� ZdS )�_PrometheusRedirectHandleraw Allow additional methods (e.g. PUT) and data forwarding in redirects. Use of this class constitute a user's explicit agreement to the redirect responses the Prometheus client will receive when using it. You should only use this class if you control or otherwise trust the redirect behavior involved and are certain it is safe to full transfer the original request (method and data) to the redirected URL. For example, if you know there is a cosmetic URL redirect in front of a local deployment of a Prometheus server, and all redirects are safe, this is the class to use to handle redirects in that case. The standard HTTPRedirectHandler does not forward request data nor does it allow redirected PUT requests (which Prometheus uses for some operations, for example `push_to_gateway`) because these cannot generically guarantee no violations of HTTP RFC 2616 requirements for the user to explicitly confirm redirects that could have unexpected side effects (such as rendering a PUT request non-idempotent or creating multiple resources not named in the original request). c C sl t |d|�� �}|dkr |dksB|dkr0|dksBt|j||||��t|�dd�|j|jd|jd �}||_ |S ) a� Apply redirect logic to a request. See parent HTTPRedirectHandler.redirect_request for parameter info. If the redirect is disallowed, this raises the corresponding HTTP error. If the redirect can't be determined, return None to allow other handlers to try. If the redirect is allowed, return the new request. This method specialized for the case when (a) the user knows that the redirect will not cause unacceptable side effects for any request method, and (b) the user knows that any request data should be passed through to the redirect. If either condition is not met, this should not be used. �method)�- �. �/ i3 )�GET�HEAD)r- r. r/ )�POST�PUT� z%20T)�headers�origin_req_host�unverifiable�data) �getattr� get_methodr �full_urlr �replacer5 r6 r8 r, ) �self�req�fp�code�msgr5 �newurl�mZnew_request� rD �J/opt/hc_python/lib/python3.8/site-packages/prometheus_client/exposition.py�redirect_requestA s �� �z+_PrometheusRedirectHandler.redirect_requestN)�__name__� __module__�__qualname__�__doc__rF rD rD rD rE r+ + s r+ c C s^ t |�\}}d|kr"| �|d �} || �}d|fg}|sTt|�rTt�|�}|�d� d||fS )zBake output for metrics output.zname[]�Content-Type)zContent-Encoding�gzip�200 OK)�choose_encoderZrestricted_registry� gzip_acceptedrL �compress�append) �registry� accept_header�accept_encoding_header�params�disable_compression�encoder�content_type�outputr5 rD rD rE �_bake_outputb s rZ F)rR rV �returnc s � �fdd�}|S )z;Create a WSGI app which serves the metrics from a registry.c sf | � d�}| � d�}t| � dd��}| d dkr@d}dg}d }nt�|||� �\}}}|||� |gS ) NZHTTP_ACCEPTZHTTP_ACCEPT_ENCODINGZQUERY_STRING� Z PATH_INFOz/favicon.icorM )r\ r\ � )�getr rZ )�environZstart_responserS rT rU �statusr5 rY �rV rR rD rE �prometheus_appt s z%make_wsgi_app.<locals>.prometheus_apprD )rR rV rb rD ra rE r$ q s r$ c @ s e Zd ZdZdd� ZdS )�_SilentHandlerz(WSGI handler that does not log requests.c G s dS �zLog nothing.NrD �r= �format�argsrD rD rE �log_message� s z_SilentHandler.log_messageN)rG rH rI rJ rh rD rD rD rE rc � s rc c @ s e Zd ZdZdZdS )�ThreadingWSGIServerzThread per request HTTP server.TN)rG rH rI rJ �daemon_threadsrD rD rD rE ri � s ri c C s8 t j| |t jt jd�}tt|��\}}}}}||d fS )z8Automatically select address family depending on address)�type�flagsr )�socket�getaddrinfo�SOCK_STREAM� AI_PASSIVE�next�iter)�address�port�infos�family�_ZsockaddrrD rD rE �_get_best_family� s rx )�certfile�keyfile�protocol�cafile�capath�client_auth_requiredr[ c C sD t j|d�}|dk s|dk r|z|�||� W q� tk rx } z.t|�}t|�} |d|�d|�d| � ���W 5 d}~X Y q�X nVz|jt jjd� W n@ tk r� } z"t|�}t|�} |d| � ���W 5 d}~X Y nX |r�t j |_ z|j| |d� W nN tk �r> } z.t|�}t|�} |d | �d |�d| � ���W 5 d}~X Y nX |S )zLoad context supports SSL.�r{ Nz+Cannot load CA certificate chain from file z or directory z: )�purposez*Cannot load default CA certificate chain: �ry rz z$Cannot load server certificate file z or its private key file )�ssl� SSLContext�load_verify_locations�IOErrorrk �str�load_default_certs�Purpose�CLIENT_AUTH� CERT_REQUIRED�verify_mode�load_cert_chain) ry rz r{ r| r} r~ Zssl_cxt�exc�exc_typerA rD rD rE �_get_ssl_ctx� s. . ,r� z0.0.0.0) rt �addrrR ry rz � client_cafile� client_capathr{ r~ r[ c C s� G dd� dt �} t|| �\| _}t|�} t|| | | td�}|rf|rft||||||�}|j|jdd�|_t j |jd�} d| _| � � || fS )z?Starts a WSGI server for prometheus metrics as a daemon thread.c @ s e Zd ZdZdS )z$start_wsgi_server.<locals>.TmpServerz<Copy of ThreadingWSGIServer to update address_family locallyN)rG rH rI rJ rD rD rD rE � TmpServer� s r� )Z handler_classT)�server_side)�target)ri rx �address_familyr$ r rc r� �wrap_socketrm � threading�Thread� serve_forever�daemon�start)rt r� rR ry rz r� r� r{ r~ r� ZappZhttpd�context�trD rD rE r) � s r) �rR r[ c C s� dd� }g }| � � D �]�}z�|j}|j}|dkr:|d }n>|dkrP|d }d}n(|dkr^d}n|d krld }n|dkrxd}|�d �||j�dd��dd��� |�d|� d|� d�� i }|jD ]F}dD ].}|j|j| kr�|�|g ��||�� q�q�|�||�� q�W n: t k �rB } z| j �p&d|f | _ � W 5 d} ~ X Y nX t|�� �D ]T\}} |�d�|j||j�dd��dd��� |�d|j� |� d�� |� | � �qPqd�|��d�S )zHReturns the metrics from the registry in latest text format as a string.c S sx | j r,d�d�dd� t| j �� �D ���}nd}d}| jd k rXdtt| j�d �d��}| j� |� dt | j �� |� d �S ) Nz{{{0}}}�,c S s4 g | ],\}}d � ||�dd��dd��dd���qS )z{}="{}"�\�\\� �\n�"z\")rf r<