<commit_before>.. _twisted-api:

Twisted Support
===============

.. currentmodule:: prometheus_async.tx

The Twisted-related APIs can be found within the ``prometheus_async.tx`` package.


Decorator Wrappers
------------------

.. autofunction:: time

   The fact it's accepting ``Deferred``\ s is useful in conjunction with `twisted.web`_ views that don't allow to return a ``Deferred``:

   .. code-block:: python

      from prometheus_client import Histogram
      from prometheus_async.tx import time
      from twisted.internet.task import deferLater
      from twisted.web.resource import Resource
      from twisted.web.server import NOT_DONE_YET
      from twisted.internet import reactor

      REQ_TIME = Histogram("req_time_seconds", "time spent in requests")

      class DelayedResource(Resource):
         def _delayedRender(self, request):
            request.write("<html><body>Sorry to keep you waiting.</body></html>")
            request.finish()

         def render_GET(self, request):
            d = deferLater(reactor, 5, lambda: request)
            time(d.addCallback(self._delayedRender))
            return NOT_DONE_YET

.. autofunction:: count_exceptions


.. _twisted-web:

Metric Exposure
---------------

TODO :(

<commit_msg>Linkify<commit_after>.. _twisted-api:

Twisted Support
===============

.. currentmodule:: prometheus_async.tx

The Twisted-related APIs can be found within the ``prometheus_async.tx`` package.


Decorator Wrappers
------------------

.. autofunction:: time

   The fact it's accepting ``Deferred``\ s is useful in conjunction with `twisted.web`_ views that don't allow to return a ``Deferred``:

   .. code-block:: python

      from prometheus_client import Histogram
      from prometheus_async.tx import time
      from twisted.internet.task import deferLater
      from twisted.web.resource import Resource
      from twisted.web.server import NOT_DONE_YET
      from twisted.internet import reactor

      REQ_TIME = Histogram("req_time_seconds", "time spent in requests")

      class DelayedResource(Resource):
         def _delayedRender(self, request):
            request.write("<html><body>Sorry to keep you waiting.</body></html>")
            request.finish()

         def render_GET(self, request):
            d = deferLater(reactor, 5, lambda: request)
            time(d.addCallback(self._delayedRender))
            return NOT_DONE_YET

.. autofunction:: count_exceptions


.. _twisted-web:

Metric Exposure
---------------

:doc:`TODO :( <future>`


{'fraction_non_alphanumeric': 0.12806324110671938, 'fraction_numerical': 0.0007905138339920949, 'mean_word_length': 2.823262839879154, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5239001', 'n_tokens_mistral': 747, 'n_tokens_neox': 692, 'n_words': 201}