question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(feat) decorator labels

See original GitHub issue

The setup of using @REQUEST_TIME.time() is nice. would it make sense to extend it to support labels so you could have different metrics for different method calls / http calls.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
drfloobcommented, Sep 25, 2018

Reviving this thread … it seems Guido had “a gut feeling” in disallowing the kind of decorator syntax that would allow method chaining https://mail.python.org/pipermail/python-dev/2004-August/046711.html. Mentioned in PEP 318

I hacked a fairly non-pythonic workaround:

engine_process_message_timer = REQUEST_TIME.labels(method="engine_process_message")
@engine_process_message_timer.time()
def my_func(...):
    pass
2reactions
luafrancommented, Sep 14, 2017

If you can live with this, a workaround is to instantiate a Histogram object with label values and then call time() on this object. Like this:

REQUEST_TIME = Histogram('http_request_duration_seconds', 'help', ['method', 'endpoint', 'service'])
REQUEST_TIME_GET_TEST = REQUEST_TIME.labels(method='GET', endpoint="test", service='webapp-python')

@app.route('/test/<time_ms>', method='GET')
@REQUEST_TIME_GET_TEST.time()
def get_test(time_ms='0'):
    logger.info('Processing GET /test/'+time_ms)

    time_to_sleep = int(time_ms) / float(1000)
    time.sleep(time_to_sleep)

    retdata = {
        "status": "OK"
    }

    return retdata

The details after a not so deep analysis of the issue

When you create a Histogram (or any other metric) with label names like this:

REQUEST_TIME = Histogram('http_request_duration_seconds', 'help', ['method', 'endpoint', 'service'])

Instead of a Histogram object, what you get is a _LabelWrapper object.

Since you need a Histogram object to call the time() decorator factory, the workaround is to get back a Histogram object calling labels() on the _LabelWrapper object before using the decorator.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Art Deco labels - 99Designs
Looking for Art Deco label ideas? We've collected thousands of professional Art Deco label designs from our global design community.
Read more >
Custom Stickers & Labels | Available in 6 Sizes | MOO US
Make Stickers & Labels online with MOO. Design your own promotional Stickers and personalized Labels or use our customizable templates.
Read more >
i5300 Labels | Brady - BradyID.com
i5300 Labels. (124 results). Can't find the right label? Design your own or work with Brady for customized label solutions.
Read more >
Graduation labels - modern, masculine design | Zazzle
Mar 3, 2019 - FREE Design Tool on Zazzle! Shop Graduation labels - modern, masculine design created by TheStationeryShop. Personalize it with photos...
Read more >
Order Personalized Return Address Labels - Walmart Checks
You'll find styles featuring Disney characters and other popular favorites, ... Just choose a design, personalize it, and we'll have your mailing labels...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found