ó
6]c           @  sŒ  d  d l  m Z d  d l Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 m
 Z
 m Z d d l m Z d d l m Z d d l m Z yl d  d	 l m Z m Z d  d
 l m Z d  d l m Z m Z m Z d  d l m Z d  d l m Z m Z Wno e k
 rd  d	 l  m Z m Z d  d
 l! m Z d  d l" m Z m Z m Z d  d l# m Z m Z m Z n Xe$ d ƒ Z% e j& d' k  Z' e d „ Z( d e f d „  ƒ  YZ) d e d „ Z* e d „ Z+ d „  Z, d e f d „  ƒ  YZ- d e e f d „  ƒ  YZ. d e d „ Z/ d „  Z0 d „  Z1 d d d  „ Z3 d d! e1 d" „ Z4 d d! e1 d# „ Z5 d d! e1 d$ „ Z6 d% „  Z7 d& „  Z8 d S((   iÿÿÿÿ(   t   unicode_literalsN(   t   closing(   t   make_servert   WSGIRequestHandleri   (   t
   exposition(   t   REGISTRY(   t   floatToGoString(   t   BaseHTTPRequestHandlert
   HTTPServer(   t   ThreadingMixIn(   t   build_openert   Requestt   HTTPHandler(   t
   quote_plus(   t   parse_qst   urlparse(   R   R   R   u(   text/plain; version=0.0.4; charset=utf-8i   i   c           s   ‡  f d †  } | S(   u;   Create a WSGI app which serves the metrics from a registry.c   	        sœ   t  |  j d d ƒ ƒ } ˆ  } t |  j d ƒ ƒ \ } } d | k r[ | j | d ƒ } n  | | ƒ } t d ƒ } t d ƒ | f g } | | | ƒ | g S(   Nu   QUERY_STRINGu    u   HTTP_ACCEPTu   name[]u   200 OKu   Content-type(   R   t   gett   choose_encodert   restricted_registryt   str(	   t   environt   start_responset   paramst   rt   encodert   content_typet   outputt   statust   headers(   t   registry(    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   prometheus_app'   s    (    (   R   R   (    (   R   s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   make_wsgi_app$   s    t   _SilentHandlerc           B  s   e  Z d  Z d „  Z RS(   u(   WSGI handler that does not log requests.c         G  s   d S(   u   Log nothing.N(    (   t   selft   formatt   args(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   log_message:   t    (   t   __name__t
   __module__t   __doc__R$   (    (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyR    7   s   u    c         C  sP   t  | ƒ } t | |  | d t ƒ} t j d | j ƒ } t | _ | j ƒ  d S(   u?   Starts a WSGI server for prometheus metrics as a daemon thread.t   handler_classt   targetN(	   R   R   R    t	   threadingt   Threadt   serve_forevert   Truet   daemont   start(   t   portt   addrR   t   appt   httpdt   t(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   start_wsgi_server>   s
    	c         C  s  d „  } g  } xì|  j  ƒ  D]Þ} yZ| j } | j } | d k rP | d } n^ | d k ro | d } d } n? | d k r„ d } n* | d k r™ d	 } n | d
 k r® d } n  | j d j | | j j d d ƒ j d d ƒ ƒ ƒ | j d j | | ƒ ƒ i  } xw | j D]l } xc d d d g D]? } | j | j | k r!| j | g  ƒ j | | ƒ ƒ Pq!q!W| j | | ƒ ƒ qWWn/ t	 k
 r­}	 |	 j
 pšd | f |	 _
 ‚  n XxI t | j ƒ  ƒ D]5 \ } }
 | j d j | j | ƒ ƒ | j |
 ƒ qÁWq Wd j | ƒ j d ƒ S(   uH   Returns the metrics from the registry in latest text format as a string.c      	   S  sÞ   |  j  r| d j d j g  t |  j  j ƒ  ƒ D]B \ } } d j | | j d d ƒ j d d ƒ j d d	 ƒ ƒ ^ q+ ƒ ƒ } n d
 } d
 } |  j d  k	 r¼ d j t t	 |  j ƒ d ƒ ƒ } n  d j |  j
 | t |  j ƒ | ƒ S(   Nu   {{{0}}}u   ,u	   {0}="{1}"u   \u   \\u   
u   \nu   "u   \"u    u    {0:d}iè  u   {0}{1} {2}{3}
(   t   labelsR"   t   joint   sortedt   itemst   replacet	   timestampt   Nonet   intt   floatt   nameR   t   value(   t   linet   kt   vt   labelstrR<   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   sample_lineJ   s    	d%u   counteru   _totalu   infou   _infou   gaugeu   statesetu   gaugehistogramu	   histogramu   unknownu   untypedu   # HELP {0} {1}
u   \u   \\u   
u   \nu   # TYPE {0} {1}
u   _createdu   _gsumu   _gcountu    u   # TYPE {0}{1} gauge
u   utf-8(   u    (   t   collectR@   t   typet   appendR"   t   documentationR;   t   samplest
   setdefaultt	   ExceptionR#   R9   R:   t   extendR8   t   encode(   R   RF   R   t   metrict   mnamet   mtypet
   om_samplest   st   suffixt	   exceptiont   lines(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   generate_latestG   sD    			
				(c         C  s_   |  p	 d }  xF |  j  d ƒ D]5 } | j  d ƒ d j ƒ  d k r t j t j f Sq Wt t f S(   Nu    u   ,u   ;i    u   application/openmetrics-text(   t   splitt   stript   openmetricsRX   t   CONTENT_TYPE_LATEST(   t   accept_headert   accepted(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyR   „   s    t   MetricsHandlerc           B  s5   e  Z d  Z e Z d „  Z d „  Z e d „  ƒ Z RS(   u2   HTTP handler that gives metrics from ``REGISTRY``.c         C  sÉ   |  j  } t t |  j ƒ j ƒ } t |  j j d ƒ ƒ \ } } d | k ra | j | d ƒ } n  y | | ƒ } Wn |  j	 d d ƒ ‚  n X|  j
 d ƒ |  j d | ƒ |  j ƒ  |  j j | ƒ d  S(   Nu   Acceptu   name[]iô  u   error generating metric outputiÈ   u   Content-Type(   R   R   R   t   patht   queryR   R   R   R   t
   send_errort   send_responset   send_headert   end_headerst   wfilet   write(   R!   R   R   R   R   R   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   do_GET‘   s    	
c         G  s   d S(   u   Log nothing.N(    (   R!   R"   R#   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyR$   ¡   R%   c         C  s2   t  |  j ƒ } t | |  t f i | d 6ƒ } | S(   uW   Returns a dynamic MetricsHandler class tied
           to the passed registry.
        u   registry(   R   R&   RH   t   object(   t   clsR   t   cls_namet   MyMetricsHandler(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   factory¤   s    
(	   R&   R'   R(   R   R   Rh   R$   t   classmethodRm   (    (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyR_      s
   		t   _ThreadingSimpleServerc           B  s   e  Z d  Z e Z RS(   u   Thread per request HTTP server.(   R&   R'   R(   R.   t   daemon_threads(    (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyRo   ´   s   c         C  sP   t  j | ƒ } t | |  f | ƒ } t j d | j ƒ } t | _ | j ƒ  d S(   u?   Starts an HTTP server for prometheus metrics as a daemon threadR*   N(	   R_   Rm   Ro   R+   R,   R-   R.   R/   R0   (   R1   R2   R   t   CustomMetricsHandlerR4   R5   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   start_http_server¾   s
    	c         C  sa   d |  t  j ƒ  t j ƒ  j f } t | d ƒ  } | j t | ƒ ƒ Wd QXt  j | |  ƒ d S(   u²   Write metrics to the given path.

    This is intended for use with the Node exporter textfile collector.
    The path must end in .prom for the textfile collector to process it.u   %s.%s.%su   wbN(	   t   ost   getpidR+   t   current_threadt   identt   openRg   RX   t   rename(   R`   R   t   tmppatht   f(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   write_to_textfileÇ   s    "c           s   ‡  ‡ ‡ ‡ ‡ f d †  } | S(   u…   Default handler that implements HTTP/HTTPS connections.

    Used by the push_to_gateway functions. Can be re-used by other handlers.c            sš   t  ˆ d ˆ  ƒ}  ‡ f d †  |  _ x$ ˆ D] \ } } |  j | | ƒ q+ Wt t ƒ j |  d ˆ ƒ} | j d k r– t d j | j | j	 ƒ ƒ ‚ n  d  S(   Nt   datac             s   ˆ  S(   N(    (    (   t   method(    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   <lambda>Ú   R%   t   timeouti  u%   error talking to pushgateway: {0} {1}(
   R   t
   get_methodt
   add_headerR
   R   Rw   t   codet   IOErrorR"   t   msg(   t   requestRC   RD   t   resp(   R|   R   R}   R   t   url(    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   handleØ   s    	(    (   R‡   R}   R   R   R|   Rˆ   (    (   R|   R   R}   R   R‡   s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   default_handlerÓ   s    
c           s%   ‡  ‡ ‡ ‡ ‡ ‡ ‡ f d †  } | S(   uÕ   Handler that implements HTTP/HTTPS connections with Basic Auth.

    Sets auth headers using supplied 'username' and 'password', if set.
    Used by the push_to_gateway functions. Can be re-used by other handlers.c            s   ˆ d k	 rb ˆ d k	 rb d j ˆ ˆ ƒ j d ƒ }  t j |  ƒ } d | } ˆ j d | g ƒ n  t ˆ ˆ ˆ ˆ ˆ  ƒ ƒ  d S(   u1   Handler that implements HTTP Basic Auth.
        u   {0}:{1}u   utf-8s   Basic u   AuthorizationN(   R=   R"   RO   t   base64t	   b64encodeRI   R‰   (   t
   auth_valuet
   auth_tokent   auth_header(   R|   R   R}   t   passwordR   R‡   t   username(    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyRˆ   ë   s    
(    (   R‡   R}   R   R   R|   R   R   Rˆ   (    (   R|   R   R}   R   R   R‡   R   s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   basic_auth_handlerå   s    !
i   c         C  s    t  d |  | | | | | ƒ d S(   uN  Push metrics to the given pushgateway.

    `gateway` the url for your push gateway. Either of the form
              'http://pushgateway.local', or 'pushgateway.local'.
              Scheme defaults to 'http' if none is provided
    `job` is the job label to be attached to all pushed metrics
    `registry` is an instance of CollectorRegistry
    `grouping_key` please see the pushgateway documentation for details.
                   Defaults to None
    `timeout` is how long push will attempt to connect before giving up.
              Defaults to 30s, can be set to None for no timeout.
    `handler` is an optional function which can be provided to perform
              requests to the 'gateway'.
              Defaults to None, in which case an http or https request
              will be carried out by a default handler.
              If not None, the argument must be a function which accepts
              the following arguments:
              url, method, timeout, headers, and content
              May be used to implement additional functionality not
              supported by the built-in default handler (such as SSL
              client certicates, and HTTP authentication mechanisms).
              'url' is the URL for the request, the 'gateway' argument
              described earlier will form the basis of this URL.
              'method' is the HTTP method which should be used when
              carrying out the request.
              'timeout' requests not successfully completed after this
              many seconds should be aborted.  If timeout is None, then
              the handler should not set a timeout.
              'headers' is a list of ("header-name","header-value") tuples
              which must be passed to the pushgateway in the form of HTTP
              request headers.
              The function should raise an exception (e.g. IOError) on
              failure.
              'content' is the data which should be used to form the HTTP
              Message Body.

    This overwrites all metrics with the same job and grouping_key.
    This uses the PUT HTTP method.u   PUTN(   t   _use_gateway(   t   gatewayt   jobR   t   grouping_keyR   t   handler(    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   push_to_gatewayø   s    )c         C  s    t  d |  | | | | | ƒ d S(   u"  PushAdd metrics to the given pushgateway.

    `gateway` the url for your push gateway. Either of the form
              'http://pushgateway.local', or 'pushgateway.local'.
              Scheme defaults to 'http' if none is provided
    `job` is the job label to be attached to all pushed metrics
    `registry` is an instance of CollectorRegistry
    `grouping_key` please see the pushgateway documentation for details.
                   Defaults to None
    `timeout` is how long push will attempt to connect before giving up.
              Defaults to 30s, can be set to None for no timeout.
    `handler` is an optional function which can be provided to perform
              requests to the 'gateway'.
              Defaults to None, in which case an http or https request
              will be carried out by a default handler.
              See the 'prometheus_client.push_to_gateway' documentation
              for implementation requirements.

    This replaces metrics with the same name, job and grouping_key.
    This uses the POST HTTP method.u   POSTN(   R’   (   R“   R”   R   R•   R   R–   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   pushadd_to_gateway$  s    c         C  s    t  d |  | d | | | ƒ d S(   uî  Delete metrics from the given pushgateway.

    `gateway` the url for your push gateway. Either of the form
              'http://pushgateway.local', or 'pushgateway.local'.
              Scheme defaults to 'http' if none is provided
    `job` is the job label to be attached to all pushed metrics
    `grouping_key` please see the pushgateway documentation for details.
                   Defaults to None
    `timeout` is how long delete will attempt to connect before giving up.
              Defaults to 30s, can be set to None for no timeout.
    `handler` is an optional function which can be provided to perform
              requests to the 'gateway'.
              Defaults to None, in which case an http or https request
              will be carried out by a default handler.
              See the 'prometheus_client.push_to_gateway' documentation
              for implementation requirements.

    This deletes metrics with the given job and grouping_key.
    This uses the DELETE HTTP method.u   DELETEN(   R’   R=   (   R“   R”   R•   R   R–   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   delete_from_gateway>  s    c   
      C  sé   t  | ƒ } | j s+ t r= | j d k r= d j | ƒ } n  d j | t | ƒ ƒ } d }	 |  d k rv t | ƒ }	 n  | d  k r‹ i  } n  | d j d „  t | j	 ƒ  ƒ Dƒ ƒ 7} | d	 | d
 |  d | d d t
 f g d |	 ƒ ƒ  d  S(   Nu   httpu   httpsu
   http://{0}u   {0}/metrics/job/{1}R%   u   DELETEu    c         s  s?   |  ]5 \ } } d  j  t t | ƒ ƒ t t | ƒ ƒ ƒ Vq d S(   u   /{0}/{1}N(   R"   R   R   (   t   .0RC   RD   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pys	   <genexpr>c  s   R‡   R}   R   R   u   Content-TypeR|   (   u   httpu   https(   R   t   schemet   PYTHON26_OR_OLDERR"   R   RX   R=   R8   R9   R:   R\   (
   R}   R“   R”   R   R•   R   R–   t   gateway_urlR‡   R|   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyR’   V  s    		c          C  sM   t  t j t j t j ƒ ƒ ) }  |  j d ƒ i |  j ƒ  d d 6SWd QXd S(   u>   Grouping key with instance set to the IP Address of this host.u	   localhosti    u   instanceN(   u	   localhosti    (   R   t   sockett   AF_INETt
   SOCK_DGRAMt   connectt   getsockname(   RT   (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   instance_ip_grouping_keyl  s    !(   i   i   (9   t
   __future__R    RŠ   t
   contextlibR   Rs   Rž   t   sysR+   t   wsgiref.simple_serverR   R   R[   R   R   R   t   utilsR   t   BaseHTTPServerR   R   t   SocketServerR	   t   urllib2R
   R   R   t   urllibR   R   R   t   ImportErrort   http.servert   socketservert   urllib.requestt   urllib.parseR   R\   t   version_infoRœ   R   R    R6   RX   R   R_   Ro   Rr   R{   R‰   R=   R‘   R—   R˜   R™   R’   R£   (    (    (    s;   lib/python2.7/site-packages/prometheus_client/exposition.pyt   <module>   sR    	=		'
			+	