@@ -92,35 +92,44 @@ class AMPConfig(PrometheusConfig):
9292 prometheus_ssl_enabled : bool = False
9393 assume_role_arn : Optional [str ] = None
9494
95- # cache the client
95+ # Refresh the AWS client (and its STS creds) every N seconds (default: 15 minutes)
96+ refresh_interval_seconds : int = 900
97+
9698 _aws_client : Optional [AWSPrometheusConnect ] = None
99+ _aws_client_created_at : float = 0.0
97100
98101 def is_amp (self ) -> bool :
99102 return True
100103
104+ def _should_refresh_client (self ) -> bool :
105+ if not self ._aws_client :
106+ return True
107+ return (
108+ time .time () - self ._aws_client_created_at
109+ ) >= self .refresh_interval_seconds
110+
101111 def get_aws_client (self ) -> Optional [AWSPrometheusConnect ]:
102- if self ._aws_client :
103- return self ._aws_client
104- try :
105- base_config = BasePrometheusConfig (
106- url = self .prometheus_url ,
107- disable_ssl = not self .prometheus_ssl_enabled ,
108- additional_labels = self .additional_labels ,
109- )
110- # Prometrix client (SigV4 signing)
111- self ._aws_client = AWSPrometheusConnect (
112- access_key = self .aws_access_key ,
113- secret_key = self .aws_secret_access_key ,
114- token = None ,
115- region = self .aws_region ,
116- service_name = self .aws_service_name ,
117- assume_role_arn = self .assume_role_arn ,
118- config = base_config ,
119- )
120- return self ._aws_client
121- except Exception :
122- logging .exception ("Failed to create aws client" )
123- return None
112+ if not self ._aws_client or self ._should_refresh_client ():
113+ try :
114+ base_config = BasePrometheusConfig (
115+ url = self .prometheus_url ,
116+ disable_ssl = not self .prometheus_ssl_enabled ,
117+ additional_labels = self .additional_labels ,
118+ )
119+ self ._aws_client = AWSPrometheusConnect (
120+ access_key = self .aws_access_key ,
121+ secret_key = self .aws_secret_access_key ,
122+ token = None ,
123+ region = self .aws_region ,
124+ service_name = self .aws_service_name ,
125+ assume_role_arn = self .assume_role_arn ,
126+ config = base_config ,
127+ )
128+ self ._aws_client_created_at = time .time ()
129+ except Exception :
130+ logging .exception ("Failed to create/refresh AWS client" )
131+ return self ._aws_client
132+ return self ._aws_client
124133
125134
126135class BasePrometheusTool (Tool ):
0 commit comments