Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix timestamp issue caused by creating the metric context too early #46

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aws_embedded_metrics/metric_scope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@


def metric_scope(fn): # type: ignore
logger = create_metrics_logger()

if asyncio.iscoroutinefunction(fn):

@wraps(fn)
async def wrapper(*args, **kwargs): # type: ignore
logger = create_metrics_logger()
if "metrics" in inspect.signature(fn).parameters:
kwargs["metrics"] = logger
try:
Expand All @@ -38,6 +38,7 @@ async def wrapper(*args, **kwargs): # type: ignore

@wraps(fn)
def wrapper(*args, **kwargs): # type: ignore
logger = create_metrics_logger()
if "metrics" in inspect.signature(fn).parameters:
kwargs["metrics"] = logger
try:
Expand Down
19 changes: 4 additions & 15 deletions tests/logger/test_metrics_context.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
from aws_embedded_metrics import config
from aws_embedded_metrics.logger.metrics_context import MetricsContext
from aws_embedded_metrics import utils
from aws_embedded_metrics.constants import DEFAULT_NAMESPACE
from _pytest.monkeypatch import MonkeyPatch
import pytest
from importlib import reload
from faker import Faker

fake = Faker()


@pytest.fixture
def mock_time():
expected_time = fake.random.randrange(0, 1000)
monkeypatch = MonkeyPatch()
monkeypatch.setattr(utils, "now", lambda: expected_time)
return expected_time


def test_can_create_context_with_no_arguments(mock_time):
def test_can_create_context_with_no_arguments():
# reload the configuration module since it is loaded on
# startup and cached
reload(config)
Expand All @@ -29,13 +18,13 @@ def test_can_create_context_with_no_arguments(mock_time):

# assert
assert context.namespace == DEFAULT_NAMESPACE
assert context.meta == {"Timestamp": mock_time}
assert context.meta["Timestamp"] > 0
assert context.properties == {}
assert context.dimensions == []
assert context.default_dimensions == {}


def test_can_set_property(mock_time):
def test_can_set_property():
# arrange
context = MetricsContext()

Expand All @@ -49,7 +38,7 @@ def test_can_set_property(mock_time):
assert context.properties == {property_key: property_value}


def test_put_dimension_adds_to_dimensions(mock_time):
def test_put_dimension_adds_to_dimensions():
# arrange
context = MetricsContext()

Expand Down
19 changes: 19 additions & 0 deletions tests/metric_scope/test_metric_scope.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from aws_embedded_metrics.metric_scope import metric_scope
from aws_embedded_metrics.logger.metrics_logger import MetricsLogger
import asyncio
import time
import pytest

flush_invocations = []
Expand Down Expand Up @@ -149,6 +150,24 @@ def my_handler(metrics):
assert InvocationTracker.invocations == 1


def test_sync_scope_sets_time_based_on_when_wrapped_fcn_is_called(mock_logger):
# arrange
sleep_duration_sec = 3

@metric_scope
def my_handler(metrics):
return metrics

time.sleep(sleep_duration_sec)

# act
expected_timestamp_second = int(round(time.time()))
logger = my_handler()

# assert
actual_timestamp_second = int(round(logger.context.meta["Timestamp"] / 1000))
assert expected_timestamp_second == actual_timestamp_second

# Test helpers


Expand Down