Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with Graphite and Grafana #20

Closed
raqibhayder opened this issue Nov 16, 2021 · 1 comment
Closed

Integration with Graphite and Grafana #20

raqibhayder opened this issue Nov 16, 2021 · 1 comment

Comments

@raqibhayder
Copy link

raqibhayder commented Nov 16, 2021

No description provided.

@steinnes
Copy link
Owner

I am not sure why you edited out the contents here, but if you want to use this middleware to send metric data to grafana, I suppose you'd just write a client which implements the very basic TimingClient interface. I am not an expert on using "plain old statsd", but I'd guess it would go something like this (the README example, slightly adapted):

import uvicorn
import statsd

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from timing_asgi import TimingMiddleware, TimingClient
from timing_asgi.integrations import StarletteScopeToName


class StatsdTimings(TimingClient):
    def __init__(self, host: Optional[str] = None, port: Optional[int] = None)
        if host is None:
            host = 'localhost'
        if port is None:
            port = 8125
        self._statsd_client = statsd.StatsdClient(host, port)

    def timing(self, metric_name, timing, tags):
        # plain-old-statsd does not support tags
        self.statsd_client.timing(metric_name, timing)


app = Starlette()


@app.route("/")
def homepage(request):
    return PlainTextResponse("hello world")


app.add_middleware(
    TimingMiddleware,
    client=StatsdTimings(),
    metric_namer=StarletteScopeToName(prefix="myapp", starlette_app=app)
)

if __name__ == "__main__":
    uvicorn.run(app)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants