async def test_configure_context_adds_ec2_metadata_props(aresponses): # arrange image_id = fake.word() instance_id = fake.word() instance_type = fake.word() private_ip = fake.ipv4() az = fake.word() configure_response( aresponses, json.dumps({ "imageId": image_id, "instanceId": instance_id, "instanceType": instance_type, "privateIp": private_ip, "availabilityZone": az, }), ) env = EC2Environment() context = MetricsContext.empty() # environment MUST be detected before we can access the metadata await env.probe() # act env.configure_context(context) # assert assert context.properties["imageId"] == image_id assert context.properties["instanceId"] == instance_id assert context.properties["instanceType"] == instance_type assert context.properties["privateIp"] == private_ip assert context.properties["availabilityZone"] == az
def __init__( self, resolve_environment: Callable[..., Awaitable[Environment]], context: MetricsContext = None, ): self.resolve_environment = resolve_environment self.context: MetricsContext = context or MetricsContext.empty()
async def flush(self) -> None: # resolve the environment and get the sink # MOST of the time this will run synchonrously # This only runs asynchronously if executing for the # first time in a non-lambda environment environment = await self.resolve_environment() self.__configureContextForEnvironment(environment) sink = environment.get_sink() # accept and reset the context sink.accept(self.context) self.context = MetricsContext.empty()
def test_accept_writes_multiple_messages_to_stdout(mock_serializer, capfd): # arrange expected_messages = [fake.word() for _ in range(10)] mock_serializer.serialize.return_value = expected_messages sink = StdoutSink(serializer=mock_serializer) context = MetricsContext.empty() context.meta["Timestamp"] = 1 # act sink.accept(context) # assert out, err = capfd.readouterr() assert len(out.split()) == len(expected_messages) assert out.split() == expected_messages
def test_accept_writes_to_stdout(capfd): # arrange sink = LambdaSink() context = MetricsContext.empty() context.meta["Timestamp"] = 1 # act sink.accept(context) # assert out, err = capfd.readouterr() assert ( out == '{"_aws": {"Timestamp": 1, "CloudWatchMetrics": [{"Dimensions": [], "Metrics": [], "Namespace": "aws-embedded-metrics"}]}}\n' )
def test_more_than_max_number_of_metrics(mock_get_socket_client): # arrange context = MetricsContext.empty() expected_metrics = 401 expected_send_message_calls = 5 for index in range(expected_metrics): context.put_metric(f"{index}", 1) mock_tcp_client = Mock() mock_get_socket_client.return_value = mock_tcp_client # act sink = AgentSink("") sink.accept(context) # assert assert expected_send_message_calls == mock_tcp_client.send_message.call_count
def test_accept_writes_to_stdout(capfd): # arrange reload(config) sink = StdoutSink() context = MetricsContext.empty() context.meta["Timestamp"] = 1 context.put_metric("Dummy", 1) # act sink.accept(context) # assert out, err = capfd.readouterr() assert ( out == '{"_aws": {"Timestamp": 1, "CloudWatchMetrics": [{"Dimensions": [], "Metrics": [{"Name": "Dummy", "Unit": "None"}], ' '"Namespace": "aws-embedded-metrics"}]}, "Dummy": 1}\n' )
def get_context(): context = MetricsContext.empty() context.meta["Timestamp"] = 0 return context
def create_metrics_logger() -> MetricsLogger: context = MetricsContext.empty() logger = MetricsLogger(resolve_environment, context) return logger