def wrapper(*args, **kwargs):
        # compute average
        average = get_average(endpoint)

        stack_info = None

        if average:
            average *= config.outlier_detection_constant

            # start a thread to log the stacktrace after 'average' ms
            stack_info = StackInfo(average)

        time1 = time.time()
        result = func(*args, **kwargs)

        if stack_info:
            stack_info.stop()

        time2 = time.time()
        t = (time2 - time1) * 1000
        add_function_call(time=t, endpoint=endpoint, ip=request.environ['REMOTE_ADDR'])

        # outlier detection
        endpoint_count[endpoint] = endpoint_count.get(endpoint, 0) + 1
        endpoint_sum[endpoint] = endpoint_sum.get(endpoint, 0) + t

        if stack_info:
            add_outlier(endpoint, t, stack_info, request)

        return result
 def add_outlier(self, session, request_id):
     if self._memory:
         add_outlier(
             session,
             request_id,
             self._cpu_percent,
             self._memory,
             self._stacktrace,
             self._request,
         )
 def add_outlier(self, request_id):
     if self._memory:
         with session_scope() as db_session:
             add_outlier(
                 db_session,
                 request_id,
                 self._cpu_percent,
                 self._memory,
                 self._stacktrace,
                 self._request,
             )
Exemple #4
0
def test_add_outlier(session, request_1):
    assert not request_1.outlier

    add_outlier(
        session,
        request_id=request_1.id,
        cpu_percent="cpu_percent",
        memory="memory",
        stacktrace="stacktrace",
        request=("headers", "environ", "url"),
    )
    session.commit()
    assert session.query(Outlier).filter(
        Outlier.request_id == request_1.id).one()
Exemple #5
0
 def test_add_outlier(self):
     """
         Test whether the function returns the right values.
     """
     from flask_monitoringdashboard.database.outlier import Outlier
     with session_scope() as db_session:
         self.assertEqual(len(db_session.query(Outlier).all()),
                          OUTLIER_COUNT)
         request = "headers", "environ", "url"
         add_outlier(db_session,
                     request_id=1,
                     cpu_percent="cpu_percent",
                     memory="memory",
                     stacktrace="stacktrace",
                     request=request)
         self.assertEqual(len(db_session.query(Outlier).all()),
                          OUTLIER_COUNT + 1)
 def stop(self, duration, status_code):
     self._exit.set()
     update_duration_cache(endpoint_name=self._endpoint.name, duration=duration * 1000)
     with session_scope() as session:
         request_id = add_request(
             session,
             duration=duration * 1000,
             endpoint_id=self._endpoint.id,
             ip=self._ip,
             group_by=self._group_by,
             status_code=status_code,
         )
         if self._memory:
             add_outlier(
                 session,
                 request_id,
                 self._cpu_percent,
                 self._memory,
                 self._stacktrace,
                 self._request,
             )