def setUpClass(cls):
        cls.time_patcher = patch.object(stats_manager.time, 'time')
        cls.time_mock = cls.time_patcher.start()
        # Initialize ServiceStats with default metrics for recent requests
        cls.time_mock.return_value = time()
        cls.stats = stats_manager.ServiceStats("my_service")

        # Initialize request_simulator for shorter form of start-finalize calls
        request = request_simulator(cls.stats, cls.time_mock)

        # Start and finalize requests using request_simulator
        request(latency=100,
                app="guestbook",
                resource="/",
                status=200,
                end_time=1515595821111)
        request(latency=150, app="guestbook", resource="/", status=200)
        request(latency=200, app="guestbook", resource="/api/foo", status=200)
        request(latency=250, app="guestbook", resource="/api/v2", status=200)
        request(latency=300, app="guestbook", resource="/api/v2", status=403)
        request(latency=350, app="guestbook", resource="/api/v2", status=403)
        request(latency=400, app="guestbook", resource="/api/v3/z", status=404)
        request(latency=450, app="guestbook", resource="/api/v3/z", status=502)
        request(latency=500, app="other", resource="/foo/bar", status=200)
        request(latency=550, app="other", resource="/foo/bar", status=200)
        request(latency=600, app="other", resource="/path", status=200)
        request(latency=650, app="other", resource="/path", status=200)
        request(latency=701,
                app="other",
                resource="/path",
                status=401,
                end_time=1515595824444)
    def setUpClass(cls):
        cls.time_patcher = patch.object(stats_manager.time, 'time')
        cls.time_mock = cls.time_patcher.start()
        # Initialize ServiceStats with default metrics for recent requests
        cls.time_mock.return_value = time()
        cls.stats = stats_manager.ServiceStats(
            "my_service",
            history_size=6,
            default_metrics_for_recent={
                "all": metrics.CountOf(matchers.ANY),
                "4xx": metrics.CountOf(matchers.CLIENT_ERROR),
                "5xx": metrics.CountOf(matchers.SERVER_ERROR),
            })

        # Start and finalize requests to fill recent requests history

        # First two are finished in the same time,
        # but one with status 500 was reported first
        cls.time_mock.return_value = 151550002
        cls.stats.start_request(app="my_app").finalize(status=500)
        cls.time_mock.return_value = 151550002
        cls.stats.start_request(app="my_app").finalize(status=400)

        # Then one request every second (only 2 latest has status 200)
        cls.time_mock.return_value = 151550003
        cls.stats.start_request(app="my_app").finalize(status=400)
        cls.time_mock.return_value = 151550004
        cls.stats.start_request(app="my_app").finalize(status=400)
        cls.time_mock.return_value = 151550005
        cls.stats.start_request(app="my_app").finalize(status=400)
        cls.time_mock.return_value = 151550006
        cls.stats.start_request(app="my_app").finalize(status=200)
        cls.time_mock.return_value = 151550007
        cls.stats.start_request(app="my_app").finalize(status=200)
    def setUp(self):
        request_fields = ["app", "namespace", "status"]

        class DefaultNsMatcher(matchers.RequestMatcher):
            def matches(self, request_info):
                return request_info.namespace == "default"

        by_app = categorizers.ExactValueCategorizer("by_app", field="app")
        by_ns = categorizers.ExactValueCategorizer("by_ns", field="namespace")
        by_status = categorizers.ExactValueCategorizer("by_status",
                                                       field="status")

        counters_config = {
            "all": matchers.ANY,
            by_app: {
                "all": matchers.ANY,
                "default_ns": DefaultNsMatcher(),
                by_ns: {
                    "all": matchers.ANY,
                    "4xx": matchers.CLIENT_ERROR,
                    "5xx": matchers.SERVER_ERROR,
                },
                by_status: matchers.ANY,
            }
        }

        self.stats = stats_manager.ServiceStats(
            "my_service",
            cumulative_counters=counters_config,
            request_fields=request_fields)
Exemple #4
0
    def test_current_requests(self):
        # Initialize ServiceStats with default metrics for recent requests
        stats = stats_manager.ServiceStats("my_service")

        # Start and finalize 3 requests
        req_info = stats.start_request()
        req_info.app = "app"
        req_info.status = 404
        req_info.finalize()
        req_info = stats.start_request()
        req_info.app = "app"
        req_info.status = 404
        req_info.finalize()
        req_info = stats.start_request()
        req_info.app = "app"
        req_info.status = 404
        req_info.finalize()

        # Start without finalization 4 requests
        stats.start_request()
        stats.start_request()
        stats.start_request()
        stats.start_request()

        self.assertEqual(stats.current_requests, 4)
Exemple #5
0
    def setUp(self):
        request_fields = [
            "app", "namespace", "status", "method", "preproc_time",
            "postproc_time"
        ]

        def data_proc_summarizer(request_info):
            return request_info.preproc_time + request_info.postproc_time

        counters_config = {
            "all": samples.summarize_all,
            "total": data_proc_summarizer,
            ("by_app", samples.categorize_by_app): {
                "all": samples.summarize_all,
                "default_ns": lambda req_info: req_info.namespace == "default",
                ("by_ns", lambda req_info: req_info.namespace): {
                    "all": samples.summarize_all,
                    "4xx": samples.summarize_client_error,
                    "5xx": samples.summarize_server_error,
                },
                ("by_status", samples.categorize_by_status):
                samples.summarize_all,
                ("by_method", samples.categorize_by_method):
                data_proc_summarizer
            }
        }

        self.stats = stats_manager.ServiceStats(
            "my_service",
            cumulative_counters=counters_config,
            request_fields=request_fields)
 def setUp(self):
     self.time_patcher = patch.object(stats_manager.time, 'time')
     self.time_mock = self.time_patcher.start()
     # Initialize ServiceStats
     current_time = time()
     self.time_mock.return_value = current_time - 0.001
     self.start_time = int((current_time - 0.001) * 1000)
     self.stats = stats_manager.ServiceStats("my_service")
     self.time_mock.return_value = current_time
Exemple #7
0
    def setUpClass(cls):
        cls.time_patcher = patch.object(stats_manager.time, 'time')
        cls.time_mock = cls.time_patcher.start()
        # Initialize ServiceStats with default metrics for recent requests
        cls.time_mock.return_value = time()
        cls.stats = stats_manager.ServiceStats("my_service",
                                               history_size=6,
                                               default_metrics_for_recent={
                                                   "all":
                                                   samples.count_all,
                                                   "4xx":
                                                   samples.count_client_errors,
                                                   "5xx":
                                                   samples.count_server_errors
                                               })

        # Start and finalize requests to fill recent requests history

        # First two are finished in the same time,
        # but one with status 500 was reported first
        cls.time_mock.return_value = 151550002
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 500
        req_info.finalize()
        cls.time_mock.return_value = 151550002
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 400
        req_info.finalize()

        # Then one request every second (only 2 latest has status 200)
        cls.time_mock.return_value = 151550003
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 400
        req_info.finalize()
        cls.time_mock.return_value = 151550004
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 400
        req_info.finalize()
        cls.time_mock.return_value = 151550005
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 400
        req_info.finalize()
        cls.time_mock.return_value = 151550006
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 200
        req_info.finalize()
        cls.time_mock.return_value = 151550007
        req_info = cls.stats.start_request()
        req_info.app = "my_app"
        req_info.status = 200
        req_info.finalize()
Exemple #8
0
# Configure ServiceStats
REQUEST_STATS_FIELDS = [
    "pb_method", "pb_status", "rest_method", "rest_status", "api"
]
CUMULATIVE_COUNTERS = {
    "all": samples.summarize_all,
    "failed": summarize_failed_request,
    "pb_reqs": summarize_protobuffer_request,
    "rest_reqs": summarize_rest_request
}
METRICS_CONFIG = {
    "all": samples.count_all,
    "failed": count_failed_requests,
    "avg_latency": samples.count_avg_latency,
    "pb_reqs": count_protobuff_requests,
    "rest_reqs": count_rest_requests,
    ("by_pb_method", categorize_by_pb_method): samples.count_all,
    ("by_rest_method", categorize_by_rest_method): samples.count_all,
    ("by_pb_status", categorize_by_pb_status): samples.count_all,
    ("by_rest_status", categorize_by_rest_status): samples.count_all
}
# Instantiate singleton ServiceStats
service_stats = stats_manager.ServiceStats(
    "taskqueue",
    request_fields=REQUEST_STATS_FIELDS,
    cumulative_counters=CUMULATIVE_COUNTERS,
    default_metrics_for_recent=METRICS_CONFIG)
# Create tornado lock for tracking concurrent requests
stats_lock = locks.Lock()
 def test_service_name(self):
     stats = stats_manager.ServiceStats("my_SERvice")
     self.assertEqual(stats.service_name, "my_SERvice")
 def setUpClass(cls):
     cls.time_patcher = patch.object(stats_manager.time, 'time')
     cls.time_mock = cls.time_patcher.start()
     # Initialize ServiceStats
     cls.time_mock.return_value = time()
     cls.stats = stats_manager.ServiceStats("my_service")