Skip to content

Commit 7645622

Browse files
AstroProfundisethercflow
authored andcommitted
Minor optimize on log file collection (#16)
* *: minor optimize of coding style * logfiles: fix bugs when collecting log files with ansible * logfiles: fix wrong checking params when called by ansible * makefile: add script to build release tarball
1 parent 8032d67 commit 7645622

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ bin/
22
data/
33

44
.vscode/
5+
.build/
6+
7+
*.log
58

69
# Binaries for programs and plugins
710
*.exe

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ all: default
99

1010
collector:
1111
$(MAKE) -C collector $(MAKECMDGOALS)
12+
13+
package:
14+
./package.sh 2>package.err.log

collector/Makefile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ ifeq "$(GOPATH)" ""
55
$(error Please set the environment variable GOPATH before running `make`)
66
endif
77

8-
GO := go
9-
GOBUILD := GOPATH=$(GOPATH) $(GO) build
8+
ifeq "$(GOBIN)" ""
9+
GO := GOPATH=$(GOPATH) go
10+
else
11+
GO := GOPATH=$(GOPATH) $(GOBIN)
12+
endif
13+
GOBUILD := $(GO) build
1014

1115
COMMIT := $(shell git rev-parse HEAD)
1216
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
@@ -26,6 +30,15 @@ debug:
2630
-i *.go
2731

2832
release:
33+
$(GOBUILD) -ldflags '-w $(LDFLAGS)' \
34+
-o ../bin/collector \
35+
-i *.go
36+
37+
deps:
38+
$(GO) get ./...
39+
40+
static:
41+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
2942
$(GOBUILD) -ldflags '-s -w -extldflags "-static" $(LDFLAGS)' \
3043
-o ../bin/collector \
3144
-i *.go

insight.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ class Insight():
4848
insight_trace = None
4949

5050
def __init__(self, args):
51-
if not args.alias:
51+
if args.alias:
52+
self.alias = args.alias
53+
else:
5254
self.alias = util.get_hostname()
55+
5356
if args.output and util.is_abs_path(args.output):
5457
self.outdir = args.output
5558
self.full_outdir = fileutils.create_dir(
@@ -195,12 +198,12 @@ def save_logfiles(self, args):
195198
return
196199
# reading logs requires root priviledge
197200
if not util.is_root_privilege():
198-
logging.fatal("It's required to read logs with root priviledge.")
199-
return
201+
logging.warn("It's required to read logs with root priviledge.")
202+
#return
200203

201204
self.insight_logfiles = logfiles.InsightLogFiles(options=args)
202-
proc_cmdline = self.format_proc_info("cmd") # cmdline of process
203205
if args.log_auto:
206+
proc_cmdline = self.format_proc_info("cmd") # cmdline of process
204207
self.insight_logfiles.save_logfiles_auto(
205208
proc_cmdline=proc_cmdline, outputdir=self.full_outdir)
206209
else:
@@ -245,9 +248,7 @@ def read_pdctl(self, args):
245248

246249
insight = Insight(args)
247250

248-
if (not args.pid and not args.proc_listen_port
249-
and not args.log_auto and not args.config_auto
250-
):
251+
if (args.log_auto or args.config_auto):
251252
insight.collector()
252253
# check size of data folder of TiDB processes
253254
insight.get_datadir_size()
@@ -269,4 +270,4 @@ def read_pdctl(self, args):
269270

270271
# compress all output to tarball
271272
if args.compress:
272-
fileutils.compress_tarball(insight.full_outdir, insight.alias)
273+
fileutils.compress_tarball(insight.outdir, insight.alias)

measurement/files/logfiles.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def save_tidb_logfiles(self, outputdir=None):
123123
# the output tarball name
124124
output_name = "%s_%s" % (file_prefix, self.log_options.alias)
125125
# the full path of output directory
126-
output_dir = os.path.join(output_base, output_name)
126+
output_dir = fileutils.create_dir(
127+
os.path.join(output_base, output_name))
127128

128129
# copy valid log files to output directory
129130
file_list = self.get_filelist_in_time(source_dir, file_prefix,

measurement/perf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def run(self, outputdir=None):
8585
full_outputdir = fileutils.build_full_output_dir(
8686
basedir=outputdir, subdir=self.data_dir)
8787

88-
if full_outputdir is None:
88+
if not full_outputdir:
8989
# something went wrong when setting output dir, exit without perfing
9090
# TODO: unified output: "Error when setting up output dir of perf data"
9191
return

package.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
# make a release tarball
4+
5+
if [ -z $1 ]; then
6+
RELVER=`git describe --tags`
7+
else
8+
RELVER=$1
9+
fi
10+
RELPATH=tidb-insight-${RELVER}
11+
12+
GO_RELEASE_BIN=go1.10.3.linux-amd64
13+
14+
BUILD_ROOT="`pwd`/.build"
15+
mkdir -p ${BUILD_ROOT}
16+
cd ${BUILD_ROOT}
17+
18+
if [ ! -f ${GO_RELEASE_BIN}.tar.gz ]; then
19+
wget https://dl.google.com/go/${GO_RELEASE_BIN}.tar.gz
20+
tar zxf ${GO_RELEASE_BIN}.tar.gz
21+
fi
22+
23+
GOROOT="${BUILD_ROOT}/go"
24+
GOPATH="${BUILD_ROOT}/.go"
25+
export GOROOT GOPATH
26+
27+
# clean exist binaries
28+
rm -rf ${BUILD_ROOT}/tidb-insight-*
29+
mkdir -p ${BUILD_ROOT}/${RELPATH}/
30+
cp -rf ${BUILD_ROOT}/../* ${BUILD_ROOT}/${RELPATH}/
31+
32+
cd ${BUILD_ROOT}/${RELPATH}/collector/
33+
34+
# prepare dependencies
35+
GOBIN=${GOROOT}/bin/go make deps
36+
# compile a static binary
37+
GOBIN=${GOROOT}/bin/go make static
38+
39+
# clean unecessary files
40+
cd ${BUILD_ROOT}/${RELPATH}
41+
rm -rf collector docs tests Makefile package.sh *.log
42+
find ${BUILD_ROOT}/${RELPATH} -name "*.pyc" | xargs rm
43+
44+
# make tarball archive
45+
cd ${BUILD_ROOT}
46+
tar zcf ${RELPATH}.tar.gz ${RELPATH}

0 commit comments

Comments
 (0)