g@fddlZddlmZddlZddlmZddlZddlZddlm Z ddl Z ddl Z ddl Z ddl mZmZmZmZmZmZmZmZddlmZddlmZmZmZddlmZmZmZm Z m!Z!m"Z"dd l#m$Z$m%Z%m&Z&d d l'm(Z'd d l)m*Z*m+Z+d d l,m-Z-dZ.dZ/ Gdde Z0dZ1e+dfde*de2defdZ3Gdde%Z4Gdde e&Z5dZ6 dMde7de7de8d ee7d!ee7d"e2de jrfd#Z:d$e+dddde jvdfd%e8d&e7de*dee7dee7d'ee7d(ee7de8d"e2dee&e jxffd)Z=e=Z>e+fde*de?fd*Z@d+e7deee*ge?fe7ffd,ZAd-e7de2fd.ZBGd/d0eZCd1e7de*ddfd2ZDd3e7d4e7d5eeEd6eee7e7fd7e?d8eeeFfdegdffd9ZGd3e7d4e7d5eeEd6eee7e7fd7e?degdff d:ZHd3e7d4e7d5eeEd6eee7e7fd7e?degdff d;ZI dNd3e7d4e7d5eeEd6eee7e7fd7e?dZJde jdfd3e7d4e7d5eeEd6eee7e7fd7e?de7de7d ee7de8d?e2degdffd@ZLddAeHfdBe7dCe7de*dDeee7efd5eeEdEeddfdFZMddAeHfdBe7dCe7dee*dDeee7efd5eeEdEeddfdGZNddAeHfdBe7dCe7dDeee7efd5eeEdEeddf dHZOd4e7dBe7dCe7dee*dDeee7efd5eeEdEeddfdIZPdJZQdee7effdKZRd dLlSmTZTy)ON)closing)BaseHTTPRequestHandler)ThreadingMixIn)AnyCallableDictListOptionalSequenceTupleUnion) HTTPError)parse_qs quote_plusurlparse) BaseHandler build_opener HTTPHandlerHTTPRedirectHandler HTTPSHandlerRequest) make_serverWSGIRequestHandler WSGIServer) exposition)CollectorRegistryREGISTRY)floatToGoString) CONTENT_TYPE_LATESTdelete_from_gatewaygenerate_latestinstance_ip_grouping_key make_asgi_app make_wsgi_appMetricsHandlerpush_to_gatewaypushadd_to_gatewaystart_http_serverstart_wsgi_serverwrite_to_textfilez(text/plain; version=0.0.4; charset=utf-8ceZdZdZdZy)_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). ct|d|j}|dvr|dvs!|dvr|dvst|j||||t |j dd|j |jd|j }||_ |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)GETHEAD)r0r1r2)POSTPUT z%20T)headersorigin_req_host unverifiabledata) getattr get_methodrfull_urlrreplacer8r9r;r/) selfreqfpcodemsgr8newurlm new_requests K/opt/hc_python/lib/python3.12/site-packages/prometheus_client/exposition.pyredirect_requestz+_PrometheusRedirectHandler.redirect_requestAs$ C3>>#3 4,,o1E?*qO/CCLL$WbA A NN3 &KK//   N)__name__ __module__ __qualname____doc__rIrJrHr-r-+s *rJr-ct|\}}d|vr|j|d}||}d|fg}|s1t|r&tj|}|j dd||fS)zBake output for metrics output.zname[] Content-Type)zContent-Encodinggzip200 OK)choose_encoderrestricted_registry gzip_acceptedrRcompressappend) registry accept_headeraccept_encoding_headerparamsdisable_compressionencoder content_typeoutputr8s rH _bake_outputrabsz+=9G\6//x0@A X F -.G =1G#Hv&34 Wf $$rJFrYr]returncfd}|S)z;Create a WSGI app which serves the metrics from a registry.cV|jd}|jd}t|jdd}|d}|dk(rd}dg}d }nN|d k7r&d }dg}d j||j}n#|d dk(rd}dg}d }nt ||| \}}}||||gS)N HTTP_ACCEPTHTTP_ACCEPT_ENCODING QUERY_STRINGREQUEST_METHODOPTIONSrS)Allowz OPTIONS,GETrJr3z405 Method Not Allowedz"# HTTP {}: {}; use OPTIONS or GET PATH_INFOz /favicon.ico)rhrh)getrformatencodera) environstart_responserZr[r\r/statusr8r`r]rYs rHprometheus_appz%make_wsgi_app..prometheus_appts M2 !(-C!D'++nb9:)* Y F/0GF u_-F/0G:AA&&QXXZF [ !^ 3FjGF '38]Lbdjl'A #FGVvw'xrJrO)rYr]rss`` rHr%r%qs: rJceZdZdZdZy)_SilentHandlerz(WSGI handler that does not log requests.cyz Log nothing.NrOr@rnargss rH log_messagez_SilentHandler.log_messagerJN)rKrLrMrNrzrOrJrHrurus 2rJruceZdZdZdZy)ThreadingWSGIServerzThread per request HTTP server.TN)rKrLrMrNdaemon_threadsrOrJrHr}r}s )NrJr}ctj||tjtj}t t |\}}}}}||dfS)z8Automatically select address family depending on address)typeflagsr)socket getaddrinfo SOCK_STREAM AI_PASSIVEnextiter)addressportinfosfamily_sockaddrs rH_get_best_familyrsN   w63E3EVM^M^ _E $T%[ 1FAq!X 8A; rJcertfilekeyfileprotocolcafilecapathclient_auth_requiredc Rtj|}|| |j||n+ |j tjj|rtj|_ |j|||S#t$r,}t |}t |} |d|d|d| d}~wwxYw#t$r&}t |}t |} |d| d}~wwxYw#t$r,}t |}t |} |d |d |d| d}~wwxYw) zLoad context supports SSL.rNz+Cannot load CA certificate chain from file z or directory z: )purposez*Cannot load default CA certificate chain: rrz$Cannot load server certificate file z or its private key file ) ssl SSLContextload_verify_locationsIOErrorrstrload_default_certsPurpose CLIENT_AUTH CERT_REQUIRED verify_modeload_cert_chain) rrrrrrssl_cxtexcexc_typerDs rH _get_ssl_ctxrs`nnh/G V/ I  ) )&& 9 O  & &s{{/F/F & G !//C7C N1 ICyHc(CH$Z~fZr#HI I I OCyHc(CGuMN N O C9#h=h\J//6kC5BC CCsFB*B?2C1 B<'B77B<? C.!C))C.1 D&:'D!!D&z0.0.0.0raddr client_cafile client_capathc hGddt} t||\| _}t|} t ||| | t } |r4|r2t ||||||} | j| jd| _tj| j} d| _ | j| | fS)z?Starts a WSGI server for prometheus metrics as a daemon thread.ceZdZdZy)$start_wsgi_server..TmpServerz.sample_lines ;; ''!'t{{'8'8': ;= ;1!!qyyu-55dEBJJ3PUVX ;=)>?H H >> %Cdnn 5 <=a@AI))XJa (C'DYKrRR=s A Dcounter_totalinfo_infogaugestatesetgaugehistogram histogramunknownuntypedz # HELP {} {} rrrrz# TYPE r7)_created_gsum_gcount)rhNz# HELP {}{} {} z gauge rhutf-8)collectrrrXrn documentationr?samples setdefault Exceptionryrrextendrro) rYrr`metricmnamemtype om_samplesssuffix exceptionliness rHr"r"s' SF""$! KKEKKE !(&*$**$)#! MM*11v++33D%@HHuUW X MMGE7!E7"5 6/1J^^>Fvvv!55"--fb9@@QP ? MM+a.1$$J$4$4$67MFE MM,33FKK4:4H4H4P4PQUW\4]4e4efjlq4rt u MMGFKK=A B MM% 8I%R 776? ! !' ** 'nn5&BIN  sCH8AH H0H++H0rZc|xsd}|jdD]I}|jddjdk(s)tjtjfcSttfS)Nrhr;rzapplication/openmetrics-text)splitstrip openmetricsr"r )rZaccepteds rHrTrT5sk!'RM!'', >># q ! ' ' )-K K//335 5- / //rJr[c|xsd}|jdD]7}|jddjjdk(s7yy)NrhrrrrRTF)rrlower)r[rs rHrVrV>sR39r*005 >># q ! ' ' ) / / 1V ;6 rJcVeZdZUdZeZeed<d dZde de ddfdZ e dede fd Zy) r&z2HTTP handler that gives metrics from ``REGISTRY``.rYrbNc|j}|jjd}|jjd}tt |j j }t||||d\}}}|jt|jdd|D]}|j||j|jj|y)NAcceptzAccept-EncodingFr7r)rYr8rmrrpathqueryra send_responserr send_header end_headerswfilewrite) r@rYrZr[r\rrr8r`headers rHdo_GETzMetricsHandler.do_GETJs== ((2 !%!1!12C!D(499-334".xH^`fhm"n 3v||C0345F D  f %   rJrnrycyrwrOrxs rHrzzMetricsHandler.log_messageYr{rJcZt|j}t||tfd|i}|S)zWReturns a dynamic MetricsHandler class tied to the passed registry. rY)rrKrobject)clsrYcls_nameMyMetricsHandlers rHfactoryzMetricsHandler.factory\s3s||$3-!+X 68rJrbN)rKrLrMrNrrYr__annotations__rrrrz classmethodrr rOrJrHr&r&FsR<"*H* !#cd  0  T    rJr&rcj|dtjdtjj}t |d5}|j t|dddtjdk(rtj||ytj||y#1swYJxYw)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..wbNnt) osgetpidrcurrent_threadidentopenrr"rr?rename)rrYtmppathfs rHr+r+ls a }Ai&>&>&@&F&F%GHG gt  )*  ww$ 7D! '4   s B))B2urlr/timeoutr8r; base_handlerc(dfd }|S)Nc t }fd|_D]\}}|j||tj |}|j dk\r%t d|j d|jy)N)r;cS)NrO)r/srHz/_make_handler..handle..sVrJ)rizerror talking to pushgateway: r7)rr= add_headerrrrCOSErrorrD) requestrrresprr;r8r/rrs rHhandlez_make_handler..handles#D)+DAq   q! $L)..w.H 99 :499+QtxxjQR R rJr rO)rr/rr8r;rr%s`````` rH _make_handlerr&|sSS MrJc*t|||||tS)zDefault handler that implements HTTP/HTTPS connections. Used by the push_to_gateway functions. Can be re-used by other handlers.)r&rrr/rr8r;s rHdefault_handlerr)s fgwk JJrJc*t|||||tS)a Handler that automatically trusts redirect responses for all HTTP methods. Augments standard HTTPRedirectHandler capability by permitting PUT requests, preserving the method upon redirect, and passing through all headers and data from the original request. Only use this handler if you control or trust the source of redirect responses you encounter when making requests via the Prometheus client. This handler will simply repeat the identical request, including same method and data, to the new redirect URL.)r&r-r(s rHpassthrough_redirect_handlerr+s" fgw>X YYrJusernamepasswordc*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 DB dj}tj|}d|z}jd|ft y)z1Handler that implements HTTP Basic Auth. N:sBasic Authorization)robase64 b64encoderXr)) auth_value auth_token auth_headerr;r8r/r-rrr,s rHr%z"basic_auth_handler..handlesi  H$8$:Qxj188:J))*5J#j0K NNO[9 :<VWgt<>rJrO)rr/rr8r;r,r-r%s``````` rHbasic_auth_handlerr7s?? MrJinsecure_skip_verifyc tj|} || j|n| j| rd| _tj | _| j||t| } t|||||| S)aLHandler that implements an HTTPS connection with TLS Auth. The default protocol (ssl.PROTOCOL_TLS_CLIENT) will also enable ssl.CERT_REQUIRED and SSLContext.check_hostname by default. This can be disabled by setting insecure_skip_verify to True. Both this handler and the TLS feature on pushgateay are experimental.rFr)r) rrrrcheck_hostname CERT_NONErrrr&) rr/rr8r;rrrrr8rhandlers rHtls_auth_handlerr=s{&nnh/G %%f-""$!&!mm Xw?7+G fgwg FFrJgatewayjob grouping_keyr<c &td||||||y)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.r6N _use_gatewayr?r@rYrArr<s rHr'r's\h gwOrJc &td||||||y)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.r5NrCrEs rHr(r( s8#xwPrJc &td||d|||y)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.DELETENrC)r?r@rArr<s rHr!r!?s47C|WgNrJct|}|jr|jdvrd|}|jd}dj|gt d|}d} |dk7r|t }t |} |i}|djd t|jDz }||||d tfg| y) N)httphttpszhttp:///z{}/metrics/{}/{}r@rJrHrhc 3xK|]2\}}djtt|t|4yw)z/{}/{}N)rn_escape_grouping_keyr).0rrs rH z_use_gateway..us720DAq -c!fc!f=>0s8:rQr() rschemerstriprnrNrr"rrrr ) r/r?r@rYrArr< gateway_urlrr;s rHrDrD\s7#K   !3!3;L!LG9%nnS!G #  # #G O.B5#.N OC D   Hx( 2772<--/02 22CG  "567drJc|dk(r|dzdfSd|vr8|dztj|jdjdfS|t |fS)Nrhz@base64=rLr)r2urlsafe_b64encoderodecoder)rrs rHrNrNs]Bw9}c!! 9}f66qxx7HIPPQXYYY*Q-rJc:ttjtjtj5}tj dk(r|j dn|j dd|jdicdddS#1swYyxYw)z>Grouping key with instance set to the IP Address of this host.darwin)z10.255.255.255r) localhostrinstancerN)rrAF_INET SOCK_DGRAMsysplatformconnect getsockname)rs rHr#r#si v~~v/@/@A Ba <<8 # II+ , II& 'AMMOA./ C B Bs A BB)r$)NNF)NN)Ur2 contextlibrrR http.serverrrr socketserverrrr^rtypingrrrr r r r r urllib.errorr urllib.parserrrurllib.requestrrrrrrwsgiref.simple_serverrrrrrrYrrutilsr__all__r r-raboolr%rur}rrrrrPROTOCOL_TLS_SERVERrr*r)bytesr"rTrVr&r+rrr&r)r+r7PROTOCOL_TLS_CLIENTr=r'r(r!rDrNr#asgir$rOrJrHrqs]  . ' NNN"77NM21" A,4!44n %19V[ - t `h F'.*!% $%* &&&& &  & # & ^^&V&."&!%'+'+//%*$3-  #   }  }# :y'' '(:&3;;+/;+u;+|0#0%:K9Le9S0TVY0Y*Z0#$# +# L !C !+< ! !  %%S/*    K-. b$h( K  K K% KeCHo& K  K b$h KZ ZZ%ZeCHo& Z  Z b$h Z4#'"& %eCHo&    3- 3-b$hF!%//%*G GG%GeCHo& G  G  GG GG#Gb$hGL26#%+ .P.P .P$.PtCH~. .P % .P  .P .Pj26#%+ QQ Q,-QtCH~. Q % Q  Q QD26#%+ OO OtCH~.O% O  O  O:    ,-  tCH~.  %    F  0$sCx. 0  rJ