Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(metrics): proxy service and namespace attrs to provider #2910

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

Merged
merged 1 commit into from
Aug 2, 2023
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
20 changes: 20 additions & 0 deletions aws_lambda_powertools/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,26 @@ def clear_default_dimensions(self) -> None:
def clear_metrics(self) -> None:
self.provider.clear_metrics()

# We now allow customers to bring their own instance
# of the AmazonCloudWatchEMFProvider provider
# So we need to define getter/setter for namespace and service properties
# To access these attributes on the provider instance.
@property
def namespace(self):
return self.provider.namespace

@namespace.setter
def namespace(self, namespace):
self.provider.namespace = namespace

@property
def service(self):
return self.provider.service

@service.setter
def service(self, service):
self.provider.service = service


# Maintenance: until v3, we can't afford to break customers.
# AmazonCloudWatchEMFProvider has the exact same functionality (non-singleton)
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,25 @@ def test_clear_default_dimensions(namespace):
assert not my_metrics.default_dimensions


def test_get_and_set_namespace_and_service_properties(namespace, service, metrics, capsys):
# GIVEN Metrics instance is initialized without namespace and service
my_metrics = Metrics()

# WHEN we set service and namespace before flushing the metric
@my_metrics.log_metrics
def lambda_handler(evt, ctx):
my_metrics.namespace = namespace
my_metrics.service = service
for metric in metrics:
my_metrics.add_metric(**metric)

lambda_handler({}, {})
invocation = capture_metrics_output(capsys)

assert service in json.dumps(invocation)
assert namespace in json.dumps(invocation)


def test_clear_default_dimensions_with_provider(namespace):
# GIVEN Metrics is initialized with provider and we persist a set of default dimensions
my_provider = AmazonCloudWatchEMFProvider(namespace=namespace)
Expand Down