Skip to content

Commit baacfe4

Browse files
fix: Make timeouts for DataPower customisable in config (#61)
* fix: Make timeouts for DataPower customisable in config Also move to use ObjectInstanceCounts to improve performance doc: include timeout in example Signed-off-by: Ricky Moorhouse <[email protected]>
1 parent 28bf86d commit baacfe4

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ logging:
3333
nets:
3434
datapower:
3535
enabled: true
36+
timeout: 5
3637
username: trawler-monitor
3738
namespace: apic-gateway
3839
product:

datapower_net.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class DataPowerNet():
1818
password = None
1919
use_kubeconfig = False
2020
items = {}
21+
timeout = 1
2122
api_tests = None
2223

2324
def __init__(self, config, trawler):
@@ -28,9 +29,10 @@ def __init__(self, config, trawler):
2829
self.namespace = config.get('namespace', None)
2930
# Datapower username to use for REST calls
3031
self.username = config.get('username', 'admin')
32+
self.timeout = config.get('timeout', 1)
3133
self.secret = config.get('secret', 'gateway-admin-secret')
3234
api_test_config = config.get('api_tests', None)
33-
if api_test_config and api_test_config['enabled'] == True:
35+
if api_test_config and api_test_config['enabled'] is True:
3436
self.api_tests = api_test_config['apis']
3537
# Load password from secret `datapower_password`
3638
try:
@@ -119,16 +121,18 @@ class DataPower():
119121
port = 5554
120122
apiPort = 9443
121123
trawler = None
124+
timeout = 1
122125
api_tests = None
123126
labels = {}
124127

125-
def __init__(self, ip, port, name, namespace, username, password, trawler, api_tests=None):
128+
def __init__(self, ip, port, name, namespace, username, password, trawler, api_tests=None, timeout=1):
126129
self.ip = ip
127130
self.port = port
128131
self.name = name
129132
self.namespace = namespace
130133
self.username = username
131134
self.password = password
135+
self.timeout = timeout
132136
self.get_info()
133137
self.trawler = trawler
134138
self.api_tests = api_tests
@@ -147,7 +151,7 @@ def get_info(self):
147151
state = requests.get(url,
148152
auth=(self.username, self.password),
149153
verify=False,
150-
timeout=1
154+
timeout=self.timeout
151155
)
152156
logger.trace(state)
153157
if state.status_code == 200:
@@ -175,7 +179,7 @@ def are_statistics_enabled(self):
175179
state = requests.get(url,
176180
auth=(self.username, self.password),
177181
verify=False,
178-
timeout=1
182+
timeout=self.timeout
179183
)
180184
logger.trace(state.text)
181185
if state.status_code == 200:
@@ -223,7 +227,7 @@ def fetch_data(self, provider, label, suffix=''):
223227
provider)
224228
status = requests.get(url,
225229
auth=(self.username, self.password),
226-
verify=False, timeout=1).json()
230+
verify=False, timeout=self.timeout).json()
227231
logger.debug(status)
228232
data = status.get(provider, {})
229233
labels = self.labels
@@ -252,30 +256,24 @@ def fetch_data(self, provider, label, suffix=''):
252256
except requests.exceptions.RequestException as e:
253257
logger.info("{}: {} (Check rest-mgmt is enabled and you have network connectivity)".format(provider, e.strerror))
254258

255-
# https://127.0.0.1:5554/mgmt/status/apiconnect/ObjectStatus
259+
# https://127.0.0.1:5554/mgmt/status/apiconnect/ObjectInstanceCounts
256260
def object_counts(self):
257261
""" Count objects within datapower domain """
258-
logger.info("Processing status provider ObjectStatus")
262+
logger.info("Processing status provider ObjectInstanceCounts")
259263
try:
260-
url = "https://{}:{}/mgmt/status/{}/ObjectStatus".format(
264+
url = "https://{}:{}/mgmt/status/{}/ObjectInstanceCounts".format(
261265
self.ip,
262266
self.port,
263267
self.domain)
264268
status = requests.get(url,
265-
auth=(self.username, self.password),
266-
verify=False, timeout=1).json()
269+
auth=(self.username, self.password),
270+
verify=False,
271+
timeout=self.timeout).json()
267272
logger.debug(status)
268-
data = status.get('ObjectStatus', [])
269-
counts = {}
273+
data = status.get('ObjectInstanceCounts', [])
270274
for item in data:
271-
if item['Class'] in counts:
272-
counts[item['Class']] += 1
273-
else:
274-
counts[item['Class']] = 1
275-
for item_class in counts:
276-
self.trawler.set_gauge('datapower', "{}_total".format(item_class), counts[item_class], pod_name=self.name, labels=self.labels)
275+
self.trawler.set_gauge('datapower', "{}_total".format(item['Class']), item['Count'], pod_name=self.name, labels=self.labels)
277276

278-
logger.debug(counts)
279277
except requests.exceptions.RequestException as e:
280278
logger.info("Failed to get object count: {} (Check rest-mgmt is enabled and you have network connectivity)".format(e.strerror))
281279

@@ -297,7 +295,7 @@ def fetch_document_cache_summary(self, suffix=''):
297295
provider)
298296
status = requests.get(url,
299297
auth=(self.username, self.password),
300-
verify=False, timeout=1).json()
298+
verify=False, timeout=self.timeout).json()
301299
logger.debug(status)
302300
data = status.get(provider, {})
303301
if type(data) is not list:
@@ -340,7 +338,7 @@ def gateway_peering_status(self):
340338
self.domain)
341339
status = requests.get(url,
342340
auth=(self.username, self.password),
343-
verify=False, timeout=1).json()
341+
verify=False, timeout=self.timeout).json()
344342
logger.debug(status)
345343

346344
for entry in status["GatewayPeeringStatus"]:
@@ -401,6 +399,7 @@ def invoke_api(self, api):
401399
0,
402400
pod_name=self.name, labels={**status_labels, **self.labels})
403401

402+
404403
if __name__ == "__main__":
405404
net = DataPowerNet()
406405
net.find()

0 commit comments

Comments
 (0)