Skip to content

feat(metrics): add support to persist default dimensions #410

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

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Apr 22, 2021

Issue #, if available: #406

Description of changes:

Checklist

  • Meet tenets criteria
  • Update tests
  • Update docs
  • PR title follows conventional commit semantics
  • Add new set_default_dimensions method
  • Add new clear_default_dimensions method
  • Docs: Update testing section to inspect Metrics created
  • Docs: Update testing section to reset Default Dimensions
  • Docs: Banner about adding metrics outside the handler
  • Docs: Banner about sparse cold start metrics for cost
  • Docs: Add Functional testing section

UX

Persists two dimensions across all metrics, every Metrics instance throughout the codebase, and across all Lambda invocations.

from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit

metrics = Metrics(namespace="ExampleApplication", service="booking")
metrics.set_default_dimensions(environment="prod", another="one")

@metrics.log_metrics
def lambda_handler(evt, ctx):
  metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)

Breaking change checklist

RFC issue #:

  • Migration process documented
  • Implement warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link

@pcolazurdo pcolazurdo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a few documentation comments and a question around the behavior of clear_metrics()

@pcolazurdo
Copy link

I'd like this approach for some use cases but I think it may add a "burden" on the dev UX to remember where the defaults where set. I'd prefer an approach where the decorator has something like:

@metrics.log_metrics(default_dimensions=**dimensions)

Which keeps the defaults very close to the function. I wouldn't expect this defaults to be shared across other decorators in my opinion - i.e. if the code is reusing a library that has it's own defaults

@heitorlessa
Copy link
Contributor Author

I'd like this approach for some use cases but I think it may add a "burden" on the dev UX to remember where the defaults where set. I'd prefer an approach where the decorator has something like:

@metrics.log_metrics(default_dimensions=**dimensions)

Which keeps the defaults very close to the function. I wouldn't expect this defaults to be shared across other decorators in my opinion - i.e. if the code is reusing a library that has it's own defaults

Yeah, I'd have loved to go down that route too, however param= doesn't accept unpacking a dictionary, it'd have to be @metrics.log_metrics(**dimensions) in which case makes it less explicit to whoever reading the code what this does.

I also experimented with:

dimensions = [{"name": "dimension_1"}, "value": "value"}, {"name": "dimension_2"}, "value": "value"}]
@metrics.log_metrics(default_dimensions=dimensions)

But this was more explicit and can work outside Lambda handler like in Lambda layers code, other modules, etc.

metrics = Metrics()

metrics.set_default_dimensions(environment="prod", dimension_one="value", ...)

@pcolazurdo
Copy link

Wouldn't something like this below help?

@my_metrics.log_metrics(default_dimensions={"environment": "dev"})
def lambda_handler(evt, context):
  return True

I have a sample version here: https://github.com/heitorlessa/aws-lambda-powertools-python/compare/feat/metrics-default-dimensions...pcolazurdo:pr_410?expand=1

@heitorlessa
Copy link
Contributor Author

This does @pcolazurdo - I was initially thinking the name=, value=, which didn't fit.

I'll include that in today now that the implementation is already done

Copy link

@pcolazurdo pcolazurdo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

@codecov-commenter
Copy link

codecov-commenter commented Apr 23, 2021

Codecov Report

Merging #410 (a1da396) into develop (68d4110) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #410   +/-   ##
========================================
  Coverage    99.89%   99.89%           
========================================
  Files          100      100           
  Lines         3779     3827   +48     
  Branches       175      178    +3     
========================================
+ Hits          3775     3823   +48     
  Misses           1        1           
  Partials         3        3           
Impacted Files Coverage Δ
aws_lambda_powertools/metrics/metrics.py 100.00% <100.00%> (ø)
aws_lambda_powertools/logging/logger.py 100.00% <0.00%> (ø)
aws_lambda_powertools/logging/formatter.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 68d4110...a1da396. Read the comment docs.

@heitorlessa heitorlessa added this to the 1.15.0 milestone Apr 23, 2021
@heitorlessa heitorlessa merged commit 776569a into aws-powertools:develop Apr 23, 2021
@heitorlessa heitorlessa deleted the feat/metrics-default-dimensions branch April 23, 2021 10:18
@heitorlessa heitorlessa changed the title feat: add support to persist default dimensions feat(metrics): add support to persist default dimensions May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants