Code Monkey home page Code Monkey logo

aiodogstatsd's Introduction

aiodogstatsd

Build Status codecov PyPI PyPI - Downloads GitHub

An asyncio-based client for sending metrics to StatsD with support of DogStatsD extension.

Library fully tested with statsd_exporter and supports gauge, counter, histogram, distribution and timing types.

aiodogstatsd client by default uses 9125 port. It's a default port for statsd_exporter and it's different from 8125 which is used by default in StatsD and DataDog. Initialize the client with the proper port you need if it's different from 9125.

Installation

Just type:

$ pip install aiodogstatsd

At a glance

Just simply use client as a context manager and send any metric you want:

import asyncio

import aiodogstatsd


async def main():
    async with aiodogstatsd.Client() as client:
        client.increment("users.online")


asyncio.run(main())

Please follow documentation or look at examples/ directory to find more examples of library usage, e.g. integration with AIOHTTP or Starlette frameworks.

Contributing

To work on the aiodogstatsd codebase, you'll want to clone the project locally and install the required dependencies via poetry:

$ git clone [email protected]:Gr1N/aiodogstatsd.git
$ make install

To run tests and linters use command below:

$ make lint && make test

If you want to run only tests or linters you can explicitly specify which test environment you want to run, e.g.:

$ make lint-black

License

aiodogstatsd is licensed under the MIT license. See the license file for details.

aiodogstatsd's People

Contributors

eserge avatar gr1n avatar jayh5 avatar reskov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

aiodogstatsd's Issues

Not right behaviour in case of CancelledErorr

Hi,

Description

We are faced with a strange number of the 500 errors in our metrics (which are collected by aiodogstatsd.contrib.aiohttp middleware) and that number was different from what we at ALB logs. The reason for this - is that when the connection is aborted before we processed it, the CancelledError would be raised -- and it will be counted as a 500 error, but this is not true -- user will not receive any response in that case.

Expected behavior

In the case of CancelledError it would be great to have the option to separate it from other errors and have an option to skip it from the metrics

 try:
     response = await handler(request)
     response_status = response.status
 except web.HTTPException as e:
     response_status = e.status
     raise e
except CancelledError: 
      response_status = ... # some code, that will indicate that is `CancelledError`
 finally:
     if _proceed_collecting(..., collect_canceled=True/False):   # allow to mute CancelledError metric
         ...

aiohttp: variable routes are tagged with different paths

If app uses variable route, requests with this urls are tagged with full path, which varies between requests.

For example:

...
app.add_route('/some/resource/{some_id}/child', handler)
...

Requests to this route are send to dogstatsd with full path, i.e.:

http_request_duration{method="GET",path="/some/resource/36d12223-1454-461c-9ed5-6d6777d98e80/child" ,",status="200",quantile="0.5"} 0.007255767000060587

It could be more convenient if such requests are reported with path pattern, i.e.:

http_request_duration{method="GET",path="/some/resource/{some_id}/child" ,",status="200",quantile="0.5"} 0.007255767000060587

Quick research shows that this info can be retrieved from request.match_info.route.resource.canonical, but this looks suspiciously long construction to be reliable. ;)

local variable 'response_status' referenced before assignment

GeneratorExit: null
  File "aiodogstatsd/contrib/aiohttp.py", line 65, in middleware
    response = await handler(request)

UnboundLocalError: local variable 'response_status' referenced before assignment
  File "aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "aiodogstatsd/contrib/aiohttp.py", line 75, in middleware
    request, response_status, collect_not_allowed, collect_not_found

Need to handle BaseException too.

Feature suggestion - timed task

Hi,
In our use case we've implemented a function for timing asyncio tasks - essentially we have a wrapper function for create_task that adds done callback that sends the metric of duration, if exceeds certain threshold.
I wonder if it's something that would be welcome in this library, if so I'll create a PR!

Documentation

Need to add useful documentation, but not only about basic library usage. Good to add documentation of how to setup library with AIOHTTP, Sanic and Starlette. Also it’s good to show examples of statsd_exporter and Prometheus integration, maybe even with PromQL queries.

Full namespaces support

Need to add namespaces support in metric names used in integrations with AIOHTTP, Sanic and Starlette.

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.