Share `metrics` object across modules
See original GitHub issueHello, 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:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@salapat11, create a file
telemetry.py
(or use the__init__.py
, whatever) where you use the app factory pattern just like @rycus86 recommended.In your module where you create the “root” Flask object:
In all other modules where you want to use the metrics object: