bg;`ddlmZddlZddlmZddlZddlZddlZddlZddl m Z m Z m Z ddl mZ ddlmZddlmZ dd lmZmZdd lmZdd lmZmZmZdd lmZdd lmZmZn-#e $r%dd l!mZmZdd l"mZdd l#mZmZmZddl$mZmZmZYnwxYwe%dZ& ej'dkZ(ej'dkZ)dZ*efdZ+Gdde Z,Gddee Z-defdZ.e.Z/efdZ0dZ1GddeZ2dZ3dZ4d)d Z5dd!e4fd"Z6dd!e4fd#Z7dd!e4fd$Z8d%Z9d&Z:d'Z; dd(l.prometheus_app/sb M22 '++nb99::!-h v!N!Nvx(((xr&)rr1s` r$ make_wsgi_appr3,s$ r&ceZdZdZdZdS)_SilentHandlerz(WSGI handler that does not log requests.cdSz Log nothing.Nr2selfformatargss r$ log_messagez_SilentHandler.log_message?r&N)__name__ __module__ __qualname____doc__r<r2r&r$r5r5<s)22r&r5ceZdZdZdZdS)ThreadingWSGIServerzThread per request HTTP server.TN)r>r?r@rAdaemon_threadsr2r&r$rCrCCs))NNNr&rCr+ct|}t|||tt}t j|j}d|_|dS)z?Starts a WSGI server for prometheus metrics as a daemon thread.) handler_class)targetTN) r3rrCr5 threadingThread serve_foreverdaemonstart)portaddrrapphttpdts r$start_wsgi_serverrRKsV  ! !C dC)z8generate_latest..sample_line..]sr===1##qyyu--55dEBBJJ3PUVVXX===r&r+z {0:d}iz{0}{1} {2}{3} ) labelsr:joinsorteditems timestampintfloatnamer value)linelabelstrrfs r$ sample_linez$generate_latest..sample_lineZs ;  ''== &t{'8'8':': ; ;===)>)>??HH H > % E$.,A,AD,H(I(IJJI '' Ix!22Fvv!555"--fb99@@QPPP6 MM++a..111 2   'n5&BIN  $J$4$4$6$677 ! !MFE MM/66v{F7=7K7S7STXZ_7`7`7h7himot7u7uww x x x MM188fMM N N N MM%  ! 776?? ! !' * **sD7E E8 E33E8c|pd}|dD]M}|dddkrtjtjfcSNttfS)Nr+rU;rzapplication/openmetrics-text)splitstrip openmetricsrCONTENT_TYPE_LATEST)raccepteds r$rrs!'RM!'',,55 >>#  q ! ' ' ) )-K K K/35 5 5 5 L / //r&c8eZdZdZeZdZdZedZ dS)MetricsHandlerz2HTTP handler that gives metrics from ``REGISTRY``.c|j}|jd}tt |jj}t|||\}}}|t| dd|j || |j |dS)NAccept r)rheadersr,rrpathqueryr% send_responsergr send_header end_headerswfilewrite)r9rrr r/r0r#s r$do_GETzMetricsHandler.do_GETs= ((22 (49--344!-h v!N!N 3v||C00344555&!!       r&cdSr7r2r8s r$r<zMetricsHandler.log_messager=r&cbt|j}t||tfd|i}|S)zWReturns a dynamic MetricsHandler class tied to the passed registry. r)rr>r}object)clsrcls_nameMyMetricsHandlers r$factoryzMetricsHandler.factorys9s|$$3-!+X 688r&N) r>r?r@rAr rrr< classmethodrr2r&r$rrsX<<H ! ! !   [    r&rc&|dtjdtjj}t |d5}|t|dddn #1swxYwYtj||dS)zWrite 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..wbN) osgetpidrHcurrent_threadidentopenrrrename)rrtmppathfs r$write_to_textfilers !DD")++++y/G/I/I/O/OPG gt  + ))***+++++++++++++++Igts#A11A58A5c"fd}|S)zDefault handler that implements HTTP/HTTPS connections. Used by the push_to_gateway functions. Can be re-used by other handlers.c<t}fd|_D]\}}|||tt|}|jdkr-td|j|j dS)N)datacS)Nr2)methodsr$z1default_handler..handle..sVr&)timeoutiz%error talking to pushgateway: {0} {1}) r get_method add_headerrrrcodeIOErrorr:msg) requestr_r`resprrrrurls r$handlezdefault_handler..handles#D)))+^^^ % %DAq   q! $ $ $ $K((--gw-GG 9  AHH 48%%&& &  r&r2)rrrrrrs````` r$default_handlerrs; &&&&&&&&& Mr&c*fd}|S)zHandler 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 [Yd d}tj|}d|z}d|gt dS)z1Handler that implements HTTP Basic Auth. Nz{0}:{1}r{sBasic Authorization)r:rbase64 b64encoder~r) auth_value auth_token auth_headerrrrpasswordrrusernames r$rz"basic_auth_handler..handles  H$8"))(H==DDWMMJ)*55J#j0K NNO[9 : : :<VWgt<<>>>>>r&r2)rrrrrrrrs``````` r$basic_auth_handlerrsG ??????????? Mr&c 0td||||||dS)aNPush 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.PUTN _use_gatewaygatewayjobr grouping_keyrhandlers r$push_to_gatewayrs%Rh gwOOOOOr&c 0td||||||dS)a"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.POSTNrrs r$pushadd_to_gatewayr s$.#xwPPPPPr&c 0td||d|||dS)aDelete 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.DELETENr)rrrrrs r$delete_from_gatewayr:s$*7C|WgNNNNNr&ct|}|jrtstr|jdvrd|}dj|gt d|R}d} |dkrt |} |i}|ddt| Dz }||||d tfg| dS) N)httphttpsz http://{0}z{0}/metrics/{1}/{2}rr&rr+c 3K|]9\}}djtt|t|V:dS)z/{0}/{1}N)r:_escape_grouping_keyrr]s r$ z_use_gateway..bs[22 Aq  /AA??@222222r&r)rrrrr) rschemePYTHON376_OR_NEWERPYTHON26_OR_OLDERr:rrrcrdrer) rrrrrrr gateway_urlrrs r$rrRs*7##K  / /#4/"*;;;%%g.. &  &w R1EeS1Q1Q R R RC D x(( 27722<--//00222 2 22CGG  "567dr&c|dkr|dzdfSd|vr?|dztj|ddfS|t |fS)Nr+z@base64=/r{)rurlsafe_b64encoderdecoder)r_r`s r$rrlsiBww9}c!! 9}f6qxx7H7HIIPPQXYYYY*Q--r&cttjtjtj5}|dd|dicdddS#1swxYwYdS)z>Grouping key with instance set to the IP Address of this host.) localhostrinstancerN)rsocketAF_INET SOCK_DGRAMconnect getsockname)rs r$instance_ip_grouping_keyrws v~v/@AA B B0a "###AMMOOA./000000000000000000s1A55A9<A9) make_asgi_app)NN)> __future__rr contextlibrrrsysrHwsgiref.simple_serverrrrrr rr utilsr BaseHTTPServerr r SocketServerrurllib2rrrurllibrrr ImportError http.server socketserverurllib.request urllib.parserr version_inforrr%r3r5rCrRstart_http_serverrrrrrrrrrrrrasgirr2r&r$rs''''''  MMMMMMMMMM222222"""""" <AAAAAAAA++++++::::::::::!!!!!!+++++++++<<<>>>>>>>>++++++AAAAAAAAAA;;;;;;;;;;;; <cDEE,$v-% 1FFF$     '.*"$h&&<+<+<+<+~000! ! ! ! ! +! ! ! H   $(.22)P)P)P)PZ.22QQQQ6$(_OOOO04   000 ######## DDs&A%%'BB D((D-