Skip to content

Commit f1ee159

Browse files
AstroProfundisethercflow
authored andcommitted
Add info collected from TiDB's server API (#33)
* tidb: add info collected from TiDB's server API * See pingcap/tidb#7082 for details of the API * tidb: minor cleanup of pdctl code * tidb: check wheather tidb server api is supported * tidb: fix runtime error when collecting pdctl
1 parent cc36955 commit f1ee159

File tree

5 files changed

+76
-23
lines changed

5 files changed

+76
-23
lines changed

insight.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from metric.importer import prometheus as import_prom
3030
from runtime import perf
3131
from tidb import pdctl
32+
from tidb import tidbinfo
3233
from utils import fileopt
3334
from utils import lsof
3435
from utils import space
@@ -49,7 +50,7 @@ class Insight():
4950
insight_perf = None
5051
insight_logfiles = None
5152
insight_configfiles = None
52-
insight_pdctl = None
53+
insight_tidb = None
5354
insight_trace = None
5455
insight_metric = None
5556

@@ -257,12 +258,16 @@ def save_configs(self, args):
257258
proc_cmdline = self.format_proc_info("cmd") # cmdline of process
258259
self.insight_configfiles.run_collecting(proc_cmdline)
259260

260-
def read_pdctl(self, args):
261-
if args.subcmd_tidb != "pdctl":
262-
logging.debug("Ignoring collecting of PD API.")
263-
self.insight_pdctl = pdctl.PDCtl(
264-
args, self.full_outdir, 'pdctl', host=args.host, port=args.port)
265-
self.insight_pdctl.run_collecting()
261+
def read_apis(self, args):
262+
if args.subcmd_tidb == "pdctl":
263+
# read and save `pd-ctl` info
264+
self.insight_tidb = pdctl.PDCtl(args, self.full_outdir, 'pdctl')
265+
self.insight_tidb.run_collecting()
266+
elif args.subcmd_tidb == 'tidbinfo':
267+
# read and save TiDB's server info
268+
self.insight_tidb = tidbinfo.TiDBInfo(
269+
args, self.full_outdir, 'tidbinfo')
270+
self.insight_tidb.run_collecting()
266271

267272
def dump_metrics(self, args):
268273
if args.subcmd_metric == "prom":
@@ -334,8 +339,8 @@ def dump_metrics(self, args):
334339
insight.save_configs(args)
335340

336341
if args.subcmd == "tidb":
337-
# read and save `pd-ctl` info
338-
insight.read_pdctl(args)
342+
# read and save info from TiDB related APIs
343+
insight.read_apis(args)
339344

340345
if args.subcmd == "metric":
341346
insight.dump_metrics(args)

metric/prometheus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, args, basedir=None, subdir=None):
2828
def get_label_names(self):
2929
result = []
3030
url = '%s%s' % (self.url_base, '/label/__name__/values')
31-
labels = json.loads(util.read_url(url))
31+
labels = json.loads(util.read_url(url)[0])
3232
if labels['status'] == 'success':
3333
result = labels['data']
3434
logging.debug("Found %s available metric keys..." % len(result))
@@ -41,7 +41,7 @@ def run_collecting(self):
4141
for metric in self.get_label_names():
4242
url = '%s/query_range?query=%s&start=%s&end=%s&step=%s' % (
4343
self.url_base, metric, self.start_time, self.end_time, self.resolution)
44-
matrix = json.loads(util.read_url(url))
44+
matrix = json.loads(util.read_url(url)[0])
4545
if not matrix['status'] == 'success':
4646
logging.info("Error querying for key '%s'." % metric)
4747
logging.debug("Output is:\n%s" % matrix)

tidb/pdctl.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ class PDCtl(MeasurementBase):
4040
pd_health_uri = "/health"
4141
pd_diagnose_uri = "/diagnose"
4242

43-
def __init__(self, args, basedir=None, subdir=None, host=None, port=None, api_ver=None):
43+
def __init__(self, args, basedir=None, subdir=None, api_ver=None):
4444
# init self.options and prepare self.outdir
4545
super(PDCtl, self).__init__(args, basedir, subdir)
46-
if host:
47-
self.host = host
48-
if port:
49-
self.port = port
46+
if args.host:
47+
self.host = args.host
48+
if args.port:
49+
self.port = args.port
5050
if api_ver:
5151
self.api_ver = api_ver
5252
self.base_url = "http://%s:%s%s%s" % (
@@ -55,20 +55,20 @@ def __init__(self, args, basedir=None, subdir=None, host=None, port=None, api_ve
5555
def read_health(self):
5656
url = "http://%s:%s/pd%s" % (self.host,
5757
self.port, self.pd_health_uri)
58-
return util.read_url(url)
58+
return util.read_url(url)[0]
5959

6060
def read_diagnose(self):
6161
url = "http://%s:%s/pd%s" % (self.host,
6262
self.port, self.pd_diagnose_uri)
63-
return util.read_url(url)
63+
return util.read_url(url)[0]
6464

6565
def read_runtime_info(self):
6666
def build_url(uri):
6767
return "%s/%s" % (self.base_url, uri)
6868

6969
runtime_info = {}
7070
for key, uri in self.api_map.items():
71-
runtime_info[key] = util.read_url(build_url(uri))
71+
runtime_info[key] = util.read_url(build_url(uri))[0]
7272
return runtime_info
7373

7474
def run_collecting(self):

tidb/tidbinfo.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
# Collect infomation with TiDB API
3+
4+
import logging
5+
import os
6+
7+
from utils import util
8+
from utils import fileopt
9+
from utils.measurement import MeasurementBase
10+
11+
12+
class TiDBInfo(MeasurementBase):
13+
# default to localhost
14+
host = "localhost"
15+
port = 10080
16+
17+
# The API's URI
18+
uri = "/info/all"
19+
20+
def __init__(self, args, basedir=None, subdir=None):
21+
# init self.options and prepare self.outdir
22+
super(TiDBInfo, self).__init__(args, basedir, subdir)
23+
if args.host:
24+
self.host = args.host
25+
if args.port:
26+
self.port = args.port
27+
self.url = "http://%s:%s%s" % (
28+
self.host, self.port, self.uri)
29+
30+
def read_api(self):
31+
result, code = util.read_url(self.url)
32+
if code == 404:
33+
logging.info(
34+
"TiDB server API is not supported by this running instance.")
35+
return None
36+
return result
37+
38+
def run_collecting(self):
39+
info = self.read_api()
40+
if info:
41+
fileopt.write_file(os.path.join(
42+
self.outdir, "%s_%s-tidb-info.json" % (self.host, self.port)), info)

utils/util.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ def parse_insight_opts():
175175
help="The host of the PD server. `localhost` by default.")
176176
parser_pdctl.add_argument("--port", type=int, action="store", default=None,
177177
help="The port of PD API service, `2379` by default.")
178+
parser_tidbinfo = subparsers_tidb.add_parser(
179+
"tidbinfo", help="Collect data from TiDB's server API.")
180+
parser_tidbinfo.add_argument("--host", action="store", default=None,
181+
help="The host of the TiDB server, `localhost` by default.")
182+
parser_tidbinfo.add_argument("--port", type=int, action="store", default=None,
183+
help="The port of TiDB server API port, `10080` by default.")
178184
####
179185

180186
# Sub-command: metric
@@ -228,18 +234,18 @@ def get_init_type():
228234

229235
def read_url(url, data=None):
230236
if not url or url == "":
231-
return None
237+
return None, None
232238

233239
try:
234240
logging.debug("Requesting URL: %s" % url)
235241
response = urlreq.urlopen(url, data)
236-
return response.read()
242+
return response.read(), response.getcode()
237243
except HTTPError as e:
238244
logging.debug("HTTP Error: %s" % e.read())
239-
return e.read()
245+
return e.read(), e.getcode()
240246
except URLError as e:
241247
logging.warning("Reading URL %s error: %s" % (url, e))
242-
return None
248+
return None, None
243249

244250

245251
def get_hostname():

0 commit comments

Comments
 (0)