Skip to content

Metrics not flushing #43

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

Closed
instil-richard opened this issue May 29, 2020 · 6 comments
Closed

Metrics not flushing #43

instil-richard opened this issue May 29, 2020 · 6 comments
Assignees
Labels
bug Something isn't working need-more-information Pending information to continue

Comments

@instil-richard
Copy link

Using Python 3.8 and aws-lambda-powertools==0.9.1

It could be that I'm not using metrics as their design intended however I would like to make use of them in the following pattern so metrics can be consumed by classes shared across my project which are imported into my lambdas as needed (so my lambdas handlers have few lines of code) e.g.

from aws_lambda_powertools.metrics import Metrics
from mynamespace.mypackage import MyClass

metrics = Metrics()

@metrics.log_metrics 
def handler(request, context):
 
    # passing metrics to my class that does all the work for the lambda: 
    return MyClass(metrics).do_stuff()

Adding metrics to MyClass might look something like this (using randint for demo purposes obviously):

class MyClass:
    def __init__(self, metrics):
        self.metrics = metrics

    def do_stuff(self):
        success = random.randint(0, 1)

        if success:
            self.metrics.add_metric(name="Success", unit=MetricUnit.Count, value=1)
            self.metrics.add_dimension(name='service', value='MyClass')
            return True
        else:
            self.metrics.add_metric(name="Failure", unit=MetricUnit.Count, value=1)
            self.metrics.add_dimension(name='service', value='MyClass')
            return False

Following this pattern the desired metrics are created correctly but on subsequent executions metrics are not flushed so subsequent executions will output both a 'Success' AND a 'Failure' metric from a single execution, so the print output in logs looks like this:

{
    "Success": 1,
    "Failure": 1,
    "_aws": {
        "Timestamp": 1590714776621,
        "CloudWatchMetrics": [
            {
                "Namespace": "richard",
                "Dimensions": [
                    [
                        "service"
                    ]
                ],
                "Metrics": [
                    {
                        "Name": "Success",
                        "Unit": "Count"
                    },
                    {
                        "Name": "Failure",
                        "Unit": "Count"
                    }
                ]
            }
        ]
    },
    "service": "MyClass"
}

Metrics are supposed to be flushed by the log_metrics decorator on each handler execution no? Would love to get your thoughts on this if it's a bug or if I'm not using them as intended.

@ghost ghost added the triage Pending triage from maintainers label May 29, 2020
@heitorlessa
Copy link
Contributor

Hi @dotorg-richard - Thank you for reporting this.

That doesn't seem right, and more like a bug - It's as if the metric set isn't being cleaned up. I'll try reproducing this on my end, and update here.

@heitorlessa heitorlessa added bug Something isn't working and removed triage Pending triage from maintainers labels May 29, 2020
@heitorlessa
Copy link
Contributor

It's a bug indeed - I'll create a test, and publish a patched version today.

heitorlessa added a commit that referenced this issue May 29, 2020
Signed-off-by: heitorlessa <[email protected]>
heitorlessa added a commit that referenced this issue May 29, 2020
heitorlessa added a commit that referenced this issue May 29, 2020
@heitorlessa
Copy link
Contributor

I'm building wheels, and publishing a new patch release 0.9.4 shortly.

I also documented how to manually serialize, flush, and clear metrics for scenarios where you want to have full control of it (no use of log_metrics)

image

heitorlessa added a commit that referenced this issue May 29, 2020
* [Sync Master] 0.7.0 release (#22)

* docs: add pypi badge

* fix: add missing single_metric example; test var name

* chore: pypi monthly download badge

* chore: fix github badge typo

* feat: add docs to CI

* fix: CI attempt 2

* fix: CI attempt 3

* fix: CI attempt 3

* fix: CI attempt 4

* chore: clean up CI workflows

* Decorator factory Feat: Create your own middleware (#17)

* feat(utils): add decorator factory

* improv: use partial to reduce complexity

* improv: add error handling

* chore: type hint

* docs: include pypi downloads badge

* feat: opt in to trace each middleware that runs

* improv: add initial util tests

* improv: test explicit and implicit trace_execution

* improv: test decorator with params

* chore: linting

* docs: include utilities

* improv: correct tests, dec_factory only for func

* improv: make util name more explicit

* improv: doc trace_execution, fix casting

* docs: add limitations, improve syntax

* docs: use new docs syntax

* fix: remove middleware decorator from libs

* feat: build docs in CI

* chore: linting

* fix: CI python-version type

* chore: remove docs CI

* chore: kick CI

* chore: include build badge master branch

* chore: refactor naming

* fix: rearrange tracing tests

* improv(tracer): toggle default auto patching

* feat(tracer): retrieve registered class instance

* fix(Makefile):  make cov target more explicit

* improv(Register): support multiple classes reg.

* improv(Register): inject class methods correctly

* docs: add how to reutilize Tracer

* improv(tracer): test auto patch method

* improv: address nicolas feedback

* improv: update example to reflect middleware feat

* fix: metric dimension in root blob

* chore: version bump

Co-authored-by: heitorlessa <[email protected]>

Co-authored-by: heitorlessa <[email protected]>

* feat: add algolia search for docs and api ref (#39) (#40)

Signed-off-by: heitorlessa <[email protected]>

* fix: revert makefile build-docs-api

Signed-off-by: heitorlessa <[email protected]>

* fix: metric_set reuse #43

Signed-off-by: heitorlessa <[email protected]>

* fix: clear metrics

Signed-off-by: heitorlessa <[email protected]>

* fix: update serialize_metrics helper function to use MetricManager instead of Metrics

* fix: clear metrics after lambda invocation #43

Signed-off-by: heitorlessa <[email protected]>

* improv: document metrics tests, remove redundants

Signed-off-by: heitorlessa <[email protected]>

#43

* chore: linting

Signed-off-by: heitorlessa <[email protected]>

* docs: add section to flush metrics manually

Signed-off-by: heitorlessa <[email protected]>

* docs: include EMF Json object

Signed-off-by: heitorlessa <[email protected]>

* chore: bump version 0.9.4

Signed-off-by: heitorlessa <[email protected]>

Co-authored-by: Tom McCarthy <[email protected]>
@heitorlessa
Copy link
Contributor

Done - 0.9.4 is now live.

Let me know if that works for you @dotorg-richard

@heitorlessa heitorlessa self-assigned this May 31, 2020
@heitorlessa heitorlessa added the need-more-information Pending information to continue label Jun 2, 2020
@instil-richard
Copy link
Author

Thanks Heitor for being so responsive, I'm deep into a big refactor right now will get you feedback when I can afford the context switch :)

@instil-richard
Copy link
Author

Tested it on v0.9.5 and the fix works, the behavior is as expected. Thanks again!!

heitorlessa referenced this issue in heitorlessa/aws-lambda-powertools-python Jun 17, 2020
* [Sync Master] 0.7.0 release (#22)

* docs: add pypi badge

* fix: add missing single_metric example; test var name

* chore: pypi monthly download badge

* chore: fix github badge typo

* feat: add docs to CI

* fix: CI attempt 2

* fix: CI attempt 3

* fix: CI attempt 3

* fix: CI attempt 4

* chore: clean up CI workflows

* Decorator factory Feat: Create your own middleware (#17)

* feat(utils): add decorator factory

* improv: use partial to reduce complexity

* improv: add error handling

* chore: type hint

* docs: include pypi downloads badge

* feat: opt in to trace each middleware that runs

* improv: add initial util tests

* improv: test explicit and implicit trace_execution

* improv: test decorator with params

* chore: linting

* docs: include utilities

* improv: correct tests, dec_factory only for func

* improv: make util name more explicit

* improv: doc trace_execution, fix casting

* docs: add limitations, improve syntax

* docs: use new docs syntax

* fix: remove middleware decorator from libs

* feat: build docs in CI

* chore: linting

* fix: CI python-version type

* chore: remove docs CI

* chore: kick CI

* chore: include build badge master branch

* chore: refactor naming

* fix: rearrange tracing tests

* improv(tracer): toggle default auto patching

* feat(tracer): retrieve registered class instance

* fix(Makefile):  make cov target more explicit

* improv(Register): support multiple classes reg.

* improv(Register): inject class methods correctly

* docs: add how to reutilize Tracer

* improv(tracer): test auto patch method

* improv: address nicolas feedback

* improv: update example to reflect middleware feat

* fix: metric dimension in root blob

* chore: version bump

Co-authored-by: heitorlessa <[email protected]>

Co-authored-by: heitorlessa <[email protected]>

* feat: add algolia search for docs and api ref (#39) (#40)

Signed-off-by: heitorlessa <[email protected]>

* fix: revert makefile build-docs-api

Signed-off-by: heitorlessa <[email protected]>

* fix: metric_set reuse #43

Signed-off-by: heitorlessa <[email protected]>

* fix: clear metrics

Signed-off-by: heitorlessa <[email protected]>

* fix: update serialize_metrics helper function to use MetricManager instead of Metrics

* fix: clear metrics after lambda invocation #43

Signed-off-by: heitorlessa <[email protected]>

* improv: document metrics tests, remove redundants

Signed-off-by: heitorlessa <[email protected]>

#43

* chore: linting

Signed-off-by: heitorlessa <[email protected]>

* docs: add section to flush metrics manually

Signed-off-by: heitorlessa <[email protected]>

* docs: include EMF Json object

Signed-off-by: heitorlessa <[email protected]>

* chore: bump version 0.9.4

Signed-off-by: heitorlessa <[email protected]>

Co-authored-by: Tom McCarthy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need-more-information Pending information to continue
Projects
Development

No branches or pull requests

2 participants