1616from grafana_client .api import GrafanaApi
1717from grafana_client .client import GrafanaClientError , GrafanaUnauthorizedError
1818from munch import Munch , munchify
19- from requests_cache import CachedSession
2019from tqdm import tqdm
2120from tqdm .contrib .logging import tqdm_logging_redirect
2221from urllib3 .exceptions import InsecureRequestWarning
2322
2423from grafana_wtf import __appname__ , __version__
24+ from grafana_wtf .compat import CachedSession
2525from grafana_wtf .model import (
2626 DashboardDetails ,
2727 DashboardExplorationItem ,
3535
3636
3737class GrafanaEngine :
38- session = niquests .Session ()
38+
39+ # Configure a larger HTTP request pool.
40+ # TODO: Review the pool settings and eventually adjust according to concurrency level or other parameters.
41+ # https://urllib3.readthedocs.io/en/latest/advanced-usage.html#customizing-pool-behavior
42+ # https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/
43+ session_args = dict (pool_connections = 100 , pool_maxsize = 100 , retries = 5 )
44+
45+ # The HTTP `User-Agent` header value.
46+ user_agent = f"{ __appname__ } /{ __version__ } "
3947
4048 def __init__ (self , grafana_url , grafana_token = None ):
4149 self .grafana_url = grafana_url
4250 self .grafana_token = grafana_token
4351
44- self .grafana = None
45- self .data = GrafanaDataModel ()
52+ self .concurrency = 5
4653
54+ self .grafana = self .grafana_client_factory (self .grafana_url , grafana_token = self .grafana_token )
55+ self .set_user_agent ()
56+ self .data = GrafanaDataModel ()
4757 self .finder = JsonPathFinder ()
4858
4959 self .taqadum = None
50- self .concurrency = 5
51-
5260 self .debug = log .getEffectiveLevel () == logging .DEBUG
5361 self .progressbar = not self .debug
5462
63+ def set_session (self , session ):
64+ self .grafana .client .s = session
65+
5566 def enable_cache (self , expire_after = 60 , drop_cache = False ):
5667 if expire_after is None :
5768 log .info (f"Response cache will never expire (infinite caching)" )
@@ -60,10 +71,13 @@ def enable_cache(self, expire_after=60, drop_cache=False):
6071 else :
6172 log .info (f"Response cache will expire after { expire_after } seconds" )
6273
63- self .session = CachedSession (cache_name = __appname__ , expire_after = expire_after , use_cache_dir = True )
74+ session = CachedSession (
75+ cache_name = __appname__ , expire_after = expire_after , use_cache_dir = True , ** self .session_args
76+ )
77+ self .set_session (session )
78+ self .set_user_agent ()
6479
65- cache = self .session .cache
66- log .info (f"Response cache database location is: { cache .db_path } " )
80+ log .info (f"Response cache database: { session .cache .db_path } " )
6781 if drop_cache :
6882 log .info ("Dropping response cache" )
6983 self .clear_cache ()
@@ -102,23 +116,14 @@ def grafana_client_factory(cls, grafana_url, grafana_token=None):
102116 url_path_prefix = url .path .lstrip ("/" ),
103117 verify = verify ,
104118 )
105- if cls .session :
106- user_agent = f"{ __appname__ } /{ __version__ } "
107- cls .session .headers ["User-Agent" ] = user_agent
108- grafana .client .s = cls .session
109119
110- return grafana
120+ # Configure HTTP session to use a larger HTTP request pool.
121+ grafana .client .s = niquests .Session (** cls .session_args )
111122
112- def setup (self ):
113- self .grafana = self .grafana_client_factory (self .grafana_url , grafana_token = self .grafana_token )
114-
115- # Configure a larger HTTP request pool.
116- # Todo: Review the pool settings and eventually adjust according to concurrency level or other parameters.
117- # https://urllib3.readthedocs.io/en/latest/advanced-usage.html#customizing-pool-behavior
118- # https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/
119- self .grafana .client .s = niquests .Session (pool_connections = 100 , pool_maxsize = 100 , retries = 5 )
123+ return grafana
120124
121- return self
125+ def set_user_agent (self ):
126+ self .grafana .client .s .headers ["User-Agent" ] = self .user_agent
122127
123128 def start_progressbar (self , total ):
124129 if self .progressbar :
0 commit comments