Skip to content

Commit

Permalink
hadoop_http.py: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliyk committed Apr 30, 2024
1 parent 981bd3a commit 19e42fa
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions collectors/lib/hadoop_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,19 @@
import json
except ImportError:
json = None
try:
from collections import OrderedDict # New in Python 2.7
except ImportError:
from ordereddict import OrderedDict # Can be easy_install'ed for <= 2.6
from collectors.lib.utils import is_numeric

try:
# noinspection PyCompatibility
from http.client import HTTPConnection
from http.client import NotConnected
except ImportError:
# noinspection PyUnresolvedReferences,PyCompatibility
from httplib import HTTPConnection
from httplib import NotConnected
from collections import OrderedDict
from collectors.lib.utils import is_numeric
from http.client import HTTPConnection


EXCLUDED_KEYS = (
"Name",
"name"
)

def recurse(key,value):

def recurse(key, value):
if type(value) == dict:
for k,v in value.items():
newkey = '_'.join([key,k])
Expand All @@ -51,7 +42,8 @@ def recurse(key,value):
for j in recurse(newi,x):
yield j
else:
yield (key,value)
yield key, value


class HadoopHttp(object):
def __init__(self, service, daemon, host, port, uri="/jmx"):
Expand All @@ -68,10 +60,10 @@ def request(self):
self.server.request('GET', self.uri)
resp = self.server.getresponse().read()
except ConnectionRefusedError as exc:
sys.stderr.write("Could not connect to %s" % (self.uri))
sys.stderr.write("Could not connect to %s" % self.uri)
sys.exit(1)
except Exception as exc:
sys.stderr.write("Unexpected error: %s" % (exc))
sys.stderr.write("Unexpected error: %s" % exc)
resp = '{}'
finally:
self.server.close()
Expand All @@ -88,7 +80,7 @@ def poll(self):
for bean in json_arr:
if (not bean['name']) or (not "name=" in bean['name']):
continue
#split the name string
# split the name string
context = bean['name'].split("name=")[1].split(",sub=")
# Create a set that keeps the first occurrence
context = list(OrderedDict.fromkeys(context).keys())
Expand All @@ -98,7 +90,7 @@ def poll(self):
context = [c for c in context if c != self.service and c != self.daemon]

for key, value in bean.items():
for m, n in recurse(key, value):
for m, n in recurse(key, value):
if m in EXCLUDED_KEYS:
continue
if not is_numeric(n):
Expand All @@ -111,8 +103,8 @@ def emit_metric(self, context, current_time, metric_name, value, tag_dict=None):
print("%s.%s.%s.%s %d %d" % (self.service, self.daemon, ".".join(context), metric_name, current_time, float(value)))
else:
tag_string = " ".join([k + "=" + v for k, v in tag_dict.items()])
print ("%s.%s.%s.%s %d %d %s" % \
(self.service, self.daemon, ".".join(context), metric_name, current_time, float(value), tag_string))
print("%s.%s.%s.%s %d %d %s" %
(self.service, self.daemon, ".".join(context), metric_name, current_time, float(value), tag_string))
# flush to protect against subclassed collectors that output few metrics not having enough output to trigger
# buffer flush within 10 mins, which then get killed by TCollector due to "inactivity"
sys.stdout.flush()
Expand Down

0 comments on commit 19e42fa

Please sign in to comment.