示例#1
0
def _before_rendered_callback(
        event: pyramid.events.BeforeRender) -> None:  # pragma: nocover
    """Finish the callback called when the rendering is starting."""
    request = event.get("request", None)
    if request:
        measure = stats.timer()
        request.add_finished_callback(_create_finished_cb("render", measure))
示例#2
0
def _request_callback(
        event: pyramid.events.NewRequest) -> None:  # pragma: nocover
    """
    Callback called when a new HTTP request is incoming.
    """
    measure = stats.timer()
    event.request.add_finished_callback(_create_finished_cb("route", measure))
 def _time_iteration(self, generator, func_name):
     while True:
         timer = stats.timer()
         try:
             tile = next(generator)
         except StopIteration:
             break
         timer.stop(self._get_stats_name(func_name, tile))
         yield tile
示例#4
0
 def callback(tile: Tile) -> Tile:
     if tile:
         if hasattr(tile, self.attr):
             timer = getattr(tile, self.attr)
             delta_t = timer.stop()
             if statistics:
                 statistics.add(delta_t)
         else:
             setattr(tile, self.attr, stats.timer([key]))
     return tile
示例#5
0
 def callback(tile):
     if tile:
         if hasattr(tile, self.attr):
             timer = getattr(tile, self.attr)
             delta_t = timer.stop()
             if statistics:
                 statistics.add(delta_t)
         else:
             setattr(tile, self.attr, stats.timer([key]))
     return tile
示例#6
0
def _create_sqlalchemy_timer_cb(what: str) -> Callable[..., Any]:
    if stats.USE_TAGS and what != 'commit':
        key = ['sql']
        tags: Optional[Dict[str, str]] = dict(query=what)
    else:
        key = ['sql', what]
        tags = None
    measure = stats.timer(key, tags)

    def after(*_args: Any, **_kwargs: Any) -> None:
        measure.stop()

    return after
示例#7
0
def _create_sqlalchemy_timer_cb(what: str) -> Callable[..., Any]:
    if stats.USE_TAGS and what != "commit":
        key = ["sql"]
        tags: Optional[Dict[str, str]] = dict(query=what)
    else:
        key = ["sql", what]
        tags = None
    measure = stats.timer(key, tags)

    def after(*_args: Any, **_kwargs: Any) -> None:
        duration = measure.stop()
        LOG.debug("Execute statement '%s' in %d.", what, duration)

    return after
示例#8
0
 def _time_iteration(self, generator, func_name):
     while True:
         timer = stats.timer()
         try:
             tile = next(generator)
         except StopIteration:
             break
         except RuntimeError as exception:
             if isinstance(exception.__cause__, StopIteration):
                 # since python 3.7, a StopIteration is wrapped in a RuntimeError (PEP 479)
                 break
             else:
                 raise
         timer.stop(self._get_stats_name(func_name, tile))
         yield tile
 def _time_iteration(self, generator: Iterable[OPTIONAL_TILE_OR_NOT],
                     func_name: str) -> Iterator[OPTIONAL_TILE_OR_NOT]:
     while True:
         timer = stats.timer()
         try:
             tile = next(generator)  # type: ignore
         except StopIteration:
             break
         except RuntimeError as exception:
             if isinstance(exception.__cause__, StopIteration):
                 # since python 3.7, a StopIteration is wrapped in a RuntimeError (PEP 479)
                 break
             else:
                 raise
         timer.stop(self._get_stats_name(func_name, tile))
         yield tile
示例#10
0
    def send_wrapper(
        self: requests.adapters.HTTPAdapter,
        request: requests.models.PreparedRequest,
        timeout: Optional[float] = None,
        **kwargs: Any,
    ) -> requests.Response:
        pyramid_request = get_current_request()
        header = ID_HEADERS[0]
        if pyramid_request is not None and header not in request.headers:
            request.headers[header] = pyramid_request.c2c_request_id

        if timeout is None:
            if DEFAULT_TIMEOUT is not None:
                timeout = DEFAULT_TIMEOUT
            else:
                LOG.warning("Doing a %s request without timeout to %s",
                            request.method, request.url)

        status = 999
        timer = stats.timer()
        try:
            response = _HTTPAdapter_send(self,
                                         request,
                                         timeout=timeout,
                                         **kwargs)
            status = response.status_code
            return response
        finally:
            if request.url is not None:
                parsed = urllib.parse.urlparse(request.url)
                port = parsed.port or (80 if parsed.scheme == "http" else 443)
                if stats.USE_TAGS:
                    key: Sequence[Any] = ["requests"]
                    tags: Optional[Dict[str, Any]] = dict(
                        scheme=parsed.scheme,
                        host=parsed.hostname,
                        port=port,
                        method=request.method,
                        status=status,
                    )
                else:
                    key = [
                        "requests", parsed.scheme, parsed.hostname, port,
                        request.method, status
                    ]
                    tags = None
                timer.stop(key, tags)
示例#11
0
 def _time_iteration(self, generator, func_name):
     while True:  # will exit when next(generator) raises StopIteration
         timer = stats.timer()
         tile = next(generator)
         timer.stop(self._get_stats_name(func_name, tile))
         yield tile