No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

39 líneas
1.3 KiB

  1. # Copyright 2015-2019 Prometheus Python Client Developers
  2. # Copyright 2019 Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from prometheus_client import REGISTRY, CollectorRegistry, generate_latest
  16. from twisted.web.resource import Resource
  17. from twisted.web.server import Request
  18. CONTENT_TYPE_LATEST = "text/plain; version=0.0.4; charset=utf-8"
  19. class MetricsResource(Resource):
  20. """
  21. Twisted ``Resource`` that serves prometheus metrics.
  22. """
  23. isLeaf = True
  24. def __init__(self, registry: CollectorRegistry = REGISTRY):
  25. self.registry = registry
  26. def render_GET(self, request: Request) -> bytes:
  27. request.setHeader(b"Content-Type", CONTENT_TYPE_LATEST.encode("ascii"))
  28. response = generate_latest(self.registry)
  29. request.setHeader(b"Content-Length", str(len(response)))
  30. return response