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.

Share `metrics` object across modules

See original GitHub issue

Hello, my API is spread across several Blueprints and namespaces while the metrics object gets instantiated in my main app.py. Is there a built-in way to share the object across modules, maybe something like current_app in Flask?

app.py

import api.internal
import api.external

app = Flask(__name__)
metrics = GunicornPrometheusMetrics(app)

I have seen the app factory example, but there everything is set up in its own app_setup.py. But I cannot import app.py in my APIs as this would introduce circular dependencies.

Am I missing something (as a beginner) or should I just create a “singleton” method somewhere?

metrics = None

def get_current_metrics(app):
    global metrics

    if metrics is None and app:
        metrics = GunicornPrometheusMetrics(app)
        
    return metrics

Thanks in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
salapat11commented, Jul 22, 2020

I see the issue now. I had the decorator at the class level instead of the method level. It works as expected. Thanks again for your time @rycus86 , @trallnag .

Thanks for putting this library, @rycus86 . Very easy to integrate and got it working in few mins.

1reaction
trallnagcommented, Jul 21, 2020

@salapat11, create a file telemetry.py (or use the __init__.py, whatever) where you use the app factory pattern just like @rycus86 recommended.

from prometheus_flask_exporter import PrometheusMetrics
METRICS = PrometheusMetrics.for_app_factory()

In your module where you create the “root” Flask object:

from telemetry import METRICS
# ...
METRICS.init_app(flask_app)

In all other modules where you want to use the metrics object:

from telemetry import METRICS
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I share global variables across modules - python
The best way to share global variables across modules across a single program is to create a config module. Just import the config...
Read more >
How to share a dynamic constant across modules?
How to share a dynamic constant across modules? ; 'use strict'; import { v4 } · "uuid" · const ssid = v4(); function...
Read more >
Programming FAQ — Python 3.11.1 documentation
How do I share global variables across modules? What are the “best practices” for using import in a module? Why are default values...
Read more >
Using modules to encapsulate and reuse resource ...
Modules are a way for you to package resource configurations for inclusion across stack templates, in a transparent, manageable, and repeatable way.
Read more >
Structure Overview — PyTorch-Metrics 0.11.0 documentation
MetricCollection object is that it will automatically try to reduce the computations needed by finding groups of metrics that share the same underlying...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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