Coder Social home page Coder Social logo

Comments (2)

nicolatjensen avatar nicolatjensen commented on August 15, 2024

I've been using Prometheus for a while and I only needed to add a service like this:

import re
import sys
import traceback

import aiohttp.web
import asab
import prometheus_client


class Metrics(asab.Service):
    def __init__(self, app: asab.Application, service_name=__name__):
        super().__init__(app, service_name)
        self._charset_regex = re.compile(r'(^|;)(\s*charset\s*=\s*([^;\s]+)\s*;*)', flags=re.IGNORECASE)

    async def initialize(self, app: asab.Application):
        await super().initialize(app)
        web_service = app.get_service('asab.WebService')
        web_service.WebApp.router.add_get('/metrics', self._metrics)

    async def finalize(self, app: asab.Application):
        await super().finalize(app)

    def _metrics(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
        try:
            accept_header = request.headers.get('Accept')
            encoder, content_type = prometheus_client.exposition.choose_encoder(accept_header)
            # separate the charset from the content-type
            matches = self._charset_regex.findall(content_type)
            if matches:
                charset = matches[-1][2]
            else:
                charset = None
            content_type = self._charset_regex.sub(r'\1', content_type)
            return aiohttp.web.Response(
                body=encoder(prometheus_client.REGISTRY), content_type=content_type, charset=charset)
        except Exception:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            error = ''.join(traceback.format_exception_only(exc_type, exc_value)).strip()
            raise aiohttp.web.HTTPInternalServerError(reason=error.replace('\n', ' # '))

from asab.

ateska avatar ateska commented on August 15, 2024

Yes - in order to explore this a bit more, we need to integrate prometheus_client with asab.metrics

from asab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.