Exemplo n.º 1
0
def on_request_created(
    api_call_stats: MultilevelCounter,
    account_id: str,
    region_name: str,
    service_name: str,
    readonly: bool,
    **kwargs: Any,
) -> None:
    """Called when a boto3 request is created. This handles api call statistics tracking.

    Args:
        api_call_stats: MultilevelCounter to increment
        account_id: request account id
        region_name: request region
        service_name: request service
        readonly: if True only allow readonly calls
        kwargs: kwargs which are passed through by the boto event callback.
    """
    _, _, operation_name = kwargs["event_name"].split(".")
    if readonly:
        if not _PERMITTED_OPERATION_NAMES_RE.search(kwargs["operation_name"]):
            raise Exception(
                f"Operation name {operation_name} did not match {_PERMITTED_OPERATION_NAMES_STR}"
            )
    api_call_stats.increment(account_id, region_name, service_name,
                             operation_name)
Exemplo n.º 2
0
    def test_merge_updates_self(self):
        ml_counter_self = MultilevelCounter()
        ml_counter_self.increment("foo", "boo", "goo")

        ml_counter_other = MultilevelCounter()
        ml_counter_other.increment("boo", "goo", "moo")

        ml_counter_self.merge(ml_counter_other)

        expected_data = {
            "count": 2,
            "foo": {
                "count": 1,
                "boo": {
                    "count": 1,
                    "goo": {
                        "count": 1
                    }
                }
            },
            "boo": {
                "count": 1,
                "goo": {
                    "count": 1,
                    "moo": {
                        "count": 1
                    }
                }
            },
        }

        self.assertDictEqual(expected_data, ml_counter_self.to_dict())
Exemplo n.º 3
0
 def test_increment(self):
     ml_counter = MultilevelCounter()
     ml_counter.increment("foo", "boo", "goo")
     expected_data = {
         "count": 1,
         "foo": {
             "count": 1,
             "boo": {
                 "count": 1,
                 "goo": {
                     "count": 1
                 }
             }
         }
     }
     self.assertDictEqual(expected_data, ml_counter.to_dict())