13
13
14
14
CONTEXT_TYPE = "cloud_resource"
15
15
16
+ HTTP_TIMEOUT = 2.0
17
+
16
18
AWS_METADATA_HOST = "169.254.169.254"
17
19
AWS_TOKEN_URL = "http://{}/latest/api/token" .format (AWS_METADATA_HOST )
18
20
AWS_METADATA_URL = "http://{}/latest/dynamic/instance-identity/document" .format (
@@ -59,7 +61,7 @@ class CloudResourceContextIntegration(Integration):
59
61
cloud_provider = ""
60
62
61
63
aws_token = ""
62
- http = urllib3 .PoolManager ()
64
+ http = urllib3 .PoolManager (timeout = HTTP_TIMEOUT )
63
65
64
66
gcp_metadata = None
65
67
@@ -83,7 +85,13 @@ def _is_aws(cls):
83
85
cls .aws_token = r .data .decode ()
84
86
return True
85
87
86
- except Exception :
88
+ except urllib3 .exceptions .TimeoutError :
89
+ logger .debug (
90
+ "AWS metadata service timed out after %s seconds" , HTTP_TIMEOUT
91
+ )
92
+ return False
93
+ except Exception as e :
94
+ logger .debug ("Error checking AWS metadata service: %s" , str (e ))
87
95
return False
88
96
89
97
@classmethod
@@ -131,8 +139,12 @@ def _get_aws_context(cls):
131
139
except Exception :
132
140
pass
133
141
134
- except Exception :
135
- pass
142
+ except urllib3 .exceptions .TimeoutError :
143
+ logger .debug (
144
+ "AWS metadata service timed out after %s seconds" , HTTP_TIMEOUT
145
+ )
146
+ except Exception as e :
147
+ logger .debug ("Error fetching AWS metadata: %s" , str (e ))
136
148
137
149
return ctx
138
150
@@ -152,7 +164,13 @@ def _is_gcp(cls):
152
164
cls .gcp_metadata = json .loads (r .data .decode ("utf-8" ))
153
165
return True
154
166
155
- except Exception :
167
+ except urllib3 .exceptions .TimeoutError :
168
+ logger .debug (
169
+ "GCP metadata service timed out after %s seconds" , HTTP_TIMEOUT
170
+ )
171
+ return False
172
+ except Exception as e :
173
+ logger .debug ("Error checking GCP metadata service: %s" , str (e ))
156
174
return False
157
175
158
176
@classmethod
@@ -201,8 +219,12 @@ def _get_gcp_context(cls):
201
219
except Exception :
202
220
pass
203
221
204
- except Exception :
205
- pass
222
+ except urllib3 .exceptions .TimeoutError :
223
+ logger .debug (
224
+ "GCP metadata service timed out after %s seconds" , HTTP_TIMEOUT
225
+ )
226
+ except Exception as e :
227
+ logger .debug ("Error fetching GCP metadata: %s" , str (e ))
206
228
207
229
return ctx
208
230
0 commit comments